-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathutils.py
More file actions
70 lines (56 loc) · 2.16 KB
/
Copy pathutils.py
File metadata and controls
70 lines (56 loc) · 2.16 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
import os
from ast import NodeVisitor, parse
from io import StringIO
class YieldSearch(NodeVisitor):
def visit_Yield(self, node):
self.has_yield = True
def visit_FunctionDef(self, node):
pass # do not visit nest function definition to determine
# if a function has a yield or not...
class Writer:
def __init__(self):
self.level = 0
self.output = StringIO()
def push(self):
self.level += 1
def pull(self):
self.level -= 1
def write(self, code):
self.output.write(' ' * 4 * self.level + code + '\n')
def value(self):
return self.output.getvalue()
def pythonium_generate_js(filepath, translator_class, requirejs=False, root_path=None, output=None, deep=None):
dirname = os.path.abspath(os.path.dirname(filepath))
if not root_path:
root_path = dirname
basename = os.path.basename(filepath)
output_name = os.path.join(dirname, basename + '.js')
if not output:
print('Generating {}'.format(output_name))
# generate js
with open(os.path.join(dirname, basename)) as f:
input = parse(f.read())
tree = parse(input)
translator = translator_class()
translator.visit(tree)
script = translator.writer.value()
if requirejs:
out = 'define(function(require) {\n'
out += script
if isinstance(translator.__all__, str):
out += '\nreturn {};\n'.format(translator.__all__)
elif isinstance(translator.__all__, list):
all = ["{!r}: {}".format(x, x) for x in translator.__all__]
public = '{{{}}}'.format(', '.join(all))
out += '\nreturn {};\n'.format(public)
else:
raise Exception('__all__ is not defined!')
out += '\n})\n'
script = out
if deep:
for dependency in translator.dependencies:
if dependency.startswith('.'):
pythonium_generate_js(os.path.join(dirname, dependency + '.py'), requirejs, root_path, output, deep)
else:
pythonium_generate_js(os.path.join(root_path, dependency[1:] + '.py'), requirejs, root_path, output, deep)
output.write(script)