-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrapyd
83 lines (61 loc) · 2.8 KB
/
rapyd
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
76
77
78
#!/usr/bin/env python3
"""
rapyd is meant to mimic python
From the command line, "rapyd hello.py" and "python hello.py" should do the same thing:
print hello
and create an importable pyj file
rapyscript -x
or
rapydscript --execute DOES THIS!!
https://www.npmjs.com/package/rapydscript#available-libraries
-o, --output Output file (default STDOUT).
-x, --execute Execute the file in-place (no compiled output generated)
-b, --bare Omit scope-protection wrapper around generated code
-p, --prettify Beautify output/specify output options.
-V, --version Print version number and exit.
-t, --test Run unit tests, making sure the compiler produces usable code, you can specify a file or test everything
--bench Run performance tests, you can specify a file or test everything (note that these tests take a while to run)
-6, --es6 Build code for ES6, cleaner output with support for more features (EXPERIMENTAL)
-m, --omit-baselib Omit base library from generated code, make sure you're including baselib.js if you use this
-i, --auto-bind Automatically bind methods to the class they belong to (more Pythonic, but could interfere with other JS libs)
-h, --help Print usage and more information on each of these options
--self Compile the compiler itself
--stats Show compilation metrics in STDERR (time to parse, generate code, etc.)
--dd Drop specified decorators (takes a comma-separated list of decorator names)
--di Drop specified imports (takes a comma-separated list of import names)
-l, --lint Check file for errors and compilation problems
"""
import subprocess
def do(command):
print('::', command)
p=subprocess.call(command, shell=True)
if p:sys.exit('rapyd aborted')
def makeHTML(pyjName):
HTMLname = pyjName.replace('.pyj', '.html')
jsName = pyjName.replace('.pyj', '.js' )
print(':: creating', HTMLname)
f = open(HTMLname, 'w')
f.write("""
<html>
<head> <meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" language="javascript" src="pyFileJS">
</script>
</body>
</html>""".replace('pyFileJS', jsName))
return HTMLname
import sys
pyjName = sys.argv[1]
if not pyjName.endswith('.pyj'):
sys.exit("""ERROR: I need a .pyj file""")
#run the py file
do ('rapydscript -x ' + pyjName )
#create .js file (for inclusion in HTML)
jsName= pyjName.replace('.pyj', '.js')
do('rapydscript ' + pyjName + ' -o ' + jsName)
#also create a virtual .py from the .pyj so python will have something to import
pyName = pyjName.replace('.pyj','.py')
do(f'ln -s -f {pyjName} {pyName}')
#create the html file (use the py.js file); open in browser
do('open ' + makeHTML(pyjName) )