From cd3fb435e33b9053d9586663b5535c257dcb2d6f Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 15 Jan 2018 16:33:48 +0800 Subject: [PATCH 1/5] js2py.require support path such as js2py.require('crypto-js/aes') --- js2py/node_import.py | 18 +++++++++++++----- simple_test.py | 8 +++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/js2py/node_import.py b/js2py/node_import.py index c679310e..83985d1f 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,7 +41,11 @@ 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) + var_name = py_name.rpartition('/')[-1] + oldcwd = os.getcwd() + os.chdir(PY_NODE_MODULES_PATH) + cached_py_npm_modules = glob.glob('**', recursive=True) + os.chdir(oldcwd) if module_filename not in cached_py_npm_modules or update: _init() in_file_name = 'tmp0in439341018923js2py.js' @@ -56,8 +60,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 +77,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 +92,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..c4903a9a 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') @@ -116,4 +123,3 @@ class Shape { a = new Shape(1,2,3) ''').x == 2 print("Passed ECMA 6!") - From 6c0d47627d208ebb0dab92e910ccd5c807d5a877 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 15 Jan 2018 16:42:35 +0800 Subject: [PATCH 2/5] remove empty line --- js2py/node_import.py | 1 - 1 file changed, 1 deletion(-) diff --git a/js2py/node_import.py b/js2py/node_import.py index 83985d1f..f9f1a6a2 100644 --- a/js2py/node_import.py +++ b/js2py/node_import.py @@ -93,4 +93,3 @@ def require(module_name, include_polyfill=False, update=False): context = {} exec(py_code, context) return context['var'][var_name].to_py() - From 21e9ea87394160995217601990bc8a0d93dbb884 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 15 Jan 2018 16:44:04 +0800 Subject: [PATCH 3/5] add empty lin --- js2py/node_import.py | 1 + 1 file changed, 1 insertion(+) diff --git a/js2py/node_import.py b/js2py/node_import.py index f9f1a6a2..83985d1f 100644 --- a/js2py/node_import.py +++ b/js2py/node_import.py @@ -93,3 +93,4 @@ def require(module_name, include_polyfill=False, update=False): context = {} exec(py_code, context) return context['var'][var_name].to_py() + From aa74bfda8ac1f3181104ffd8835b3f77060c0c8a Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 15 Jan 2018 16:45:18 +0800 Subject: [PATCH 4/5] add empty line --- simple_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/simple_test.py b/simple_test.py index c4903a9a..904dfa3c 100644 --- a/simple_test.py +++ b/simple_test.py @@ -123,3 +123,4 @@ class Shape { a = new Shape(1,2,3) ''').x == 2 print("Passed ECMA 6!") + From f8cdb7dda6e0b29d61817ffc31fd1637b2809524 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 15 Jan 2018 16:56:33 +0800 Subject: [PATCH 5/5] support for py2 --- js2py/node_import.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/js2py/node_import.py b/js2py/node_import.py index 83985d1f..30dc1b60 100644 --- a/js2py/node_import.py +++ b/js2py/node_import.py @@ -42,11 +42,7 @@ def require(module_name, include_polyfill=False, update=False): py_name = module_name.replace('-', '_') module_filename = '%s.py'%py_name var_name = py_name.rpartition('/')[-1] - oldcwd = os.getcwd() - os.chdir(PY_NODE_MODULES_PATH) - cached_py_npm_modules = glob.glob('**', recursive=True) - os.chdir(oldcwd) - if module_filename not in cached_py_npm_modules or update: + 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'