From 4b113af7dca13436b38bcda289b3a483deef4c33 Mon Sep 17 00:00:00 2001 From: pmp-p Date: Mon, 19 Aug 2024 00:41:34 +0200 Subject: [PATCH] 0.9.2 --- scripts/build-loader.sh | 7 --- src/pygbag/__init__.py | 4 +- src/pygbag/__main__.py | 6 +-- src/pygbag/aio.py | 8 ++-- src/pygbag/app.py | 12 ++--- src/pygbag/gathering.py | 2 + src/pygbag/optimizing.py | 2 +- src/pygbag/support/_xterm_parser/_types.py | 12 ++--- src/pygbag/support/cpythonrc.py | 28 ++++++------ src/pygbag/support/cross/__EMSCRIPTEN__.py | 4 +- src/pygbag/support/cross/aio/clock.py | 3 +- src/pygbag/support/cross/aio/pep0723.py | 53 ++++++++++++---------- src/pygbag/support/cross/aio/toplevel.py | 3 +- src/pygbag/support/pygbag_app.py | 5 +- src/pygbag/support/pygbag_fsm.py | 6 +-- src/pygbag/support/pygbag_ui.py | 4 ++ src/pygbag/support/pyodide.py | 23 +++++++--- src/pygbag/support/readline.py | 23 +++++----- src/pygbag/testserver.py | 6 ++- src/pygbag/web.py | 1 + static/default.tmpl | 2 +- static/noctx.tmpl | 2 +- 22 files changed, 112 insertions(+), 104 deletions(-) diff --git a/scripts/build-loader.sh b/scripts/build-loader.sh index 5ced255..5d0699f 100755 --- a/scripts/build-loader.sh +++ b/scripts/build-loader.sh @@ -384,13 +384,6 @@ END done popd fi -#echo " -# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -# emsdk tot js gen temp fix -# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -#" -# sed -i 's/_glfwSetWindowContentScaleCallback_sig=iii/_glfwSetWindowContentScaleCallback_sig="iii"/g' \ -# ${DIST_DIR}/python${PYMAJOR}${PYMINOR}/${MODE}.js du -hs ${DIST_DIR}/* else echo "pymain+loader linking failed" diff --git a/src/pygbag/__init__.py b/src/pygbag/__init__.py index 698a3b0..8f178df 100644 --- a/src/pygbag/__init__.py +++ b/src/pygbag/__init__.py @@ -24,9 +24,9 @@ VERSION = "0.0.0" # make aio available -if not sys.platform in ('emscripten','wasi'): +if not sys.platform in ("emscripten", "wasi"): # must be first for readline - sys.path.insert(0, str(Path(__file__).parent / "support" )) + sys.path.insert(0, str(Path(__file__).parent / "support")) sys.path.append(str(Path(__file__).parent / "support/cross")) diff --git a/src/pygbag/__main__.py b/src/pygbag/__main__.py index 48ee42f..eaee578 100644 --- a/src/pygbag/__main__.py +++ b/src/pygbag/__main__.py @@ -139,8 +139,7 @@ class fake_EventTarget: clients = {} events = [] - async def process(self): - ... + async def process(self): ... def patch_platform_system(): return "Emscripten" @@ -180,8 +179,7 @@ def system(cls): return "Linux" @classmethod - def no_op(cls, *argv, **kw): - ... + def no_op(cls, *argv, **kw): ... run = no_op diff --git a/src/pygbag/aio.py b/src/pygbag/aio.py index 2451744..032e0da 100644 --- a/src/pygbag/aio.py +++ b/src/pygbag/aio.py @@ -34,9 +34,11 @@ if aio.pep0723.Config.dev_mode: aio.pep0723.Config.PKG_INDEXES.extend(["http://localhost:8000/archives/repo/"]) else: - aio.pep0723.Config.PKG_INDEXES.extend([ - os.environ.get('PYGPY', "https://pygame-web.github.io/archives/repo/"), - ]) + aio.pep0723.Config.PKG_INDEXES.extend( + [ + os.environ.get("PYGPY", "https://pygame-web.github.io/archives/repo/"), + ] + ) import pygbag.__main__ diff --git a/src/pygbag/app.py b/src/pygbag/app.py index 06e7120..ab35766 100644 --- a/src/pygbag/app.py +++ b/src/pygbag/app.py @@ -17,11 +17,12 @@ from .__init__ import VERSION -if '--no_ssl_check' in sys.argv: +if "--no_ssl_check" in sys.argv: import ssl + # ssl local server testing. ssl._create_default_https_context = ssl._create_unverified_context - #os.environ["REQUESTS_CA_BUNDLE"]="/etc/ssl/certs/ca-bundle.crt" + # os.environ["REQUESTS_CA_BUNDLE"]="/etc/ssl/certs/ca-bundle.crt" sys.argv.remove("--no_ssl_check") import pygbag @@ -396,12 +397,11 @@ async def main_run(app_folder, mainscript, cdn=DEFAULT_CDN): "spdx": "cookiecutter.spdx", "version": VERSION, "PYBUILD": args.PYBUILD, - "COLUMNS" : args.COLUMNS, - "LINES" : args.LINES, - "CONSOLE" : args.CONSOLE, + "COLUMNS": args.COLUMNS, + "LINES": args.LINES, + "CONSOLE": args.CONSOLE, } - pygbag.config = CC await pack.archive(f"{app_name}.apk", app_folder, build_dir) diff --git a/src/pygbag/gathering.py b/src/pygbag/gathering.py index ce16a97..f501c6f 100644 --- a/src/pygbag/gathering.py +++ b/src/pygbag/gathering.py @@ -1,9 +1,11 @@ import os from pathlib import Path + class Error(Exception): pass + def gather(root: Path, *kw): if root.is_file(): if root.name == "main.py": diff --git a/src/pygbag/optimizing.py b/src/pygbag/optimizing.py index 9e3835d..f1441aa 100644 --- a/src/pygbag/optimizing.py +++ b/src/pygbag/optimizing.py @@ -31,7 +31,7 @@ "wav": "ogg", "bmp": "png", "mp3": "ogg", - "xm" : "ogg", + "xm": "ogg", } diff --git a/src/pygbag/support/_xterm_parser/_types.py b/src/pygbag/support/_xterm_parser/_types.py index b1ad797..4fe929f 100644 --- a/src/pygbag/support/_xterm_parser/_types.py +++ b/src/pygbag/support/_xterm_parser/_types.py @@ -11,19 +11,15 @@ class MessageTarget(Protocol): """Protocol that must be followed by objects that can receive messages.""" - async def _post_message(self, message: "Message") -> bool: - ... + async def _post_message(self, message: "Message") -> bool: ... - def post_message(self, message: "Message") -> bool: - ... + def post_message(self, message: "Message") -> bool: ... class EventTarget(Protocol): - async def _post_message(self, message: "Message") -> bool: - ... + async def _post_message(self, message: "Message") -> bool: ... - def post_message(self, message: "Message") -> bool: - ... + def post_message(self, message: "Message") -> bool: ... class UnusedParameter: diff --git a/src/pygbag/support/cpythonrc.py b/src/pygbag/support/cpythonrc.py index 07df62f..21cd10d 100644 --- a/src/pygbag/support/cpythonrc.py +++ b/src/pygbag/support/cpythonrc.py @@ -219,7 +219,7 @@ def dump_code(): else: # for the various emulations/tools provided - sys.path.append('/data/data/org.python/assets') + sys.path.append("/data/data/org.python/assets") PyConfig["pkg_repolist"] = [] @@ -253,9 +253,9 @@ def dump_code(): del home # now in pep0723 -#PYCONFIG_PKG_INDEXES = [ +# PYCONFIG_PKG_INDEXES = [ # os.environ.get('PYGPY', "https://pygame-web.github.io/archives/repo/"), -#] +# ] PyConfig["imports_ready"] = False PyConfig["pygbag"] = 0 @@ -688,8 +688,8 @@ async def preload_code(cls, code, callback=None, loaderhome=".", hint=""): if not len(Config.repos): await aio.pep0723.async_repos() - # TODO switch to METADATA:Requires-Dist - # see https://github.com/pygame-web/pygbag/issues/156 + # TODO switch to METADATA:Requires-Dist + # see https://github.com/pygame-web/pygbag/issues/156 for cdn in Config.PKG_INDEXES: async with platform.fopen(Path(cdn) / Config.REPO_DATA) as source: @@ -708,13 +708,13 @@ async def preload_code(cls, code, callback=None, loaderhome=".", hint=""): DBG(f"635: {maybe_wanted=} known failed {aio.pep0723.hint_failed=}") # FIXME use an hybrid wheel - if 'pyodide' in aio.pep0723.hint_failed: + if "pyodide" in aio.pep0723.hint_failed: - for no_need in ('_zengl','pyodide','beautifulsoup4'): + for no_need in ("_zengl", "pyodide", "beautifulsoup4"): if no_need in maybe_wanted: maybe_wanted.remove(no_need) # force - maybe_wanted.append('beautifulsoup4') + maybe_wanted.append("beautifulsoup4") for dep in maybe_wanted: if not dep in deps: @@ -1073,10 +1073,10 @@ def urlretrieve(maybe_url, filename=None, reporthook=None, data=None): PyConfig.pkg_indexes.append(redirect) print("807: DEV MODE ON", PyConfig.pkg_indexes) -# now in pep0723 -# else: -# # address cdn -# PyConfig.pkg_indexes = PYCONFIG_PKG_INDEXES + # now in pep0723 + # else: + # # address cdn + # PyConfig.pkg_indexes = PYCONFIG_PKG_INDEXES from platform import window, document, ffi @@ -1375,6 +1375,7 @@ def __call__(self, callid, fn, *argv, **env): return None print(f"{self.__dlref}.{fn}({argv},{env}) {callid=}") + async def rv(): obj = await platform.jsiter(window.dlcall(callid, jshex)) return json.loads(obj) @@ -1475,6 +1476,7 @@ def log(*argv, **kw): LOCK except: import builtins + builtins.LOCK = False @@ -1617,7 +1619,7 @@ async def main(): local = tmpdir / file break DBG("1862: import_site: found ", local) - elif str(source).startswith('http'): + elif str(source).startswith("http"): print("Remote file :", source) local = tmpdir / "remote.py" await shell.exec(shell.wget(f"-O{local}", source)) diff --git a/src/pygbag/support/cross/__EMSCRIPTEN__.py b/src/pygbag/support/cross/__EMSCRIPTEN__.py index 39cbf2b..176c297 100644 --- a/src/pygbag/support/cross/__EMSCRIPTEN__.py +++ b/src/pygbag/support/cross/__EMSCRIPTEN__.py @@ -96,7 +96,7 @@ def requestAnimationFrame(fn): async def main(): while not aio.exit: if len(AnimatedFrames): - frames.append( AnimatedFrames.pop(0) ) + frames.append(AnimatedFrames.pop(0)) await asyncio.sleep(0) asyncio.run(main()) @@ -105,7 +105,7 @@ async def main(): # just a workaround until bridge support js "options" from **kw def ffi(arg=0xDEADBEEF, **kw): - if arg==0xDEADBEEF: + if arg == 0xDEADBEEF: return window.JSON.parse(json.dumps(kw)) return window.JSON.parse(json.dumps(arg)) diff --git a/src/pygbag/support/cross/aio/clock.py b/src/pygbag/support/cross/aio/clock.py index f5c983b..bc89496 100644 --- a/src/pygbag/support/cross/aio/clock.py +++ b/src/pygbag/support/cross/aio/clock.py @@ -10,8 +10,7 @@ class tui: out = sys.__stdout__.write else: - def out(self, *argv, **kw): - ... + def out(self, *argv, **kw): ... try: LINES = int(os.environ.get("LINES", 200)) diff --git a/src/pygbag/support/cross/aio/pep0723.py b/src/pygbag/support/cross/aio/pep0723.py index d0785ab..f7391f0 100644 --- a/src/pygbag/support/cross/aio/pep0723.py +++ b/src/pygbag/support/cross/aio/pep0723.py @@ -55,9 +55,9 @@ class Config: - READ_722 = False + # READ_722 = False READ_723 = True - BLOCK_RE_722 = r"(?i)^#\s+script\s+dependencies:\s*$" + # BLOCK_RE_722 = r"(?i)^#\s+script\s+dependencies:\s*$" BLOCK_RE_723 = r"(?m)^# /// (?P[a-zA-Z0-9-]+)$\s(?P(^#(| .*)$\s)+)^# ///$" PKG_BASE_DEFAULT = "https://pygame-web.github.io/archives/repo/" PKG_INDEXES = [] @@ -72,8 +72,8 @@ class Config: "pygame_ce": "pygame.base", "python_i18n": "i18n", "pillow": "PIL", - "pyglm" : "glm", - "opencv_python" : "cv2", + "pyglm": "glm", + "opencv_python": "cv2", } @@ -110,7 +110,7 @@ def read_dependency_block_723(code): content = [] for line in code.split("\n"): if not has_block: - if line.strip() in ["# /// pyproject","# /// script"]: + if line.strip() in ["# /// pyproject", "# /// script"]: has_block = True continue @@ -192,12 +192,12 @@ async def async_repos(): apitag = apitag.replace("-", "_") # user can override "PYPI" index - if os.environ.get('PYGPI',""): - Config.PKG_INDEXES= [os.environ.get('PYGPI')] + if os.environ.get("PYGPI", ""): + Config.PKG_INDEXES = [os.environ.get("PYGPI")] # default to "official" cdn if not len(Config.PKG_INDEXES): - Config.PKG_INDEXES = [ Config.PKG_BASE_DEFAULT ] + Config.PKG_INDEXES = [Config.PKG_BASE_DEFAULT] print("200: async_repos", Config.PKG_INDEXES) @@ -218,18 +218,18 @@ async def async_repos(): if repo not in Config.pkg_repolist: Config.pkg_repolist.append(repo) except FileNotFoundError: - print("\n"*4) - print("!"*75) + print("\n" * 4) + print("!" * 75) print("Sorry, there is no pygbag package repository for your python version") - print("!"*75,"\n"*4) + print("!" * 75, "\n" * 4) raise SystemExit - if not aio.cross.simulator: rewritecdn = "" import platform - if os.environ.get('PYGPI',""): - rewritecdn = os.environ.get('PYGPI') + + if os.environ.get("PYGPI", ""): + rewritecdn = os.environ.get("PYGPI") elif platform.window.location.href.startswith("http://localhost:8"): rewritecdn = "http://localhost:8000/archives/repo/" @@ -238,6 +238,7 @@ async def async_repos(): for idx, repo in enumerate(Config.pkg_repolist): repo["-CDN-"] = rewritecdn + async def install_pkg(sysconf, wheel_url, wheel_pkg): target_filename = f"/tmp/{wheel_pkg}" async with fopen(wheel_url, "rb") as pkg: @@ -321,20 +322,21 @@ async def pip_install(pkg, sysconf={}): PYGAME = 0 + async def parse_code(code, env): global PATCHLIST, PYGAME maybe_missing = [] - if Config.READ_722: - for req in read_dependency_block_722(code): - pkg = str(req) - if (env / pkg).is_dir(): - print("found in env :", pkg) - continue - elif pkg not in maybe_missing: - # do not change case ( eg PIL ) - maybe_missing.append(pkg.lower().replace("-", "_")) + # if Config.READ_722: + # for req in read_dependency_block_722(code): + # pkg = str(req) + # if (env / pkg).is_dir(): + # print("found in env :", pkg) + # continue + # elif pkg not in maybe_missing: + # # do not change case ( eg PIL ) + # maybe_missing.append(pkg.lower().replace("-", "_")) if Config.READ_723: for req in read_dependency_block_723(code): @@ -436,12 +438,13 @@ async def check_list(code=None, filename=None): if not aio.cross.simulator: import platform import asyncio + print(f'# 439: Scanning {sconf["platlib"]} for WebAssembly library') platform.explore(sconf["platlib"], verbose=True) - for compilation in range(1+embed.preloading()): + for compilation in range(1 + embed.preloading()): await asyncio.sleep(0) - if embed.preloading()<=0: + if embed.preloading() <= 0: break else: print("# 442: ERROR: remaining wasm {embed.preloading()}") diff --git a/src/pygbag/support/cross/aio/toplevel.py b/src/pygbag/support/cross/aio/toplevel.py index c97c390..e0ce267 100644 --- a/src/pygbag/support/cross/aio/toplevel.py +++ b/src/pygbag/support/cross/aio/toplevel.py @@ -261,5 +261,4 @@ async def start_toplevel(cls, shell, console=True, ns="__main__"): else: # TODO upy event driven async repl - class AsyncInteractiveConsole: - ... + class AsyncInteractiveConsole: ... diff --git a/src/pygbag/support/pygbag_app.py b/src/pygbag/support/pygbag_app.py index 3c943d0..77b9d7f 100644 --- a/src/pygbag/support/pygbag_app.py +++ b/src/pygbag/support/pygbag_app.py @@ -49,7 +49,8 @@ def get(self): platform.shell.interactive(True) aio.toplevel.handler.muted = False - clear(LINES, CONSOLE-1) # , '>C> ') + clear(LINES, CONSOLE - 1) # , '>C> ') return self -__ALL__ = [ ui, ux ] + +__ALL__ = [ui, ux] diff --git a/src/pygbag/support/pygbag_fsm.py b/src/pygbag/support/pygbag_fsm.py index 2c573d2..6aaeb05 100644 --- a/src/pygbag/support/pygbag_fsm.py +++ b/src/pygbag/support/pygbag_fsm.py @@ -5,12 +5,10 @@ index = {} -class State(object): - ... +class State(object): ... -class Draft(object): - ... +class Draft(object): ... @dataclass diff --git a/src/pygbag/support/pygbag_ui.py b/src/pygbag/support/pygbag_ui.py index d5b1517..a03cbb9 100644 --- a/src/pygbag/support/pygbag_ui.py +++ b/src/pygbag/support/pygbag_ui.py @@ -334,13 +334,17 @@ def clear(LINES=0, CONSOLE=0, prompt=""): import sys + try: import re + ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") except: + class ansi_escape: def sub(self, sb, line): return line + ansi_escape = ansi_escape() diff --git a/src/pygbag/support/pyodide.py b/src/pygbag/support/pyodide.py index d66fbc3..5be28ae 100644 --- a/src/pygbag/support/pyodide.py +++ b/src/pygbag/support/pyodide.py @@ -1,10 +1,12 @@ import os -os.environ['PYODIDE'] = "1" + +os.environ["PYODIDE"] = "1" import sys import platform -sys.modules['js'] = platform + +sys.modules["js"] = platform import js @@ -13,32 +15,38 @@ def create_proxy(self, fn): print(fn) return fn -ffi=ffi() + +ffi = ffi() + class m_pyodide: - ffi=ffi + ffi = ffi + sys.modules["pyodide"] = m_pyodide() del m_pyodide import pyodide + class m_canvas: - def getCanvas3D(self, name='canvas',width=0,height=0): + def getCanvas3D(self, name="canvas", width=0, height=0): canvas = platform.document.getElementById(name) try: width = width or canvas.width height = height or canvas.height - #print(f"canvas size was previously set to : {width=} x {height=}") + # print(f"canvas size was previously set to : {width=} x {height=}") except: pass canvas.width = width or 1024 canvas.height = height or 1024 return canvas + class m_pyodide_js: canvas = m_canvas() _module = platform.window + sys.modules["pyodide_js"] = m_pyodide_js() del m_canvas del m_pyodide_js @@ -46,7 +54,8 @@ class m_pyodide_js: # fix pygbag bridge -def jseval(code:str): +def jseval(code: str): return platform.window.eval(code) + platform.eval = jseval diff --git a/src/pygbag/support/readline.py b/src/pygbag/support/readline.py index 7ce9ee2..7ae7a7a 100644 --- a/src/pygbag/support/readline.py +++ b/src/pygbag/support/readline.py @@ -562,20 +562,20 @@ def process_char(self, char): action = self.state[None] args = (char,) -# if DEBUG: -# self.log( -# "process_char '%c' 0x%02x - Action %-20s caret = %2d line = %s esc_seq = '%s' (before)" -# % (printable(char), ord(char), action.__name__, self.caret, repr(self.line), self.esc_seq) -# ) + # if DEBUG: + # self.log( + # "process_char '%c' 0x%02x - Action %-20s caret = %2d line = %s esc_seq = '%s' (before)" + # % (printable(char), ord(char), action.__name__, self.caret, repr(self.line), self.esc_seq) + # ) try: result = action(*args) except AssertionError as e: result = None -# self.log( -# e, -# "process_char '%c' 0x%02x - Action %-20s caret = %2d line = %s esc_seq = '%s' (after)" -# % (printable(char), ord(char), action.__name__, self.caret, repr(self.line), self.esc_seq), -# ) + # self.log( + # e, + # "process_char '%c' 0x%02x - Action %-20s caret = %2d line = %s esc_seq = '%s' (after)" + # % (printable(char), ord(char), action.__name__, self.caret, repr(self.line), self.esc_seq), + # ) if not FBO: self.redraw() @@ -666,8 +666,7 @@ def add_history(line: str): readline.history.append(line) -def get_history_item(index: int): - ... +def get_history_item(index: int): ... def get_current_history_length(): diff --git a/src/pygbag/testserver.py b/src/pygbag/testserver.py index c6f1e99..0567709 100644 --- a/src/pygbag/testserver.py +++ b/src/pygbag/testserver.py @@ -90,10 +90,12 @@ def send_head(self): global VERB, CDN, PROXY, BCDN, BPROXY, AUTO_REBUILD path = self.translate_path(self.path) if VERB: - print(f""" + print( + f""" {self.path=} {path=} -""") +""" + ) f = None if os.path.isdir(path): diff --git a/src/pygbag/web.py b/src/pygbag/web.py index 4061621..9435bc4 100644 --- a/src/pygbag/web.py +++ b/src/pygbag/web.py @@ -19,6 +19,7 @@ class ssl: import sys import ssl + # https://stackoverflow.com/questions/42098126/mac-osx-python-ssl-sslerror-ssl-certificate-verify-failed-certificate-verify def fixcert(): wd = os.getcwd() diff --git a/static/default.tmpl b/static/default.tmpl index b2576c9..5455039 100644 --- a/static/default.tmpl +++ b/static/default.tmpl @@ -3,7 +3,7 @@ print(""" Loading {{cookiecutter.title}} from {{cookiecutter.archive}}.apk Pygbag Version : {{cookiecutter.version}} - Template Version : 0.9.1 + Template Version : 0.9.2 Python : {{cookiecutter.PYBUILD}} CDN URL : {{cookiecutter.cdn}} Screen : {{cookiecutter.width}}x{{cookiecutter.height}} diff --git a/static/noctx.tmpl b/static/noctx.tmpl index bc0f7dc..a2eb068 100644 --- a/static/noctx.tmpl +++ b/static/noctx.tmpl @@ -3,7 +3,7 @@ print(""" Loading {{cookiecutter.title}} from {{cookiecutter.archive}}.apk Pygbag Version : {{cookiecutter.version}} - Template Version : 0.9.0 + Template Version : 0.9.2 Python : {{cookiecutter.PYBUILD}} CDN URL : {{cookiecutter.cdn}} Screen : {{cookiecutter.width}}x{{cookiecutter.height}}