@pyscript/core 0.6.68 → 0.6.70
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.
- package/dist/core-BEsfJxiI.js +4 -0
- package/dist/core-BEsfJxiI.js.map +1 -0
- package/dist/core.js +1 -1
- package/dist/{deprecations-manager-D1JCaoik.js → deprecations-manager-DmWjDzvH.js} +2 -2
- package/dist/{deprecations-manager-D1JCaoik.js.map → deprecations-manager-DmWjDzvH.js.map} +1 -1
- package/dist/{donkey-CM92-SJF.js → donkey-BuG4KY4c.js} +2 -2
- package/dist/{donkey-CM92-SJF.js.map → donkey-BuG4KY4c.js.map} +1 -1
- package/dist/{error-BkHvnmyu.js → error-CYIJakmx.js} +2 -2
- package/dist/{error-BkHvnmyu.js.map → error-CYIJakmx.js.map} +1 -1
- package/dist/{mpy-61tR9ZQz.js → mpy-BH-uExUr.js} +2 -2
- package/dist/{mpy-61tR9ZQz.js.map → mpy-BH-uExUr.js.map} +1 -1
- package/dist/{py-CzEKg5Vq.js → py-CZ57diPV.js} +2 -2
- package/dist/{py-CzEKg5Vq.js.map → py-CZ57diPV.js.map} +1 -1
- package/dist/{py-editor-C_VolMVE.js → py-editor-Dnbv5U4x.js} +2 -2
- package/dist/{py-editor-C_VolMVE.js.map → py-editor-Dnbv5U4x.js.map} +1 -1
- package/dist/{py-game-TYE6CZwh.js → py-game-C1AFmXwj.js} +2 -2
- package/dist/{py-game-TYE6CZwh.js.map → py-game-C1AFmXwj.js.map} +1 -1
- package/dist/{py-terminal-Bjixd_gx.js → py-terminal-DugA2dHL.js} +2 -2
- package/dist/{py-terminal-Bjixd_gx.js.map → py-terminal-DugA2dHL.js.map} +1 -1
- package/dist/zip-Cv6FEM4N.js +2 -0
- package/dist/zip-Cv6FEM4N.js.map +1 -0
- package/package.json +7 -7
- package/src/stdlib/pyscript/display.py +6 -5
- package/src/stdlib/pyscript/ffi.py +6 -0
- package/src/stdlib/pyscript/fs.py +22 -0
- package/src/stdlib/pyscript/magic_js.py +5 -1
- package/src/stdlib/pyscript/storage.py +2 -1
- package/src/stdlib/pyscript/web.py +7 -5
- package/src/stdlib/pyscript/websocket.py +22 -5
- package/src/stdlib/pyscript.js +7 -7
- package/src/sync.js +12 -1
- package/types/core.d.ts +1 -1
- package/types/sync.d.ts +8 -2
- package/dist/core-BL6RO8cs.js +0 -4
- package/dist/core-BL6RO8cs.js.map +0 -1
- package/dist/zip-BU7PvvcW.js +0 -2
- package/dist/zip-BU7PvvcW.js.map +0 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@pyscript/core",
|
3
|
-
"version": "0.6.
|
3
|
+
"version": "0.6.70",
|
4
4
|
"type": "module",
|
5
5
|
"description": "PyScript",
|
6
6
|
"module": "./index.js",
|
@@ -42,7 +42,7 @@
|
|
42
42
|
},
|
43
43
|
"scripts": {
|
44
44
|
"server": "echo \"➡️ TESTS @ $(tput bold)http://localhost:8080/tests/$(tput sgr0)\"; npx static-handler --coi .",
|
45
|
-
"build": "export ESLINT_USE_FLAT_CONFIG=true;npm run build:3rd-party && npm run build:stdlib && npm run build:plugins && npm run build:core && npm run build:tests-index && if [ -z \"$NO_MIN\" ]; then eslint src/ && npm run
|
45
|
+
"build": "export ESLINT_USE_FLAT_CONFIG=true;npm run build:3rd-party && npm run build:stdlib && npm run build:plugins && npm run build:core && npm run build:tests-index && if [ -z \"$NO_MIN\" ]; then eslint src/ && npm run test:integration; fi",
|
46
46
|
"build:core": "rm -rf dist && rollup --config rollup/core.config.js && cp src/3rd-party/*.css dist/",
|
47
47
|
"build:flatted": "node rollup/flatted.cjs",
|
48
48
|
"build:plugins": "node rollup/plugins.cjs",
|
@@ -70,7 +70,7 @@
|
|
70
70
|
"@webreflection/utils": "^0.1.1",
|
71
71
|
"add-promise-listener": "^0.1.3",
|
72
72
|
"basic-devtools": "^0.1.6",
|
73
|
-
"polyscript": "^0.18.
|
73
|
+
"polyscript": "^0.18.9",
|
74
74
|
"sticky-module": "^0.1.1",
|
75
75
|
"to-json-callback": "^0.1.1",
|
76
76
|
"type-checked-collections": "^0.1.7"
|
@@ -81,7 +81,7 @@
|
|
81
81
|
"@codemirror/language": "^6.11.2",
|
82
82
|
"@codemirror/state": "^6.5.2",
|
83
83
|
"@codemirror/view": "^6.38.1",
|
84
|
-
"@playwright/test": "^1.54.
|
84
|
+
"@playwright/test": "^1.54.2",
|
85
85
|
"@rollup/plugin-commonjs": "^28.0.6",
|
86
86
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
87
87
|
"@rollup/plugin-terser": "^0.4.4",
|
@@ -93,14 +93,14 @@
|
|
93
93
|
"chokidar": "^4.0.3",
|
94
94
|
"codedent": "^0.1.2",
|
95
95
|
"codemirror": "^6.0.2",
|
96
|
-
"eslint": "^9.
|
96
|
+
"eslint": "^9.32.0",
|
97
97
|
"flatted": "^3.3.3",
|
98
|
-
"rollup": "^4.
|
98
|
+
"rollup": "^4.46.2",
|
99
99
|
"rollup-plugin-postcss": "^4.0.2",
|
100
100
|
"rollup-plugin-string": "^3.0.0",
|
101
101
|
"static-handler": "^0.5.3",
|
102
102
|
"string-width": "^7.2.0",
|
103
|
-
"typescript": "^5.
|
103
|
+
"typescript": "^5.9.2",
|
104
104
|
"xterm-readline": "^1.1.2"
|
105
105
|
},
|
106
106
|
"repository": {
|
@@ -4,6 +4,7 @@ import io
|
|
4
4
|
import re
|
5
5
|
|
6
6
|
from pyscript.magic_js import current_target, document, window
|
7
|
+
from pyscript.ffi import is_none
|
7
8
|
|
8
9
|
_MIME_METHODS = {
|
9
10
|
"savefig": "image/png",
|
@@ -105,13 +106,13 @@ def _format_mime(obj):
|
|
105
106
|
else:
|
106
107
|
output = _eval_formatter(obj, method)
|
107
108
|
|
108
|
-
if output
|
109
|
+
if is_none(output):
|
109
110
|
continue
|
110
111
|
if mime_type not in _MIME_RENDERERS:
|
111
112
|
not_available.append(mime_type)
|
112
113
|
continue
|
113
114
|
break
|
114
|
-
if output
|
115
|
+
if is_none(output):
|
115
116
|
if not_available:
|
116
117
|
window.console.warn(
|
117
118
|
f"Rendered object requested unavailable MIME renderers: {not_available}"
|
@@ -135,7 +136,7 @@ def _write(element, value, append=False):
|
|
135
136
|
element.append(out_element)
|
136
137
|
else:
|
137
138
|
out_element = element.lastElementChild
|
138
|
-
if out_element
|
139
|
+
if is_none(out_element):
|
139
140
|
out_element = element
|
140
141
|
|
141
142
|
if mime_type in ("application/javascript", "text/html"):
|
@@ -146,7 +147,7 @@ def _write(element, value, append=False):
|
|
146
147
|
|
147
148
|
|
148
149
|
def display(*values, target=None, append=True):
|
149
|
-
if target
|
150
|
+
if is_none(target):
|
150
151
|
target = current_target()
|
151
152
|
elif not isinstance(target, str):
|
152
153
|
msg = f"target must be str or None, not {target.__class__.__name__}"
|
@@ -162,7 +163,7 @@ def display(*values, target=None, append=True):
|
|
162
163
|
element = document.getElementById(target)
|
163
164
|
|
164
165
|
# If target cannot be found on the page, a ValueError is raised
|
165
|
-
if element
|
166
|
+
if is_none(element):
|
166
167
|
msg = f"Invalid selector with id={target}. Cannot be found in the page."
|
167
168
|
raise ValueError(msg)
|
168
169
|
|
@@ -2,8 +2,10 @@ try:
|
|
2
2
|
import js
|
3
3
|
from pyodide.ffi import create_proxy as _cp
|
4
4
|
from pyodide.ffi import to_js as _py_tjs
|
5
|
+
from pyodide.ffi import jsnull
|
5
6
|
|
6
7
|
from_entries = js.Object.fromEntries
|
8
|
+
is_none = lambda value: value is None or value is jsnull
|
7
9
|
|
8
10
|
def _tjs(value, **kw):
|
9
11
|
if not hasattr(kw, "dict_converter"):
|
@@ -13,6 +15,10 @@ try:
|
|
13
15
|
except:
|
14
16
|
from jsffi import create_proxy as _cp
|
15
17
|
from jsffi import to_js as _tjs
|
18
|
+
import js
|
19
|
+
|
20
|
+
jsnull = js.Object.getPrototypeOf(js.Object.prototype)
|
21
|
+
is_none = lambda value: value is None or value is jsnull
|
16
22
|
|
17
23
|
create_proxy = _cp
|
18
24
|
to_js = _tjs
|
@@ -49,6 +49,28 @@ async def mount(path, mode="readwrite", root="", id="pyscript"):
|
|
49
49
|
mounted[path] = await interpreter.mountNativeFS(path, handler)
|
50
50
|
|
51
51
|
|
52
|
+
async def revoke(path, id="pyscript"):
|
53
|
+
from _pyscript import fs, interpreter
|
54
|
+
from pyscript.magic_js import (
|
55
|
+
RUNNING_IN_WORKER,
|
56
|
+
sync,
|
57
|
+
)
|
58
|
+
|
59
|
+
uid = f"{path}@{id}"
|
60
|
+
|
61
|
+
if RUNNING_IN_WORKER:
|
62
|
+
had = sync.deleteFSHandler(uid)
|
63
|
+
else:
|
64
|
+
had = await fs.idb.has(uid)
|
65
|
+
if had:
|
66
|
+
had = await fs.idb.delete(uid)
|
67
|
+
|
68
|
+
if had:
|
69
|
+
interpreter._module.FS.unmount(path)
|
70
|
+
|
71
|
+
return had
|
72
|
+
|
73
|
+
|
52
74
|
async def sync(path):
|
53
75
|
await mounted[path].syncfs()
|
54
76
|
|
@@ -67,7 +67,11 @@ if RUNNING_IN_WORKER:
|
|
67
67
|
|
68
68
|
else:
|
69
69
|
import _pyscript
|
70
|
-
from _pyscript import PyWorker, js_import
|
70
|
+
from _pyscript import PyWorker as _PyWorker, js_import
|
71
|
+
from pyscript.ffi import to_js
|
72
|
+
|
73
|
+
def PyWorker(url, **kw):
|
74
|
+
return _PyWorker(url, to_js(kw))
|
71
75
|
|
72
76
|
window = globalThis
|
73
77
|
document = globalThis.document
|
@@ -1,11 +1,12 @@
|
|
1
1
|
from polyscript import storage as _storage
|
2
2
|
from pyscript.flatted import parse as _parse
|
3
3
|
from pyscript.flatted import stringify as _stringify
|
4
|
+
from pyscript.ffi import is_none
|
4
5
|
|
5
6
|
|
6
7
|
# convert a Python value into an IndexedDB compatible entry
|
7
8
|
def _to_idb(value):
|
8
|
-
if value
|
9
|
+
if is_none(value):
|
9
10
|
return _stringify(["null", 0])
|
10
11
|
if isinstance(value, (bool, float, int, str, list, dict, tuple)):
|
11
12
|
return _stringify(["generic", value])
|
@@ -6,7 +6,7 @@
|
|
6
6
|
# from __future__ import annotations # CAUTION: This is not supported in MicroPython.
|
7
7
|
|
8
8
|
from pyscript import document, when, Event # noqa: F401
|
9
|
-
from pyscript.ffi import create_proxy
|
9
|
+
from pyscript.ffi import create_proxy, is_none
|
10
10
|
|
11
11
|
|
12
12
|
def wrap_dom_element(dom_element):
|
@@ -68,8 +68,10 @@ class Element:
|
|
68
68
|
If `dom_element` is None we are being called to *create* a new element.
|
69
69
|
Otherwise, we are being called to *wrap* an existing DOM element.
|
70
70
|
"""
|
71
|
-
self._dom_element =
|
72
|
-
type(self).get_tag_name()
|
71
|
+
self._dom_element = (
|
72
|
+
document.createElement(type(self).get_tag_name())
|
73
|
+
if is_none(dom_element)
|
74
|
+
else dom_element
|
73
75
|
)
|
74
76
|
|
75
77
|
# HTML on_events attached to the element become pyscript.Event instances.
|
@@ -195,7 +197,7 @@ class Element:
|
|
195
197
|
@property
|
196
198
|
def parent(self):
|
197
199
|
"""Return the element's `parent `Element`."""
|
198
|
-
if self._dom_element.parentElement
|
200
|
+
if is_none(self._dom_element.parentElement):
|
199
201
|
return None
|
200
202
|
|
201
203
|
return Element.wrap_dom_element(self._dom_element.parentElement)
|
@@ -1134,7 +1136,7 @@ class video(ContainerElement):
|
|
1134
1136
|
width = width if width is not None else self.videoWidth
|
1135
1137
|
height = height if height is not None else self.videoHeight
|
1136
1138
|
|
1137
|
-
if to
|
1139
|
+
if is_none(to):
|
1138
1140
|
to = canvas(width=width, height=height)
|
1139
1141
|
|
1140
1142
|
elif isinstance(to, Element):
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import js
|
2
2
|
from pyscript.ffi import create_proxy
|
3
|
-
from pyscript.util import as_bytearray
|
3
|
+
from pyscript.util import as_bytearray, is_awaitable
|
4
4
|
|
5
5
|
code = "code"
|
6
6
|
protocols = "protocols"
|
@@ -8,6 +8,23 @@ reason = "reason"
|
|
8
8
|
methods = ["onclose", "onerror", "onmessage", "onopen"]
|
9
9
|
|
10
10
|
|
11
|
+
def add_listener(socket, onevent, listener):
|
12
|
+
p = create_proxy(listener)
|
13
|
+
|
14
|
+
if is_awaitable(listener):
|
15
|
+
|
16
|
+
async def wrapper(e):
|
17
|
+
await p(EventMessage(e))
|
18
|
+
|
19
|
+
m = wrapper
|
20
|
+
|
21
|
+
else:
|
22
|
+
m = lambda e: p(EventMessage(e))
|
23
|
+
|
24
|
+
# Pyodide fails at setting socket[onevent] directly
|
25
|
+
setattr(socket, onevent, m)
|
26
|
+
|
27
|
+
|
11
28
|
class EventMessage:
|
12
29
|
def __init__(self, event):
|
13
30
|
self._event = event
|
@@ -36,20 +53,20 @@ class WebSocket:
|
|
36
53
|
socket = js.WebSocket.new(url, kw[protocols])
|
37
54
|
else:
|
38
55
|
socket = js.WebSocket.new(url)
|
56
|
+
|
57
|
+
socket.binaryType = "arraybuffer"
|
39
58
|
object.__setattr__(self, "_ws", socket)
|
40
59
|
|
41
60
|
for t in methods:
|
42
61
|
if t in kw:
|
43
|
-
|
44
|
-
setattr(socket, t, create_proxy(kw[t]))
|
62
|
+
add_listener(socket, t, kw[t])
|
45
63
|
|
46
64
|
def __getattr__(self, attr):
|
47
65
|
return getattr(self._ws, attr)
|
48
66
|
|
49
67
|
def __setattr__(self, attr, value):
|
50
68
|
if attr in methods:
|
51
|
-
|
52
|
-
setattr(self._ws, attr, create_proxy(m))
|
69
|
+
add_listener(self._ws, attr, value)
|
53
70
|
else:
|
54
71
|
setattr(self._ws, attr, value)
|
55
72
|
|
package/src/stdlib/pyscript.js
CHANGED
@@ -2,18 +2,18 @@
|
|
2
2
|
export default {
|
3
3
|
"pyscript": {
|
4
4
|
"__init__.py": "from polyscript import lazy_py_modules as py_import\nfrom pyscript.magic_js import RUNNING_IN_WORKER,PyWorker,config,current_target,document,js_import,js_modules,sync,window\nfrom pyscript.display import HTML,display\nfrom pyscript.fetch import fetch\nfrom pyscript.storage import Storage,storage\nfrom pyscript.websocket import WebSocket\nfrom pyscript.events import when,Event\nif not RUNNING_IN_WORKER:from pyscript.workers import create_named_worker,workers",
|
5
|
-
"display.py": "
|
5
|
+
"display.py": "_K='_repr_mimebundle_'\n_J='image/svg+xml'\n_I='application/json'\n_H='__repr__'\n_G='savefig'\n_F='text/html'\n_E='image/jpeg'\n_D='application/javascript'\n_C='utf-8'\n_B='text/plain'\n_A='image/png'\nimport base64,html,io,re\nfrom pyscript.magic_js import current_target,document,window\nfrom pyscript.ffi import is_none\n_MIME_METHODS={_G:_A,'_repr_javascript_':_D,'_repr_json_':_I,'_repr_latex':'text/latex','_repr_png_':_A,'_repr_jpeg_':_E,'_repr_pdf_':'application/pdf','_repr_svg_':_J,'_repr_markdown_':'text/markdown','_repr_html_':_F,_H:_B}\ndef _render_image(mime,value,meta):\n\tA=value\n\tif isinstance(A,bytes):A=base64.b64encode(A).decode(_C)\n\tB=re.compile('^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$')\n\tif len(A)>0 and not B.match(A):A=base64.b64encode(A.encode(_C)).decode(_C)\n\tC=f\"data:{mime};charset=utf-8;base64,{A}\";D=' '.join(['{k}=\"{v}\"'for(A,B)in meta.items()]);return f'<img src=\"{C}\" {D}></img>'\ndef _identity(value,meta):return value\n_MIME_RENDERERS={_B:html.escape,_F:_identity,_A:lambda value,meta:_render_image(_A,value,meta),_E:lambda value,meta:_render_image(_E,value,meta),_J:_identity,_I:_identity,_D:lambda value,meta:f\"<script>{value}<\\\\/script>\"}\nclass HTML:\n\tdef __init__(A,html):A._html=html\n\tdef _repr_html_(A):return A._html\ndef _eval_formatter(obj,print_method):\n\tB=obj;A=print_method\n\tif A==_H:return repr(B)\n\tif hasattr(B,A):\n\t\tif A==_G:C=io.BytesIO();B.savefig(C,format='png');C.seek(0);return base64.b64encode(C.read()).decode(_C)\n\t\treturn getattr(B,A)()\n\tif A==_K:return{},{}\ndef _format_mime(obj):\n\tC=obj\n\tif isinstance(C,str):return html.escape(C),_B\n\tD=_eval_formatter(C,_K)\n\tif isinstance(D,tuple):E,I=D\n\telse:E=D\n\tA,F=None,[]\n\tfor(H,B)in _MIME_METHODS.items():\n\t\tif B in E:A=E[B]\n\t\telse:A=_eval_formatter(C,H)\n\t\tif is_none(A):continue\n\t\tif B not in _MIME_RENDERERS:F.append(B);continue\n\t\tbreak\n\tif is_none(A):\n\t\tif F:window.console.warn(f\"Rendered object requested unavailable MIME renderers: {F}\")\n\t\tA=repr(A);B=_B\n\telif isinstance(A,tuple):A,G=A\n\telse:G={}\n\treturn _MIME_RENDERERS[B](A,G),B\ndef _write(element,value,append=False):\n\tB=element;C,D=_format_mime(value)\n\tif C=='\\\\n':return\n\tif append:A=document.createElement('div');B.append(A)\n\telse:\n\t\tA=B.lastElementChild\n\t\tif is_none(A):A=B\n\tif D in(_D,_F):E=document.createRange().createContextualFragment(C);A.append(E)\n\telse:A.innerHTML=C\ndef display(*E,target=None,append=True):\n\tD=append;A=target\n\tif is_none(A):A=current_target()\n\telif not isinstance(A,str):C=f\"target must be str or None, not {A.__class__.__name__}\";raise TypeError(C)\n\telif A=='':C='Cannot have an empty target';raise ValueError(C)\n\telif A.startswith('#'):A=A[1:]\n\tB=document.getElementById(A)\n\tif is_none(B):C=f\"Invalid selector with id={A}. Cannot be found in the page.\";raise ValueError(C)\n\tif B.tagName=='SCRIPT'and hasattr(B,'target'):B=B.target\n\tfor F in E:\n\t\tif not D:B.replaceChildren()\n\t\t_write(B,F,append=D)",
|
6
6
|
"events.py": "import asyncio,inspect,sys\nfrom functools import wraps\nfrom pyscript.magic_js import document\nfrom pyscript.ffi import create_proxy\nfrom pyscript.util import is_awaitable\nfrom pyscript import config\nclass Event:\n\tdef __init__(A):A._listeners=[]\n\tdef trigger(C,result):\n\t\tB=result\n\t\tfor A in C._listeners:\n\t\t\tif is_awaitable(A):asyncio.create_task(A(B))\n\t\t\telse:A(B)\n\tdef add_listener(B,listener):\n\t\tA=listener\n\t\tif is_awaitable(A)or callable(A):\n\t\t\tif A not in B._listeners:B._listeners.append(A)\n\t\telse:C='Listener must be callable or awaitable.';raise ValueError(C)\n\tdef remove_listener(A,*B):\n\t\tif B:\n\t\t\tfor C in B:A._listeners.remove(C)\n\t\telse:A._listeners=[]\ndef when(target,*B,**D):\n\tG='handler';C=target;E=None\n\tif B and(callable(B[0])or is_awaitable(B[0])):E=B[0]\n\telif callable(D.get(G))or is_awaitable(D.get(G)):E=D.pop(G)\n\tif isinstance(C,str):\n\t\tA=B[0]if B else D.pop('selector')\n\t\tif not A:I='No selector provided.';raise ValueError(I)\n\t\tfrom pyscript.web import Element as J,ElementCollection as K\n\t\tif isinstance(A,str):F=document.querySelectorAll(A)\n\t\telif isinstance(A,J):F=[A._dom_element]\n\t\telif isinstance(A,K):F=[A._dom_element for A in A]\n\t\telse:F=A if isinstance(A,list)else[A]\n\tdef H(func):\n\t\tE='positional arguments';D='takes';A=func\n\t\tif config['type']=='mpy':\n\t\t\tif is_awaitable(A):\n\t\t\t\tasync def B(*C,**F):\n\t\t\t\t\ttry:return await A(*C,**F)\n\t\t\t\t\texcept TypeError as B:\n\t\t\t\t\t\tif D in str(B)and E in str(B):return await A()\n\t\t\t\t\t\traise\n\t\t\telse:\n\t\t\t\tdef B(*C,**F):\n\t\t\t\t\ttry:return A(*C,**F)\n\t\t\t\t\texcept TypeError as B:\n\t\t\t\t\t\tif D in str(B)and E in str(B):return A()\n\t\t\t\t\t\traise\n\t\telse:\n\t\t\tG=inspect.signature(A)\n\t\t\tif G.parameters:\n\t\t\t\tif is_awaitable(A):\n\t\t\t\t\tasync def B(event):return await A(event)\n\t\t\t\telse:B=A\n\t\t\telif is_awaitable(A):\n\t\t\t\tasync def B(*B,**C):return await A()\n\t\t\telse:\n\t\t\t\tdef B(*B,**C):return A()\n\t\tB=wraps(A)(B)\n\t\tif isinstance(C,Event):C.add_listener(B)\n\t\telif isinstance(C,list)and all(isinstance(A,Event)for A in C):\n\t\t\tfor H in C:H.add_listener(B)\n\t\telse:\n\t\t\tfor I in F:I.addEventListener(C,create_proxy(B))\n\t\treturn B\n\treturn H(E)if E else H",
|
7
7
|
"fetch.py": "import json,js\nfrom pyscript.util import as_bytearray\nclass _Response:\n\tdef __init__(A,response):A._response=response\n\tdef __getattr__(A,attr):return getattr(A._response,attr)\n\tasync def arrayBuffer(B):\n\t\tA=await B._response.arrayBuffer()\n\t\tif hasattr(A,'to_py'):return A.to_py()\n\t\treturn memoryview(as_bytearray(A))\n\tasync def blob(A):return await A._response.blob()\n\tasync def bytearray(A):B=await A._response.arrayBuffer();return as_bytearray(B)\n\tasync def json(A):return json.loads(await A.text())\n\tasync def text(A):return await A._response.text()\nclass _DirectResponse:\n\t@staticmethod\n\tdef setup(promise,response):A=promise;A._response=_Response(response);return A._response\n\tdef __init__(B,promise):A=promise;B._promise=A;A._response=None;A.arrayBuffer=B.arrayBuffer;A.blob=B.blob;A.bytearray=B.bytearray;A.json=B.json;A.text=B.text\n\tasync def _response(A):\n\t\tif not A._promise._response:await A._promise\n\t\treturn A._promise._response\n\tasync def arrayBuffer(A):B=await A._response();return await B.arrayBuffer()\n\tasync def blob(A):B=await A._response();return await B.blob()\n\tasync def bytearray(A):B=await A._response();return await B.bytearray()\n\tasync def json(A):B=await A._response();return await B.json()\n\tasync def text(A):B=await A._response();return await B.text()\ndef fetch(url,**B):C=js.JSON.parse(json.dumps(B));D=lambda response,*B:_DirectResponse.setup(A,response);A=js.fetch(url,C).then(D);_DirectResponse(A);return A",
|
8
|
-
"ffi.py": "try:\n\timport js;from pyodide.ffi import create_proxy as _cp,to_js as _py_tjs;from_entries=js.Object.fromEntries\n\tdef _tjs(value,**A):\n\t\tB='dict_converter'\n\t\tif not hasattr(A,B):A[B]=from_entries\n\t\treturn _py_tjs(value,**A)\nexcept:from jsffi import create_proxy as _cp;from jsffi import to_js as _tjs\ncreate_proxy=_cp\nto_js=_tjs\ntry:\n\tfrom polyscript import ffi as _ffi;direct=_ffi.direct;gather=_ffi.gather;query=_ffi.query\n\tdef assign(source,*B):\n\t\tA=source\n\t\tfor C in B:_ffi.assign(A,to_js(C))\n\t\treturn A\nexcept:\n\timport js;_assign=js.Object.assign;direct=lambda source:source\n\tdef assign(source,*B):\n\t\tA=source\n\t\tfor C in B:_assign(A,to_js(C))\n\t\treturn A",
|
8
|
+
"ffi.py": "try:\n\timport js;from pyodide.ffi import create_proxy as _cp,to_js as _py_tjs,jsnull;from_entries=js.Object.fromEntries;is_none=lambda value:value is None or value is jsnull\n\tdef _tjs(value,**A):\n\t\tB='dict_converter'\n\t\tif not hasattr(A,B):A[B]=from_entries\n\t\treturn _py_tjs(value,**A)\nexcept:from jsffi import create_proxy as _cp;from jsffi import to_js as _tjs;import js;jsnull=js.Object.getPrototypeOf(js.Object.prototype);is_none=lambda value:value is None or value is jsnull\ncreate_proxy=_cp\nto_js=_tjs\ntry:\n\tfrom polyscript import ffi as _ffi;direct=_ffi.direct;gather=_ffi.gather;query=_ffi.query\n\tdef assign(source,*B):\n\t\tA=source\n\t\tfor C in B:_ffi.assign(A,to_js(C))\n\t\treturn A\nexcept:\n\timport js;_assign=js.Object.assign;direct=lambda source:source\n\tdef assign(source,*B):\n\t\tA=source\n\t\tfor C in B:_assign(A,to_js(C))\n\t\treturn A",
|
9
9
|
"flatted.py": "import json as _json\nclass _Known:\n\tdef __init__(A):A.key=[];A.value=[]\nclass _String:\n\tdef __init__(A,value):A.value=value\ndef _array_keys(value):\n\tA=[];B=0\n\tfor C in value:A.append(B);B+=1\n\treturn A\ndef _object_keys(value):\n\tA=[]\n\tfor B in value:A.append(B)\n\treturn A\ndef _is_array(value):return isinstance(value,(list,tuple))\ndef _is_object(value):return isinstance(value,dict)\ndef _is_string(value):return isinstance(value,str)\ndef _index(known,input,value):B=value;A=known;input.append(B);C=str(len(input)-1);A.key.append(B);A.value.append(C);return C\ndef _loop(keys,input,known,output):\n\tA=output\n\tfor B in keys:\n\t\tC=A[B]\n\t\tif isinstance(C,_String):_ref(B,input[int(C.value)],input,known,A)\n\treturn A\ndef _ref(key,value,input,known,output):\n\tB=known;A=value\n\tif _is_array(A)and A not in B:B.append(A);A=_loop(_array_keys(A),input,B,A)\n\telif _is_object(A)and A not in B:B.append(A);A=_loop(_object_keys(A),input,B,A)\n\toutput[key]=A\ndef _relate(known,input,value):\n\tB=known;A=value\n\tif _is_string(A)or _is_array(A)or _is_object(A):\n\t\ttry:return B.value[B.key.index(A)]\n\t\texcept:return _index(B,input,A)\n\treturn A\ndef _transform(known,input,value):\n\tB=known;A=value\n\tif _is_array(A):\n\t\tC=[]\n\t\tfor F in A:C.append(_relate(B,input,F))\n\t\treturn C\n\tif _is_object(A):\n\t\tD={}\n\t\tfor E in A:D[E]=_relate(B,input,A[E])\n\t\treturn D\n\treturn A\ndef _wrap(value):\n\tA=value\n\tif _is_string(A):return _String(A)\n\tif _is_array(A):\n\t\tB=0\n\t\tfor D in A:A[B]=_wrap(D);B+=1\n\telif _is_object(A):\n\t\tfor C in A:A[C]=_wrap(A[C])\n\treturn A\ndef parse(value,*C,**D):\n\tA=value;E=_json.loads(A,*C,**D);B=[]\n\tfor A in E:B.append(_wrap(A))\n\tinput=[]\n\tfor A in B:\n\t\tif isinstance(A,_String):input.append(A.value)\n\t\telse:input.append(A)\n\tA=input[0]\n\tif _is_array(A):return _loop(_array_keys(A),input,[A],A)\n\tif _is_object(A):return _loop(_object_keys(A),input,[A],A)\n\treturn A\ndef stringify(value,*D,**E):\n\tB=_Known();input=[];C=[];A=int(_index(B,input,value))\n\twhile A<len(input):C.append(_transform(B,input,input[A]));A+=1\n\treturn _json.dumps(C,*D,**E)",
|
10
|
-
"fs.py": "
|
11
|
-
"magic_js.py": "import json,sys,js as globalThis\nfrom polyscript import config as _config,js_modules\nfrom pyscript.util import NotSupported\nRUNNING_IN_WORKER=not hasattr(globalThis,'document')\nconfig=json.loads(globalThis.JSON.stringify(_config))\nif'MicroPython'in sys.version:config['type']='mpy'\nelse:config['type']='py'\nclass JSModule:\n\tdef __init__(A,name):A.name=name\n\tdef __getattr__(B,field):\n\t\tA=field\n\t\tif not A.startswith('_'):return getattr(getattr(js_modules,B.name),A)\nfor name in globalThis.Reflect.ownKeys(js_modules):sys.modules[f\"pyscript.js_modules.{name}\"]=JSModule(name)\nsys.modules['pyscript.js_modules']=js_modules\nif RUNNING_IN_WORKER:\n\timport polyscript;PyWorker=NotSupported('pyscript.PyWorker','pyscript.PyWorker works only when running in the main thread')\n\ttry:import js;window=polyscript.xworker.window;document=window.document;js.document=document;js_import=window.Function('return (...urls) => Promise.all(urls.map((url) => import(url)))')()\n\texcept:message='Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer';globalThis.console.warn(message);window=NotSupported('pyscript.window',message);document=NotSupported('pyscript.document',message);js_import=None\n\tsync=polyscript.xworker.sync\n\tdef current_target():return polyscript.target\nelse:\n\timport _pyscript;from _pyscript import PyWorker,js_import;
|
10
|
+
"fs.py": "_A='pyscript'\nmounted={}\nasync def mount(path,mode='readwrite',root='',id=_A):\n\tE=path;import js;from _pyscript import fs as A,interpreter as I;from pyscript.ffi import to_js as H;from pyscript.magic_js import RUNNING_IN_WORKER as J,sync;js.console.warn('experimental pyscript.fs ⚠️');B=None;C=f\"{E}@{id}\";F={'id':id,'mode':mode}\n\tif root!='':F['startIn']=root\n\tif J:\n\t\tG=sync.storeFSHandler(C,H(F))\n\t\tif isinstance(G,bool):D=G\n\t\telse:D=await G\n\t\tif D:from polyscript import IDBMap as K;L=K.new(A.NAMESPACE);B=await L.get(C)\n\t\telse:raise RuntimeError(A.ERROR)\n\telse:\n\t\tD=await A.idb.has(C)\n\t\tif D:B=await A.idb.get(C)\n\t\telse:B=await A.getFileSystemDirectoryHandle(H(F));await A.idb.set(C,B)\n\tmounted[E]=await I.mountNativeFS(E,B)\nasync def revoke(path,id=_A):\n\tfrom _pyscript import fs,interpreter as C;from pyscript.magic_js import RUNNING_IN_WORKER as D,sync;B=f\"{path}@{id}\"\n\tif D:A=sync.deleteFSHandler(B)\n\telse:\n\t\tA=await fs.idb.has(B)\n\t\tif A:A=await fs.idb.delete(B)\n\tif A:C._module.FS.unmount(path)\n\treturn A\nasync def sync(path):await mounted[path].syncfs()\nasync def unmount(path):from _pyscript import interpreter as A;await sync(path);A._module.FS.unmount(path)",
|
11
|
+
"magic_js.py": "import json,sys,js as globalThis\nfrom polyscript import config as _config,js_modules\nfrom pyscript.util import NotSupported\nRUNNING_IN_WORKER=not hasattr(globalThis,'document')\nconfig=json.loads(globalThis.JSON.stringify(_config))\nif'MicroPython'in sys.version:config['type']='mpy'\nelse:config['type']='py'\nclass JSModule:\n\tdef __init__(A,name):A.name=name\n\tdef __getattr__(B,field):\n\t\tA=field\n\t\tif not A.startswith('_'):return getattr(getattr(js_modules,B.name),A)\nfor name in globalThis.Reflect.ownKeys(js_modules):sys.modules[f\"pyscript.js_modules.{name}\"]=JSModule(name)\nsys.modules['pyscript.js_modules']=js_modules\nif RUNNING_IN_WORKER:\n\timport polyscript;PyWorker=NotSupported('pyscript.PyWorker','pyscript.PyWorker works only when running in the main thread')\n\ttry:import js;window=polyscript.xworker.window;document=window.document;js.document=document;js_import=window.Function('return (...urls) => Promise.all(urls.map((url) => import(url)))')()\n\texcept:message='Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer';globalThis.console.warn(message);window=NotSupported('pyscript.window',message);document=NotSupported('pyscript.document',message);js_import=None\n\tsync=polyscript.xworker.sync\n\tdef current_target():return polyscript.target\nelse:\n\timport _pyscript;from _pyscript import PyWorker as _PyWorker,js_import;from pyscript.ffi import to_js\n\tdef PyWorker(url,**A):return _PyWorker(url,to_js(A))\n\twindow=globalThis;document=globalThis.document;sync=NotSupported('pyscript.sync','pyscript.sync works only when running in a worker')\n\tdef current_target():return _pyscript.target",
|
12
12
|
"media.py": "from pyscript import window\nfrom pyscript.ffi import to_js\nclass Device:\n\tdef __init__(A,device):A._dom_element=device\n\t@property\n\tdef id(self):return self._dom_element.deviceId\n\t@property\n\tdef group(self):return self._dom_element.groupId\n\t@property\n\tdef kind(self):return self._dom_element.kind\n\t@property\n\tdef label(self):return self._dom_element.label\n\tdef __getitem__(A,key):return getattr(A,key)\n\t@classmethod\n\tasync def load(E,audio=False,video=True):\n\t\tC='video';B=video;A={};A['audio']=audio\n\t\tif isinstance(B,bool):A[C]=B\n\t\telse:\n\t\t\tA[C]={}\n\t\t\tfor D in B:A[C][D]=B[D]\n\t\treturn await window.navigator.mediaDevices.getUserMedia(to_js(A))\n\tasync def get_stream(A):B=A.kind.replace('input','').replace('output','');C={B:{'deviceId':{'exact':A.id}}};return await A.load(**C)\nasync def list_devices():return[Device(A)for A in await window.navigator.mediaDevices.enumerateDevices()]",
|
13
|
-
"storage.py": "_C='memoryview'\n_B='bytearray'\n_A='generic'\nfrom polyscript import storage as _storage\nfrom pyscript.flatted import parse as _parse\nfrom pyscript.flatted import stringify as _stringify\ndef _to_idb(value):\n\tA=value\n\tif A
|
13
|
+
"storage.py": "_C='memoryview'\n_B='bytearray'\n_A='generic'\nfrom polyscript import storage as _storage\nfrom pyscript.flatted import parse as _parse\nfrom pyscript.flatted import stringify as _stringify\nfrom pyscript.ffi import is_none\ndef _to_idb(value):\n\tA=value\n\tif is_none(A):return _stringify(['null',0])\n\tif isinstance(A,(bool,float,int,str,list,dict,tuple)):return _stringify([_A,A])\n\tif isinstance(A,bytearray):return _stringify([_B,list(A)])\n\tif isinstance(A,memoryview):return _stringify([_C,list(A)])\n\tB=f\"Unexpected value: {A}\";raise TypeError(B)\ndef _from_idb(value):\n\tC=value;A,B=_parse(C)\n\tif A=='null':return\n\tif A==_A:return B\n\tif A==_B:return bytearray(B)\n\tif A==_C:return memoryview(bytearray(B))\n\treturn C\nclass Storage(dict):\n\tdef __init__(B,store):A=store;super().__init__({A:_from_idb(B)for(A,B)in A.entries()});B.__store__=A\n\tdef __delitem__(A,attr):A.__store__.delete(attr);super().__delitem__(attr)\n\tdef __setitem__(B,attr,value):A=value;B.__store__.set(attr,_to_idb(A));super().__setitem__(attr,A)\n\tdef clear(A):A.__store__.clear();super().clear()\n\tasync def sync(A):await A.__store__.sync()\nasync def storage(name='',storage_class=Storage):\n\tif not name:A='The storage name must be defined';raise ValueError(A)\n\treturn storage_class(await _storage(f\"@pyscript/{name}\"))",
|
14
14
|
"util.py": "import js,sys,inspect\ndef as_bytearray(buffer):\n\tA=js.Uint8Array.new(buffer);B=A.length;C=bytearray(B)\n\tfor D in range(B):C[D]=A[D]\n\treturn C\nclass NotSupported:\n\tdef __init__(A,name,error):object.__setattr__(A,'name',name);object.__setattr__(A,'error',error)\n\tdef __repr__(A):return f\"<NotSupported {A.name} [{A.error}]>\"\n\tdef __getattr__(A,attr):raise AttributeError(A.error)\n\tdef __setattr__(A,attr,value):raise AttributeError(A.error)\n\tdef __call__(A,*B):raise TypeError(A.error)\ndef is_awaitable(obj):\n\tA=obj;from pyscript import config as B\n\tif B['type']=='mpy':\n\t\tif'<closure <generator>'in repr(A):return True\n\t\treturn inspect.isgeneratorfunction(A)\n\treturn inspect.iscoroutinefunction(A)",
|
15
|
-
"web.py": "_C='htmlFor'\n_B='on_'\n_A=None\nfrom pyscript import document,when,Event\nfrom pyscript.ffi import create_proxy\ndef wrap_dom_element(dom_element):return Element.wrap_dom_element(dom_element)\nclass Element:\n\telement_classes_by_tag_name={}\n\t@classmethod\n\tdef get_tag_name(A):return A.__name__.replace('_','')\n\t@classmethod\n\tdef register_element_classes(B,element_classes):\n\t\tfor A in element_classes:C=A.get_tag_name();B.element_classes_by_tag_name[C]=A\n\t@classmethod\n\tdef unregister_element_classes(A,element_classes):\n\t\tfor B in element_classes:C=B.get_tag_name();A.element_classes_by_tag_name.pop(C,_A)\n\t@classmethod\n\tdef wrap_dom_element(A,dom_element):B=dom_element;C=A.element_classes_by_tag_name.get(B.tagName.lower(),A);return C(dom_element=B)\n\tdef __init__(A,dom_element=_A,classes=_A,style=_A,**E):\n\t\tA._dom_element=dom_element or document.createElement(type(A).get_tag_name());A._on_events={};C={}\n\t\tfor(B,D)in E.items():\n\t\t\tif B.startswith(_B):F=A.get_event(B);F.add_listener(D)\n\t\t\telse:C[B]=D\n\t\tA._classes=Classes(A);A._style=Style(A);A.update(classes=classes,style=style,**C)\n\tdef __eq__(A,obj):return isinstance(obj,Element)and obj._dom_element==A._dom_element\n\tdef __getitem__(B,key):\n\t\tA=key\n\t\tif isinstance(A,(int,slice)):return B.children[A]\n\t\treturn B.find(A)\n\tdef __getattr__(B,name):\n\t\tA=name\n\t\tif A.startswith(_B):return B.get_event(A)\n\t\tif A.endswith('_'):A=A[:-1]\n\t\tif A=='for':A=_C\n\t\treturn getattr(B._dom_element,A)\n\tdef __setattr__(C,name,value):\n\t\tB=value;A=name\n\t\tif A.startswith('_'):super().__setattr__(A,B)\n\t\telse:\n\t\t\tif A.endswith('_'):A=A[:-1]\n\t\t\tif A=='for':A=_C\n\t\t\tif A.startswith(_B):C._on_events[A]=B\n\t\t\tsetattr(C._dom_element,A,B)\n\tdef get_event(A,name):\n\t\tB=name\n\t\tif not B.startswith(_B):C=\"Event names must start with 'on_'.\";raise ValueError(C)\n\t\tD=B[3:]\n\t\tif not hasattr(A._dom_element,D):C=f\"Element has no '{D}' event.\";raise ValueError(C)\n\t\tif B in A._on_events:return A._on_events[B]\n\t\tE=Event();A._on_events[B]=E;A._dom_element.addEventListener(D,create_proxy(E.trigger));return E\n\t@property\n\tdef children(self):return ElementCollection.wrap_dom_elements(self._dom_element.children)\n\t@property\n\tdef classes(self):return self._classes\n\t@property\n\tdef parent(self):\n\t\tif self._dom_element.parentElement is _A:return\n\t\treturn Element.wrap_dom_element(self._dom_element.parentElement)\n\t@property\n\tdef style(self):return self._style\n\tdef append(B,*C):\n\t\tfor A in C:\n\t\t\tif isinstance(A,Element):B._dom_element.appendChild(A._dom_element)\n\t\t\telif isinstance(A,ElementCollection):\n\t\t\t\tfor D in A:B._dom_element.appendChild(D._dom_element)\n\t\t\telif isinstance(A,(list,tuple)):\n\t\t\t\tfor E in A:B.append(E)\n\t\t\telse:\n\t\t\t\ttry:A.tagName;B._dom_element.appendChild(A)\n\t\t\t\texcept AttributeError:\n\t\t\t\t\ttry:\n\t\t\t\t\t\tA.length\n\t\t\t\t\t\tfor F in A:B._dom_element.appendChild(F)\n\t\t\t\t\texcept AttributeError:G=f'Element \"{A}\" is a proxy object, \"but not a valid element or a NodeList.';raise TypeError(G)\n\tdef clone(B,clone_id=_A):A=Element.wrap_dom_element(B._dom_element.cloneNode(True));A.id=clone_id;return A\n\tdef find(A,selector):return ElementCollection.wrap_dom_elements(A._dom_element.querySelectorAll(selector))\n\tdef show_me(A):A._dom_element.scrollIntoView()\n\tdef update(A,classes=_A,style=_A,**D):\n\t\tC=style;B=classes\n\t\tif B:A.classes.add(B)\n\t\tif C:A.style.set(**C)\n\t\tfor(E,F)in D.items():setattr(A,E,F)\nclass Classes:\n\tdef __init__(A,element):A._element=element;A._class_list=A._element._dom_element.classList\n\tdef __contains__(A,item):return item in A._class_list\n\tdef __eq__(C,other):\n\t\tA=other\n\t\tif isinstance(A,Classes):B=list(A._class_list)\n\t\telse:\n\t\t\ttry:B=iter(A)\n\t\t\texcept TypeError:return False\n\t\treturn set(C._class_list)==set(B)\n\tdef __iter__(A):return iter(A._class_list)\n\tdef __len__(A):return A._class_list.length\n\tdef __repr__(A):return f\"Classes({\", \".join(A._class_list)})\"\n\tdef __str__(A):return' '.join(A._class_list)\n\tdef add(B,*C):\n\t\tfor A in C:\n\t\t\tif isinstance(A,list):\n\t\t\t\tfor D in A:B.add(D)\n\t\t\telse:B._class_list.add(A)\n\tdef contains(A,class_name):return class_name in A\n\tdef remove(B,*C):\n\t\tfor A in C:\n\t\t\tif isinstance(A,list):\n\t\t\t\tfor D in A:B.remove(D)\n\t\t\telse:B._class_list.remove(A)\n\tdef replace(A,old_class,new_class):A.remove(old_class);A.add(new_class)\n\tdef toggle(A,*C):\n\t\tfor B in C:\n\t\t\tif B in A:A.remove(B)\n\t\t\telse:A.add(B)\nclass HasOptions:\n\t@property\n\tdef options(self):\n\t\tA=self\n\t\tif not hasattr(A,'_options'):A._options=Options(A)\n\t\treturn A._options\nclass Options:\n\tdef __init__(A,element):A._element=element\n\tdef __getitem__(A,key):return A.options[key]\n\tdef __iter__(A):yield from A.options\n\tdef __len__(A):return len(A.options)\n\tdef __repr__(A):return f\"{A.__class__.__name__} (length: {len(A)}) {A.options}\"\n\t@property\n\tdef options(self):return[Element.wrap_dom_element(A)for A in self._element._dom_element.options]\n\t@property\n\tdef selected(self):return self.options[self._element._dom_element.selectedIndex]\n\tdef add(D,value=_A,html=_A,text=_A,before=_A,**B):\n\t\tC=value;A=before\n\t\tif C is not _A:B['value']=C\n\t\tif html is not _A:B['innerHTML']=html\n\t\tif text is not _A:B['text']=text\n\t\tE=option(**B)\n\t\tif A and isinstance(A,Element):A=A._dom_element\n\t\tD._element._dom_element.add(E._dom_element,A)\n\tdef clear(A):\n\t\twhile len(A)>0:A.remove(0)\n\tdef remove(A,index):A._element._dom_element.remove(index)\nclass Style:\n\tdef __init__(A,element):A._element=element;A._style=A._element._dom_element.style\n\tdef __getitem__(A,key):return A._style.getPropertyValue(key)\n\tdef __setitem__(A,key,value):A._style.setProperty(key,value)\n\tdef remove(A,key):A._style.removeProperty(key)\n\tdef set(A,**B):\n\t\tfor(C,D)in B.items():A._element._dom_element.style.setProperty(C,D)\n\t@property\n\tdef visible(self):return self._element._dom_element.style.visibility\n\t@visible.setter\n\tdef visible(self,value):self._element._dom_element.style.visibility=value\nclass ContainerElement(Element):\n\tdef __init__(B,*C,children=_A,dom_element=_A,style=_A,classes=_A,**D):\n\t\tsuper().__init__(dom_element=dom_element,style=style,classes=classes,**D)\n\t\tfor A in list(C)+(children or[]):\n\t\t\tif isinstance(A,(Element,ElementCollection)):B.append(A)\n\t\t\telse:B._dom_element.insertAdjacentHTML('beforeend',A)\n\tdef __iter__(A):yield from A.children\nclass ClassesCollection:\n\tdef __init__(A,collection):A._collection=collection\n\tdef __contains__(A,class_name):\n\t\tfor B in A._collection:\n\t\t\tif class_name in B.classes:return True\n\t\treturn False\n\tdef __eq__(B,other):A=other;return isinstance(A,ClassesCollection)and B._collection==A._collection\n\tdef __iter__(A):yield from A._all_class_names()\n\tdef __len__(A):return len(A._all_class_names())\n\tdef __repr__(A):return f\"ClassesCollection({A._collection!r})\"\n\tdef __str__(A):return' '.join(A._all_class_names())\n\tdef add(A,*B):\n\t\tfor C in A._collection:C.classes.add(*B)\n\tdef contains(A,class_name):return class_name in A\n\tdef remove(A,*B):\n\t\tfor C in A._collection:C.classes.remove(*B)\n\tdef replace(A,old_class,new_class):\n\t\tfor B in A._collection:B.classes.replace(old_class,new_class)\n\tdef toggle(A,*B):\n\t\tfor C in A._collection:C.classes.toggle(*B)\n\tdef _all_class_names(B):\n\t\tA=set()\n\t\tfor C in B._collection:\n\t\t\tfor D in C.classes:A.add(D)\n\t\treturn A\nclass StyleCollection:\n\tdef __init__(A,collection):A._collection=collection\n\tdef __getitem__(A,key):return[A.style[key]for A in A._collection._elements]\n\tdef __setitem__(A,key,value):\n\t\tfor B in A._collection._elements:B.style[key]=value\n\tdef __repr__(A):return f\"StyleCollection({A._collection!r})\"\n\tdef remove(A,key):\n\t\tfor B in A._collection._elements:B.style.remove(key)\nclass ElementCollection:\n\t@classmethod\n\tdef wrap_dom_elements(A,dom_elements):return A([Element.wrap_dom_element(A)for A in dom_elements])\n\tdef __init__(A,elements):A._elements=elements;A._classes=ClassesCollection(A);A._style=StyleCollection(A)\n\tdef __eq__(A,obj):return isinstance(obj,ElementCollection)and obj._elements==A._elements\n\tdef __getitem__(B,key):\n\t\tA=key\n\t\tif isinstance(A,int):return B._elements[A]\n\t\tif isinstance(A,slice):return ElementCollection(B._elements[A])\n\t\treturn B.find(A)\n\tdef __iter__(A):yield from A._elements\n\tdef __len__(A):return len(A._elements)\n\tdef __repr__(A):return f\"{A.__class__.__name__} (length: {len(A._elements)}) {A._elements}\"\n\tdef __getattr__(A,name):return[getattr(A,name)for A in A._elements]\n\tdef __setattr__(C,name,value):\n\t\tB=value;A=name\n\t\tif A.startswith('_'):super().__setattr__(A,B)\n\t\telse:\n\t\t\tfor D in C._elements:setattr(D,A,B)\n\t@property\n\tdef classes(self):return self._classes\n\t@property\n\tdef elements(self):return self._elements\n\t@property\n\tdef style(self):return self._style\n\tdef find(B,selector):\n\t\tA=[]\n\t\tfor C in B._elements:A.extend(C.find(selector))\n\t\treturn ElementCollection(A)\nclass a(ContainerElement):0\nclass abbr(ContainerElement):0\nclass address(ContainerElement):0\nclass area(Element):0\nclass article(ContainerElement):0\nclass aside(ContainerElement):0\nclass audio(ContainerElement):0\nclass b(ContainerElement):0\nclass base(Element):0\nclass blockquote(ContainerElement):0\nclass body(ContainerElement):0\nclass br(Element):0\nclass button(ContainerElement):0\nclass canvas(ContainerElement):\n\tdef download(A,filename='snapped.png'):B=a(download=filename,href=A._dom_element.toDataURL());A.append(B);B._dom_element.click()\n\tdef draw(E,what,width=_A,height=_A):\n\t\tC=height;B=width;A=what\n\t\tif isinstance(A,Element):A=A._dom_element\n\t\tD=E._dom_element.getContext('2d')\n\t\tif B or C:D.drawImage(A,0,0,B,C)\n\t\telse:D.drawImage(A,0,0)\nclass caption(ContainerElement):0\nclass cite(ContainerElement):0\nclass code(ContainerElement):0\nclass col(Element):0\nclass colgroup(ContainerElement):0\nclass data(ContainerElement):0\nclass datalist(ContainerElement,HasOptions):0\nclass dd(ContainerElement):0\nclass del_(ContainerElement):0\nclass details(ContainerElement):0\nclass dialog(ContainerElement):0\nclass div(ContainerElement):0\nclass dl(ContainerElement):0\nclass dt(ContainerElement):0\nclass em(ContainerElement):0\nclass embed(Element):0\nclass fieldset(ContainerElement):0\nclass figcaption(ContainerElement):0\nclass figure(ContainerElement):0\nclass footer(ContainerElement):0\nclass form(ContainerElement):0\nclass h1(ContainerElement):0\nclass h2(ContainerElement):0\nclass h3(ContainerElement):0\nclass h4(ContainerElement):0\nclass h5(ContainerElement):0\nclass h6(ContainerElement):0\nclass head(ContainerElement):0\nclass header(ContainerElement):0\nclass hgroup(ContainerElement):0\nclass hr(Element):0\nclass html(ContainerElement):0\nclass i(ContainerElement):0\nclass iframe(ContainerElement):0\nclass img(Element):0\nclass input_(Element):0\nclass ins(ContainerElement):0\nclass kbd(ContainerElement):0\nclass label(ContainerElement):0\nclass legend(ContainerElement):0\nclass li(ContainerElement):0\nclass link(Element):0\nclass main(ContainerElement):0\nclass map_(ContainerElement):0\nclass mark(ContainerElement):0\nclass menu(ContainerElement):0\nclass meta(ContainerElement):0\nclass meter(ContainerElement):0\nclass nav(ContainerElement):0\nclass object_(ContainerElement):0\nclass ol(ContainerElement):0\nclass optgroup(ContainerElement,HasOptions):0\nclass option(ContainerElement):0\nclass output(ContainerElement):0\nclass p(ContainerElement):0\nclass param(ContainerElement):0\nclass picture(ContainerElement):0\nclass pre(ContainerElement):0\nclass progress(ContainerElement):0\nclass q(ContainerElement):0\nclass s(ContainerElement):0\nclass script(ContainerElement):0\nclass section(ContainerElement):0\nclass select(ContainerElement,HasOptions):0\nclass small(ContainerElement):0\nclass source(Element):0\nclass span(ContainerElement):0\nclass strong(ContainerElement):0\nclass style(ContainerElement):0\nclass sub(ContainerElement):0\nclass summary(ContainerElement):0\nclass sup(ContainerElement):0\nclass table(ContainerElement):0\nclass tbody(ContainerElement):0\nclass td(ContainerElement):0\nclass template(ContainerElement):0\nclass textarea(ContainerElement):0\nclass tfoot(ContainerElement):0\nclass th(ContainerElement):0\nclass thead(ContainerElement):0\nclass time(ContainerElement):0\nclass title(ContainerElement):0\nclass tr(ContainerElement):0\nclass track(Element):0\nclass u(ContainerElement):0\nclass ul(ContainerElement):0\nclass var(ContainerElement):0\nclass video(ContainerElement):\n\tdef snap(E,to=_A,width=_A,height=_A):\n\t\tH='CANVAS';G='Element to snap to must be a canvas.';C=height;B=width;A=to;B=B if B is not _A else E.videoWidth;C=C if C is not _A else E.videoHeight\n\t\tif A is _A:A=canvas(width=B,height=C)\n\t\telif isinstance(A,Element):\n\t\t\tif A.tag!='canvas':D=G;raise TypeError(D)\n\t\telif getattr(A,'tagName','')==H:A=canvas(dom_element=A)\n\t\telif isinstance(A,str):\n\t\t\tF=document.querySelectorAll(A)\n\t\t\tif F.length==0:D='No element with selector {to} to snap to.';raise TypeError(D)\n\t\t\tif F[0].tagName!=H:D=G;raise TypeError(D)\n\t\t\tA=canvas(dom_element=F[0])\n\t\tA.draw(E,B,C);return A\nclass wbr(Element):0\nELEMENT_CLASSES=[a,abbr,address,area,article,aside,audio,b,base,blockquote,body,br,button,canvas,caption,cite,code,col,colgroup,data,datalist,dd,del_,details,dialog,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,img,input_,ins,kbd,label,legend,li,link,main,map_,mark,menu,meta,meter,nav,object_,ol,optgroup,option,output,p,param,picture,pre,progress,q,s,script,section,select,small,source,span,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,u,ul,var,video,wbr]\nElement.register_element_classes(ELEMENT_CLASSES)\nclass Page:\n\tdef __init__(A):A.html=Element.wrap_dom_element(document.documentElement);A.body=Element.wrap_dom_element(document.body);A.head=Element.wrap_dom_element(document.head)\n\tdef __getitem__(A,selector):return A.find(selector)\n\t@property\n\tdef title(self):return document.title\n\t@title.setter\n\tdef title(self,value):document.title=value\n\tdef append(A,*B):A.body.append(*B)\n\tdef find(A,selector):return ElementCollection.wrap_dom_elements(document.querySelectorAll(selector))\npage=Page()",
|
16
|
-
"websocket.py": "import js\nfrom pyscript.ffi import create_proxy\nfrom pyscript.util import as_bytearray\ncode='code'\nprotocols='protocols'\nreason='reason'\nmethods=['onclose','onerror','onmessage','onopen']\nclass EventMessage:\n\tdef __init__(A,event):A._event=event\n\tdef __getattr__(B,attr):\n\t\tA=getattr(B._event,attr)\n\t\tif attr=='data'and not isinstance(A,str):\n\t\t\tif hasattr(A,'to_py'):return A.to_py()\n\t\t\treturn memoryview(as_bytearray(A))\n\t\treturn A\nclass WebSocket:\n\tCONNECTING=0;OPEN=1;CLOSING=2;CLOSED=3\n\tdef __init__(E,**A):\n\t\tD=A['url']\n\t\tif protocols in A:B=js.WebSocket.new(D,A[protocols])\n\t\telse:B=js.WebSocket.new(D)\n\t\
|
15
|
+
"web.py": "_C='htmlFor'\n_B='on_'\n_A=None\nfrom pyscript import document,when,Event\nfrom pyscript.ffi import create_proxy,is_none\ndef wrap_dom_element(dom_element):return Element.wrap_dom_element(dom_element)\nclass Element:\n\telement_classes_by_tag_name={}\n\t@classmethod\n\tdef get_tag_name(A):return A.__name__.replace('_','')\n\t@classmethod\n\tdef register_element_classes(B,element_classes):\n\t\tfor A in element_classes:C=A.get_tag_name();B.element_classes_by_tag_name[C]=A\n\t@classmethod\n\tdef unregister_element_classes(A,element_classes):\n\t\tfor B in element_classes:C=B.get_tag_name();A.element_classes_by_tag_name.pop(C,_A)\n\t@classmethod\n\tdef wrap_dom_element(A,dom_element):B=dom_element;C=A.element_classes_by_tag_name.get(B.tagName.lower(),A);return C(dom_element=B)\n\tdef __init__(A,dom_element=_A,classes=_A,style=_A,**F):\n\t\tC=dom_element;A._dom_element=document.createElement(type(A).get_tag_name())if is_none(C)else C;A._on_events={};D={}\n\t\tfor(B,E)in F.items():\n\t\t\tif B.startswith(_B):G=A.get_event(B);G.add_listener(E)\n\t\t\telse:D[B]=E\n\t\tA._classes=Classes(A);A._style=Style(A);A.update(classes=classes,style=style,**D)\n\tdef __eq__(A,obj):return isinstance(obj,Element)and obj._dom_element==A._dom_element\n\tdef __getitem__(B,key):\n\t\tA=key\n\t\tif isinstance(A,(int,slice)):return B.children[A]\n\t\treturn B.find(A)\n\tdef __getattr__(B,name):\n\t\tA=name\n\t\tif A.startswith(_B):return B.get_event(A)\n\t\tif A.endswith('_'):A=A[:-1]\n\t\tif A=='for':A=_C\n\t\treturn getattr(B._dom_element,A)\n\tdef __setattr__(C,name,value):\n\t\tB=value;A=name\n\t\tif A.startswith('_'):super().__setattr__(A,B)\n\t\telse:\n\t\t\tif A.endswith('_'):A=A[:-1]\n\t\t\tif A=='for':A=_C\n\t\t\tif A.startswith(_B):C._on_events[A]=B\n\t\t\tsetattr(C._dom_element,A,B)\n\tdef get_event(A,name):\n\t\tB=name\n\t\tif not B.startswith(_B):C=\"Event names must start with 'on_'.\";raise ValueError(C)\n\t\tD=B[3:]\n\t\tif not hasattr(A._dom_element,D):C=f\"Element has no '{D}' event.\";raise ValueError(C)\n\t\tif B in A._on_events:return A._on_events[B]\n\t\tE=Event();A._on_events[B]=E;A._dom_element.addEventListener(D,create_proxy(E.trigger));return E\n\t@property\n\tdef children(self):return ElementCollection.wrap_dom_elements(self._dom_element.children)\n\t@property\n\tdef classes(self):return self._classes\n\t@property\n\tdef parent(self):\n\t\tif is_none(self._dom_element.parentElement):return\n\t\treturn Element.wrap_dom_element(self._dom_element.parentElement)\n\t@property\n\tdef style(self):return self._style\n\tdef append(B,*C):\n\t\tfor A in C:\n\t\t\tif isinstance(A,Element):B._dom_element.appendChild(A._dom_element)\n\t\t\telif isinstance(A,ElementCollection):\n\t\t\t\tfor D in A:B._dom_element.appendChild(D._dom_element)\n\t\t\telif isinstance(A,(list,tuple)):\n\t\t\t\tfor E in A:B.append(E)\n\t\t\telse:\n\t\t\t\ttry:A.tagName;B._dom_element.appendChild(A)\n\t\t\t\texcept AttributeError:\n\t\t\t\t\ttry:\n\t\t\t\t\t\tA.length\n\t\t\t\t\t\tfor F in A:B._dom_element.appendChild(F)\n\t\t\t\t\texcept AttributeError:G=f'Element \"{A}\" is a proxy object, \"but not a valid element or a NodeList.';raise TypeError(G)\n\tdef clone(B,clone_id=_A):A=Element.wrap_dom_element(B._dom_element.cloneNode(True));A.id=clone_id;return A\n\tdef find(A,selector):return ElementCollection.wrap_dom_elements(A._dom_element.querySelectorAll(selector))\n\tdef show_me(A):A._dom_element.scrollIntoView()\n\tdef update(A,classes=_A,style=_A,**D):\n\t\tC=style;B=classes\n\t\tif B:A.classes.add(B)\n\t\tif C:A.style.set(**C)\n\t\tfor(E,F)in D.items():setattr(A,E,F)\nclass Classes:\n\tdef __init__(A,element):A._element=element;A._class_list=A._element._dom_element.classList\n\tdef __contains__(A,item):return item in A._class_list\n\tdef __eq__(C,other):\n\t\tA=other\n\t\tif isinstance(A,Classes):B=list(A._class_list)\n\t\telse:\n\t\t\ttry:B=iter(A)\n\t\t\texcept TypeError:return False\n\t\treturn set(C._class_list)==set(B)\n\tdef __iter__(A):return iter(A._class_list)\n\tdef __len__(A):return A._class_list.length\n\tdef __repr__(A):return f\"Classes({\", \".join(A._class_list)})\"\n\tdef __str__(A):return' '.join(A._class_list)\n\tdef add(B,*C):\n\t\tfor A in C:\n\t\t\tif isinstance(A,list):\n\t\t\t\tfor D in A:B.add(D)\n\t\t\telse:B._class_list.add(A)\n\tdef contains(A,class_name):return class_name in A\n\tdef remove(B,*C):\n\t\tfor A in C:\n\t\t\tif isinstance(A,list):\n\t\t\t\tfor D in A:B.remove(D)\n\t\t\telse:B._class_list.remove(A)\n\tdef replace(A,old_class,new_class):A.remove(old_class);A.add(new_class)\n\tdef toggle(A,*C):\n\t\tfor B in C:\n\t\t\tif B in A:A.remove(B)\n\t\t\telse:A.add(B)\nclass HasOptions:\n\t@property\n\tdef options(self):\n\t\tA=self\n\t\tif not hasattr(A,'_options'):A._options=Options(A)\n\t\treturn A._options\nclass Options:\n\tdef __init__(A,element):A._element=element\n\tdef __getitem__(A,key):return A.options[key]\n\tdef __iter__(A):yield from A.options\n\tdef __len__(A):return len(A.options)\n\tdef __repr__(A):return f\"{A.__class__.__name__} (length: {len(A)}) {A.options}\"\n\t@property\n\tdef options(self):return[Element.wrap_dom_element(A)for A in self._element._dom_element.options]\n\t@property\n\tdef selected(self):return self.options[self._element._dom_element.selectedIndex]\n\tdef add(D,value=_A,html=_A,text=_A,before=_A,**B):\n\t\tC=value;A=before\n\t\tif C is not _A:B['value']=C\n\t\tif html is not _A:B['innerHTML']=html\n\t\tif text is not _A:B['text']=text\n\t\tE=option(**B)\n\t\tif A and isinstance(A,Element):A=A._dom_element\n\t\tD._element._dom_element.add(E._dom_element,A)\n\tdef clear(A):\n\t\twhile len(A)>0:A.remove(0)\n\tdef remove(A,index):A._element._dom_element.remove(index)\nclass Style:\n\tdef __init__(A,element):A._element=element;A._style=A._element._dom_element.style\n\tdef __getitem__(A,key):return A._style.getPropertyValue(key)\n\tdef __setitem__(A,key,value):A._style.setProperty(key,value)\n\tdef remove(A,key):A._style.removeProperty(key)\n\tdef set(A,**B):\n\t\tfor(C,D)in B.items():A._element._dom_element.style.setProperty(C,D)\n\t@property\n\tdef visible(self):return self._element._dom_element.style.visibility\n\t@visible.setter\n\tdef visible(self,value):self._element._dom_element.style.visibility=value\nclass ContainerElement(Element):\n\tdef __init__(B,*C,children=_A,dom_element=_A,style=_A,classes=_A,**D):\n\t\tsuper().__init__(dom_element=dom_element,style=style,classes=classes,**D)\n\t\tfor A in list(C)+(children or[]):\n\t\t\tif isinstance(A,(Element,ElementCollection)):B.append(A)\n\t\t\telse:B._dom_element.insertAdjacentHTML('beforeend',A)\n\tdef __iter__(A):yield from A.children\nclass ClassesCollection:\n\tdef __init__(A,collection):A._collection=collection\n\tdef __contains__(A,class_name):\n\t\tfor B in A._collection:\n\t\t\tif class_name in B.classes:return True\n\t\treturn False\n\tdef __eq__(B,other):A=other;return isinstance(A,ClassesCollection)and B._collection==A._collection\n\tdef __iter__(A):yield from A._all_class_names()\n\tdef __len__(A):return len(A._all_class_names())\n\tdef __repr__(A):return f\"ClassesCollection({A._collection!r})\"\n\tdef __str__(A):return' '.join(A._all_class_names())\n\tdef add(A,*B):\n\t\tfor C in A._collection:C.classes.add(*B)\n\tdef contains(A,class_name):return class_name in A\n\tdef remove(A,*B):\n\t\tfor C in A._collection:C.classes.remove(*B)\n\tdef replace(A,old_class,new_class):\n\t\tfor B in A._collection:B.classes.replace(old_class,new_class)\n\tdef toggle(A,*B):\n\t\tfor C in A._collection:C.classes.toggle(*B)\n\tdef _all_class_names(B):\n\t\tA=set()\n\t\tfor C in B._collection:\n\t\t\tfor D in C.classes:A.add(D)\n\t\treturn A\nclass StyleCollection:\n\tdef __init__(A,collection):A._collection=collection\n\tdef __getitem__(A,key):return[A.style[key]for A in A._collection._elements]\n\tdef __setitem__(A,key,value):\n\t\tfor B in A._collection._elements:B.style[key]=value\n\tdef __repr__(A):return f\"StyleCollection({A._collection!r})\"\n\tdef remove(A,key):\n\t\tfor B in A._collection._elements:B.style.remove(key)\nclass ElementCollection:\n\t@classmethod\n\tdef wrap_dom_elements(A,dom_elements):return A([Element.wrap_dom_element(A)for A in dom_elements])\n\tdef __init__(A,elements):A._elements=elements;A._classes=ClassesCollection(A);A._style=StyleCollection(A)\n\tdef __eq__(A,obj):return isinstance(obj,ElementCollection)and obj._elements==A._elements\n\tdef __getitem__(B,key):\n\t\tA=key\n\t\tif isinstance(A,int):return B._elements[A]\n\t\tif isinstance(A,slice):return ElementCollection(B._elements[A])\n\t\treturn B.find(A)\n\tdef __iter__(A):yield from A._elements\n\tdef __len__(A):return len(A._elements)\n\tdef __repr__(A):return f\"{A.__class__.__name__} (length: {len(A._elements)}) {A._elements}\"\n\tdef __getattr__(A,name):return[getattr(A,name)for A in A._elements]\n\tdef __setattr__(C,name,value):\n\t\tB=value;A=name\n\t\tif A.startswith('_'):super().__setattr__(A,B)\n\t\telse:\n\t\t\tfor D in C._elements:setattr(D,A,B)\n\t@property\n\tdef classes(self):return self._classes\n\t@property\n\tdef elements(self):return self._elements\n\t@property\n\tdef style(self):return self._style\n\tdef find(B,selector):\n\t\tA=[]\n\t\tfor C in B._elements:A.extend(C.find(selector))\n\t\treturn ElementCollection(A)\nclass a(ContainerElement):0\nclass abbr(ContainerElement):0\nclass address(ContainerElement):0\nclass area(Element):0\nclass article(ContainerElement):0\nclass aside(ContainerElement):0\nclass audio(ContainerElement):0\nclass b(ContainerElement):0\nclass base(Element):0\nclass blockquote(ContainerElement):0\nclass body(ContainerElement):0\nclass br(Element):0\nclass button(ContainerElement):0\nclass canvas(ContainerElement):\n\tdef download(A,filename='snapped.png'):B=a(download=filename,href=A._dom_element.toDataURL());A.append(B);B._dom_element.click()\n\tdef draw(E,what,width=_A,height=_A):\n\t\tC=height;B=width;A=what\n\t\tif isinstance(A,Element):A=A._dom_element\n\t\tD=E._dom_element.getContext('2d')\n\t\tif B or C:D.drawImage(A,0,0,B,C)\n\t\telse:D.drawImage(A,0,0)\nclass caption(ContainerElement):0\nclass cite(ContainerElement):0\nclass code(ContainerElement):0\nclass col(Element):0\nclass colgroup(ContainerElement):0\nclass data(ContainerElement):0\nclass datalist(ContainerElement,HasOptions):0\nclass dd(ContainerElement):0\nclass del_(ContainerElement):0\nclass details(ContainerElement):0\nclass dialog(ContainerElement):0\nclass div(ContainerElement):0\nclass dl(ContainerElement):0\nclass dt(ContainerElement):0\nclass em(ContainerElement):0\nclass embed(Element):0\nclass fieldset(ContainerElement):0\nclass figcaption(ContainerElement):0\nclass figure(ContainerElement):0\nclass footer(ContainerElement):0\nclass form(ContainerElement):0\nclass h1(ContainerElement):0\nclass h2(ContainerElement):0\nclass h3(ContainerElement):0\nclass h4(ContainerElement):0\nclass h5(ContainerElement):0\nclass h6(ContainerElement):0\nclass head(ContainerElement):0\nclass header(ContainerElement):0\nclass hgroup(ContainerElement):0\nclass hr(Element):0\nclass html(ContainerElement):0\nclass i(ContainerElement):0\nclass iframe(ContainerElement):0\nclass img(Element):0\nclass input_(Element):0\nclass ins(ContainerElement):0\nclass kbd(ContainerElement):0\nclass label(ContainerElement):0\nclass legend(ContainerElement):0\nclass li(ContainerElement):0\nclass link(Element):0\nclass main(ContainerElement):0\nclass map_(ContainerElement):0\nclass mark(ContainerElement):0\nclass menu(ContainerElement):0\nclass meta(ContainerElement):0\nclass meter(ContainerElement):0\nclass nav(ContainerElement):0\nclass object_(ContainerElement):0\nclass ol(ContainerElement):0\nclass optgroup(ContainerElement,HasOptions):0\nclass option(ContainerElement):0\nclass output(ContainerElement):0\nclass p(ContainerElement):0\nclass param(ContainerElement):0\nclass picture(ContainerElement):0\nclass pre(ContainerElement):0\nclass progress(ContainerElement):0\nclass q(ContainerElement):0\nclass s(ContainerElement):0\nclass script(ContainerElement):0\nclass section(ContainerElement):0\nclass select(ContainerElement,HasOptions):0\nclass small(ContainerElement):0\nclass source(Element):0\nclass span(ContainerElement):0\nclass strong(ContainerElement):0\nclass style(ContainerElement):0\nclass sub(ContainerElement):0\nclass summary(ContainerElement):0\nclass sup(ContainerElement):0\nclass table(ContainerElement):0\nclass tbody(ContainerElement):0\nclass td(ContainerElement):0\nclass template(ContainerElement):0\nclass textarea(ContainerElement):0\nclass tfoot(ContainerElement):0\nclass th(ContainerElement):0\nclass thead(ContainerElement):0\nclass time(ContainerElement):0\nclass title(ContainerElement):0\nclass tr(ContainerElement):0\nclass track(Element):0\nclass u(ContainerElement):0\nclass ul(ContainerElement):0\nclass var(ContainerElement):0\nclass video(ContainerElement):\n\tdef snap(E,to=_A,width=_A,height=_A):\n\t\tH='CANVAS';G='Element to snap to must be a canvas.';C=height;B=width;A=to;B=B if B is not _A else E.videoWidth;C=C if C is not _A else E.videoHeight\n\t\tif is_none(A):A=canvas(width=B,height=C)\n\t\telif isinstance(A,Element):\n\t\t\tif A.tag!='canvas':D=G;raise TypeError(D)\n\t\telif getattr(A,'tagName','')==H:A=canvas(dom_element=A)\n\t\telif isinstance(A,str):\n\t\t\tF=document.querySelectorAll(A)\n\t\t\tif F.length==0:D='No element with selector {to} to snap to.';raise TypeError(D)\n\t\t\tif F[0].tagName!=H:D=G;raise TypeError(D)\n\t\t\tA=canvas(dom_element=F[0])\n\t\tA.draw(E,B,C);return A\nclass wbr(Element):0\nELEMENT_CLASSES=[a,abbr,address,area,article,aside,audio,b,base,blockquote,body,br,button,canvas,caption,cite,code,col,colgroup,data,datalist,dd,del_,details,dialog,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,img,input_,ins,kbd,label,legend,li,link,main,map_,mark,menu,meta,meter,nav,object_,ol,optgroup,option,output,p,param,picture,pre,progress,q,s,script,section,select,small,source,span,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,u,ul,var,video,wbr]\nElement.register_element_classes(ELEMENT_CLASSES)\nclass Page:\n\tdef __init__(A):A.html=Element.wrap_dom_element(document.documentElement);A.body=Element.wrap_dom_element(document.body);A.head=Element.wrap_dom_element(document.head)\n\tdef __getitem__(A,selector):return A.find(selector)\n\t@property\n\tdef title(self):return document.title\n\t@title.setter\n\tdef title(self,value):document.title=value\n\tdef append(A,*B):A.body.append(*B)\n\tdef find(A,selector):return ElementCollection.wrap_dom_elements(document.querySelectorAll(selector))\npage=Page()",
|
16
|
+
"websocket.py": "import js\nfrom pyscript.ffi import create_proxy\nfrom pyscript.util import as_bytearray,is_awaitable\ncode='code'\nprotocols='protocols'\nreason='reason'\nmethods=['onclose','onerror','onmessage','onopen']\ndef add_listener(socket,onevent,listener):\n\tA=listener;B=create_proxy(A)\n\tif is_awaitable(A):\n\t\tasync def D(e):await B(EventMessage(e))\n\t\tC=D\n\telse:C=lambda e:B(EventMessage(e))\n\tsetattr(socket,onevent,C)\nclass EventMessage:\n\tdef __init__(A,event):A._event=event\n\tdef __getattr__(B,attr):\n\t\tA=getattr(B._event,attr)\n\t\tif attr=='data'and not isinstance(A,str):\n\t\t\tif hasattr(A,'to_py'):return A.to_py()\n\t\t\treturn memoryview(as_bytearray(A))\n\t\treturn A\nclass WebSocket:\n\tCONNECTING=0;OPEN=1;CLOSING=2;CLOSED=3\n\tdef __init__(E,**A):\n\t\tD=A['url']\n\t\tif protocols in A:B=js.WebSocket.new(D,A[protocols])\n\t\telse:B=js.WebSocket.new(D)\n\t\tB.binaryType='arraybuffer';object.__setattr__(E,'_ws',B)\n\t\tfor C in methods:\n\t\t\tif C in A:add_listener(B,C,A[C])\n\tdef __getattr__(A,attr):return getattr(A._ws,attr)\n\tdef __setattr__(B,attr,value):\n\t\tC=value;A=attr\n\t\tif A in methods:add_listener(B._ws,A,C)\n\t\telse:setattr(B._ws,A,C)\n\tdef close(B,**A):\n\t\tif code in A and reason in A:B._ws.close(A[code],A[reason])\n\t\telif code in A:B._ws.close(A[code])\n\t\telse:B._ws.close()\n\tdef send(B,data):\n\t\tA=data\n\t\tif isinstance(A,str):B._ws.send(A)\n\t\telse:\n\t\t\tC=js.Uint8Array.new(len(A))\n\t\t\tfor(D,E)in enumerate(A):C[D]=E\n\t\t\tB._ws.send(C)",
|
17
17
|
"workers.py": "import js as _js\nfrom polyscript import workers as _workers\n_get=_js.Reflect.get\ndef _set(script,name,value=''):script.setAttribute(name,value)\nclass _ReadOnlyProxy:\n\tdef __getitem__(A,name):return _get(_workers,name)\n\tdef __getattr__(A,name):return _get(_workers,name)\nworkers=_ReadOnlyProxy()\nasync def create_named_worker(src='',name='',config=None,type='py'):\n\tC=name;B=config;from json import dumps\n\tif not src:D='Named workers require src';raise ValueError(D)\n\tif not C:D='Named workers require a name';raise ValueError(D)\n\tA=_js.document.createElement('script');A.type=type;A.src=src;_set(A,'worker');_set(A,'name',C)\n\tif B:_set(A,'config',isinstance(B,str)and B or dumps(B))\n\t_js.document.body.append(A);return await workers[C]"
|
18
18
|
}
|
19
19
|
};
|
package/src/sync.js
CHANGED
@@ -16,7 +16,7 @@ export default {
|
|
16
16
|
* Ask a user action via dialog and returns the directory handler once granted.
|
17
17
|
* @param {string} uid
|
18
18
|
* @param {{id?:string, mode?:"read"|"readwrite", hint?:"desktop"|"documents"|"downloads"|"music"|"pictures"|"videos"}} options
|
19
|
-
* @returns {boolean}
|
19
|
+
* @returns {Promise<boolean>}
|
20
20
|
*/
|
21
21
|
async storeFSHandler(uid, options = {}) {
|
22
22
|
if (await idb.has(uid)) return true;
|
@@ -28,4 +28,15 @@ export default {
|
|
28
28
|
() => false,
|
29
29
|
);
|
30
30
|
},
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Explicitly remove the unique identifier for the FS handler.
|
34
|
+
* @param {string} uid
|
35
|
+
* @returns {Promise<boolean>}
|
36
|
+
*/
|
37
|
+
async deleteFSHandler(uid) {
|
38
|
+
const had = await idb.has(uid);
|
39
|
+
if (had) await idb.delete(uid);
|
40
|
+
return had;
|
41
|
+
},
|
31
42
|
};
|
package/types/core.d.ts
CHANGED
@@ -63,5 +63,5 @@ declare const exportedHooks: {
|
|
63
63
|
};
|
64
64
|
};
|
65
65
|
declare const exportedConfig: {};
|
66
|
-
declare const exportedWhenDefined:
|
66
|
+
declare const exportedWhenDefined: (type: string) => Promise<object>;
|
67
67
|
export { codemirror, stdlib, optional, inputFailure, TYPES, relative_url, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
|
package/types/sync.d.ts
CHANGED
@@ -9,12 +9,18 @@ declare namespace _default {
|
|
9
9
|
* Ask a user action via dialog and returns the directory handler once granted.
|
10
10
|
* @param {string} uid
|
11
11
|
* @param {{id?:string, mode?:"read"|"readwrite", hint?:"desktop"|"documents"|"downloads"|"music"|"pictures"|"videos"}} options
|
12
|
-
* @returns {boolean}
|
12
|
+
* @returns {Promise<boolean>}
|
13
13
|
*/
|
14
14
|
function storeFSHandler(uid: string, options?: {
|
15
15
|
id?: string;
|
16
16
|
mode?: "read" | "readwrite";
|
17
17
|
hint?: "desktop" | "documents" | "downloads" | "music" | "pictures" | "videos";
|
18
|
-
}): boolean
|
18
|
+
}): Promise<boolean>;
|
19
|
+
/**
|
20
|
+
* Explicitly remove the unique identifier for the FS handler.
|
21
|
+
* @param {string} uid
|
22
|
+
* @returns {Promise<boolean>}
|
23
|
+
*/
|
24
|
+
function deleteFSHandler(uid: string): Promise<boolean>;
|
19
25
|
}
|
20
26
|
export default _default;
|