//!wrt $BSPEC:{"icn":"mime/python-obj","cpr":"Kelbaz","dsc":"A simple python interpretor using Brython","frn":"Python","ver":1,"ssy":"cli"} const { term, cwd } = this.boxedEnv; if (!term) return; class PythonInterpretor extends WApplication { /** * Application constructor. */ constructor() { super(); } /** * Main entry point. * @param {String[]} argv The program arguments. */ async main(argv) { super.main(argv); const brython = await include("C:/local/python/brython.js"); const cmdReg = [ { name: "run", exec: runFile.bind(this, argv[2]), usage: " - Execute a Python script from a file" }, { name: "compile", exec: compileFile.bind(this, argv[2]), usage: " - Compile a Python script from a file" }, { name: "help", exec: showHelp, usage: "[command] - Shows help" } ] // Function definitions: async function compileFile(arg) { if (!arg) return term.writeln("\x1b[31mSpecify the path to the file\x1b[0m"); let path = arg; if (!(await FS.exists(path))) { if (await FS.exists(FSUtil.combinePath(cwd, path))) path = FSUtil.combinePath(cwd, path); else return term.writeln("\x1b[31mThe specified file doesn't exist\x1b[0m"); } if (!(await FS.isFile(path))) return term.writeln("\x1b[31mThe specified path doesn't link to a file\x1b[0m"); const file = await FS.readstr(path); await (() => { let jsCode; try { jsCode = brython.py2js(file).to_js(); } catch (e) { return term.writeln("\x1b[31m" + "Error while compiling: " + e + "\x1b[0m"); } let pathNoExt = path.split("."); pathNoExt.pop(); pathNoExt = pathNoExt.join("."); FS.writestr(pathNoExt, [ "//!wrt", "const __BRYTHON__ = await include('C:/local/python/brython.js');", "await (() => {", jsCode, "})();" ].join("\n")) })(); } async function runFile(arg) { if (!arg) return term.writeln("\x1b[31mSpecify the path to the file\x1b[0m"); let path = arg; if (!(await FS.exists(path))) { if (await FS.exists(FSUtil.combinePath(cwd, path))) path = FSUtil.combinePath(cwd, path); else return term.writeln("\x1b[31mThe specified file doesn't exist\x1b[0m"); } if (!(await FS.isFile(path))) return term.writeln("\x1b[31mThe specified path doesn't link to a file\x1b[0m"); const file = await FS.readstr(path); await (() => { let jsCode; try { jsCode = brython.py2js(file).to_js(); } catch (e) { return term.writeln("\x1b[31m" + e + "\x1b[0m"); } new Function([ "const { __BRYTHON__ } = this;", jsCode ].join("\n")).call( Object.assign( { __BRYTHON__: brython }, globalThis ) ) })(); } function showHelp(showTitle = 1) { if (!argv[2]) { if (showTitle) { term.writeln([ "\x1b[1;34mPython Interpretor", "\x1b[0m", ].join("\n")); } term.writeln([ "Usage: python [arguments]", "", "Commands:" ].join("\n")); for (const cmd of cmdReg) { term.writeln(` ${cmd.name} ${cmd.usage}`); } term.writeln(""); } else { let i = 0; for (const cmd of cmdReg) { if (cmd.name === argv[2]) { term.writeln(`Usage: ${cmd.name} ${cmd.usage}`); } else if (++i >= cmdReg.length) { term.writeln([ "\x1b[31mCommand not found\x1b[0m", "Commands:" ].join("\n")); for (const cmd of cmdReg) { term.writeln(` ${cmd.name} ${cmd.usage}`); } term.writeln(""); } } } } // Command handler: if (!argv[1]) { showHelp(); } else { let i = 0; for await (const cmd of cmdReg) { if (cmd.name === argv[1]) { await cmd.exec(); } else if (++i >= cmdReg.length) { runFile(argv[1]); } } } this.terminate(); } } return await WApplication.execAsync(new PythonInterpretor(), this.boxedEnv.args, this);