-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_python.js
87 lines (79 loc) · 2.62 KB
/
_python.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as FLATTED from '@webreflection/coincident/flatted';
const JSON = { parse: FLATTED.decode, stringify: FLATTED.encode };
import { fetchFiles, fetchJSModules, fetchPaths } from './_utils.js';
import { IDBMapSync, dedent } from '../utils.js';
import { io } from './_io.js';
export const loader = new WeakMap();
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export const loadProgress = async (self, progress, interpreter, config, baseURL) => {
if (config.files) {
progress('Loading files');
await fetchFiles(self, interpreter, config.files, baseURL);
progress('Loaded files');
}
if (config.fetch) {
progress('Loading fetch');
await fetchPaths(self, interpreter, config.fetch, baseURL);
progress('Loaded fetch');
}
if (config.js_modules) {
progress('Loading JS modules');
await fetchJSModules(config.js_modules, baseURL);
progress('Loaded JS modules');
}
};
export const registerJSModule = (interpreter, name, value) => {
if (name === 'polyscript') {
value.lazy_py_modules = async (...packages) => {
await loader.get(interpreter)(packages);
return packages.map(name => interpreter.pyimport(name));
};
value.storage = async (name) => {
const storage = new IDBMapSync(name);
await storage.sync();
return storage;
};
value.JSON = JSON;
}
interpreter.registerJsModule(name, value);
};
export const getFormat = (path, url) => {
if (path.endsWith('/*')) {
if (/\.(zip|whl|tgz|tar(?:\.gz)?)$/.test(url))
return RegExp.$1;
throw new Error(`Unsupported archive ${url}`);
}
return '';
};
export const run = (interpreter, code, ...args) => {
try {
return interpreter.runPython(dedent(code), ...args);
}
catch (error) {
io.get(interpreter).stderr(error);
}
};
export const runAsync = async (interpreter, code, ...args) => {
try {
return await interpreter.runPythonAsync(dedent(code), ...args);
}
catch (error) {
io.get(interpreter).stderr(error);
}
};
export const runEvent = async (interpreter, code, event) => {
// allows method(event) as well as namespace.method(event)
// it does not allow fancy brackets names for now
const [name, ...keys] = code.split('.');
let target = interpreter.globals.get(name);
let context;
for (const key of keys) [context, target] = [target, target[key]];
try {
await target.call(context, event);
}
catch (error) {
io.get(interpreter).stderr(error);
}
};
/* c8 ignore stop */