Skip to content

Commit

Permalink
added custom theme option and minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
IgdaliasCabamba committed Dec 22, 2023
1 parent 6d43e63 commit 5d0715d
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 179 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
dev:
@bots/dev
@bots/dev.sh

release:
@bots/builder
@bots/builder.sh

run:
@bots/runner
@bots/runner.sh

build-spec:
@bots/spec-builder
@bots/spec-builder.sh
43 changes: 43 additions & 0 deletions __main__.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- mode: python ; coding: utf-8 -*-


a = Analysis(
['__main__.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
)
pyz = PYZ(a.pure)

exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='__main__',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='__main__',
)
10 changes: 9 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from _system import *
from server import *
from utils import list_all_themes
from src.rpc import evalRPC

app.on_startup.append(on_startup)
app.router.add_static('/static', STATIC_PATH)
Expand Down Expand Up @@ -44,6 +46,12 @@ def run():
help="Enter a theme to uxterm",
default="default",
type = str)

parser.add_argument('-ct',
'--custom-theme',
help="Use a custom theme on uxterm",
default="",
type = str)

parser.add_argument('--list-themes', help="List all themes and exit", action='store_true')

Expand All @@ -53,7 +61,7 @@ def run():
sys.exit(list_all_themes())


sys.exit(run_new_term(args.command, args.host, args.port, args.binargs, args.theme))
sys.exit(run_new_term(args.command, args.host, args.port, args.binargs, args.theme, args.custom_theme))



Expand Down
99 changes: 9 additions & 90 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import pprint

import aiohttp_jinja2
import jinja2
import socketio
import src.urequirements as urequirements
import src.uterm as uterm
from _system import *
from aiohttp import web
import rich

urequirements.main()


usocket = socketio.AsyncServer()
app = web.Application()
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(TEMPLATES_PATH))
Expand All @@ -19,9 +17,10 @@

terminal_api = uterm.TerminalCoreApi(rows=50, cols=50)


class PtyInterface:

def input_(text:str, *args, **kwargs) -> None:
def input_(text: str, *args, **kwargs) -> None:
terminal_api.write(text.encode())


Expand All @@ -34,7 +33,7 @@ def post_theme(request) -> web.Response:
...

async def get_theme(request) -> web.Response:
theme_name = request.match_info['theme_name']
theme_name:str = request.match_info['theme_name']

