diff --git a/js2py/node_import.py b/js2py/node_import.py index c679310e..30dc1b60 100644 --- a/js2py/node_import.py +++ b/js2py/node_import.py @@ -1,5 +1,5 @@ __all__ = ['require'] -import subprocess, os, codecs +import subprocess, os, codecs, glob from .evaljs import translate_js import six DID_INIT = False @@ -41,8 +41,8 @@ def require(module_name, include_polyfill=False, update=False): assert isinstance(module_name, str), 'module_name must be a string!' py_name = module_name.replace('-', '_') module_filename = '%s.py'%py_name - cached_py_npm_modules = os.listdir(PY_NODE_MODULES_PATH) - if module_filename not in cached_py_npm_modules or update: + var_name = py_name.rpartition('/')[-1] + if not os.path.exists(os.path.join(PY_NODE_MODULES_PATH, module_filename)) or update: _init() in_file_name = 'tmp0in439341018923js2py.js' out_file_name = 'tmp0out439341018923js2py.js' @@ -56,8 +56,9 @@ def require(module_name, include_polyfill=False, update=False): with open(os.path.join(DIRNAME, in_file_name), 'wb') as f: f.write(code.encode('utf-8') if six.PY3 else code) + pkg_name = module_name.partition('/')[0] # make sure the module is installed - assert subprocess.call('cd %s;npm install %s' %(repr(DIRNAME), module_name), shell=True, cwd=DIRNAME)==0, 'Could not install the required module: ' + module_name + assert subprocess.call('cd %s;npm install %s' %(repr(DIRNAME), pkg_name), shell=True, cwd=DIRNAME)==0, 'Could not install the required module: ' + pkg_name # convert the module assert subprocess.call( @@ -72,10 +73,13 @@ def require(module_name, include_polyfill=False, update=False): os.remove(os.path.join(DIRNAME, out_file_name)) js_code += GET_FROM_GLOBALS_FUNC - js_code += ';var %s = getFromGlobals(%s);%s' % (py_name, repr(module_name), py_name) + js_code += ';var %s = getFromGlobals(%s);%s' % (var_name, repr(module_name), var_name) print('Please wait, translating...') py_code = translate_js(js_code) + dirname = os.path.dirname(os.path.join(PY_NODE_MODULES_PATH, module_filename)) + if not os.path.isdir(dirname): + os.makedirs(dirname) with open(os.path.join(PY_NODE_MODULES_PATH, module_filename), 'wb') as f: f.write(py_code.encode('utf-8') if six.PY3 else py_code) else: @@ -84,5 +88,5 @@ def require(module_name, include_polyfill=False, update=False): context = {} exec(py_code, context) - return context['var'][py_name].to_py() + return context['var'][var_name].to_py() diff --git a/simple_test.py b/simple_test.py index 8ad00355..904dfa3c 100644 --- a/simple_test.py +++ b/simple_test.py @@ -42,6 +42,13 @@ decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)).to_list() assert decryptedData == data +AES = js2py.require('crypto-js/aes') +ciphertext = AES.encrypt(JSON.stringify(data), 'secret key 123') +bytes = AES.decrypt(ciphertext.toString(), 'secret key 123') +decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)).to_list() +assert decryptedData == data + + # esprima ( https://www.npmjs.com/package/esprima ) # escodegen ( https://github.com/estools/escodegen ) print('Testing esprima & escodegen')