Js2Py
Js2Py copied to clipboard
Success story, but had to mock require/module/exports manually
I wanted to use the Easysync library in Etherpad Lite from Python. To my surprise, I succeeded with relatively little tweaking, and the test suite runs successfully.
To replicate what I did, you can install Js2Py and and run this in the root of the Etherpad Lite repository:
from pathlib import Path
import js2py.pyjs
def import_js(path, **modules):
def require(js_path):
for module_name, module in modules.items():
if js_path.to_python().endswith(f'/{module_name}'):
return module
exports = js2py.pyjs.Scope({})
context = js2py.EvalJs({'require': require,
'exports': exports,
'module': {'exports': exports}})
js2py.run_file(path, context)
return context
ETHERPAD_SRC_DIR = Path('./src')
ETHERPAD_JS_DIR = ETHERPAD_SRC_DIR / 'static/js'
changeset = import_js(ETHERPAD_JS_DIR / 'Changeset.js').exports
attributepool = import_js(ETHERPAD_JS_DIR / 'AttributePool.js').module.exports
easysync_tests = import_js(ETHERPAD_SRC_DIR / 'node/easysync_tests.js',
Changeset=changeset,
AttributePool=attributepool)
The problem I had to work around was missing support for require and catching exports or module.exports from the JavaScript modules. I wonder if I'm missing something, or whether Js2Py could improve support for JavaScript modules to make this work without extra effort? Are #57, #63 and #150 related to this?