diff --git a/pyscript.core/src/core.js b/pyscript.core/src/core.js index d3955557..fb7092ab 100644 --- a/pyscript.core/src/core.js +++ b/pyscript.core/src/core.js @@ -26,8 +26,8 @@ import { ErrorCode } from "./exceptions.js"; import { robustFetch as fetch, getText } from "./fetch.js"; import { hooks, main, worker, codeFor, createFunction } from "./hooks.js"; -import stdlib from "./stdlib.js"; -export { stdlib }; +import { stdlib, optional } from "./stdlib.js"; +export { stdlib, optional }; // generic helper to disambiguate between custom element and script const isScript = ({ tagName }) => tagName === "SCRIPT"; @@ -168,7 +168,7 @@ for (const [TYPE, interpreter] of TYPES) { // specific main and worker hooks const hooks = { main: { - ...codeFor(main), + ...codeFor(main, TYPE), async onReady(wrap, element) { registerModule(wrap); @@ -265,7 +265,7 @@ for (const [TYPE, interpreter] of TYPES) { }, }, worker: { - ...codeFor(worker), + ...codeFor(worker, TYPE), // these are lazy getters that returns a composition // of the current hooks or undefined, if no hook is present get onReady() { diff --git a/pyscript.core/src/hooks.js b/pyscript.core/src/hooks.js index 921ae6a9..03950bf4 100644 --- a/pyscript.core/src/hooks.js +++ b/pyscript.core/src/hooks.js @@ -2,7 +2,7 @@ import { typedSet } from "type-checked-collections"; import { dedent } from "polyscript/exports"; 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 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 = {}; - code(hooks, branch, `codeBeforeRun`, stdlib); - code(hooks, branch, `codeBeforeRunAsync`, stdlib); + code(hooks, branch, `codeBeforeRun`, pylib); + code(hooks, branch, `codeBeforeRunAsync`, pylib); code(hooks, branch, `codeAfterRun`); code(hooks, branch, `codeAfterRunAsync`); return hooks; diff --git a/pyscript.core/src/stdlib.js b/pyscript.core/src/stdlib.js index ddc4efc2..45f2e162 100644 --- a/pyscript.core/src/stdlib.js +++ b/pyscript.core/src/stdlib.js @@ -8,6 +8,27 @@ 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 python = [ @@ -16,16 +37,19 @@ const python = [ "_path = None", ]; +const ignore = new Ignore(python, "./pyweb"); + const write = (base, 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") { const code = JSON.stringify(value); - python.push(`_path.write_text(${code},encoding="utf-8")`); + ignore.push(`_path.write_text(${code},encoding="utf-8")`); } else { // @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909 - python.push(`if not _os.path.exists("${base}/${key}"):`); - python.push(" _path.mkdir(parents=True, exist_ok=True)"); + ignore.push(`if not _os.path.exists("${base}/${key}"):`); + ignore.push(" _path.mkdir(parents=True, exist_ok=True)"); write(`${base}/${key}`, value); } } @@ -42,4 +66,5 @@ python.push( ); python.push("\n"); -export default python.join("\n"); +export const stdlib = python.join("\n"); +export const optional = ignore.join("\n"); diff --git a/pyscript.core/types/core.d.ts b/pyscript.core/types/core.d.ts index a5680153..629011e3 100644 --- a/pyscript.core/types/core.d.ts +++ b/pyscript.core/types/core.d.ts @@ -1,4 +1,5 @@ -import stdlib from "./stdlib.js"; +import { stdlib } from "./stdlib.js"; +import { optional } from "./stdlib.js"; import TYPES from "./types.js"; /** * 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 exportedWhenDefined: (type: string) => Promise; -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 }; diff --git a/pyscript.core/types/hooks.d.ts b/pyscript.core/types/hooks.d.ts index e79ff791..6e676d42 100644 --- a/pyscript.core/types/hooks.d.ts +++ b/pyscript.core/types/hooks.d.ts @@ -1,6 +1,6 @@ export function main(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 namespace hooks { namespace main { diff --git a/pyscript.core/types/stdlib.d.ts b/pyscript.core/types/stdlib.d.ts index ae80ecad..c42c11ac 100644 --- a/pyscript.core/types/stdlib.d.ts +++ b/pyscript.core/types/stdlib.d.ts @@ -1,2 +1,2 @@ -declare const _default: string; -export default _default; +export const stdlib: string; +export const optional: string;