Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
__author__ = 'Piotr Dabkowski'
__all__ = [
'EvalJs', 'translate_js', 'import_js', 'eval_js', 'parse_js',
'translate_file', 'run_file', 'disable_pyimport', 'eval_js6',
'translate_file', 'translate_file6', 'run_file', 'disable_pyimport', 'eval_js6',
'translate_js6', 'PyJsException', 'get_file_contents',
'write_file_contents', 'require'
]
Expand Down
26 changes: 25 additions & 1 deletion js2py/evaljs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import codecs

__all__ = [
'EvalJs', 'translate_js', 'import_js', 'eval_js', 'translate_file',
'EvalJs', 'translate_js', 'import_js', 'eval_js', 'translate_file', 'translate_file6',
'eval_js6', 'translate_js6', 'run_file', 'disable_pyimport',
'get_file_contents', 'write_file_contents'
]
Expand Down Expand Up @@ -81,6 +81,30 @@ def translate_file(input_path, output_path):
write_file_contents(output_path, out)


def translate_file6(input_path, output_path):
'''
Translates input JS file to python and saves the it to the output path just like translate_file.
Other than translate_file, this function adds the experimental ES6 support.

For example we have a file 'example.js' with: var a = function(x) {return x}
translate_file('example.js', 'example.py')

Now example.py can be easily importend and used:
>>> from example import example
>>> example.a(30)
30
'''
js = get_file_contents(input_path)

py_code = translate_js(js6_to_js5(js))
lib_name = os.path.basename(output_path).split('.')[0]
head = '__all__ = [%s]\n\n# Don\'t look below, you will not understand this Python code :) I don\'t.\n\n' % repr(
lib_name)
tail = '\n\n# Add lib to the module scope\n%s = var.to_python()' % lib_name
out = head + py_code + tail
write_file_contents(output_path, out)


def run_file(path_or_file, context=None):
''' Context must be EvalJS object. Runs given path as a JS program. Returns (eval_value, context).
'''
Expand Down