@pyscript/core 0.1.22 → 0.2.0

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.
Files changed (37) hide show
  1. package/dist/core.js +2 -2
  2. package/dist/core.js.map +1 -1
  3. package/dist/error-e4fe78fd.js +2 -0
  4. package/dist/error-e4fe78fd.js.map +1 -0
  5. package/package.json +2 -2
  6. package/src/core.js +19 -33
  7. package/src/plugins/error.js +2 -2
  8. package/src/stdlib/pyscript/display.py +7 -0
  9. package/src/stdlib/pyscript.js +1 -1
  10. package/dist/error-87e0706c.js +0 -2
  11. package/dist/error-87e0706c.js.map +0 -1
  12. package/tests/integration/__init__.py +0 -0
  13. package/tests/integration/conftest.py +0 -184
  14. package/tests/integration/support.py +0 -1038
  15. package/tests/integration/test_00_support.py +0 -495
  16. package/tests/integration/test_01_basic.py +0 -353
  17. package/tests/integration/test_02_display.py +0 -452
  18. package/tests/integration/test_03_element.py +0 -303
  19. package/tests/integration/test_assets/line_plot.png +0 -0
  20. package/tests/integration/test_assets/tripcolor.png +0 -0
  21. package/tests/integration/test_async.py +0 -197
  22. package/tests/integration/test_event_handling.py +0 -193
  23. package/tests/integration/test_importmap.py +0 -66
  24. package/tests/integration/test_interpreter.py +0 -98
  25. package/tests/integration/test_plugins.py +0 -419
  26. package/tests/integration/test_py_config.py +0 -294
  27. package/tests/integration/test_py_repl.py +0 -663
  28. package/tests/integration/test_py_terminal.py +0 -270
  29. package/tests/integration/test_runtime_attributes.py +0 -64
  30. package/tests/integration/test_script_type.py +0 -121
  31. package/tests/integration/test_shadow_root.py +0 -33
  32. package/tests/integration/test_splashscreen.py +0 -124
  33. package/tests/integration/test_stdio_handling.py +0 -370
  34. package/tests/integration/test_style.py +0 -47
  35. package/tests/integration/test_warnings_and_banners.py +0 -32
  36. package/tests/integration/test_zz_examples.py +0 -419
  37. package/tests/integration/test_zzz_docs_snippets.py +0 -305
