forked from PythonJS/PythonJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
executable file
·75 lines (64 loc) · 2.25 KB
/
translator.py
File metadata and controls
executable file
·75 lines (64 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import sys, traceback, json
from python_to_pythonjs import main as python_to_pythonjs
from pythonjs import main as pythonjs_to_javascript
from pythonjs_to_dart import main as pythonjs_to_dart
from pythonjs_to_coffee import main as pythonjs_to_coffee
from pythonjs_to_lua import main as pythonjs_to_lua
from pythonjs_to_luajs import main as pythonjs_to_luajs
def main(script):
if '--visjs' in sys.argv:
import python_to_visjs
return python_to_visjs.main( script )
else:
code = ''
if '--dart' in sys.argv:
a = python_to_pythonjs(script, dart=True)
code = pythonjs_to_dart( a )
elif '--coffee' in sys.argv:
a = python_to_pythonjs(script, coffee=True)
code = pythonjs_to_coffee( a )
elif '--lua' in sys.argv:
a = python_to_pythonjs(script, lua=True)
try: code = pythonjs_to_lua( a )
except SyntaxError:
err = traceback.format_exc()
lineno = 0
for line in err.splitlines():
if "<unknown>" in line:
lineno = int(line.split()[-1])
b = a.splitlines()[ lineno ]
sys.stderr.write( '\n'.join([err,b]) )
elif '--luajs' in sys.argv:
a = python_to_pythonjs(script, lua=True)
code = pythonjs_to_luajs( a )
else:
a = python_to_pythonjs(script)
if isinstance(a, dict):
res = {}
for jsfile in a:
res[ jsfile ] = pythonjs_to_javascript( a[jsfile], webworker=jsfile != 'main' )
return res
else:
code = pythonjs_to_javascript( a )
return code
def command():
scripts = []
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg.endswith('.py'):
scripts.append( arg )
if len(scripts):
a = []
for script in scripts:
a.append( open(script, 'rb').read() )
data = '\n'.join( a )
else:
data = sys.stdin.read()
js = main(data)
if isinstance(js, dict):
print( json.dumps(js) )
else:
print(js)
if __name__ == '__main__':
command()