Skip to content

Commit 6d7af79

Browse files
author
hartsantler
committed
ignore doc strings in function definitions
1 parent f841952 commit 6d7af79

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

pythonscript/python_to_pythonjs.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
import sys
3+
from types import GeneratorType
34

45
from ast import Str
56
from ast import Call
@@ -433,7 +434,7 @@ def visit_FunctionDef(self, node):
433434
if node.args.kwarg:
434435
keywords.append(keyword(Name('varkwarg', None), Str(node.args.kwarg)))
435436

436-
prebody = list() ## NOT USED?
437+
#prebody = list() ## NOT USED?
437438

438439
# create a JS Object to store the value of each parameter
439440
signature = ', '.join(map(lambda x: '%s=%s' % (self.visit(x.arg), self.visit(x.value)), keywords))
@@ -454,7 +455,18 @@ def visit_FunctionDef(self, node):
454455
expr = expr % (node.args.kwarg, node.args.kwarg)
455456
writer.write(expr)
456457

457-
map(self.visit, node.body)
458+
#map(self.visit, node.body)
459+
for child in node.body:
460+
# simple test to drop triple quote comments
461+
if hasattr(child, 'value'):
462+
if isinstance(child.value, Str):
463+
continue
464+
if isinstance(child, GeneratorType):
465+
for sub in child:
466+
self.visit(sub)
467+
else:
468+
self.visit(child)
469+
458470
writer.pull()
459471

460472
# apply decorators

tests/server.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525

2626

27+
2728
def python_to_pythonjs( src ):
2829
p = subprocess.Popen(
2930
['python2', os.path.join( PATHS['pythonscript'], 'python_to_pythonjs.py')],
@@ -49,14 +50,15 @@ def pythonjs_to_javascript( src, closure_compiler=False ):
4950
'java', '-jar', PATHS['closure'],
5051
'--compilation_level', 'ADVANCED_OPTIMIZATIONS',
5152
'--js', x, '--js_output_file', y,
53+
'--formatting', 'PRETTY_PRINT',
5254
#'--create_name_map_files',
5355
])
5456
f = open(y, 'rb'); a = f.read().decode('utf-8'); f.close()
5557

5658
return a
5759

5860
def python_to_javascript( src, closure_compiler=False ):
59-
a = python_to_pythonjs( src )
61+
a = python_to_pythonjs( src ); print(a)
6062
return pythonjs_to_javascript( a, closure_compiler=closure_compiler )
6163

6264

@@ -154,8 +156,30 @@ def get(self, path=None):
154156
self.write('Hello World')
155157

156158

159+
LIBS = dict(
160+
three = {'three.min.js' : os.path.expanduser( '~/three.js/build/three.min.js')},
161+
tween = {'tween' : os.path.expanduser( '~/tween.js/build/tween.min.js')},
162+
)
163+
164+
class LibsHandler( tornado.web.RequestHandler ):
165+
def get(self, path=None):
166+
print('path', path)
167+
module, name = path.split('/')
168+
assert module in LIBS
169+
assert name in LIBS[ module ]
170+
if os.path.isfile( LIBS[module][name] ):
171+
data = open( LIBS[module][name], 'rb').read()
172+
else:
173+
raise tornado.web.HTTPError(404)
174+
175+
self.set_header("Content-Type", "text/javascript; charset=utf-8")
176+
self.set_header("Content-Length", len(data))
177+
self.write( data )
178+
179+
157180
Handlers = [
158-
(r'/(.*)', MainHandler)
181+
(r'/libs/(.*)', LibsHandler),
182+
(r'/(.*)', MainHandler), ## order is important, this comes last.
159183
]
160184

161185

tests/test_threejs.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html>
2+
<head>
3+
<script src="libs/three/three.min.js"></script>
4+
<script src="bindings/three.py"></script>
5+
<script src="pythonscript.js"></script>
6+
7+
<script type="text/python" closure="true">
8+
def inline_me():
9+
return 1
10+
11+
def test():
12+
i = 0
13+
while i < 10:
14+
i += inline_me()
15+
16+
print 'hello world', i
17+
18+
</script>
19+
</head>
20+
21+
<body>
22+
<button onclick="test()">click me</button>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)