@@ -0,0 +1,2 @@
1
+ import{hooks as e}from"./core.js";function r(e){const r=document.createElement("div");r.className="py-error",r.textContent=e,r.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(r)}e.onInterpreterReady.add((function n(t){e.onInterpreterReady.delete(n);const{stderr:o}=t.io;t.io.stderr=(e,...n)=>(r(e.message||e),o(e,...n)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&r(e)}))}));export{r as notify};
2
+ //# sourceMappingURL=error-e4fe78fd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-e4fe78fd.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.onInterpreterReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.onInterpreterReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","onInterpreterReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA0BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CAtCAS,EAAMC,mBAAmBC,KAAI,SAASC,EAASC,GAE3CJ,EAAMC,mBAAmBI,OAAOF,GAGhC,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BpB,EAAOmB,EAAMlB,SAAWkB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGpB,cACrBA,EAAQqB,WAAW,yBAAyBtB,EAAOC,EAAQ,GAEvE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.1.22",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -33,7 +33,7 @@
33
33
  "dependencies": {
34
34
  "@ungap/with-resolvers": "^0.1.0",
35
35
  "basic-devtools": "^0.1.6",
36
- "polyscript": "^0.3.10"
36
+ "polyscript": "^0.4.2"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@rollup/plugin-node-resolve": "^15.2.1",
package/src/core.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*! (c) PyScript Development Team */
2
2
 
3
3
  import "@ungap/with-resolvers";
4
- import { define, XWorker } from "polyscript";
4
+ import { INVALID_CONTENT, define, XWorker } from "polyscript";
5
5
 
6
6
  // TODO: this is not strictly polyscript related but handy ... not sure
7
7
  // we should factor this utility out a part but this works anyway.
@@ -41,36 +41,6 @@ const after = () => {
41
41
  delete document.currentScript;
42
42
  };
43
43
 
44
- /**
45
- * Some content that might contain Python/JS comments only.
46
- * @param {string} text some content to evaluate
47
- * @returns {boolean}
48
- */
49
- const hasCommentsOnly = (text) =>
50
- !text
51
- .replace(/\/\*[\s\S]*?\*\//g, "")
52
- .replace(/^\s*(?:\/\/|#).*/gm, "")
53
- .trim();
54
-
55
- /**
56
- *
57
- * @param {Element} scriptOrPyScript the element with possible `src` or `worker` content
58
- * @returns {boolean}
59
- */
60
- const hasAmbiguousContent = (
61
- io,
62
- { localName, textContent, attributes: { src, worker } },
63
- ) => {
64
- // any `src` or a non-empty `worker` attribute + not just comments
65
- if ((src || worker?.value) && !hasCommentsOnly(textContent)) {
66
- io.stderr(
67
- `(${ErrorCode.CONFLICTING_CODE}) a ${localName} tag has content shadowed by attributes`,
68
- );
69
- return true;
70
- }
71
- return false;
72
- };
73
-
74
44
  /**
75
45
  * Given a generic DOM Element, tries to fetch the 'src' attribute, if present.
76
46
  * It either throws an error if the 'src' can't be fetched or it returns a fallback
@@ -158,6 +128,9 @@ const workerHooks = {
158
128
  [...hooks.codeAfterRunWorkerAsync].map(dedent).join("\n"),
159
129
  };
160
130
 
131
+ // possible early errors sent by polyscript
132
+ const errors = new Map();
133
+
161
134
  // define the module as both `<script type="py">` and `<py-script>`
162
135
  // but only if the config didn't throw an error
163
136
  error ||
@@ -165,6 +138,9 @@ error ||
165
138
  config,
166
139
  env: `${TYPE}-script`,
167
140
  interpreter: "pyodide",
141
+ onerror(error, element) {
142
+ errors.set(element, error);
143
+ },
168
144
  ...workerHooks,
169
145
  onWorkerReady(_, xworker) {
170
146
  assign(xworker.sync, sync);
@@ -202,8 +178,19 @@ error ||
202
178
  for (const callback of hooks.onInterpreterReady)
203
179
  callback(pyodide, element);
204
180
 
181
+ // now that all possible plugins are configured,
182
+ // bail out if polyscript encountered an error
183
+ if (errors.has(element)) {
184
+ let { message } = errors.get(element);
185
+ errors.delete(element);
186
+ const clone = message === INVALID_CONTENT;
187
+ message = `(${ErrorCode.CONFLICTING_CODE}) ${message} for `;
188
+ message += element.cloneNode(clone).outerHTML;
189
+ pyodide.io.stderr(message);
190
+ return;
191
+ }
192
+
205
193
  if (isScript(element)) {
206
- if (hasAmbiguousContent(pyodide.io, element)) return;
207
194
  const {
208
195
  attributes: { async: isAsync, target },
209
196
  } = element;
@@ -254,7 +241,6 @@ class PyScriptElement extends HTMLElement {
254
241
  if (!this.executed) {
255
242
  this.executed = true;
256
243
  const { io, run, runAsync } = await this._pyodide.promise;
257
- if (hasAmbiguousContent(io, this)) return;
258
244
  const runner = this.hasAttribute("async") ? runAsync : run;
259
245
  this.srcCode = await fetchSource(this, io, !this.childElementCount);
260
246
  this.replaceChildren();
@@ -1,9 +1,9 @@
1
1
  // PyScript Error Plugin
2
2
  import { hooks } from "../core.js";
3
3
 
4
- hooks.onBeforeRun.add(function override(pyScript) {
4
+ hooks.onInterpreterReady.add(function override(pyScript) {
5
5
  // be sure this override happens only once
6
- hooks.onBeforeRun.delete(override);
6
+ hooks.onInterpreterReady.delete(override);
7
7
 
8
8
  // trap generic `stderr` to propagate to it regardless
9
9
  const { stderr } = pyScript.io;
@@ -150,5 +150,12 @@ def display(*values, target=None, append=True):
150
150
  target = current_target()
151
151
 
152
152
  element = document.getElementById(target)
153
+
154
+ # if element is a <script type="py">, it has a 'target' attribute which
155
+ # points to the visual element holding the displayed values. In that case,
156
+ # use that.
157
+ if element.tagName == 'SCRIPT' and hasattr(element, 'target'):
158
+ element = element.target
159
+
153
160
  for v in values:
154
161
  _write(element, v, append=append)
@@ -2,7 +2,7 @@
2
2
  export default {
3
3
  "pyscript": {
4
4
  "__init__.py": "# Some notes about the naming conventions and the relationship between various\n# similar-but-different names.\n#\n# import pyscript\n# this package contains the main user-facing API offered by pyscript. All\n# the names which are supposed be used by end users should be made\n# available in pyscript/__init__.py (i.e., this file)\n#\n# import _pyscript\n# this is an internal module implemented in JS. It is used internally by\n# the pyscript package, end users should not use it directly. For its\n# implementation, grep for `interpreter.registerJsModule(\"_pyscript\",\n# ...)` in core.js\n#\n# import js\n# this is the JS globalThis, as exported by pyodide and/or micropython's\n# FFIs. As such, it contains different things in the main thread or in a\n# worker.\n#\n# import pyscript.magic_js\n# this submodule abstracts away some of the differences between the main\n# thread and the worker. In particular, it defines `window` and `document`\n# in such a way that these names work in both cases: in the main thread,\n# they are the \"real\" objects, in the worker they are proxies which work\n# thanks to coincident.\n#\n# from pyscript import window, document\n# these are just the window and document objects as defined by\n# pyscript.magic_js. This is the blessed way to access them from pyscript,\n# as it works transparently in both the main thread and worker cases.\n\nfrom pyscript.magic_js import RUNNING_IN_WORKER, window, document, sync\nfrom pyscript.display import HTML, display\nfrom pyscript.event_handling import when\n",
5
- "display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import document, window, current_target\n\n_MIME_METHODS = {\n \"__repr__\": \"text/plain\",\n \"_repr_html_\": \"text/html\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_png_\": \"image/png\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n\n element = document.getElementById(target)\n for v in values:\n _write(element, v, append=append)\n",
5
+ "display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import document, window, current_target\n\n_MIME_METHODS = {\n \"__repr__\": \"text/plain\",\n \"_repr_html_\": \"text/html\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_png_\": \"image/png\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n\n element = document.getElementById(target)\n\n # if element is a <script type=\"py\">, it has a 'target' attribute which\n # points to the visual element holding the displayed values. In that case,\n # use that.\n if element.tagName == 'SCRIPT' and hasattr(element, 'target'):\n element = element.target\n\n for v in values:\n _write(element, v, append=append)\n",
6
6
  "event_handling.py": "import inspect\n\nfrom pyodide.ffi.wrappers import add_event_listener\nfrom pyscript.magic_js import document\n\n\ndef when(event_type=None, selector=None):\n \"\"\"\n Decorates a function and passes py-* events to the decorated function\n The events might or not be an argument of the decorated function\n \"\"\"\n\n def decorator(func):\n if isinstance(selector, str):\n elements = document.querySelectorAll(selector)\n else:\n # TODO: This is a hack that will be removed when pyscript becomes a package\n # and we can better manage the imports without circular dependencies\n from pyweb import pydom\n\n if isinstance(selector, pydom.Element):\n elements = [selector._js]\n elif isinstance(selector, pydom.ElementCollection):\n elements = [el._js for el in selector]\n else:\n raise ValueError(\n f\"Invalid selector: {selector}. Selector must\"\n \" be a string, a pydom.Element or a pydom.ElementCollection.\"\n )\n\n sig = inspect.signature(func)\n # Function doesn't receive events\n if not sig.parameters:\n\n def wrapper(*args, **kwargs):\n func()\n\n for el in elements:\n add_event_listener(el, event_type, wrapper)\n else:\n for el in elements:\n add_event_listener(el, event_type, func)\n return func\n\n return decorator\n",
7
7
  "magic_js.py": "from pyscript.util import NotSupported\nimport js as globalThis\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\nif RUNNING_IN_WORKER:\n import polyscript\n\n PyWorker = NotSupported(\n 'pyscript.PyWorker',\n 'pyscript.PyWorker works only when running in the main thread')\n window = polyscript.xworker.window\n document = window.document\n sync = polyscript.xworker.sync\n\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n def current_target():\n return polyscript.target\n\nelse:\n import _pyscript\n from _pyscript import PyWorker\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n 'pyscript.sync',\n 'pyscript.sync works only when running in a worker')\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n",
8
8
  "util.py": "class NotSupported:\n \"\"\"\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n \"\"\"\n\n def __init__(self, name, error):\n # we set attributes using self.__dict__ to bypass the __setattr__\n self.__dict__['name'] = name\n self.__dict__['error'] = error\n\n def __repr__(self):\n return f'<NotSupported {self.name} [{self.error}]>'\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n"
@@ -1,2 +0,0 @@
1
- import{hooks as e}from"./core.js";function o(e){const o=document.createElement("div");o.className="py-error",o.textContent=e,o.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(o)}e.onBeforeRun.add((function n(r){e.onBeforeRun.delete(n);const{stderr:t}=r.io;r.io.stderr=(e,...n)=>(o(e.message||e),t(e,...n)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&o(e)}))}));export{o as notify};
2
- //# sourceMappingURL=error-87e0706c.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error-87e0706c.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.onBeforeRun.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.onBeforeRun.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","onBeforeRun","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA0BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CAtCAS,EAAMC,YAAYC,KAAI,SAASC,EAASC,GAEpCJ,EAAMC,YAAYI,OAAOF,GAGzB,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BpB,EAAOmB,EAAMlB,SAAWkB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGpB,cACrBA,EAAQqB,WAAW,yBAAyBtB,EAAOC,EAAQ,GAEvE"}
File without changes
@@ -1,184 +0,0 @@
1
- import shutil
2
- import threading
3
- from http.server import HTTPServer as SuperHTTPServer
4
- from http.server import SimpleHTTPRequestHandler
5
-
6
- import pytest
7
-
8
- from .support import Logger
9
-
10
-
11
- def pytest_cmdline_main(config):
12
- """
13
- If we pass --clear-http-cache, we don't enter the main pytest logic, but
14
- use our custom main instead
15
- """
16
-
17
- def mymain(config, session):
18
- print()
19
- print("-" * 20, "SmartRouter HTTP cache", "-" * 20)
20
- # unfortunately pytest-cache doesn't offer a public API to selectively
21
- # clear the cache, so we need to peek its internal. The good news is
22
- # that pytest-cache is very old, stable and robust, so it's likely
23
- # that this won't break anytime soon.
24
- cache = config.cache
25
- base = cache._cachedir.joinpath(cache._CACHE_PREFIX_VALUES, "pyscript")
26
- if not base.exists():
27
- print("No cache found, nothing to do")
28
- return 0
29
- #
30
- print("Requests found in the cache:")
31
- for f in base.rglob("*"):
32
- if f.is_file():
33
- # requests are saved in dirs named pyscript/http:/foo/bar, let's turn
34
- # them into a proper url
35
- url = str(f.relative_to(base))
36
- url = url.replace(":/", "://")
37
- print(" ", url)
38
- shutil.rmtree(base)
39
- print("Cache cleared")
40
- return 0
41
-
42
- if config.option.clear_http_cache:
43
- from _pytest.main import wrap_session
44
-
45
- return wrap_session(config, mymain)
46
- return None
47
-
48
-
49
- def pytest_configure(config):
50
- """
51
- THIS IS A WORKAROUND FOR A pytest QUIRK!
52
-
53
- At the moment of writing this conftest defines two new options, --dev and
54
- --no-fake-server, but because of how pytest works, they are available only
55
- if this is the "root conftest" for the test session.
56
-
57
- This means that if you are in the pyscriptjs directory:
58
-
59
- $ py.test # does NOT work
60
- $ py.test tests/integration/ # works
61
-
62
- This happens because there is also test py-unit directory, so in the first
63
- case the "root conftest" would be tests/conftest.py (which doesn't exist)
64
- instead of this.
65
-
66
- There are various workarounds, but for now we can just detect it and
67
- inform the user.
68
-
69
- Related StackOverflow answer: https://stackoverflow.com/a/51733980
70
- """
71
- if not hasattr(config.option, "dev"):
72
- msg = """
73
- Running a bare "pytest" command from the pyscriptjs directory
74
- is not supported. Please use one of the following commands:
75
- - pytest tests/integration
76
- - pytest tests/py-unit
77
- - pytest tests/*
78
- - cd tests/integration; pytest
79
- """
80
- pytest.fail(msg)
81
- else:
82
- if config.option.dev:
83
- config.option.headed = True
84
- config.option.no_fake_server = True
85
-
86
-
87
- @pytest.fixture(scope="session")
88
- def logger():
89
- return Logger()
90
-
91
-
92
- def pytest_addoption(parser):
93
- parser.addoption(
94
- "--no-fake-server",
95
- action="store_true",
96
- help="Use a real HTTP server instead of http://fakeserver",
97
- )
98
- parser.addoption(
99
- "--dev",
100
- action="store_true",
101
- help="Automatically open a devtools panel. Implies --headed and --no-fake-server",
102
- )
103
- parser.addoption(
104
- "--clear-http-cache",
105
- action="store_true",
106
- help="Clear the cache of HTTP requests for SmartRouter",
107
- )
108
-
109
-
110
- @pytest.fixture(scope="session")
111
- def browser_type_launch_args(request):
112
- """
113
- Override the browser_type_launch_args defined by pytest-playwright to
114
- support --devtools.
115
-
116
- NOTE: this has been tested with pytest-playwright==0.3.0. It might break
117
- with newer versions of it.
118
- """
119
- # this calls the "original" fixture defined by pytest_playwright.py
120
- launch_options = request.getfixturevalue("browser_type_launch_args")
121
- if request.config.option.dev:
122
- launch_options["devtools"] = True
123
- return launch_options
124
-
125
-
126
- class DevServer(SuperHTTPServer):
127
- """
128
- Class for wrapper to run SimpleHTTPServer on Thread.
129
- Ctrl +Only Thread remains dead when terminated with C.
130
- Keyboard Interrupt passes.
131
- """
132
-
133
- def __init__(self, base_url, *args, **kwargs):
134
- self.base_url = base_url
135
- super().__init__(*args, **kwargs)
136
-
137
- def run(self):
138
- try:
139
- self.serve_forever()
140
- except KeyboardInterrupt:
141
- pass
142
- finally:
143
- self.server_close()
144
-
145
-
146
- @pytest.fixture(scope="session")
147
- def dev_server(logger):
148
- class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
149
- enable_cors_headers = True
150
-
151
- @classmethod
152
- def my_headers(cls):
153
- if cls.enable_cors_headers:
154
- return {
155
- "Cross-Origin-Embedder-Policy": "require-corp",
156
- "Cross-Origin-Opener-Policy": "same-origin",
157
- }
158
- return {}
159
-
160
- def end_headers(self):
161
- self.send_my_headers()
162
- SimpleHTTPRequestHandler.end_headers(self)
163
-
164
- def send_my_headers(self):
165
- for k, v in self.my_headers().items():
166
- self.send_header(k, v)
167
-
168
- def log_message(self, fmt, *args):
169
- logger.log("http_server", fmt % args, color="blue")
170
-
171
- host, port = "localhost", 8080
172
- base_url = f"http://{host}:{port}"
173
-
174
- # serve_Run forever under thread
175
- server = DevServer(base_url, (host, port), MyHTTPRequestHandler)
176
-
177
- thread = threading.Thread(None, server.run)
178
- thread.start()
179
-
180
- yield server # Transition to test here
181
-
182
- # End thread
183
- server.shutdown()
184
- thread.join()