theme = uterm.XTERM_SETTINGS.themes.get(
theme_name.lower(),
Expand Down Expand Up @@ -73,83 +72,6 @@ async def on_startup(app):
usocket_config["theme"] = "default"


async def eval_payload(prompt: str, aio: bool) -> str:
if aio:
try:
return await eval(prompt)
except:
return None

try:
return eval(prompt)
except:
return None


async def exec_payload(prompt: str, aio: bool) -> None:
if aio:
try:
await exec(prompt)
except:
pass

try:
exec(prompt)
except:
pass


async def evalRPC(request) -> web.Response:
payload = await request.json()

if "eval" in payload.keys():
try:
evaluated_res = await eval_payload(
prompt=payload["eval"][">>>"],
aio=payload["eval"]["async"]
)
except Exception as e:
return web.json_response(
{
"status": False,
"log": "Not Evaluated",
"code": 1,
f"Exception {e}": {
"name": str(e),
"args": e.args,
}
}
)

if "exec" in payload.keys():
try:
_ = await exec_payload(
prompt=payload["exec"][">>>"],
aio=payload["exec"]["async"]
)
except Exception as e:
return web.json_response(
{
"status": False,
"log": "Not Excuted",
"code": 2,
f"Exception {e}": {
"name": str(e),
"args": e.args,
}
}
)

return web.json_response(
{
"status": True,
"log": "sucess",
"code": 0,
"evaluated": evaluated_res
}
)


@aiohttp_jinja2.template('index.html')
async def index(request) -> dict:
"""Serve the client-side application."""
Expand Down Expand Up @@ -183,15 +105,12 @@ async def connect(sid, environ) -> None:
await usocket.emit("pty-set-theme", {"name": usocket_config["theme"]}, namespace="/pty")


def run_new_term(command: str = "python3", host: str = "127.0.0.1", port: int = 9990, command_args: str = "", theme: str = "default") -> None:
def run_new_term(command: str = "python3", host: str = "127.0.0.1", port: int = 9990, command_args: str = "", theme: str = "default", custom_theme: str = None) -> None:
usocket_config["cmd"] = uterm.TermUtils.get_split_command(
command, command_args)
usocket_config["theme"] = theme.lower()
web.run_app(app, port=port, host=host)


if custom_theme:
usocket_config["theme"] = uterm.XTERM_SETTINGS.add_theme(custom_theme)

def list_all_themes() -> None:
themes = uterm.XTERM_SETTINGS.themes.keys()
for theme in themes:
rich.print(f"[bold green]{theme}")
rich.print(f"[bold yellow]TOTAL[/]: {len(themes)}")
web.run_app(app, port=port, host=host)
77 changes: 77 additions & 0 deletions app/src/rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from aiohttp import web

async def eval_payload(prompt: str, aio: bool) -> str:
if aio:
try:
return await eval(prompt)
except:
return None

try:
return eval(prompt)
except:
return None


async def exec_payload(prompt: str, aio: bool) -> None:
if aio:
try:
await exec(prompt)
except:
pass

try:
exec(prompt)
except:
pass


async def evalRPC(request) -> web.Response:
payload = await request.json()

if "eval" in payload.keys():
try:
evaluated_res = await eval_payload(
prompt=payload["eval"][">>>"],
aio=payload["eval"]["async"]
)
except Exception as e:
return web.json_response(
{
"status": False,
"log": "Not Evaluated",
"code": 1,
f"Exception {e}": {
"name": str(e),
"args": e.args,
}
}
)

if "exec" in payload.keys():
try:
_ = await exec_payload(
prompt=payload["exec"][">>>"],
aio=payload["exec"]["async"]
)
except Exception as e:
return web.json_response(
{
"status": False,
"log": "Not Excuted",
"code": 2,
f"Exception {e}": {
"name": str(e),
"args": e.args,
}
}
)

return web.json_response(
{
"status": True,
"log": "sucess",
"code": 0,
"evaluated": evaluated_res
}
)
25 changes: 14 additions & 11 deletions app/src/uterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ class Defaults:
THEME = {"background": "#212121"}

def __init__(self):
self.__themes = self.read_themes()
self.__themes = dict()
self.load_themes()

@property
def themes(self) -> dict:
return self.__themes

def add_theme(self, theme_file:str) -> str:
name = pathlib.Path(theme_file).name
name = name.split(".")[0].lower()
with open(theme_file, "r") as fp:
self.__themes[name] = hjson.load(fp)
return name

def update_themes(self) -> None:
self.__themes.clear()
self.__themes = self.read_themes()
self.__themes = self.load_themes()

def read_themes(self) -> dict:
themes = dict()
def load_themes(self) -> None:

possible_themes = glob.glob(
os.path.join("/",os.environ["UTERM_ROOT_PATH"],"settings","themes")+"/**.theme.json",
recursive=True
)
for file in possible_themes:
name = pathlib.Path(file).name
name = name.split(".")[0].lower()
with open(file, "r") as fp:
themes[name] = hjson.load(fp)

return themes
for theme_file in possible_themes:
self.add_theme(theme_file)


XTERM_SETTINGS = XTermSettings()

Expand Down
9 changes: 9 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import src.uterm as uterm
from _system import *
import rich

def list_all_themes() -> None:
themes = uterm.XTERM_SETTINGS.themes.keys()
for theme in themes:
rich.print(f"[bold green]{theme}")
rich.print(f"[bold yellow]TOTAL[/]: {len(themes)}")
11 changes: 0 additions & 11 deletions bots/builder

This file was deleted.

1 change: 1 addition & 0 deletions bots/builder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyinstaller build.spec
5 changes: 0 additions & 5 deletions bots/dev

This file was deleted.

2 changes: 2 additions & 0 deletions bots/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd app
aiohttp-devtools runserver
Loading

0 comments on commit 5d0715d

Please sign in to comment.