forked from qsnake/py2js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyjs.py
executable file
·54 lines (44 loc) · 1.67 KB
/
pyjs.py
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
#! /usr/bin/env python
import sys
import os.path
from optparse import OptionParser
from py2js import Compiler
def main():
parser = OptionParser(usage="%prog [options] filename",
description="Python to JavaScript compiler.")
parser.add_option("--output",
action="store",
dest="output",
help="write output to OUTPUT")
parser.add_option("-i", "--include-builtins",
action="store_true",
dest="include_builtins",
default=False,
help="include py-builtins.js library in the output")
parser.add_option("-I", "--import-builtins",
action="store_true",
dest="import_builtins",
default=False,
help="call load('py-builtins.js') to source the standard library")
options, args = parser.parse_args()
if len(args) == 1:
filename = args[0]
if options.output:
output = open(options.output, "w")
else:
output = sys.stdout
if options.include_builtins:
if os.path.dirname(__file__):
builtins = open(os.path.join(os.path.dirname(__file__), "py-builtins.js")).read()
else:
builtins = open("py-builtins.js").read()
output.write(builtins)
elif options.import_builtins:
output.write('load("py-builtins.js");\n')
c = Compiler()
c.append_string(open(filename).read())
output.write(str(c))
else:
parser.print_help()
if __name__ == '__main__':
main()