mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
Merge branch 'main' into poc_ui_blocks
This commit is contained in:
@@ -26,8 +26,8 @@ import { ErrorCode } from "./exceptions.js";
|
|||||||
import { robustFetch as fetch, getText } from "./fetch.js";
|
import { robustFetch as fetch, getText } from "./fetch.js";
|
||||||
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
|
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
|
||||||
|
|
||||||
import stdlib from "./stdlib.js";
|
import { stdlib, optional } from "./stdlib.js";
|
||||||
export { stdlib };
|
export { stdlib, optional };
|
||||||
|
|
||||||
// generic helper to disambiguate between custom element and script
|
// generic helper to disambiguate between custom element and script
|
||||||
const isScript = ({ tagName }) => tagName === "SCRIPT";
|
const isScript = ({ tagName }) => tagName === "SCRIPT";
|
||||||
@@ -168,7 +168,7 @@ for (const [TYPE, interpreter] of TYPES) {
|
|||||||
// specific main and worker hooks
|
// specific main and worker hooks
|
||||||
const hooks = {
|
const hooks = {
|
||||||
main: {
|
main: {
|
||||||
...codeFor(main),
|
...codeFor(main, TYPE),
|
||||||
async onReady(wrap, element) {
|
async onReady(wrap, element) {
|
||||||
registerModule(wrap);
|
registerModule(wrap);
|
||||||
|
|
||||||
@@ -265,7 +265,7 @@ for (const [TYPE, interpreter] of TYPES) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
worker: {
|
worker: {
|
||||||
...codeFor(worker),
|
...codeFor(worker, TYPE),
|
||||||
// these are lazy getters that returns a composition
|
// these are lazy getters that returns a composition
|
||||||
// of the current hooks or undefined, if no hook is present
|
// of the current hooks or undefined, if no hook is present
|
||||||
get onReady() {
|
get onReady() {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { typedSet } from "type-checked-collections";
|
|||||||
import { dedent } from "polyscript/exports";
|
import { dedent } from "polyscript/exports";
|
||||||
import toJSONCallback from "to-json-callback";
|
import toJSONCallback from "to-json-callback";
|
||||||
|
|
||||||
import stdlib from "./stdlib.js";
|
import { stdlib, optional } from "./stdlib.js";
|
||||||
|
|
||||||
export const main = (name) => hooks.main[name];
|
export const main = (name) => hooks.main[name];
|
||||||
export const worker = (name) => hooks.worker[name];
|
export const worker = (name) => hooks.worker[name];
|
||||||
@@ -15,10 +15,11 @@ const code = (hooks, branch, key, lib) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const codeFor = (branch) => {
|
export const codeFor = (branch, type) => {
|
||||||
|
const pylib = type === "mpy" ? stdlib.replace(optional, "") : stdlib;
|
||||||
const hooks = {};
|
const hooks = {};
|
||||||
code(hooks, branch, `codeBeforeRun`, stdlib);
|
code(hooks, branch, `codeBeforeRun`, pylib);
|
||||||
code(hooks, branch, `codeBeforeRunAsync`, stdlib);
|
code(hooks, branch, `codeBeforeRunAsync`, pylib);
|
||||||
code(hooks, branch, `codeAfterRun`);
|
code(hooks, branch, `codeAfterRun`);
|
||||||
code(hooks, branch, `codeAfterRunAsync`);
|
code(hooks, branch, `codeAfterRunAsync`);
|
||||||
return hooks;
|
return hooks;
|
||||||
|
|||||||
@@ -8,6 +8,27 @@
|
|||||||
|
|
||||||
import pyscript from "./stdlib/pyscript.js";
|
import pyscript from "./stdlib/pyscript.js";
|
||||||
|
|
||||||
|
class Ignore extends Array {
|
||||||
|
#add = false;
|
||||||
|
#paths;
|
||||||
|
#array;
|
||||||
|
constructor(array, ...paths) {
|
||||||
|
super();
|
||||||
|
this.#array = array;
|
||||||
|
this.#paths = paths;
|
||||||
|
}
|
||||||
|
push(...values) {
|
||||||
|
if (this.#add) super.push(...values);
|
||||||
|
return this.#array.push(...values);
|
||||||
|
}
|
||||||
|
path(path) {
|
||||||
|
for (const _path of this.#paths) {
|
||||||
|
// bails out at the first `true` value
|
||||||
|
if ((this.#add = path.startsWith(_path))) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const { entries } = Object;
|
const { entries } = Object;
|
||||||
|
|
||||||
const python = [
|
const python = [
|
||||||
@@ -16,16 +37,19 @@ const python = [
|
|||||||
"_path = None",
|
"_path = None",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const ignore = new Ignore(python, "./pyweb");
|
||||||
|
|
||||||
const write = (base, literal) => {
|
const write = (base, literal) => {
|
||||||
for (const [key, value] of entries(literal)) {
|
for (const [key, value] of entries(literal)) {
|
||||||
python.push(`_path = _Path("${base}/${key}")`);
|
ignore.path(`${base}/${key}`);
|
||||||
|
ignore.push(`_path = _Path("${base}/${key}")`);
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
const code = JSON.stringify(value);
|
const code = JSON.stringify(value);
|
||||||
python.push(`_path.write_text(${code},encoding="utf-8")`);
|
ignore.push(`_path.write_text(${code},encoding="utf-8")`);
|
||||||
} else {
|
} else {
|
||||||
// @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909
|
// @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909
|
||||||
python.push(`if not _os.path.exists("${base}/${key}"):`);
|
ignore.push(`if not _os.path.exists("${base}/${key}"):`);
|
||||||
python.push(" _path.mkdir(parents=True, exist_ok=True)");
|
ignore.push(" _path.mkdir(parents=True, exist_ok=True)");
|
||||||
write(`${base}/${key}`, value);
|
write(`${base}/${key}`, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,4 +66,5 @@ python.push(
|
|||||||
);
|
);
|
||||||
python.push("\n");
|
python.push("\n");
|
||||||
|
|
||||||
export default python.join("\n");
|
export const stdlib = python.join("\n");
|
||||||
|
export const optional = ignore.join("\n");
|
||||||
|
|||||||
5
pyscript.core/types/core.d.ts
vendored
5
pyscript.core/types/core.d.ts
vendored
@@ -1,4 +1,5 @@
|
|||||||
import stdlib from "./stdlib.js";
|
import { stdlib } from "./stdlib.js";
|
||||||
|
import { optional } from "./stdlib.js";
|
||||||
import TYPES from "./types.js";
|
import TYPES from "./types.js";
|
||||||
/**
|
/**
|
||||||
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
||||||
@@ -51,4 +52,4 @@ declare const exportedHooks: {
|
|||||||
};
|
};
|
||||||
declare const exportedConfig: {};
|
declare const exportedConfig: {};
|
||||||
declare const exportedWhenDefined: (type: string) => Promise<any>;
|
declare const exportedWhenDefined: (type: string) => Promise<any>;
|
||||||
export { stdlib, TYPES, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
|
export { stdlib, optional, TYPES, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
|
||||||
|
|||||||
2
pyscript.core/types/hooks.d.ts
vendored
2
pyscript.core/types/hooks.d.ts
vendored
@@ -1,6 +1,6 @@
|
|||||||
export function main(name: any): any;
|
export function main(name: any): any;
|
||||||
export function worker(name: any): any;
|
export function worker(name: any): any;
|
||||||
export function codeFor(branch: any): {};
|
export function codeFor(branch: any, type: any): {};
|
||||||
export function createFunction(self: any, name: any): any;
|
export function createFunction(self: any, name: any): any;
|
||||||
export namespace hooks {
|
export namespace hooks {
|
||||||
namespace main {
|
namespace main {
|
||||||
|
|||||||
4
pyscript.core/types/stdlib.d.ts
vendored
4
pyscript.core/types/stdlib.d.ts
vendored
@@ -1,2 +1,2 @@
|
|||||||
declare const _default: string;
|
export const stdlib: string;
|
||||||
export default _default;
|
export const optional: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user