8000 Merge branch 'main' into poc_ui_blocks · pyscript/pyscript@6dc75e5 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 6dc75e5

Browse files
Merge branch 'main' into poc_ui_blocks
2 parents 9b46e7c + cf92996 commit 6dc75e5

File tree

6 files changed

+45
-18
lines changed

6 files changed

+45
-18
lines changed

pyscript.core/src/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { ErrorCode } from "./exceptions.js";
2626
import { robustFetch as fetch, getText } from "./fetch.js";
2727
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
2828

29-
import stdlib from "./stdlib.js";
30-
export { stdlib };
29+
import { stdlib, optional } from "./stdlib.js";
30+
export { stdlib, optional };
3131

3232
// generic helper to disambiguate between custom element and script
3333
const isScript = ({ tagName }) => tagName === "SCRIPT";
@@ -168,7 +168,7 @@ for (const [TYPE, interpreter] of TYPES) {
168168
// specific main and worker hooks
169169
const hooks = {
170170
main: {
171-
...codeFor(main),
171+
...codeFor(main, TYPE),
172172
async onReady(wrap, element) {
173173
registerModule(wrap);
174174

@@ -265,7 +265,7 @@ for (const [TYPE, interpreter] of TYPES) {
265265
},
266266
},
267267
worker: {
268-
...codeFor(worker),
268+
...codeFor(worker, TYPE),
269269
// these are lazy getters that returns a composition
270270
// of the current hooks or undefined, if no hook is present
271271
get onReady() {

pyscript.core/src/hooks.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { typedSet } from "type-checked-collections";
22
import { dedent } from "polyscript/exports";
33
import toJSONCallback from "to-json-callback";
44

5-
import stdlib from "./stdlib.js";
5+
import { stdlib, optional } from "./stdlib.js";
66

77
export const main = (name) => hooks.main[name];
88
export const worker = (name) => hooks.worker[name];
@@ -15,10 +15,11 @@ const code = (hooks, branch, key, lib) => {
1515
};
1616
};
1717

18-
export const codeFor = (branch) => {
18+
export const codeFor = (branch, type) => {
19+
const pylib = type === "mpy" ? stdlib.replace(optional, "") : stdlib;
1920
const hooks = {};
20-
code(hooks, branch, `codeBeforeRun`, stdlib);
21-
code(hooks, branch, `codeBeforeRunAsync`, stdlib);
21+
code(hooks, branch, `codeBeforeRun`, pylib);
22+
code(hooks, branch, `codeBeforeRunAsync`, pylib);
2223
code(hooks, branch, `codeAfterRun`);
2324
code(hooks, branch, `codeAfterRunAsync`);
2425
return hooks;

pyscript.core/src/stdlib.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88

99
import pyscript from "./stdlib/pyscript.js";
1010

11+
class Ignore extends Array {
12+
#add = false;
13+
#paths;
14+
#array;
15+
constructor(array, ...paths) {
16+
super();
17+
this.#array = array;
18+
this.#paths = paths;
19+
}
20+
push(...values) {
21+
if (this.#add) super.push(...values);
22+
return this.#array.push(...values);
23+
}
24+
path(path) {
25+
for (const _path of this.#paths) {
26+
// bails out at the first `true` value
27+
if ((this.#add = path.startsWith(_path))) break;
28+
}
29+
}
30+
}
31+
1132
const { entries } = Object;
1233

1334
const python = [
@@ -16,16 +37,19 @@ const python = [
1637
"_path = None",
1738
];
1839

40+
const ignore = new Ignore(python, "./pyweb");
41+
1942
const write = (base, literal) => {
2043
for (const [key, value] of entries(literal)) {
21-
python.push(`_path = _Path("${base}/${key}")`);
44+
ignore.path(`${base}/${key}`);
45+
ignore.push(`_path = _Path("${base}/${key}")`);
2246
if (typeof value === "string") {
2347
const code = JSON.stringify(value);
24-
python.push(`_path.write_text(${code},encoding="utf-8")`);
48+
ignore.push(`_path.write_text(${code},encoding="utf-8")`);
2549
} else {
2650
// @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909
27-
python.push(`if not _os.path.exists("${base}/${key}"):`);
28-
python.push(" _path.mkdir(parents=True, exist_ok=True)");
51+
ignore.push(`if not _os.path.exists("${base}/${key}"):`);
52+
ignore.push(" _path.mkdir(parents=True, exist_ok=True)");
2953
write(`${base}/${key}`, value);
3054
}
3155
}
@@ -42,4 +66,5 @@ python.push(
4266
);
4367
python.push("\n");
4468

45-
export default python.join("\n");
69+
export const stdlib = python.join("\n");
70+
export const optional = ignore.join("\n");

pyscript.core/types/core.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import stdlib from "./stdlib.js";
1+
import { stdlib } from "./stdlib.js";
2+
import { optional } from "./stdlib.js";
23
import TYPES from "./types.js";
34
/**
45
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
@@ -51,4 +52,4 @@ declare const exportedHooks: {
5152
};
5253
declare const exportedConfig: {};
5354
declare const exportedWhenDefined: (type: string) => Promise<any>;
54-
export { stdlib, TYPES, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
55+
export { stdlib, optional, TYPES, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };

pyscript.core/types/hooks.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function main(name: any): any;
22
export function worker(name: any): any;
3-
export function codeFor(branch: any): {};
3+
export function codeFor(branch: any, type: any): {};
44
export function createFunction(self: any, name: any): any;
55
export namespace hooks {
66
namespace main {

pyscript.core/types/stdlib.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
declare const _default: string;
2-
export default _default;
1+
export const stdlib: string;
2+
export const optional: string;

0 commit comments

Comments
 (0)
0