-
Notifications
You must be signed in to change notification settings - Fork 17
/
pyjs.py
executable file
·46 lines (37 loc) · 1.33 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
#! /usr/bin/env python
import sys
import os.path
from optparse import OptionParser
from py2js import convert_py2js
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("--include-builtins",
action="store_true",
dest="include_builtins",
default=False,
help="include py-builtins.js library in the output")
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)
s = open(filename).read() #unsafe for large files!
output.write(convert_py2js(s))
else:
parser.print_help()
if __name__ == '__main__':
main()