See More

// python-js module for nodejs // var fs = require('fs'); var path = require('path'); var requirejs = require('requirejs'); var pyjs = require('./pythonjs.js'); var empythoned = require('./empythoned.js'); var Python = empythoned.Python; var buffer = []; var _on_stderr = function(chr) { if (chr == null || chr == 0 || chr == 10) { console.log( buffer.join('') ); buffer.length = 0; } else { buffer.push( String.fromCharCode(chr) ); } } // https://github.com/kripken/emscripten/wiki/Filesystem-API empythoned.FS.createLazyFile(".", "python_to_pythonjs.py", "./python_to_pythonjs.py", true,false ); empythoned.FS.createLazyFile(".", "pythonjs.py", "./pythonjs.py", true,false ); empythoned.FS.createLazyFile(".", "ministdlib.py", "./ministdlib.py", true,false ); empythoned.FS.createLazyFile(".", "typedpython.py", "./typedpython.py", true,false ); empythoned.FS.createLazyFile(".", "pythonjs_to_dart.py", "./pythonjs_to_dart.py", true,false ); empythoned.FS.createLazyFile(".", "pythonjs_to_coffee.py", "./pythonjs_to_coffee.py", true,false ); empythoned.FS.createLazyFile(".", "pythonjs_to_lua.py", "./pythonjs_to_lua.py", true,false ); empythoned.FS.createLazyFile(".", "python_to_visjs.py", "./python_to_visjs.py", true,false ); empythoned.FS.createLazyFile(".", "code_writer.py", "./code_writer.py", true,false ); empythoned.FS.createLazyFile(".", "inline_function.py", "./inline_function.py", true,false ); empythoned.FS.createLazyFile(".", "ast_utils.py", "./ast_utils.py", true,false ); empythoned.FS.createLazyFile(".", "pythonjs.js", "./pythonjs.js", true,false ); empythoned.FS.createFolder(".","fakelibs",true,true); empythoned.FS.createLazyFile("./fakelibs", "tornado.py", "./fakelibs/tornado.py", true,false ); empythoned.FS.createLazyFile("./fakelibs", "os.py", "./fakelibs/os.py", true,false ); empythoned.FS.createLazyFile("./fakelibs", "sys.py", "./fakelibs/sys.py", true,false ); empythoned.FS.createLazyFile("./fakelibs", "tempfile.py", "./fakelibs/tempfile.py", true,false ); empythoned.FS.createLazyFile("./fakelibs", "subprocess.py", "./fakelibs/subprocess.py", true,false ); Python.initialize( null, // stdin null, // stdout _on_stderr // stderr ); Python.eval('from python_to_pythonjs import main as to_pythonjs'); Python.eval('from pythonjs import main as _to_javascript'); //Python.eval('from pythonjs_to_dart import main as _to_dart'); //Python.eval('from pythonjs_to_coffee import main as _to_coffee'); //Python.eval('from pythonjs_to_lua import main as _to_lua'); Python.eval('from python_to_visjs import main as to_visjs'); Python.eval('def to_javascript_module(src, runtime=True): return _to_javascript(to_pythonjs(src), requirejs=True, insert_runtime=runtime)'); Python.eval('def to_javascript(src, runtime=False): return _to_javascript(to_pythonjs(src), requirejs=False, insert_runtime=runtime)'); Python.eval('def to_dart(src): from pythonjs_to_dart import main as _to_dart; return _to_dart(to_pythonjs(src, dart=True))'); Python.eval('def to_coffee(src): from pythonjs_to_coffee import main as _to_coffee; return _to_coffee(to_pythonjs(src, coffee=True))'); Python.eval('def to_lua(src): from pythonjs_to_lua import main as _to_lua; return _to_lua(to_pythonjs(src, lua=True))'); function clean_code( code ) { code = code.substring(1, code.length-1); // strip quotes code = code.split('\\n').join('\n'); code = code.split('\\t').join('\t'); code = code.split("\\'").join("\'"); code = code.split('\\"').join('\"'); return code; } // data is written to a file to avoid any problems with escaping string quotes // note: `FS.createDataFile` is part of the old Emscripten 1.0 API exports.translator = { to_javascript_module : function(data, runtime) { empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true ); if (runtime == true || runtime === undefined) { var code = Python.eval('to_javascript_module(open("/sandbox/temp","r").read(), runtime=True)'); } else { var code = Python.eval('to_javascript_module(open("/sandbox/temp","r").read(), runtime=False)'); } if (code !== null && code !== undefined) { return clean_code( code ); } }, to_javascript : function(data, runtime) { empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true ); if (runtime == true) { var code = Python.eval('to_javascript(open("/sandbox/temp","r").read(), runtime=True)'); } else { var code = Python.eval('to_javascript(open("/sandbox/temp","r").read(), runtime=False)'); } if (code !== null && code !== undefined) { return clean_code( code ); } }, to_dart : function(data) { empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true ); var code = Python.eval('to_dart(open("/sandbox/temp","r").read())'); if (code !== null && code !== undefined) { return clean_code( code ); } }, to_coffee : function(data) { empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true ); var code = Python.eval('to_coffee(open("/sandbox/temp","r").read())'); if (code !== null && code !== undefined) { return clean_code( code ); } }, to_lua : function(data) { empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true ); var code = Python.eval('to_lua(open("/sandbox/temp","r").read())'); if (code !== null && code !== undefined) { return clean_code( code ); } }, to_visjs : function(data) { empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true ); var code = Python.eval('to_visjs(open("/sandbox/temp","r").read())'); if (code !== null && code !== undefined) { return clean_code( code ); } } } var runtime = {}; var basepath = path.dirname( __filename ); Object.defineProperty( runtime, 'javascript', { enumerable:true, get : function() {return fs.readFileSync( basepath+'/pythonjs.js', {'encoding':'utf8'} )} } ); Object.defineProperty( runtime, 'dart', { enumerable:true, get : function() {return fs.readFileSync( basepath+'/runtime/dart_builtins.py', {'encoding':'utf8'} )} } ); Object.defineProperty( runtime, 'lua', { enumerable:true, get : function() {return fs.readFileSync( basepath+'/runtime/lua_builtins.py', {'encoding':'utf8'} )} } ); exports.runtime = runtime;