Skip to content

Commit e72eb64

Browse files
author
hartsantler
committed
new nodejs command line "python-js" (cli.js) translates scripts to: javascript, dart, coffee, lua, and vis.js
1 parent fe1b58c commit e72eb64

9 files changed

Lines changed: 49 additions & 10 deletions

File tree

pythonjs/cli.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
4+
if (process.argv.length <= 2) {
5+
console.log('python-js input.py output.js [--dart, --coffee, --lua, --visjs]')
6+
console.log('(the default output is javascript)')
7+
} else {
8+
var fs = require('fs')
9+
var pythonjs = require('./python-js')
10+
var input = fs.readFileSync( process.argv[2], {'encoding':'utf8'} )
11+
12+
if (process.argv.indexOf('--dart') != -1) {
13+
var output = pythonjs.translator.to_dart( input )
14+
} else if (process.argv.indexOf('--coffee') != -1) {
15+
var output = pythonjs.translator.to_coffee( input )
16+
} else if (process.argv.indexOf('--lua') != -1) {
17+
var output = pythonjs.translator.to_lua( input )
18+
} else if (process.argv.indexOf('--visjs') != -1) {
19+
var output = pythonjs.translator.to_visjs( input )
20+
} else {
21+
var output = pythonjs.translator.to_javascript( input )
22+
23+
}
24+
25+
if (process.argv.length >= 4) {
26+
fs.writeFileSync( process.argv[3], output, {'encoding':'utf8'} )
27+
} else {
28+
console.log(output)
29+
}
30+
31+
}

pythonjs/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"preferGlobal":true,
2020

2121
"main": "./python-js",
22+
"bin" : {"python-js" : "./cli.js"},
2223

2324
"files": [
2425
"lib",
@@ -31,6 +32,8 @@
3132
"pythonjs_to_dart.py",
3233
"pythonjs_to_coffee.py",
3334
"pythonjs_to_lua.py",
34-
"python_to_visjs.py"
35+
"python_to_visjs.py",
36+
"cli.js",
37+
"runtime"
3538
]
3639
}

pythonjs/python-js

100755100644
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env node
1+
// python-js module for nodejs //
22

33
var _path = require('path');
44
var pyjs = require('./pythonjs.js');
@@ -59,9 +59,9 @@ Python.eval('from pythonjs_to_lua import main as _to_lua');
5959
Python.eval('from python_to_visjs import main as to_visjs');
6060

6161
Python.eval('def to_javascript(src): return _to_javascript(to_pythonjs(src))');
62-
Python.eval('def to_dart(src): return _to_dart(to_pythonjs(src))');
63-
Python.eval('def to_coffee(src): return _to_coffee(to_pythonjs(src))');
64-
Python.eval('def to_lua(src): return _to_lua(to_pythonjs(src))');
62+
Python.eval('def to_dart(src): return _to_dart(to_pythonjs(src, dart=True))');
63+
Python.eval('def to_coffee(src): return _to_coffee(to_pythonjs(src, coffee=True))');
64+
Python.eval('def to_lua(src): return _to_lua(to_pythonjs(src, lua=True))');
6565

6666
function clean_code( code ) {
6767
code = code.substring(1, code.length-1); // strip quotes

pythonjs/python_to_pythonjs.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,13 @@ def inspect_method( node ):
30303030
info['properties'] = retrieve_properties( node.body )
30313031
return info
30323032

3033-
def main(script):
3034-
PythonToPythonJS( source=script, dart='--dart' in sys.argv )
3033+
def main(script, dart=False, coffee=False, lua=False):
3034+
PythonToPythonJS(
3035+
source = script,
3036+
dart = dart or '--dart' in sys.argv,
3037+
coffee = coffee,
3038+
lua = lua
3039+
)
30353040
code = writer.getvalue()
30363041
if '--debug' in sys.argv:
30373042
try:

tests/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
runtime = os.path.abspath('../pythonjs/pythonjs.js'),
2424
module_cache = '/tmp',
2525

26-
runtime_pythonjs = os.path.abspath('../runtime/pythonpythonjs.py'), ## handwritten pythonjs
27-
runtime_builtins = os.path.abspath('../runtime/builtins.py'),
28-
runtime_dart = os.path.abspath('../runtime/dart_builtins.py'),
26+
runtime_pythonjs = os.path.abspath('../pythonjs/runtime/pythonpythonjs.py'), ## handwritten pythonjs
27+
runtime_builtins = os.path.abspath('../pythonjs/runtime/builtins.py'),
28+
runtime_dart = os.path.abspath('../pythonjs/runtime/dart_builtins.py'),
2929

3030
dart2js = os.path.expanduser( '~/dart/dart-sdk/bin/dart2js'),
3131
dartanalyzer = os.path.expanduser( '~/dart/dart-sdk/bin/dartanalyzer'),

0 commit comments

Comments
 (0)