workpeg 0.153.1__tar.gz → 0.154.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {workpeg-0.153.1/src/workpeg.egg-info → workpeg-0.154.0}/PKG-INFO +1 -1
- {workpeg-0.153.1 → workpeg-0.154.0}/pyproject.toml +1 -1
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/__init__.py +2 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/app.py +32 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/compiler/ast_parser.py +170 -2
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/compiler/js_generator.py +17 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/workpeg-runtime.js +537 -1
- {workpeg-0.153.1 → workpeg-0.154.0/src/workpeg.egg-info}/PKG-INFO +1 -1
- {workpeg-0.153.1 → workpeg-0.154.0}/LICENSE +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/MANIFEST.in +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/README.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/setup.cfg +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/auth.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/build.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/cdn.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/cli.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/config.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/context.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/create_new.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/doctor.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/project.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/run.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/runtime.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/sandbox/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/sandbox/docker_driver.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/sandbox/driver.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/submit.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/functions/Dockerfile +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/functions/LICENSE +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/functions/README.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/functions/app/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/functions/app/main.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/functions/requirements.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/README.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/function/Dockerfile +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/function/app/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/function/app/main.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/function/requirements.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/ui/main.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/templates/project/workpeg.json +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/terminal.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/actions.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/actions_manifest.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/build.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/calendar.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/compiler/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/compiler/ir.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/create.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/dates.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/dev_refused_names.json +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/devmode.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/devserve.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/devsite.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/fake_cloud.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/minify.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/render_nodes.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/renderers_manifest.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/ai-concurrent-replies.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/ai-prompt-queue.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/ai-question-frame.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/ai-subagent-frame.md +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/ai.js +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/editor.js +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/OFL.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic-ext.af944491.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic.88be6946.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin-ext.370a8962.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin.7611b95c.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-italic-vietnamese.8dfcf303.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic-ext.b7a545bb.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic.6129fc85.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin-ext.58a6b173.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin.9b6f5ff4.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/fonts/geist-normal-vietnamese.f689f638.woff2 +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/icons.css.gz +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/overlay.js +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/pegment.css +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/runtime/scaffold.js +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/scaffold.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/search_manifest.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/serve.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/skills_manifest.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/state.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/validators.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/watcher.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/__init__.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/base.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/canvas.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/display.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/fields.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/forms.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/futures.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/gestures.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/interactive.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/layout.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/ui/widgets/pickers.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg/utils.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg.egg-info/SOURCES.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg.egg-info/dependency_links.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg.egg-info/entry_points.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg.egg-info/requires.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/src/workpeg.egg-info/top_level.txt +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_auth.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_build.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_cdn.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_cdn_publish.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_changelog.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_changelog_guard.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_cli.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_config.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_context.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_create_new.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_doctor.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_project_create.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_release_script.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_run.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_runtime.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_submit.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_utils.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_version.py +0 -0
- {workpeg-0.153.1 → workpeg-0.154.0}/tests/test_watch_pipeline.py +0 -0
|
@@ -34,6 +34,7 @@ from workpeg.ui.app import (
|
|
|
34
34
|
navigate,
|
|
35
35
|
on_color_scheme_change,
|
|
36
36
|
on_shortcut,
|
|
37
|
+
on_table_change,
|
|
37
38
|
platform,
|
|
38
39
|
query,
|
|
39
40
|
reload,
|
|
@@ -327,6 +328,7 @@ __all__ = [
|
|
|
327
328
|
"max_date",
|
|
328
329
|
"on_color_scheme_change",
|
|
329
330
|
"on_shortcut",
|
|
331
|
+
"on_table_change",
|
|
330
332
|
"platform",
|
|
331
333
|
"http",
|
|
332
334
|
"Code",
|
|
@@ -574,6 +574,38 @@ def on_color_scheme_change(callback: Any) -> None:
|
|
|
574
574
|
"""
|
|
575
575
|
|
|
576
576
|
|
|
577
|
+
def on_table_change(callback: Any, *, table: Any = None) -> None:
|
|
578
|
+
"""Run ``callback`` (a ``self.<method>`` reference) when a table this page
|
|
579
|
+
can read changes, or a row in one does, so the page can fetch again::
|
|
580
|
+
|
|
581
|
+
class Orders(App):
|
|
582
|
+
rows = State([])
|
|
583
|
+
|
|
584
|
+
async def on_mount(self):
|
|
585
|
+
on_table_change(self.changed) # every table of this peg
|
|
586
|
+
await self.reload()
|
|
587
|
+
|
|
588
|
+
async def changed(self, event):
|
|
589
|
+
await self.reload() # whatever the kind
|
|
590
|
+
|
|
591
|
+
async def reload(self):
|
|
592
|
+
self.rows = await workpeg.get_rows(ORDERS, limit=100)
|
|
593
|
+
|
|
594
|
+
The method gets ``{"kind": ..., "table": ...}``. ``kind`` is ``"rows"``
|
|
595
|
+
(a row of ``table`` was added, changed or removed), ``"table"`` (that
|
|
596
|
+
table's name, columns or sharing changed; ``table`` is ``""`` when the
|
|
597
|
+
list of tables changed) or ``"resync"`` (changes may have been missed;
|
|
598
|
+
fetch everything again). The event never carries data: fetch it with
|
|
599
|
+
``workpeg.get_rows``, under the same permissions as any read.
|
|
600
|
+
|
|
601
|
+
Pass ``table=`` (a uuid, a module constant, State or a route param) to
|
|
602
|
+
hear one table only. Register it in ``on_mount`` of a page or a shell;
|
|
603
|
+
it is removed when the page goes. A page must still work if no event
|
|
604
|
+
ever comes. Compiled to ``Workpeg.onTableChange``; a no-op in plain
|
|
605
|
+
Python. See ``docs/data-realtime.md``.
|
|
606
|
+
"""
|
|
607
|
+
|
|
608
|
+
|
|
577
609
|
def on_shortcut(key: str, callback: Any, *, modifier: bool = True) -> None:
|
|
578
610
|
"""Run ``callback`` (a ``self.<method>`` reference) on a keyboard shortcut.
|
|
579
611
|
|
|
@@ -915,6 +915,55 @@ def _refuse_twin_import(node: ast.ImportFrom) -> None:
|
|
|
915
915
|
"import the dates capability itself: from workpeg.ui import dates")
|
|
916
916
|
|
|
917
917
|
|
|
918
|
+
#: A table's uuid, as on_table_change(table=...) takes it.
|
|
919
|
+
_TABLE_UUID_RE = re.compile(
|
|
920
|
+
r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
|
|
921
|
+
)
|
|
922
|
+
_LISTENER = "on_table_change"
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
def _refuse_rebound_listener(tree: ast.Module) -> None:
|
|
926
|
+
"""``on_table_change`` bound to anything but the SDK's own.
|
|
927
|
+
|
|
928
|
+
The call is dispatched by its name, so a local, a parameter, a ``def``,
|
|
929
|
+
a class or an import of another thing under that name would read as the
|
|
930
|
+
author's in the source and compile as the platform's on the page. The
|
|
931
|
+
one binding allowed is ``from workpeg.ui import on_table_change``, with
|
|
932
|
+
no alias, and an alias of it to another name is refused too, because
|
|
933
|
+
that name would compile as an unknown function."""
|
|
934
|
+
def refuse(node, how):
|
|
935
|
+
raise UnsupportedSyntaxError(
|
|
936
|
+
f"line {getattr(node, 'lineno', '?')}: {how} '{_LISTENER}', which "
|
|
937
|
+
"is the SDK's own call. Rename it; the only binding allowed is "
|
|
938
|
+
f"'from workpeg.ui import {_LISTENER}'.")
|
|
939
|
+
for node in ast.walk(tree):
|
|
940
|
+
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef,
|
|
941
|
+
ast.ClassDef)) and node.name == _LISTENER:
|
|
942
|
+
refuse(node, "a def or class cannot be called")
|
|
943
|
+
elif isinstance(node, ast.Name) and node.id == _LISTENER \
|
|
944
|
+
and isinstance(node.ctx, (ast.Store, ast.Del)):
|
|
945
|
+
refuse(node, "a name cannot be assigned as")
|
|
946
|
+
elif isinstance(node, ast.arg) and node.arg == _LISTENER:
|
|
947
|
+
refuse(node, "a parameter cannot be called")
|
|
948
|
+
elif isinstance(node, ast.ExceptHandler) and node.name == _LISTENER:
|
|
949
|
+
refuse(node, "an exception cannot be bound as")
|
|
950
|
+
elif (isinstance(node, (ast.MatchAs, ast.MatchStar))
|
|
951
|
+
and node.name == _LISTENER) or (
|
|
952
|
+
isinstance(node, ast.MatchMapping)
|
|
953
|
+
and node.rest == _LISTENER):
|
|
954
|
+
refuse(node, "a match pattern cannot capture")
|
|
955
|
+
elif isinstance(node, (ast.Import, ast.ImportFrom)):
|
|
956
|
+
ours = (isinstance(node, ast.ImportFrom)
|
|
957
|
+
and node.module in ("workpeg.ui", "workpeg.ui.app"))
|
|
958
|
+
for alias in node.names:
|
|
959
|
+
bound = alias.asname or alias.name.split(".")[0]
|
|
960
|
+
if alias.name == _LISTENER and alias.asname:
|
|
961
|
+
refuse(node, "an import cannot rename")
|
|
962
|
+
if bound == _LISTENER and not (ours
|
|
963
|
+
and alias.name == _LISTENER):
|
|
964
|
+
refuse(node, "an import cannot bind another thing as")
|
|
965
|
+
|
|
966
|
+
|
|
918
967
|
def _names_dates(node: ast.AST, *shadows) -> bool:
|
|
919
968
|
"""Whether ``node`` is the name ``dates`` meaning the capability.
|
|
920
969
|
|
|
@@ -1061,7 +1110,8 @@ _RUNTIME_INSTANCE_MEMBERS = frozenset((
|
|
|
1061
1110
|
"_applyProps", "_eventSource", "_files", "_frameCanvasEl", "_frameHandler",
|
|
1062
1111
|
"_frameOn", "_geoWatch", "_media", "_mic", "_mountEl", "_onEachMount",
|
|
1063
1112
|
"_ovEsc", "_ovOrd", "_pageTitle", "_schemeWatchers", "_shortcuts",
|
|
1064
|
-
"_socket", "_socketQueue", "_streams", "
|
|
1113
|
+
"_socket", "_socketQueue", "_streams", "_tableListeners", "_timers",
|
|
1114
|
+
"_workpegMounted",
|
|
1065
1115
|
"build", "on_mount", "on_unmount", "render",
|
|
1066
1116
|
))
|
|
1067
1117
|
|
|
@@ -1654,6 +1704,7 @@ class ASTParser:
|
|
|
1654
1704
|
for node in ast.walk(tree):
|
|
1655
1705
|
if isinstance(node, ast.ImportFrom):
|
|
1656
1706
|
_refuse_twin_import(node)
|
|
1707
|
+
_refuse_rebound_listener(tree)
|
|
1657
1708
|
|
|
1658
1709
|
# Icon subsetting: the set of literal icon names this build renders (so
|
|
1659
1710
|
# build.py can trim icons.css to just those), and whether any icon name
|
|
@@ -3136,6 +3187,7 @@ class ASTParser:
|
|
|
3136
3187
|
) -> "tuple[dict[str, ast.ClassDef], dict[str, str], dict[str, Any]]":
|
|
3137
3188
|
with open(file_path) as f:
|
|
3138
3189
|
tree = ast.parse(f.read(), filename=_display_path(file_path))
|
|
3190
|
+
_refuse_rebound_listener(tree)
|
|
3139
3191
|
self._scan_icon_literals(tree) # icon literals in imported modules
|
|
3140
3192
|
cdefs = {
|
|
3141
3193
|
n.name: n for n in ast.walk(tree) if isinstance(n, ast.ClassDef)
|
|
@@ -5521,6 +5573,7 @@ class ASTParser:
|
|
|
5521
5573
|
source = f.read()
|
|
5522
5574
|
try:
|
|
5523
5575
|
tree = ast.parse(source, filename=display)
|
|
5576
|
+
_refuse_rebound_listener(tree)
|
|
5524
5577
|
self._scan_icon_literals(tree) # icon literals in the page module
|
|
5525
5578
|
class_defs: dict[str, ast.ClassDef] = {}
|
|
5526
5579
|
for node in ast.walk(tree):
|
|
@@ -6268,6 +6321,7 @@ class ASTParser:
|
|
|
6268
6321
|
"socket.connect() url must be a ws:// or wss:// URL; "
|
|
6269
6322
|
f"got {url_node.value!r}"
|
|
6270
6323
|
)
|
|
6324
|
+
self._refuse_workpeg_stream(url_node, scope, "socket.connect")
|
|
6271
6325
|
data: dict[str, Any] = {
|
|
6272
6326
|
"url": self._parse_text_value(url_node, scope),
|
|
6273
6327
|
"on_message": None, "on_open": None, "on_close": None,
|
|
@@ -6334,6 +6388,7 @@ class ASTParser:
|
|
|
6334
6388
|
"events.connect() url must be an http:// or https:// "
|
|
6335
6389
|
f"URL (or a relative path); got {url_node.value!r}"
|
|
6336
6390
|
)
|
|
6391
|
+
self._refuse_workpeg_stream(url_node, scope, "events.connect")
|
|
6337
6392
|
data: dict[str, Any] = {
|
|
6338
6393
|
"url": self._parse_text_value(url_node, scope),
|
|
6339
6394
|
"on_message": None, "on_open": None, "on_error": None,
|
|
@@ -6608,6 +6663,9 @@ class ASTParser:
|
|
|
6608
6663
|
"handler": self._parse_method_ref(call.args[0]),
|
|
6609
6664
|
})
|
|
6610
6665
|
|
|
6666
|
+
if isinstance(call.func, ast.Name) and call.func.id == _LISTENER:
|
|
6667
|
+
return self._parse_table_listener(call, scope)
|
|
6668
|
+
|
|
6611
6669
|
if isinstance(call.func, ast.Name) and call.func.id == "on_shortcut":
|
|
6612
6670
|
if len(call.args) != 2:
|
|
6613
6671
|
raise UnsupportedSyntaxError(
|
|
@@ -6647,7 +6705,7 @@ class ASTParser:
|
|
|
6647
6705
|
f"Unsupported call in method '{method_name}': {fn_repr} "
|
|
6648
6706
|
"(supported: print(), set_interval/set_timeout(), set_theme(), "
|
|
6649
6707
|
"set_title(), on_color_scheme_change(), on_shortcut(), "
|
|
6650
|
-
"list methods)"
|
|
6708
|
+
"on_table_change(), list methods)"
|
|
6651
6709
|
)
|
|
6652
6710
|
if call.keywords:
|
|
6653
6711
|
raise UnsupportedSyntaxError(
|
|
@@ -11410,6 +11468,116 @@ class ASTParser:
|
|
|
11410
11468
|
return f"${{location.{inner.attr}}}"
|
|
11411
11469
|
return None
|
|
11412
11470
|
|
|
11471
|
+
def _refuse_workpeg_stream(self, node: ast.expr, scope: frozenset,
|
|
11472
|
+
what: str) -> None:
|
|
11473
|
+
"""A raw socket or event stream to a Workpeg host, written as a literal
|
|
11474
|
+
or a module constant. Workpeg's own sockets belong to the runtime: a
|
|
11475
|
+
page hears tables through on_table_change, whose token no page's code
|
|
11476
|
+
can hold. A dynamic url meets the same check in the runtime."""
|
|
11477
|
+
value = None
|
|
11478
|
+
if isinstance(node, ast.Constant):
|
|
11479
|
+
value = node.value
|
|
11480
|
+
elif (isinstance(node, ast.Name) and node.id not in scope
|
|
11481
|
+
and node.id in self._ctx_module_consts):
|
|
11482
|
+
value = self._ctx_module_consts[node.id]
|
|
11483
|
+
if isinstance(value, str) and _is_workpeg_host(value):
|
|
11484
|
+
raise UnsupportedSyntaxError(
|
|
11485
|
+
f"{what}() cannot open a Workpeg host ({value!r}); to hear "
|
|
11486
|
+
f"table and row changes, use {_LISTENER}()")
|
|
11487
|
+
|
|
11488
|
+
def _parse_table_listener(
|
|
11489
|
+
self, call: ast.Call, scope: frozenset
|
|
11490
|
+
) -> StatementIR:
|
|
11491
|
+
"""``on_table_change(self.method)`` or ``on_table_change(self.method,
|
|
11492
|
+
table=t)``.
|
|
11493
|
+
|
|
11494
|
+
There is no URL, token, header or socket to give it, and nothing that
|
|
11495
|
+
could take one: one positional, a method this class declares, and one
|
|
11496
|
+
keyword, the table. The runtime holds the connection and the token,
|
|
11497
|
+
and calls the method with ``{"kind", "table"}``."""
|
|
11498
|
+
usage = (f"{_LISTENER}(self.method, table=None) takes one self.method "
|
|
11499
|
+
"reference and an optional table; no URL, token or header")
|
|
11500
|
+
if self._current_component:
|
|
11501
|
+
raise UnsupportedSyntaxError(
|
|
11502
|
+
f"{_LISTENER}() cannot be used in a Component "
|
|
11503
|
+
f"('{self._current_component}'): a Component's on_mount never "
|
|
11504
|
+
"runs and nothing tears its listener down. Call it in the "
|
|
11505
|
+
"page's or the shell's on_mount.")
|
|
11506
|
+
if (len(call.args) != 1 or isinstance(call.args[0], ast.Starred)
|
|
11507
|
+
or any(k.arg != "table" for k in call.keywords)
|
|
11508
|
+
or len(call.keywords) > 1):
|
|
11509
|
+
raise UnsupportedSyntaxError(usage)
|
|
11510
|
+
node = call.args[0]
|
|
11511
|
+
if not (isinstance(node, ast.Attribute)
|
|
11512
|
+
and isinstance(node.value, ast.Name)
|
|
11513
|
+
and node.value.id == "self"):
|
|
11514
|
+
raise UnsupportedSyntaxError(
|
|
11515
|
+
f"{_LISTENER}() takes a self.method reference, got: "
|
|
11516
|
+
f"{ast.unparse(node)}")
|
|
11517
|
+
name = node.attr
|
|
11518
|
+
self._refuse_as_a_handler(name)
|
|
11519
|
+
if name in ("on_mount", "on_unmount"):
|
|
11520
|
+
# Every event would run on_mount again, which registers another
|
|
11521
|
+
# listener: they double with each change, and the page floods
|
|
11522
|
+
# get_rows until it freezes.
|
|
11523
|
+
raise UnsupportedSyntaxError(
|
|
11524
|
+
f"{_LISTENER}(self.{name}): the runtime calls {name} itself, "
|
|
11525
|
+
"and as a handler it would run on every change. Give the "
|
|
11526
|
+
"handler its own method.")
|
|
11527
|
+
declared = self._current_methods.get(name)
|
|
11528
|
+
if declared is None or _is_dunder(name) or name.startswith("__"):
|
|
11529
|
+
raise UnsupportedSyntaxError(
|
|
11530
|
+
f"{_LISTENER}(self.{name}): {name} is not a method of this "
|
|
11531
|
+
f"class. Declare def {name}(self, event): on it.")
|
|
11532
|
+
if declared.args.vararg or declared.args.kwarg \
|
|
11533
|
+
or len(declared.args.args) > 2 or declared.args.kwonlyargs:
|
|
11534
|
+
raise UnsupportedSyntaxError(
|
|
11535
|
+
f"{_LISTENER}(self.{name}): {name} takes one argument, the "
|
|
11536
|
+
"event, and no more: the runtime passes one.")
|
|
11537
|
+
for inner in ast.walk(declared):
|
|
11538
|
+
if (isinstance(inner, ast.Call) and isinstance(inner.func, ast.Name)
|
|
11539
|
+
and inner.func.id == _LISTENER):
|
|
11540
|
+
# Each event would add a listener, so they would double.
|
|
11541
|
+
raise UnsupportedSyntaxError(
|
|
11542
|
+
f"line {inner.lineno}: {_LISTENER}(self.{name}): {name} "
|
|
11543
|
+
f"calls {_LISTENER}() itself, so every change would add "
|
|
11544
|
+
"another listener. Register listeners in on_mount only.")
|
|
11545
|
+
table: Any = None
|
|
11546
|
+
dynamic = False
|
|
11547
|
+
if call.keywords:
|
|
11548
|
+
value = call.keywords[0].value
|
|
11549
|
+
literal = self._listener_literal(value, scope)
|
|
11550
|
+
if literal is _NO_CONST:
|
|
11551
|
+
table = self._parse_expr(value, scope)
|
|
11552
|
+
dynamic = True
|
|
11553
|
+
elif literal is None:
|
|
11554
|
+
table = None
|
|
11555
|
+
elif isinstance(literal, str) and _TABLE_UUID_RE.fullmatch(literal):
|
|
11556
|
+
table = literal
|
|
11557
|
+
else:
|
|
11558
|
+
raise UnsupportedSyntaxError(
|
|
11559
|
+
f"{_LISTENER}(table=...) must be a table's uuid; got "
|
|
11560
|
+
f"{literal!r}")
|
|
11561
|
+
return StatementIR(type="table_listener", data={
|
|
11562
|
+
"handler": name, "table": table, "dynamic": dynamic,
|
|
11563
|
+
})
|
|
11564
|
+
|
|
11565
|
+
def _listener_literal(self, node: ast.expr, scope: frozenset) -> Any:
|
|
11566
|
+
"""What ``table=`` is written as, or ``_NO_CONST`` when only the runtime
|
|
11567
|
+
knows. A module constant is inlined, so it counts as a literal, and so
|
|
11568
|
+
does anything that could never be a uuid (a list, a lambda, a sum)."""
|
|
11569
|
+
if isinstance(node, ast.Constant):
|
|
11570
|
+
return node.value
|
|
11571
|
+
if (isinstance(node, ast.Name) and node.id not in scope
|
|
11572
|
+
and node.id in self._ctx_module_consts):
|
|
11573
|
+
return self._ctx_module_consts[node.id]
|
|
11574
|
+
if isinstance(node, (ast.List, ast.Tuple, ast.Set, ast.Dict,
|
|
11575
|
+
ast.Lambda, ast.ListComp, ast.SetComp,
|
|
11576
|
+
ast.DictComp, ast.GeneratorExp, ast.UnaryOp,
|
|
11577
|
+
ast.BinOp, ast.JoinedStr, ast.Call, ast.Starred)):
|
|
11578
|
+
return ast.unparse(node)
|
|
11579
|
+
return _NO_CONST
|
|
11580
|
+
|
|
11413
11581
|
def _refuse_as_a_handler(self, name: str) -> None:
|
|
11414
11582
|
"""The two methods a class declares that cannot be handed anywhere."""
|
|
11415
11583
|
if name in self._current_helpers:
|
|
@@ -708,6 +708,23 @@ class JSGenerator:
|
|
|
708
708
|
handler = stmt.data["handler"]
|
|
709
709
|
return f"{pad}Workpeg.onColorScheme(this, () => this.{handler}());"
|
|
710
710
|
|
|
711
|
+
if stmt.type == "table_listener":
|
|
712
|
+
# Hear a table or row change; the runtime holds the token and the
|
|
713
|
+
# socket. The handler is a method of this class (the parser
|
|
714
|
+
# checked), and a dynamic table is made a string, so a value that
|
|
715
|
+
# is None at runtime is refused there instead of widening one
|
|
716
|
+
# table into all of them.
|
|
717
|
+
handler = stmt.data["handler"]
|
|
718
|
+
table = stmt.data["table"]
|
|
719
|
+
if table is None:
|
|
720
|
+
table_js = "null"
|
|
721
|
+
elif stmt.data.get("dynamic"):
|
|
722
|
+
table_js = f'("" + ({self._gen_expr(table)}))'
|
|
723
|
+
else:
|
|
724
|
+
table_js = json.dumps(table)
|
|
725
|
+
return (f"{pad}Workpeg.onTableChange(this, "
|
|
726
|
+
f"(e) => this.{handler}(e), {table_js});")
|
|
727
|
+
|
|
711
728
|
if stmt.type == "shortcut":
|
|
712
729
|
# A global keyboard shortcut, registered against the app so it's
|
|
713
730
|
# cleared on navigation. modifier=True → Cmd/Ctrl + key; False → a
|