Fix #1814 - Basic mpy integration (#1815)

This commit is contained in:
Andrea Giammarchi
2023-10-27 15:30:21 +02:00
committed by GitHub
parent cd95a42e5e
commit d9bf5cae12
7 changed files with 146 additions and 26 deletions

View File

@@ -10,7 +10,11 @@ import pyscript from "./stdlib/pyscript.js";
const { entries } = Object;
const python = ["from pathlib import Path as _Path", "_path = None"];
const python = [
"import os as _os",
"from pathlib import Path as _Path",
"_path = None",
];
const write = (base, literal) => {
for (const [key, value] of entries(literal)) {
@@ -19,7 +23,9 @@ const write = (base, literal) => {
const code = JSON.stringify(value);
python.push(`_path.write_text(${code})`);
} else {
python.push("_path.mkdir(parents=True, exist_ok=True)");
// @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)");
write(`${base}/${key}`, value);
}
}
@@ -29,6 +35,7 @@ write(".", pyscript);
python.push("del _Path");
python.push("del _path");
python.push("del _os");
python.push("\n");
export default python.join("\n");