@pyscript/core 0.6.4 → 0.6.6
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/{codemirror-RC6SaMW2.js → codemirror-BFaBdIfQ.js} +2 -2
- package/dist/{codemirror-RC6SaMW2.js.map → codemirror-BFaBdIfQ.js.map} +1 -1
- package/dist/{codemirror_commands-CPwsCOyn.js → codemirror_commands-CvnouHng.js} +2 -2
- package/dist/{codemirror_commands-CPwsCOyn.js.map → codemirror_commands-CvnouHng.js.map} +1 -1
- package/dist/{core-C6pIgnlT.js → core-Bk1svgtw.js} +2 -2
- package/dist/{core-C6pIgnlT.js.map → core-Bk1svgtw.js.map} +1 -1
- package/dist/core.js +1 -1
- package/dist/{deprecations-manager-CQeGJF7e.js → deprecations-manager-DrH_iInu.js} +2 -2
- package/dist/{deprecations-manager-CQeGJF7e.js.map → deprecations-manager-DrH_iInu.js.map} +1 -1
- package/dist/donkey-BzM8aNdi.js +2 -0
- package/dist/donkey-BzM8aNdi.js.map +1 -0
- package/dist/{error-oHmERCV4.js → error-C0sVzi_b.js} +2 -2
- package/dist/{error-oHmERCV4.js.map → error-C0sVzi_b.js.map} +1 -1
- package/dist/{mpy-zn-VS8P4.js → mpy-CDiWIkAk.js} +2 -2
- package/dist/{mpy-zn-VS8P4.js.map → mpy-CDiWIkAk.js.map} +1 -1
- package/dist/{py-CfujgViF.js → py-BSBnX2Kn.js} +2 -2
- package/dist/{py-CfujgViF.js.map → py-BSBnX2Kn.js.map} +1 -1
- package/dist/{py-editor-KtwikReM.js → py-editor-BAhtHNsB.js} +2 -2
- package/dist/{py-editor-KtwikReM.js.map → py-editor-BAhtHNsB.js.map} +1 -1
- package/dist/{py-terminal-D1ektG7S.js → py-terminal-BExfmqM_.js} +2 -2
- package/dist/{py-terminal-D1ektG7S.js.map → py-terminal-BExfmqM_.js.map} +1 -1
- package/package.json +2 -1
- package/src/plugins/donkey.js +98 -49
- package/types/core.d.ts +8 -1
- package/types/plugins/donkey.d.ts +8 -1
- package/dist/donkey-4afK0Hep.js +0 -2
- package/dist/donkey-4afK0Hep.js.map +0 -1
package/src/plugins/donkey.js
CHANGED
@@ -1,64 +1,113 @@
|
|
1
|
+
import addPromiseListener from "add-promise-listener";
|
1
2
|
import { assign, dedent } from "polyscript/exports";
|
2
3
|
|
4
|
+
const { stringify } = JSON;
|
5
|
+
|
3
6
|
const invoke = (name, args) => `${name}(code, ${args.join(", ")})`;
|
4
7
|
|
5
|
-
|
6
|
-
const
|
7
|
-
|
8
|
-
? ["globals()", "__locals__"]
|
9
|
-
: ["{}", "{}"];
|
8
|
+
const donkey = ({ type = "py", persistent, terminal, config }) => {
|
9
|
+
const args = persistent ? ["globals()", "__locals__"] : ["{}", "{}"];
|
10
|
+
|
10
11
|
const src = URL.createObjectURL(
|
11
12
|
new Blob([
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
sync.
|
31
|
-
|
13
|
+
[
|
14
|
+
// this array is to better minify this code once in production
|
15
|
+
"from pyscript import sync, config",
|
16
|
+
'__message__ = lambda e,v: f"\x1b[31m\x1b[1m{e.__name__}\x1b[0m: {v}"',
|
17
|
+
"__locals__ = {}",
|
18
|
+
'if config["type"] == "py":',
|
19
|
+
" import sys",
|
20
|
+
" def __error__(_):",
|
21
|
+
" info = sys.exc_info()",
|
22
|
+
" return __message__(info[0], info[1])",
|
23
|
+
"else:",
|
24
|
+
" __error__ = lambda e: __message__(e.__class__, e.value)",
|
25
|
+
"def execute(code):",
|
26
|
+
` try: return ${invoke("exec", args)};`,
|
27
|
+
" except Exception as e: print(__error__(e));",
|
28
|
+
"def evaluate(code):",
|
29
|
+
` try: return ${invoke("eval", args)};`,
|
30
|
+
" except Exception as e: print(__error__(e));",
|
31
|
+
"sync.execute = execute",
|
32
|
+
"sync.evaluate = evaluate",
|
33
|
+
].join("\n"),
|
32
34
|
]),
|
33
35
|
);
|
34
36
|
|
37
|
+
// create the script that exposes the code to execute or evaluate
|
35
38
|
const script = assign(document.createElement("script"), { type, src });
|
36
|
-
|
37
39
|
script.toggleAttribute("worker", true);
|
38
40
|
script.toggleAttribute("terminal", true);
|
39
|
-
if (
|
40
|
-
if (
|
41
|
-
script.setAttribute(
|
41
|
+
if (terminal) script.setAttribute("target", terminal);
|
42
|
+
if (config) {
|
43
|
+
script.setAttribute(
|
44
|
+
"config",
|
45
|
+
typeof config === "string" ? config : stringify(config),
|
46
|
+
);
|
47
|
+
}
|
42
48
|
|
43
|
-
return
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
resolve({
|
51
|
-
process,
|
52
|
-
execute: (code) => execute(dedent(code)),
|
53
|
-
evaluate: (code) => evaluate(dedent(code)),
|
54
|
-
clear: () => terminal.clear(),
|
55
|
-
reset: () => terminal.reset(),
|
56
|
-
kill: () => {
|
57
|
-
xworker.terminate();
|
58
|
-
terminal.dispose();
|
59
|
-
},
|
60
|
-
});
|
61
|
-
});
|
62
|
-
document.body.append(script);
|
49
|
+
return addPromiseListener(
|
50
|
+
document.body.appendChild(script),
|
51
|
+
`${type}:done`,
|
52
|
+
{ stopPropagation: true },
|
53
|
+
).then(() => {
|
54
|
+
URL.revokeObjectURL(src);
|
55
|
+
return script;
|
63
56
|
});
|
64
57
|
};
|
58
|
+
|
59
|
+
const utils = async (options) => {
|
60
|
+
const script = await donkey(options);
|
61
|
+
const { xworker, process, terminal } = script;
|
62
|
+
const { execute, evaluate } = xworker.sync;
|
63
|
+
script.remove();
|
64
|
+
return {
|
65
|
+
xworker,
|
66
|
+
process,
|
67
|
+
terminal,
|
68
|
+
execute,
|
69
|
+
evaluate,
|
70
|
+
};
|
71
|
+
};
|
72
|
+
|
73
|
+
export default async (options = {}) => {
|
74
|
+
let farmer = await utils(options);
|
75
|
+
let working = false;
|
76
|
+
const kill = () => {
|
77
|
+
if (farmer) {
|
78
|
+
farmer.xworker.terminate();
|
79
|
+
farmer.terminal.dispose();
|
80
|
+
farmer = null;
|
81
|
+
}
|
82
|
+
working = false;
|
83
|
+
};
|
84
|
+
const reload = async () => {
|
85
|
+
kill();
|
86
|
+
farmer = await utils(options);
|
87
|
+
};
|
88
|
+
const asyncTask = (method) => async (code) => {
|
89
|
+
// race condition ... a new task has been
|
90
|
+
// assigned while the previous one didn't finish
|
91
|
+
if (working) await reload();
|
92
|
+
working = true;
|
93
|
+
try {
|
94
|
+
return await farmer[method](dedent(code));
|
95
|
+
} catch (e) {
|
96
|
+
console.error(e);
|
97
|
+
} finally {
|
98
|
+
working = false;
|
99
|
+
}
|
100
|
+
};
|
101
|
+
const asyncMethod = method => async () => {
|
102
|
+
if (working) await reload();
|
103
|
+
else farmer?.terminal[method]();
|
104
|
+
};
|
105
|
+
return {
|
106
|
+
process: asyncTask("process"),
|
107
|
+
execute: asyncTask("execute"),
|
108
|
+
evaluate: asyncTask("evaluate"),
|
109
|
+
clear: asyncMethod("clear"),
|
110
|
+
reset: asyncMethod("reset"),
|
111
|
+
kill,
|
112
|
+
};
|
113
|
+
};
|
package/types/core.d.ts
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
-
export function donkey(options: any): Promise<
|
1
|
+
export function donkey(options: any): Promise<{
|
2
|
+
process: (code: any) => Promise<any>;
|
3
|
+
execute: (code: any) => Promise<any>;
|
4
|
+
evaluate: (code: any) => Promise<any>;
|
5
|
+
clear: () => Promise<void>;
|
6
|
+
reset: () => Promise<void>;
|
7
|
+
kill: () => void;
|
8
|
+
}>;
|
2
9
|
export function offline_interpreter(config: any): string;
|
3
10
|
import { stdlib } from "./stdlib.js";
|
4
11
|
import { optional } from "./stdlib.js";
|
@@ -1,2 +1,9 @@
|
|
1
|
-
declare function _default(options?: {}): Promise<
|
1
|
+
declare function _default(options?: {}): Promise<{
|
2
|
+
process: (code: any) => Promise<any>;
|
3
|
+
execute: (code: any) => Promise<any>;
|
4
|
+
evaluate: (code: any) => Promise<any>;
|
5
|
+
clear: () => Promise<void>;
|
6
|
+
reset: () => Promise<void>;
|
7
|
+
kill: () => void;
|
8
|
+
}>;
|
2
9
|
export default _default;
|
package/dist/donkey-4afK0Hep.js
DELETED
@@ -1,2 +0,0 @@
|
|
1
|
-
import{d as e,a as t}from"./core-C6pIgnlT.js";const r=(e,t)=>`${e}(code, ${t.join(", ")})`;var n=(n={})=>{const o=n.type||"py",s=n.persistent?["globals()","__locals__"]:["{}","{}"],a=URL.createObjectURL(new Blob([e(`\n from pyscript import sync, config\n __message__ = lambda e,v: f"[31m[1m{e.__name__}[0m: {v}"\n __locals__ = {}\n if config["type"] == "py":\n import sys\n def __error__(_):\n info = sys.exc_info()\n return __message__(info[0], info[1])\n else:\n __error__ = lambda e: __message__(e.__class__, e.value)\n def execute(code):\n try: return ${r("exec",s)};\n except Exception as e: print(__error__(e));\n def evaluate(code):\n try: return ${r("eval",s)};\n except Exception as e: print(__error__(e));\n sync.execute = execute\n sync.evaluate = evaluate\n `)])),_=t(document.createElement("script"),{type:o,src:a});return _.toggleAttribute("worker",!0),_.toggleAttribute("terminal",!0),n.terminal&&_.setAttribute("target",n.terminal),n.config&&_.setAttribute("config",JSON.stringify(n.config)),new Promise((t=>{_.addEventListener(`${o}:done`,(r=>{r.stopPropagation(),URL.revokeObjectURL(a);const{xworker:n,process:o,terminal:s}=_,{execute:c,evaluate:i}=n.sync;_.remove(),t({process:o,execute:t=>c(e(t)),evaluate:t=>i(e(t)),clear:()=>s.clear(),reset:()=>s.reset(),kill:()=>{n.terminate(),s.dispose()}})})),document.body.append(_)}))};export{n as default};
|
2
|
-
//# sourceMappingURL=donkey-4afK0Hep.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"donkey-4afK0Hep.js","sources":["../src/plugins/donkey.js"],"sourcesContent":["import { assign, dedent } from \"polyscript/exports\";\n\nconst invoke = (name, args) => `${name}(code, ${args.join(\", \")})`;\n\nexport default (options = {}) => {\n const type = options.type || \"py\";\n const args = options.persistent\n ? [\"globals()\", \"__locals__\"]\n : [\"{}\", \"{}\"];\n const src = URL.createObjectURL(\n new Blob([\n dedent(`\n from pyscript import sync, config\n __message__ = lambda e,v: f\"\\x1b[31m\\x1b[1m{e.__name__}\\x1b[0m: {v}\"\n __locals__ = {}\n if config[\"type\"] == \"py\":\n import sys\n def __error__(_):\n info = sys.exc_info()\n return __message__(info[0], info[1])\n else:\n __error__ = lambda e: __message__(e.__class__, e.value)\n def execute(code):\n try: return ${invoke(\"exec\", args)};\n except Exception as e: print(__error__(e));\n def evaluate(code):\n try: return ${invoke(\"eval\", args)};\n except Exception as e: print(__error__(e));\n sync.execute = execute\n sync.evaluate = evaluate\n `),\n ]),\n );\n\n const script = assign(document.createElement(\"script\"), { type, src });\n\n script.toggleAttribute(\"worker\", true);\n script.toggleAttribute(\"terminal\", true);\n if (options.terminal) script.setAttribute(\"target\", options.terminal);\n if (options.config)\n script.setAttribute(\"config\", JSON.stringify(options.config));\n\n return new Promise((resolve) => {\n script.addEventListener(`${type}:done`, (event) => {\n event.stopPropagation();\n URL.revokeObjectURL(src);\n const { xworker, process, terminal } = script;\n const { execute, evaluate } = xworker.sync;\n script.remove();\n resolve({\n process,\n execute: (code) => execute(dedent(code)),\n evaluate: (code) => evaluate(dedent(code)),\n clear: () => terminal.clear(),\n reset: () => terminal.reset(),\n kill: () => {\n xworker.terminate();\n terminal.dispose();\n },\n });\n });\n document.body.append(script);\n });\n};\n"],"names":["invoke","name","args","join","donkey","options","type","persistent","src","URL","createObjectURL","Blob","dedent","script","assign","document","createElement","toggleAttribute","terminal","setAttribute","config","JSON","stringify","Promise","resolve","addEventListener","event","stopPropagation","revokeObjectURL","xworker","process","execute","evaluate","sync","remove","code","clear","reset","kill","terminate","dispose","body","append"],"mappings":"8CAEA,MAAMA,EAAS,CAACC,EAAMC,IAAS,GAAGD,WAAcC,EAAKC,KAAK,SAE1D,IAAAC,EAAe,CAACC,EAAU,CAAA,KACtB,MAAMC,EAAOD,EAAQC,MAAQ,KACvBJ,EAAOG,EAAQE,WACf,CAAC,YAAa,cACd,CAAC,KAAM,MACPC,EAAMC,IAAIC,gBACZ,IAAIC,KAAK,CACLC,EAAO,8iBAYeZ,EAAO,OAAQE,8IAGfF,EAAO,OAAQE,2KAQvCW,EAASC,EAAOC,SAASC,cAAc,UAAW,CAAEV,OAAME,QAQhE,OANAK,EAAOI,gBAAgB,UAAU,GACjCJ,EAAOI,gBAAgB,YAAY,GAC/BZ,EAAQa,UAAUL,EAAOM,aAAa,SAAUd,EAAQa,UACxDb,EAAQe,QACRP,EAAOM,aAAa,SAAUE,KAAKC,UAAUjB,EAAQe,SAElD,IAAIG,SAASC,IAChBX,EAAOY,iBAAiB,GAAGnB,UAAcoB,IACrCA,EAAMC,kBACNlB,IAAImB,gBAAgBpB,GACpB,MAAMqB,QAAEA,EAAOC,QAAEA,EAAOZ,SAAEA,GAAaL,GACjCkB,QAAEA,EAAOC,SAAEA,GAAaH,EAAQI,KACtCpB,EAAOqB,SACPV,EAAQ,CACJM,UACAC,QAAUI,GAASJ,EAAQnB,EAAOuB,IAClCH,SAAWG,GAASH,EAASpB,EAAOuB,IACpCC,MAAO,IAAMlB,EAASkB,QACtBC,MAAO,IAAMnB,EAASmB,QACtBC,KAAM,KACFT,EAAQU,YACRrB,EAASsB,SAAS,GAExB,IAENzB,SAAS0B,KAAKC,OAAO7B,EAAO,GAC9B"}
|