- paths:
- ./fangcore.py
'''
FDEE2, a demonstration of FangCore, and a ground for testing FangCore apps and Extensions
Jacob Scrapchansky
Unix/Linux and MacOS edition - Modified for Browser Support
'''
import sys
from js import XMLHttpRequest
import time
VERSION = "1.0"
#Location of app/extension folders
init_location = sys.path[0] + "/"
output_element = Element("output").element
extension_index_installed = False
continue_input = False
def print(*args, **kwargs):
final = " ".join([str(i) for i in args])
output_element.innerText += "\n" + final
import time
try:
import fangcore
except Exception:
print("The FangCore library is required for this application")
time.sleep(5)
quit()
import os
print("Gathering Extension Index...")
request = XMLHttpRequest.new()
request.open("GET", "./extension_index.txt", False)
request.send(None)
extension_index = str(request.response)
print(extension_index)
print("Extensions Index Retrieved.")
fang = fangcore.fang()
fang.set_defaults()
fang.set_extension_print_pipe(print)
fang.extension_define("Element", Element)
def _exec(args):
try:
exec(args[3].strip(), globals())
except Exception as exc:
print("The following error occured while executing '" + args[3].strip() + "': " + str(exc))
def _quit():
quit()
def _load(args):
try:
if len(args[0]) >= 1:
fang.run_appfile(args[0][0])
else:
for x in fang.appfiles:
print(x[0])
except Exception:
print("Error while loading Script")
def _debug():
print('Debug Information')
print("Extensions:", fang.extensions)
print("Apps:", fang.appfiles)
def _scrapworks():
print("Scrapworks Homepage")
def _help():
print("FDEE2, an environment for deploying, testing, and using FangCore Applications.")
print()
print("load [filename] - load a script from the FDEE_APPS folder")
print("clear - clears past commands")
print("debug - displays Appfiles, extensions, and their subsequent information")
print("reinit - reload appfiles and extensions in case of a change")
print("help - displays this message")
print("list_extensions - lists any available commands through extensions")
print("cd - display current directory")
print("import_extension [url] - imports and installs an extension from a URL")
print("extension_index - list all extensions recommended in index")
print("install_extension_index - install all extensions listed in the recommended extension index.")
print("exec [python command]- execute a python command within the global context of FDEE2")
print("Any Extensions located in your FDEE_EXTENSIONS folder may also be used")
def _list_extension_index():
print(extension_index)
def _install_extension_index(*args, **kwargs):
global extension_index_installed
if extension_index_installed:
print("Extensions already installed.")
return
print("Installing Extension Index...")
for url in extension_index.strip().split("\n"):
url = url.strip()
_import_extension([[],[],"",url])
extension_index_installed = True
print("Extension Index Installed.")
def _list_extensions():
print("Extension Commands:")
for x in fang.extensions:
print(x[0])
print("Folder Contents:")
for x in os.listdir(init_location + "/FDEE_EXTENSIONS"):
print(x)
def _import_extension(args):
address = args[3].strip()
print(f"Importing {address}...")
try:
request = XMLHttpRequest.new()
request.open("GET", address, False)
request.send(None)
except Exception as exc:
print("There was an error fetching that resource: " + str(exc))
return
print("Fetched extension, installing...")
with open(init_location + "/FDEE_EXTENSIONS/" + str(time.time()) + ".py", "w") as file:
file.write(str(request.response))
init_files(init_location)
print("Installed.")
def _cd():
print("Working Directory:" + os.getcwd())
print("File Directory:" + init_location)
def start_screen(*args, **kwargs):
print()
Element('output').element.innerText = ""
print()
print("FDEE2 (FangOS Development Emulated Environment 2) - FangCore " + fangcore.VERSION)
print("PyScript Browser Edition v" + VERSION)
print("------------------------------")
def init_files(Location):
fang.clear_appfiles()
fang.clear_extensions()
try:
os.mkdir(Location + '/FDEE_APPS')
except Exception:
pass
try:
os.mkdir(Location + '/FDEE_EXTENSIONS')
except Exception:
pass
for file in os.listdir(Location + "/FDEE_APPS"):
if file[0] != ".":
opened = open(Location + "/FDEE_APPS/" + file, 'r')
fang.create_appfile(str(file), opened.read())
opened.close()
for file in os.listdir(Location + "/FDEE_EXTENSIONS"):
if file[0] != ".":
opened = open(Location + "/FDEE_EXTENSIONS/" + file, 'r')
readin = opened.read()
fang.create_extension(str(readin.split()[0].replace("'''", "")), readin)
opened.close()
def init_commands():
fang.bind_command("load", _load)
fang.bind_command("quit", _quit)
fang.bind_command("clear", start_screen)
fang.bind_command("debug", _debug)
fang.bind_command("reinit", lambda: init_files(init_location))
fang.bind_command("help", _help)
fang.bind_command("list_extensions", _list_extensions)
fang.bind_command("cd", _cd)
fang.bind_command("import_extension", _import_extension)
fang.bind_command("extension_index", _list_extension_index)
fang.bind_command("install_extension_index", _install_extension_index)
fang.bind_command("exec", _exec)
fang.bind_command("scrapworks", _scrapworks)
def command_loop_function():
command = input("FDEE2|: ")
if command.strip() != "":
command_result = fang.command(command)
if command_result:
print("Command not found: " + command_result[2])
print()
else:
print("No Input Detected")
init_files(init_location)
init_commands()
start_screen()
_help()
def input_box_command(*args, **kwargs):
command = Element("input").element.value
Element("input").element.value = ""
if command.strip() != "":
command_result = fang.command(command)
if command_result:
print("Command not found: " + command_result[2])
else:
print()
else:
print("No Input Detected")
fang.extension_define("FDEE", globals())