author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Fri, 15 Sep 2017 19:53:49 +0300 | |
changeset 1810 | 70768bd1dab3 |
parent 1785 | 0ff2a45dcefa |
child 1826 | 91796f408540 |
permissions | -rw-r--r-- |
371 | 1 |
#!/usr/bin/env python |
2 |
# Copyright 2006 James Tauber and contributors |
|
3 |
# |
|
4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
# you may not use this file except in compliance with the License. |
|
6 |
# You may obtain a copy of the License at |
|
7 |
# |
|
8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
# |
|
10 |
# Unless required by applicable law or agreed to in writing, software |
|
11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
# See the License for the specific language governing permissions and |
|
14 |
# limitations under the License. |
|
15 |
||
16 |
||
17 |
import sys |
|
18 |
from types import StringType |
|
19 |
import compiler |
|
20 |
from compiler import ast |
|
21 |
import os |
|
22 |
import copy |
|
1783
3311eea28d56
clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
23 |
import cStringIO |
371 | 24 |
|
25 |
# the standard location for builtins (e.g. pyjslib) can be |
|
26 |
# over-ridden by changing this. it defaults to sys.prefix |
|
27 |
# so that on a system-wide install of pyjamas the builtins |
|
28 |
# can be found in e.g. {sys.prefix}/share/pyjamas |
|
29 |
# |
|
30 |
# over-rides can be done by either explicitly modifying |
|
31 |
# pyjs.prefix or by setting an environment variable, PYJSPREFIX. |
|
32 |
||
33 |
prefix = sys.prefix |
|
34 |
||
1763
bcc07ff2362c
clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1757
diff
changeset
|
35 |
if 'PYJSPREFIX' in os.environ: |
371 | 36 |
prefix = os.environ['PYJSPREFIX'] |
37 |
||
38 |
# pyjs.path is the list of paths, just like sys.path, from which |
|
39 |
# library modules will be searched for, for compile purposes. |
|
40 |
# obviously we don't want to use sys.path because that would result |
|
41 |
# in compiling standard python modules into javascript! |
|
42 |
||
43 |
path = [os.path.abspath('')] |
|
44 |
||
1763
bcc07ff2362c
clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1757
diff
changeset
|
45 |
if 'PYJSPATH' in os.environ: |
371 | 46 |
for p in os.environ['PYJSPATH'].split(os.pathsep): |
47 |
p = os.path.abspath(p) |
|
48 |
if os.path.isdir(p): |
|
49 |
path.append(p) |
|
50 |
||
51 |
# this is the python function used to wrap native javascript |
|
52 |
NATIVE_JS_FUNC_NAME = "JS" |
|
53 |
||
54 |
UU = "" |
|
55 |
||
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
56 |
PYJSLIB_BUILTIN_FUNCTIONS = ("cmp", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
57 |
"map", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
58 |
"filter", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
59 |
"dir", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
60 |
"getattr", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
61 |
"setattr", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
62 |
"hasattr", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
63 |
"int", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
64 |
"float", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
65 |
"str", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
66 |
"repr", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
67 |
"range", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
68 |
"len", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
69 |
"hash", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
70 |
"abs", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
71 |
"ord", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
72 |
"chr", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
73 |
"enumerate", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
74 |
"min", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
75 |
"max", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
76 |
"bool", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
77 |
"type", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
78 |
"isinstance") |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
79 |
|
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
80 |
PYJSLIB_BUILTIN_CLASSES = ("BaseException", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
81 |
"Exception", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
82 |
"StandardError", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
83 |
"StopIteration", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
84 |
"AttributeError", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
85 |
"TypeError", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
86 |
"KeyError", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
87 |
"LookupError", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
88 |
"list", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
89 |
"dict", |
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
90 |
"object", |
1773
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1771
diff
changeset
|
91 |
"tuple") |
371 | 92 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
93 |
|
371 | 94 |
def pyjs_builtin_remap(name): |
95 |
# XXX HACK! |
|
96 |
if name == 'list': |
|
97 |
name = 'List' |
|
98 |
if name == 'object': |
|
99 |
name = '__Object' |
|
100 |
if name == 'dict': |
|
101 |
name = 'Dict' |
|
102 |
if name == 'tuple': |
|
103 |
name = 'Tuple' |
|
104 |
return name |
|
105 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1746
diff
changeset
|
106 |
|
371 | 107 |
# XXX: this is a hack: these should be dealt with another way |
108 |
# however, console is currently the only global name which is causing |
|
109 |
# problems. |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
110 |
PYJS_GLOBAL_VARS = ("console") |
371 | 111 |
|
112 |
# This is taken from the django project. |
|
113 |
# Escape every ASCII character with a value less than 32. |
|
114 |
JS_ESCAPES = ( |
|
115 |
('\\', r'\x5C'), |
|
116 |
('\'', r'\x27'), |
|
117 |
('"', r'\x22'), |
|
118 |
('>', r'\x3E'), |
|
119 |
('<', r'\x3C'), |
|
120 |
('&', r'\x26'), |
|
121 |
(';', r'\x3B') |
|
122 |
) + tuple([('%c' % z, '\\x%02X' % z) for z in range(32)]) |
|
123 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
124 |
|
371 | 125 |
def escapejs(value): |
126 |
"""Hex encodes characters for use in JavaScript strings.""" |
|
127 |
for bad, good in JS_ESCAPES: |
|
128 |
value = value.replace(bad, good) |
|
129 |
return value |
|
130 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
131 |
|
371 | 132 |
def uuprefix(name, leave_alone=0): |
133 |
name = name.split(".") |
|
134 |
name = name[:leave_alone] + map(lambda x: "__%s" % x, name[leave_alone:]) |
|
135 |
return '.'.join(name) |
|
136 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
137 |
|
371 | 138 |
class Klass: |
139 |
||
140 |
klasses = {} |
|
141 |
||
142 |
def __init__(self, name, name_): |
|
143 |
self.name = name |
|
144 |
self.name_ = name_ |
|
145 |
self.klasses[name] = self |
|
146 |
self.functions = set() |
|
147 |
||
148 |
def set_base(self, base_name): |
|
149 |
self.base = self.klasses.get(base_name) |
|
150 |
||
151 |
def add_function(self, function_name): |
|
152 |
self.functions.add(function_name) |
|
153 |
||
154 |
||
155 |
class TranslationError(Exception): |
|
156 |
def __init__(self, message, node): |
|
157 |
self.message = "line %s:\n%s\n%s" % (node.lineno, message, node) |
|
158 |
||
159 |
def __str__(self): |
|
160 |
return self.message |
|
161 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
162 |
|
371 | 163 |
def strip_py(name): |
164 |
return name |
|
165 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
166 |
|
371 | 167 |
def mod_var_name_decl(raw_module_name): |
168 |
""" function to get the last component of the module e.g. |
|
169 |
pyjamas.ui.DOM into the "namespace". i.e. doing |
|
170 |
"import pyjamas.ui.DOM" actually ends up with _two_ |
|
171 |
variables - one pyjamas.ui.DOM, the other just "DOM". |
|
172 |
but "DOM" is actually local, hence the "var" prefix. |
|
173 |
||
174 |
for PyV8, this might end up causing problems - we'll have |
|
175 |
to see: gen_mod_import and mod_var_name_decl might have |
|
176 |
to end up in a library-specific module, somewhere. |
|
177 |
""" |
|
178 |
name = raw_module_name.split(".") |
|
179 |
if len(name) == 1: |
|
180 |
return '' |
|
181 |
child_name = name[-1] |
|
182 |
return "var %s = %s;\n" % (child_name, raw_module_name) |
|
183 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
184 |
|
371 | 185 |
def gen_mod_import(parentName, importName, dynamic=1): |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
186 |
# pyjs_ajax_eval("%(n)s.cache.js", null, true); |
371 | 187 |
return """ |
188 |
pyjslib.import_module(sys.loadpath, '%(p)s', '%(n)s', %(d)d, false); |
|
189 |
""" % ({'p': parentName, 'd': dynamic, 'n': importName}) + \ |
|
1776
81aa8aaccdd4
clean-up: fix PEP8 E122 continuation line missing indentation or outdented
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset
|
190 |
mod_var_name_decl(importName) |
371 | 191 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
192 |
|
371 | 193 |
class Translator: |
194 |
||
195 |
def __init__(self, mn, module_name, raw_module_name, src, debug, mod, output, |
|
196 |
dynamic=0, optimize=False, |
|
197 |
findFile=None): |
|
198 |
||
199 |
if module_name: |
|
200 |
self.module_prefix = module_name + "." |
|
201 |
else: |
|
202 |
self.module_prefix = "" |
|
203 |
self.raw_module_name = raw_module_name |
|
204 |
src = src.replace("\r\n", "\n") |
|
205 |
src = src.replace("\n\r", "\n") |
|
206 |
src = src.replace("\r", "\n") |
|
207 |
self.src = src.split("\n") |
|
208 |
self.debug = debug |
|
209 |
self.imported_modules = [] |
|
210 |
self.imported_modules_as = [] |
|
211 |
self.imported_js = set() |
|
212 |
self.top_level_functions = set() |
|
213 |
self.top_level_classes = set() |
|
214 |
self.top_level_vars = set() |
|
215 |
self.local_arg_stack = [[]] |
|
216 |
self.output = output |
|
217 |
self.imported_classes = {} |
|
218 |
self.method_imported_globals = set() |
|
219 |
self.method_self = None |
|
220 |
self.nextTupleAssignID = 1 |
|
221 |
self.dynamic = dynamic |
|
222 |
self.optimize = optimize |
|
223 |
self.findFile = findFile |
|
224 |
||
225 |
if module_name.find(".") >= 0: |
|
226 |
vdec = '' |
|
227 |
else: |
|
228 |
vdec = 'var ' |
|
229 |
print >>self.output, UU+"%s%s = function (__mod_name__) {" % (vdec, module_name) |
|
230 |
||
231 |
print >>self.output, " if("+module_name+".__was_initialized__) return;" |
|
232 |
print >>self.output, " "+UU+module_name+".__was_initialized__ = true;" |
|
233 |
print >>self.output, UU+"if (__mod_name__ == null) __mod_name__ = '%s';" % (mn) |
|
234 |
print >>self.output, UU+"%s.__name__ = __mod_name__;" % (raw_module_name) |
|
235 |
||
236 |
decl = mod_var_name_decl(raw_module_name) |
|
237 |
if decl: |
|
238 |
print >>self.output, decl |
|
239 |
||
240 |
if self.debug: |
|
241 |
haltException = self.module_prefix + "HaltException" |
|
242 |
print >>self.output, haltException + ' = function () {' |
|
243 |
print >>self.output, ' this.message = "Program Halted";' |
|
244 |
print >>self.output, ' this.name = "' + haltException + '";' |
|
245 |
print >>self.output, '}' |
|
246 |
print >>self.output, '' |
|
247 |
print >>self.output, haltException + ".prototype.__str__ = function()" |
|
248 |
print >>self.output, '{' |
|
249 |
print >>self.output, 'return this.message ;' |
|
250 |
print >>self.output, '}' |
|
251 |
||
252 |
print >>self.output, haltException + ".prototype.toString = function()" |
|
253 |
print >>self.output, '{' |
|
254 |
print >>self.output, 'return this.name + ": \\"" + this.message + "\\"";' |
|
255 |
print >>self.output, '}' |
|
256 |
||
257 |
isHaltFunction = self.module_prefix + "IsHaltException" |
|
258 |
print >>self.output, """ |
|
259 |
%s = function (s) { |
|
260 |
var suffix="HaltException"; |
|
261 |
if (s.length < suffix.length) { |
|
262 |
//alert(s + " " + suffix); |
|
263 |
return false; |
|
264 |
} else { |
|
265 |
var ss = s.substring(s.length, (s.length - suffix.length)); |
|
266 |
//alert(s + " " + suffix + " " + ss); |
|
267 |
return ss == suffix; |
|
268 |
} |
|
269 |
} |
|
270 |
""" % isHaltFunction |
|
271 |
for child in mod.node: |
|
272 |
if isinstance(child, ast.Function): |
|
273 |
self.top_level_functions.add(child.name) |
|
274 |
elif isinstance(child, ast.Class): |
|
275 |
self.top_level_classes.add(child.name) |
|
276 |
||
277 |
for child in mod.node: |
|
278 |
if isinstance(child, ast.Function): |
|
279 |
self._function(child, False) |
|
280 |
elif isinstance(child, ast.Class): |
|
281 |
self._class(child) |
|
282 |
elif isinstance(child, ast.Import): |
|
283 |
importName = child.names[0][0] |
|
1737
a39c2918c015
clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
284 |
if importName == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter |
371 | 285 |
pass |
286 |
elif importName.endswith('.js'): |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
287 |
self.imported_js.add(importName) |
371 | 288 |
else: |
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
289 |
self.add_imported_module(strip_py(importName)) |
371 | 290 |
elif isinstance(child, ast.From): |
1737
a39c2918c015
clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
291 |
if child.modname == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter |
371 | 292 |
pass |
293 |
else: |
|
294 |
self.add_imported_module(child.modname) |
|
295 |
self._from(child) |
|
296 |
elif isinstance(child, ast.Discard): |
|
297 |
self._discard(child, None) |
|
298 |
elif isinstance(child, ast.Assign): |
|
299 |
self._assign(child, None, True) |
|
300 |
elif isinstance(child, ast.AugAssign): |
|
301 |
self._augassign(child, None) |
|
302 |
elif isinstance(child, ast.If): |
|
303 |
self._if(child, None) |
|
304 |
elif isinstance(child, ast.For): |
|
305 |
self._for(child, None) |
|
306 |
elif isinstance(child, ast.While): |
|
307 |
self._while(child, None) |
|
308 |
elif isinstance(child, ast.Subscript): |
|
309 |
self._subscript_stmt(child, None) |
|
310 |
elif isinstance(child, ast.Global): |
|
311 |
self._global(child, None) |
|
312 |
elif isinstance(child, ast.Printnl): |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
313 |
self._print(child, None) |
371 | 314 |
elif isinstance(child, ast.Print): |
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
315 |
self._print(child, None) |
371 | 316 |
elif isinstance(child, ast.TryExcept): |
317 |
self._tryExcept(child, None) |
|
318 |
elif isinstance(child, ast.Raise): |
|
319 |
self._raise(child, None) |
|
320 |
elif isinstance(child, ast.Stmt): |
|
321 |
self._stmt(child, None) |
|
322 |
else: |
|
323 |
raise TranslationError("unsupported type (in __init__)", child) |
|
324 |
||
325 |
# Initialize all classes for this module |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
326 |
# print >> self.output, "__"+self.modpfx()+\ |
371 | 327 |
# "classes_initialize = function() {\n" |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
328 |
# for className in self.top_level_classes: |
371 | 329 |
# print >> self.output, "\t"+UU+self.modpfx()+"__"+className+"_initialize();" |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
330 |
# print >> self.output, "};\n" |
371 | 331 |
|
332 |
print >> self.output, "return this;\n" |
|
1754
63f4af6bf6d9
clean-up: fix most PEP8 E221 multiple spaces before operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1752
diff
changeset
|
333 |
print >> self.output, "}; /* end %s */ \n" % module_name |
371 | 334 |
|
335 |
def module_imports(self): |
|
336 |
return self.imported_modules + self.imported_modules_as |
|
337 |
||
338 |
def add_local_arg(self, varname): |
|
339 |
local_vars = self.local_arg_stack[-1] |
|
340 |
if varname not in local_vars: |
|
341 |
local_vars.append(varname) |
|
342 |
||
343 |
def add_imported_module(self, importName): |
|
344 |
||
345 |
if importName in self.imported_modules: |
|
346 |
return |
|
347 |
self.imported_modules.append(importName) |
|
348 |
name = importName.split(".") |
|
349 |
if len(name) != 1: |
|
350 |
# add the name of the module to the namespace, |
|
351 |
# but don't add the short name to imported_modules |
|
352 |
# because then the short name would be attempted to be |
|
353 |
# added to the dependencies, and it's half way up the |
|
354 |
# module import directory structure! |
|
355 |
child_name = name[-1] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
356 |
self.imported_modules_as.append(child_name) |
371 | 357 |
print >> self.output, gen_mod_import(self.raw_module_name, |
358 |
strip_py(importName), |
|
359 |
self.dynamic) |
|
360 |
||
361 |
def _default_args_handler(self, node, arg_names, current_klass, |
|
362 |
output=None): |
|
363 |
if len(node.defaults): |
|
364 |
output = output or self.output |
|
365 |
default_pos = len(arg_names) - len(node.defaults) |
|
366 |
if arg_names and arg_names[0] == self.method_self: |
|
367 |
default_pos -= 1 |
|
368 |
for default_node in node.defaults: |
|
369 |
if isinstance(default_node, ast.Const): |
|
370 |
default_value = self._const(default_node) |
|
371 |
elif isinstance(default_node, ast.Name): |
|
372 |
default_value = self._name(default_node, current_klass) |
|
373 |
elif isinstance(default_node, ast.UnarySub): |
|
374 |
default_value = self._unarysub(default_node, current_klass) |
|
375 |
else: |
|
376 |
raise TranslationError("unsupported type (in _method)", default_node) |
|
377 |
||
378 |
default_name = arg_names[default_pos] |
|
379 |
default_pos += 1 |
|
380 |
print >> output, " if (typeof %s == 'undefined') %s=%s;" % (default_name, default_name, default_value) |
|
381 |
||
382 |
def _varargs_handler(self, node, varargname, arg_names, current_klass): |
|
383 |
print >>self.output, " var", varargname, '= new pyjslib.Tuple();' |
|
384 |
print >>self.output, " for(var __va_arg="+str(len(arg_names))+"; __va_arg < arguments.length; __va_arg++) {" |
|
385 |
print >>self.output, " var __arg = arguments[__va_arg];" |
|
386 |
print >>self.output, " "+varargname+".append(__arg);" |
|
387 |
print >>self.output, " }" |
|
388 |
||
389 |
def _kwargs_parser(self, node, function_name, arg_names, current_klass): |
|
390 |
if len(node.defaults) or node.kwargs: |
|
391 |
default_pos = len(arg_names) - len(node.defaults) |
|
392 |
if arg_names and arg_names[0] == self.method_self: |
|
393 |
default_pos -= 1 |
|
394 |
print >>self.output, function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {" |
|
395 |
for default_node in node.defaults: |
|
396 |
default_value = self.expr(default_node, current_klass) |
|
397 |
# if isinstance(default_node, ast.Const): |
|
398 |
# default_value = self._const(default_node) |
|
399 |
# elif isinstance(default_node, ast.Name): |
|
400 |
# default_value = self._name(default_node) |
|
401 |
# elif isinstance(default_node, ast.UnarySub): |
|
402 |
# default_value = self._unarysub(default_node, current_klass) |
|
403 |
# else: |
|
404 |
# raise TranslationError("unsupported type (in _method)", default_node) |
|
405 |
||
406 |
default_name = arg_names[default_pos] |
|
1734
750eeb7230a1
clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
407 |
print >>self.output, " if (typeof %s == 'undefined')" % (default_name) |
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
408 |
print >>self.output, " %s=__kwargs.%s;" % (default_name, default_name) |
371 | 409 |
default_pos += 1 |
410 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
411 |
# self._default_args_handler(node, arg_names, current_klass) |
1756
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
412 |
if node.kwargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
413 |
arg_names += ["pyjslib.Dict(__kwargs)"] |
371 | 414 |
print >>self.output, " var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";" |
415 |
if node.varargs: |
|
416 |
self._varargs_handler(node, "__args", arg_names, current_klass) |
|
417 |
print >>self.output, " __r.push.apply(__r, __args.getArray())" |
|
418 |
print >>self.output, " return __r;" |
|
419 |
print >>self.output, "};" |
|
420 |
||
421 |
def _function(self, node, local=False): |
|
422 |
if local: |
|
423 |
function_name = node.name |
|
424 |
self.add_local_arg(function_name) |
|
425 |
else: |
|
426 |
function_name = UU + self.modpfx() + node.name |
|
427 |
||
428 |
arg_names = list(node.argnames) |
|
429 |
normal_arg_names = list(arg_names) |
|
1756
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
430 |
if node.kwargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
431 |
kwargname = normal_arg_names.pop() |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
432 |
if node.varargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
433 |
varargname = normal_arg_names.pop() |
371 | 434 |
declared_arg_names = list(normal_arg_names) |
1756
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
435 |
if node.kwargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
436 |
declared_arg_names.append(kwargname) |
371 | 437 |
|
438 |
function_args = "(" + ", ".join(declared_arg_names) + ")" |
|
439 |
print >>self.output, "%s = function%s {" % (function_name, function_args) |
|
440 |
self._default_args_handler(node, normal_arg_names, None) |
|
441 |
||
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
442 |
local_arg_names = normal_arg_names + declared_arg_names |
371 | 443 |
|
444 |
if node.varargs: |
|
445 |
self._varargs_handler(node, varargname, declared_arg_names, None) |
|
446 |
local_arg_names.append(varargname) |
|
447 |
||
448 |
# stack of local variable names for this function call |
|
449 |
self.local_arg_stack.append(local_arg_names) |
|
450 |
||
451 |
for child in node.code: |
|
452 |
self._stmt(child, None) |
|
453 |
||
454 |
# remove the top local arg names |
|
455 |
self.local_arg_stack.pop() |
|
456 |
||
457 |
# we need to return null always, so it is not undefined |
|
458 |
lastStmt = [p for p in node.code][-1] |
|
459 |
if not isinstance(lastStmt, ast.Return): |
|
460 |
if not self._isNativeFunc(lastStmt): |
|
461 |
print >>self.output, " return null;" |
|
462 |
||
463 |
print >>self.output, "};" |
|
464 |
print >>self.output, "%s.__name__ = '%s';\n" % (function_name, node.name) |
|
465 |
||
466 |
self._kwargs_parser(node, function_name, normal_arg_names, None) |
|
467 |
||
468 |
def _return(self, node, current_klass): |
|
469 |
expr = self.expr(node.value, current_klass) |
|
470 |
# in python a function call always returns None, so we do it |
|
471 |
# here too |
|
472 |
print >>self.output, " return " + expr + ";" |
|
473 |
||
474 |
def _break(self, node, current_klass): |
|
475 |
print >>self.output, " break;" |
|
476 |
||
477 |
def _continue(self, node, current_klass): |
|
478 |
print >>self.output, " continue;" |
|
479 |
||
480 |
def _callfunc(self, v, current_klass): |
|
481 |
||
482 |
if isinstance(v.node, ast.Name): |
|
483 |
if v.node.name in self.top_level_functions: |
|
484 |
call_name = self.modpfx() + v.node.name |
|
485 |
elif v.node.name in self.top_level_classes: |
|
486 |
call_name = self.modpfx() + v.node.name |
|
1763
bcc07ff2362c
clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1757
diff
changeset
|
487 |
elif v.node.name in self.imported_classes: |
371 | 488 |
call_name = self.imported_classes[v.node.name] + '.' + v.node.name |
489 |
elif v.node.name in PYJSLIB_BUILTIN_FUNCTIONS: |
|
490 |
call_name = 'pyjslib.' + v.node.name |
|
491 |
elif v.node.name in PYJSLIB_BUILTIN_CLASSES: |
|
492 |
name = pyjs_builtin_remap(v.node.name) |
|
493 |
call_name = 'pyjslib.' + name |
|
494 |
elif v.node.name == "callable": |
|
495 |
call_name = "pyjslib.isFunction" |
|
496 |
else: |
|
497 |
call_name = v.node.name |
|
498 |
call_args = [] |
|
499 |
elif isinstance(v.node, ast.Getattr): |
|
500 |
attr_name = v.node.attrname |
|
501 |
||
502 |
if isinstance(v.node.expr, ast.Name): |
|
503 |
call_name = self._name2(v.node.expr, current_klass, attr_name) |
|
504 |
call_args = [] |
|
505 |
elif isinstance(v.node.expr, ast.Getattr): |
|
506 |
call_name = self._getattr2(v.node.expr, current_klass, attr_name) |
|
507 |
call_args = [] |
|
508 |
elif isinstance(v.node.expr, ast.CallFunc): |
|
509 |
call_name = self._callfunc(v.node.expr, current_klass) + "." + v.node.attrname |
|
510 |
call_args = [] |
|
511 |
elif isinstance(v.node.expr, ast.Subscript): |
|
512 |
call_name = self._subscript(v.node.expr, current_klass) + "." + v.node.attrname |
|
513 |
call_args = [] |
|
514 |
elif isinstance(v.node.expr, ast.Const): |
|
515 |
call_name = self.expr(v.node.expr, current_klass) + "." + v.node.attrname |
|
516 |
call_args = [] |
|
517 |
else: |
|
518 |
raise TranslationError("unsupported type (in _callfunc)", v.node.expr) |
|
519 |
else: |
|
520 |
raise TranslationError("unsupported type (in _callfunc)", v.node) |
|
521 |
||
522 |
call_name = strip_py(call_name) |
|
523 |
||
524 |
kwargs = [] |
|
525 |
star_arg_name = None |
|
526 |
if v.star_args: |
|
527 |
star_arg_name = self.expr(v.star_args, current_klass) |
|
528 |
||
529 |
for ch4 in v.args: |
|
530 |
if isinstance(ch4, ast.Keyword): |
|
531 |
kwarg = ch4.name + ":" + self.expr(ch4.expr, current_klass) |
|
532 |
kwargs.append(kwarg) |
|
533 |
else: |
|
534 |
arg = self.expr(ch4, current_klass) |
|
535 |
call_args.append(arg) |
|
536 |
||
537 |
if kwargs: |
|
538 |
fn_args = ", ".join(['{' + ', '.join(kwargs) + '}']+call_args) |
|
539 |
else: |
|
540 |
fn_args = ", ".join(call_args) |
|
541 |
||
542 |
if kwargs or star_arg_name: |
|
543 |
if not star_arg_name: |
|
544 |
star_arg_name = 'null' |
|
1756
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
545 |
try: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
546 |
call_this, method_name = call_name.rsplit(".", 1) |
371 | 547 |
except ValueError: |
548 |
# Must be a function call ... |
|
1785
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
549 |
return ("pyjs_kwargs_function_call("+call_name+", " + |
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
550 |
star_arg_name + ", ["+fn_args+"]" + ")") |
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
551 |
else: |
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
552 |
return ("pyjs_kwargs_method_call("+call_this+", '"+method_name+"', " + |
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
553 |
star_arg_name + ", ["+fn_args+"]" + ")") |
371 | 554 |
else: |
555 |
return call_name + "(" + ", ".join(call_args) + ")" |
|
556 |
||
557 |
def _print(self, node, current_klass): |
|
558 |
if self.optimize: |
|
559 |
return |
|
560 |
call_args = [] |
|
561 |
for ch4 in node.nodes: |
|
562 |
arg = self.expr(ch4, current_klass) |
|
563 |
call_args.append(arg) |
|
564 |
||
565 |
print >>self.output, "pyjslib.printFunc([", ', '.join(call_args), "],", int(isinstance(node, ast.Printnl)), ");" |
|
566 |
||
567 |
def _tryExcept(self, node, current_klass): |
|
568 |
if len(node.handlers) != 1: |
|
569 |
raise TranslationError("except statements in this form are" + |
|
570 |
" not supported", node) |
|
571 |
||
572 |
expr = node.handlers[0][0] |
|
573 |
as_ = node.handlers[0][1] |
|
574 |
if as_: |
|
575 |
errName = as_.name |
|
576 |
else: |
|
577 |
errName = 'err' |
|
578 |
||
579 |
# XXX TODO: check that this should instead be added as a _separate_ |
|
580 |
# local scope, temporary to the function. oh dearie me. |
|
581 |
self.add_local_arg(errName) |
|
582 |
||
583 |
print >>self.output, " try {" |
|
584 |
for stmt in node.body.nodes: |
|
585 |
self._stmt(stmt, current_klass) |
|
586 |
print >> self.output, " } catch(%s) {" % errName |
|
587 |
if expr: |
|
1755
624b9694cb0d
clean-up: fix PEP8 E741 ambiguous variable name
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1754
diff
changeset
|
588 |
k = [] |
371 | 589 |
if isinstance(expr, ast.Tuple): |
590 |
for x in expr.nodes: |
|
1771
f68a105000be
clean-up: fix PEP8 E211 whitespace before '[' or '('
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
591 |
k.append("(%(err)s.__name__ == %(expr)s.__name__)" % dict(err=errName, expr=self.expr(x, current_klass))) |
f68a105000be
clean-up: fix PEP8 E211 whitespace before '[' or '('
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
592 |
else: |
f68a105000be
clean-up: fix PEP8 E211 whitespace before '[' or '('
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
593 |
k = [" (%(err)s.__name__ == %(expr)s.__name__) " % dict(err=errName, expr=self.expr(expr, current_klass))] |
1755
624b9694cb0d
clean-up: fix PEP8 E741 ambiguous variable name
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1754
diff
changeset
|
594 |
print >> self.output, " if(%s) {" % '||\n\t\t'.join(k) |
371 | 595 |
for stmt in node.handlers[0][2]: |
596 |
self._stmt(stmt, current_klass) |
|
597 |
if expr: |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
598 |
# print >> self.output, "} else { throw(%s); } " % errName |
371 | 599 |
print >> self.output, "}" |
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
600 |
if node.else_ is not None: |
371 | 601 |
print >>self.output, " } finally {" |
602 |
for stmt in node.else_: |
|
603 |
self._stmt(stmt, current_klass) |
|
604 |
print >>self.output, " }" |
|
605 |
||
606 |
# XXX: change use_getattr to True to enable "strict" compilation |
|
607 |
# but incurring a 100% performance penalty. oops. |
|
608 |
def _getattr(self, v, current_klass, use_getattr=False): |
|
609 |
attr_name = v.attrname |
|
610 |
if isinstance(v.expr, ast.Name): |
|
611 |
obj = self._name(v.expr, current_klass, return_none_for_module=True) |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
612 |
if obj is None and v.expr.name in self.module_imports(): |
371 | 613 |
# XXX TODO: distinguish between module import classes |
614 |
# and variables. right now, this is a hack to get |
|
615 |
# the sys module working. |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
616 |
# if v.expr.name == 'sys': |
371 | 617 |
return v.expr.name+'.'+attr_name |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
618 |
# return v.expr.name+'.__'+attr_name+'.prototype.__class__' |
371 | 619 |
if not use_getattr or attr_name == '__class__' or \ |
620 |
attr_name == '__name__': |
|
621 |
return obj + "." + attr_name |
|
622 |
return "pyjslib.getattr(%s, '%s')" % (obj, attr_name) |
|
623 |
elif isinstance(v.expr, ast.Getattr): |
|
624 |
return self._getattr(v.expr, current_klass) + "." + attr_name |
|
625 |
elif isinstance(v.expr, ast.Subscript): |
|
626 |
return self._subscript(v.expr, self.modpfx()) + "." + attr_name |
|
627 |
elif isinstance(v.expr, ast.CallFunc): |
|
628 |
return self._callfunc(v.expr, self.modpfx()) + "." + attr_name |
|
629 |
else: |
|
630 |
raise TranslationError("unsupported type (in _getattr)", v.expr) |
|
631 |
||
632 |
def modpfx(self): |
|
633 |
return strip_py(self.module_prefix) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
634 |
|
371 | 635 |
def _name(self, v, current_klass, top_level=False, |
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
636 |
return_none_for_module=False): |
371 | 637 |
|
638 |
if v.name == 'ilikesillynamesfornicedebugcode': |
|
639 |
print top_level, current_klass, repr(v) |
|
640 |
print self.top_level_vars |
|
641 |
print self.top_level_functions |
|
642 |
print self.local_arg_stack |
|
643 |
print "error..." |
|
644 |
||
645 |
local_var_names = None |
|
646 |
las = len(self.local_arg_stack) |
|
647 |
if las > 0: |
|
648 |
local_var_names = self.local_arg_stack[-1] |
|
649 |
||
650 |
if v.name == "True": |
|
651 |
return "true" |
|
652 |
elif v.name == "False": |
|
653 |
return "false" |
|
654 |
elif v.name == "None": |
|
655 |
return "null" |
|
656 |
elif v.name == '__name__' and current_klass is None: |
|
657 |
return self.modpfx() + v.name |
|
658 |
elif v.name == self.method_self: |
|
659 |
return "this" |
|
660 |
elif v.name in self.top_level_functions: |
|
661 |
return UU+self.modpfx() + v.name |
|
662 |
elif v.name in self.method_imported_globals: |
|
663 |
return UU+self.modpfx() + v.name |
|
664 |
elif not current_klass and las == 1 and v.name in self.top_level_vars: |
|
665 |
return UU+self.modpfx() + v.name |
|
666 |
elif v.name in local_var_names: |
|
667 |
return v.name |
|
1763
bcc07ff2362c
clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1757
diff
changeset
|
668 |
elif v.name in self.imported_classes: |
371 | 669 |
return UU+self.imported_classes[v.name] + '.__' + v.name + ".prototype.__class__" |
670 |
elif v.name in self.top_level_classes: |
|
671 |
return UU+self.modpfx() + "__" + v.name + ".prototype.__class__" |
|
672 |
elif v.name in self.module_imports() and return_none_for_module: |
|
673 |
return None |
|
674 |
elif v.name in PYJSLIB_BUILTIN_CLASSES: |
|
1746
45d6f5fba016
clean-up: fix PEP8 E202 whitespace before ')'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
675 |
return "pyjslib." + pyjs_builtin_remap(v.name) |
371 | 676 |
elif current_klass: |
677 |
if v.name not in local_var_names and \ |
|
678 |
v.name not in self.top_level_vars and \ |
|
679 |
v.name not in PYJS_GLOBAL_VARS and \ |
|
680 |
v.name not in self.top_level_functions: |
|
681 |
||
682 |
cls_name = current_klass |
|
683 |
if hasattr(cls_name, "name"): |
|
684 |
cls_name_ = cls_name.name_ |
|
685 |
cls_name = cls_name.name |
|
686 |
else: |
|
1737
a39c2918c015
clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
687 |
cls_name_ = current_klass + "_" # XXX ??? |
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
688 |
name = UU+cls_name_ + ".prototype.__class__." + v.name |
371 | 689 |
if v.name == 'listener': |
690 |
name = 'listener+' + name |
|
691 |
return name |
|
692 |
||
693 |
return v.name |
|
694 |
||
695 |
def _name2(self, v, current_klass, attr_name): |
|
696 |
obj = v.name |
|
697 |
||
698 |
if obj in self.method_imported_globals: |
|
699 |
call_name = UU+self.modpfx() + obj + "." + attr_name |
|
1763
bcc07ff2362c
clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1757
diff
changeset
|
700 |
elif obj in self.imported_classes: |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
701 |
# attr_str = "" |
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
702 |
# if attr_name != "__init__": |
371 | 703 |
attr_str = ".prototype.__class__." + attr_name |
704 |
call_name = UU+self.imported_classes[obj] + '.__' + obj + attr_str |
|
705 |
elif obj in self.module_imports(): |
|
706 |
call_name = obj + "." + attr_name |
|
1737
a39c2918c015
clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
707 |
elif obj[0] == obj[0].upper(): # XXX HACK ALERT |
371 | 708 |
call_name = UU + self.modpfx() + "__" + obj + ".prototype.__class__." + attr_name |
709 |
else: |
|
710 |
call_name = UU+self._name(v, current_klass) + "." + attr_name |
|
711 |
||
712 |
return call_name |
|
713 |
||
714 |
def _getattr2(self, v, current_klass, attr_name): |
|
715 |
if isinstance(v.expr, ast.Getattr): |
|
716 |
call_name = self._getattr2(v.expr, current_klass, v.attrname + "." + attr_name) |
|
717 |
elif isinstance(v.expr, ast.Name) and v.expr.name in self.module_imports(): |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
718 |
call_name = UU+v.expr.name + '.__' + v.attrname+".prototype.__class__."+attr_name |
371 | 719 |
else: |
720 |
obj = self.expr(v.expr, current_klass) |
|
721 |
call_name = obj + "." + v.attrname + "." + attr_name |
|
722 |
||
723 |
return call_name |
|
724 |
||
725 |
def _class(self, node): |
|
726 |
""" |
|
727 |
Handle a class definition. |
|
728 |
||
729 |
In order to translate python semantics reasonably well, the following |
|
730 |
structure is used: |
|
731 |
||
732 |
A special object is created for the class, which inherits attributes |
|
733 |
from the superclass, or Object if there's no superclass. This is the |
|
734 |
class object; the object which you refer to when specifying the |
|
735 |
class by name. Static, class, and unbound methods are copied |
|
736 |
from the superclass object. |
|
737 |
||
738 |
A special constructor function is created with the same name as the |
|
739 |
class, which is used to create instances of that class. |
|
740 |
||
741 |
A javascript class (e.g. a function with a prototype attribute) is |
|
742 |
created which is the javascript class of created instances, and |
|
743 |
which inherits attributes from the class object. Bound methods are |
|
744 |
copied from the superclass into this class rather than inherited, |
|
745 |
because the class object contains unbound, class, and static methods |
|
746 |
that we don't necessarily want to inherit. |
|
747 |
||
748 |
The type of a method can now be determined by inspecting its |
|
749 |
static_method, unbound_method, class_method, or instance_method |
|
750 |
attribute; only one of these should be true. |
|
751 |
||
752 |
Much of this work is done in pyjs_extend, is pyjslib.py |
|
753 |
""" |
|
754 |
class_name = self.modpfx() + uuprefix(node.name, 1) |
|
755 |
class_name_ = self.modpfx() + uuprefix(node.name) |
|
756 |
current_klass = Klass(class_name, class_name_) |
|
757 |
init_method = None |
|
758 |
for child in node.code: |
|
759 |
if isinstance(child, ast.Function): |
|
760 |
current_klass.add_function(child.name) |
|
761 |
if child.name == "__init__": |
|
762 |
init_method = child |
|
763 |
||
764 |
if len(node.bases) == 0: |
|
765 |
base_class = "pyjslib.__Object" |
|
766 |
elif len(node.bases) == 1: |
|
767 |
if isinstance(node.bases[0], ast.Name): |
|
1763
bcc07ff2362c
clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1757
diff
changeset
|
768 |
if node.bases[0].name in self.imported_classes: |
371 | 769 |
base_class_ = self.imported_classes[node.bases[0].name] + '.__' + node.bases[0].name |
770 |
base_class = self.imported_classes[node.bases[0].name] + '.' + node.bases[0].name |
|
771 |
else: |
|
772 |
base_class_ = self.modpfx() + "__" + node.bases[0].name |
|
773 |
base_class = self.modpfx() + node.bases[0].name |
|
774 |
elif isinstance(node.bases[0], ast.Getattr): |
|
775 |
# the bases are not in scope of the class so do not |
|
776 |
# pass our class to self._name |
|
777 |
base_class_ = self._name(node.bases[0].expr, None) + \ |
|
778 |
".__" + node.bases[0].attrname |
|
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
779 |
base_class = \ |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
780 |
self._name(node.bases[0].expr, None) + \ |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
781 |
"." + node.bases[0].attrname |
371 | 782 |
else: |
783 |
raise TranslationError("unsupported type (in _class)", node.bases[0]) |
|
784 |
||
785 |
current_klass.set_base(base_class) |
|
786 |
else: |
|
787 |
raise TranslationError("more than one base (in _class)", node) |
|
788 |
||
789 |
print >>self.output, UU+class_name_ + " = function () {" |
|
790 |
# call superconstructor |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
791 |
# if base_class: |
371 | 792 |
# print >>self.output, " __" + base_class + ".call(this);" |
793 |
print >>self.output, "}" |
|
794 |
||
795 |
if not init_method: |
|
796 |
init_method = ast.Function([], "__init__", ["self"], [], 0, None, []) |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
797 |
# self._method(init_method, current_klass, class_name) |
371 | 798 |
|
799 |
# Generate a function which constructs the object |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
800 |
clsfunc = ast.Function( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
801 |
[], node.name, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
802 |
init_method.argnames[1:], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
803 |
init_method.defaults, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
804 |
init_method.flags, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
805 |
None, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
806 |
[ast.Discard(ast.CallFunc(ast.Name("JS"), [ast.Const( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
807 |
# I attempted lazy initialization, but then you can't access static class members |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
808 |
# " if(!__"+base_class+".__was_initialized__)"+ |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
809 |
# " __" + class_name + "_initialize();\n" + |
1776
81aa8aaccdd4
clean-up: fix PEP8 E122 continuation line missing indentation or outdented
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset
|
810 |
" var instance = new " + UU + class_name_ + "();\n" + |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
811 |
" if(instance.__init__) instance.__init__.apply(instance, arguments);\n" + |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
812 |
" return instance;" |
371 | 813 |
)]))]) |
814 |
||
815 |
self._function(clsfunc, False) |
|
816 |
print >>self.output, UU+class_name_ + ".__initialize__ = function () {" |
|
817 |
print >>self.output, " if("+UU+class_name_+".__was_initialized__) return;" |
|
818 |
print >>self.output, " "+UU+class_name_+".__was_initialized__ = true;" |
|
819 |
cls_obj = UU+class_name_ + '.prototype.__class__' |
|
820 |
||
821 |
if class_name == "pyjslib.__Object": |
|
822 |
print >>self.output, " "+cls_obj+" = {};" |
|
823 |
else: |
|
824 |
if base_class and base_class not in ("object", "pyjslib.__Object"): |
|
825 |
print >>self.output, " if(!"+UU+base_class_+".__was_initialized__)" |
|
826 |
print >>self.output, " "+UU+base_class_+".__initialize__();" |
|
827 |
print >>self.output, " pyjs_extend(" + UU+class_name_ + ", "+UU+base_class_+");" |
|
828 |
else: |
|
829 |
print >>self.output, " pyjs_extend(" + UU+class_name_ + ", "+UU+"pyjslib.__Object);" |
|
830 |
||
831 |
print >>self.output, " "+cls_obj+".__new__ = "+UU+class_name+";" |
|
832 |
print >>self.output, " "+cls_obj+".__name__ = '"+UU+node.name+"';" |
|
833 |
||
834 |
for child in node.code: |
|
835 |
if isinstance(child, ast.Pass): |
|
836 |
pass |
|
837 |
elif isinstance(child, ast.Function): |
|
838 |
self._method(child, current_klass, class_name, class_name_) |
|
839 |
elif isinstance(child, ast.Assign): |
|
840 |
self.classattr(child, current_klass) |
|
841 |
elif isinstance(child, ast.Discard) and isinstance(child.expr, ast.Const): |
|
842 |
# Probably a docstring, turf it |
|
843 |
pass |
|
844 |
else: |
|
845 |
raise TranslationError("unsupported type (in _class)", child) |
|
846 |
print >>self.output, "}" |
|
847 |
||
848 |
print >> self.output, class_name_+".__initialize__();" |
|
849 |
||
850 |
def classattr(self, node, current_klass): |
|
851 |
self._assign(node, current_klass, True) |
|
852 |
||
853 |
def _raise(self, node, current_klass): |
|
854 |
if node.expr2: |
|
855 |
raise TranslationError("More than one expression unsupported", |
|
856 |
node) |
|
857 |
print >> self.output, "throw (%s);" % self.expr( |
|
858 |
node.expr1, current_klass) |
|
859 |
||
860 |
def _method(self, node, current_klass, class_name, class_name_): |
|
861 |
# reset global var scope |
|
862 |
self.method_imported_globals = set() |
|
863 |
||
864 |
arg_names = list(node.argnames) |
|
865 |
||
866 |
classmethod = False |
|
867 |
staticmethod = False |
|
868 |
if node.decorators: |
|
869 |
for d in node.decorators: |
|
870 |
if d.name == "classmethod": |
|
871 |
classmethod = True |
|
872 |
elif d.name == "staticmethod": |
|
873 |
staticmethod = True |
|
874 |
||
875 |
if staticmethod: |
|
876 |
staticfunc = ast.Function([], class_name_+"."+node.name, node.argnames, node.defaults, node.flags, node.doc, node.code, node.lineno) |
|
877 |
self._function(staticfunc, True) |
|
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
878 |
print >>self.output, " " + UU+class_name_ + ".prototype.__class__." + node.name + " = " + class_name_+"."+node.name+";" |
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
879 |
print >>self.output, " " + UU+class_name_ + ".prototype.__class__." + node.name + ".static_method = true;" |
371 | 880 |
return |
881 |
else: |
|
882 |
if len(arg_names) == 0: |
|
883 |
raise TranslationError("methods must take an argument 'self' (in _method)", node) |
|
884 |
self.method_self = arg_names[0] |
|
885 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
886 |
# if not classmethod and arg_names[0] != "self": |
371 | 887 |
# raise TranslationError("first arg not 'self' (in _method)", node) |
888 |
||
889 |
normal_arg_names = arg_names[1:] |
|
1756
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
890 |
if node.kwargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
891 |
kwargname = normal_arg_names.pop() |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
892 |
if node.varargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
893 |
varargname = normal_arg_names.pop() |
371 | 894 |
declared_arg_names = list(normal_arg_names) |
1756
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
895 |
if node.kwargs: |
08e4394ff4fb
clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1755
diff
changeset
|
896 |
declared_arg_names.append(kwargname) |
371 | 897 |
|
898 |
function_args = "(" + ", ".join(declared_arg_names) + ")" |
|
899 |
||
900 |
if classmethod: |
|
901 |
fexpr = UU + class_name_ + ".prototype.__class__." + node.name |
|
902 |
else: |
|
903 |
fexpr = UU + class_name_ + ".prototype." + node.name |
|
904 |
print >>self.output, " "+fexpr + " = function" + function_args + " {" |
|
905 |
||
906 |
# default arguments |
|
907 |
self._default_args_handler(node, normal_arg_names, current_klass) |
|
908 |
||
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
909 |
local_arg_names = normal_arg_names + declared_arg_names |
371 | 910 |
|
911 |
if node.varargs: |
|
912 |
self._varargs_handler(node, varargname, declared_arg_names, current_klass) |
|
913 |
local_arg_names.append(varargname) |
|
914 |
||
915 |
# stack of local variable names for this function call |
|
916 |
self.local_arg_stack.append(local_arg_names) |
|
917 |
||
918 |
for child in node.code: |
|
919 |
self._stmt(child, current_klass) |
|
920 |
||
921 |
# remove the top local arg names |
|
922 |
self.local_arg_stack.pop() |
|
923 |
||
924 |
print >>self.output, " };" |
|
925 |
||
926 |
self._kwargs_parser(node, fexpr, normal_arg_names, current_klass) |
|
927 |
||
928 |
if classmethod: |
|
929 |
# Have to create a version on the instances which automatically passes the |
|
930 |
# class as "self" |
|
931 |
altexpr = UU + class_name_ + ".prototype." + node.name |
|
932 |
print >>self.output, " "+altexpr + " = function() {" |
|
933 |
print >>self.output, " return " + fexpr + ".apply(this.__class__, arguments);" |
|
934 |
print >>self.output, " };" |
|
935 |
print >>self.output, " "+fexpr+".class_method = true;" |
|
936 |
print >>self.output, " "+altexpr+".instance_method = true;" |
|
937 |
else: |
|
938 |
# For instance methods, we need an unbound version in the class object |
|
939 |
altexpr = UU + class_name_ + ".prototype.__class__." + node.name |
|
940 |
print >>self.output, " "+altexpr + " = function() {" |
|
941 |
print >>self.output, " return " + fexpr + ".call.apply("+fexpr+", arguments);" |
|
942 |
print >>self.output, " };" |
|
943 |
print >>self.output, " "+altexpr+".unbound_method = true;" |
|
944 |
print >>self.output, " "+fexpr+".instance_method = true;" |
|
945 |
print >>self.output, " "+altexpr+".__name__ = '%s';" % node.name |
|
946 |
||
947 |
print >>self.output, UU + class_name_ + ".prototype.%s.__name__ = '%s';" % \ |
|
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
948 |
(node.name, node.name) |
371 | 949 |
|
950 |
if node.kwargs or len(node.defaults): |
|
951 |
print >>self.output, " "+altexpr + ".parse_kwargs = " + fexpr + ".parse_kwargs;" |
|
952 |
||
953 |
self.method_self = None |
|
954 |
self.method_imported_globals = set() |
|
955 |
||
956 |
def _isNativeFunc(self, node): |
|
957 |
if isinstance(node, ast.Discard): |
|
958 |
if isinstance(node.expr, ast.CallFunc): |
|
959 |
if isinstance(node.expr.node, ast.Name) and \ |
|
960 |
node.expr.node.name == NATIVE_JS_FUNC_NAME: |
|
961 |
return True |
|
962 |
return False |
|
963 |
||
964 |
def _stmt(self, node, current_klass): |
|
965 |
debugStmt = self.debug and not self._isNativeFunc(node) |
|
966 |
if debugStmt: |
|
967 |
print >>self.output, ' try {' |
|
968 |
||
969 |
if isinstance(node, ast.Return): |
|
970 |
self._return(node, current_klass) |
|
971 |
elif isinstance(node, ast.Break): |
|
972 |
self._break(node, current_klass) |
|
973 |
elif isinstance(node, ast.Continue): |
|
974 |
self._continue(node, current_klass) |
|
975 |
elif isinstance(node, ast.Assign): |
|
976 |
self._assign(node, current_klass) |
|
977 |
elif isinstance(node, ast.AugAssign): |
|
978 |
self._augassign(node, current_klass) |
|
979 |
elif isinstance(node, ast.Discard): |
|
980 |
self._discard(node, current_klass) |
|
981 |
elif isinstance(node, ast.If): |
|
982 |
self._if(node, current_klass) |
|
983 |
elif isinstance(node, ast.For): |
|
984 |
self._for(node, current_klass) |
|
985 |
elif isinstance(node, ast.While): |
|
986 |
self._while(node, current_klass) |
|
987 |
elif isinstance(node, ast.Subscript): |
|
988 |
self._subscript_stmt(node, current_klass) |
|
989 |
elif isinstance(node, ast.Global): |
|
990 |
self._global(node, current_klass) |
|
991 |
elif isinstance(node, ast.Pass): |
|
992 |
pass |
|
993 |
elif isinstance(node, ast.Function): |
|
994 |
self._function(node, True) |
|
995 |
elif isinstance(node, ast.Printnl): |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
996 |
self._print(node, current_klass) |
371 | 997 |
elif isinstance(node, ast.Print): |
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
998 |
self._print(node, current_klass) |
371 | 999 |
elif isinstance(node, ast.TryExcept): |
1000 |
self._tryExcept(node, current_klass) |
|
1001 |
elif isinstance(node, ast.Raise): |
|
1002 |
self._raise(node, current_klass) |
|
1003 |
else: |
|
1004 |
raise TranslationError("unsupported type (in _stmt)", node) |
|
1005 |
||
1006 |
if debugStmt: |
|
1007 |
||
1008 |
lt = self.get_line_trace(node) |
|
1009 |
||
1010 |
haltException = self.module_prefix + "HaltException" |
|
1011 |
isHaltFunction = self.module_prefix + "IsHaltException" |
|
1012 |
||
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1013 |
out = ( |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1014 |
' } catch (__err) {', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1015 |
' if (' + isHaltFunction + '(__err.name)) {', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1016 |
' throw __err;', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1017 |
' } else {', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1018 |
' st = sys.printstack() + ' + '"%s"' % lt + "+ '\\n' ;" |
1785
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
1019 |
' alert("' + 'Error in ' + lt + '"' + |
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
1020 |
'+"\\n"+__err.name+": "+__err.message' + |
0ff2a45dcefa
clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
1021 |
'+"\\n\\nStack trace:\\n"' + '+st' + ');', |
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1022 |
' debugger;', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1023 |
' throw new ' + self.module_prefix + 'HaltException();', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1024 |
' }', |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1025 |
' }' |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1026 |
) |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1027 |
for s in out: |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1028 |
print >>self.output, s |
371 | 1029 |
|
1030 |
def get_line_trace(self, node): |
|
1031 |
lineNum = "Unknown" |
|
1032 |
srcLine = "" |
|
1033 |
if hasattr(node, "lineno"): |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
1034 |
if node.lineno is not None: |
371 | 1035 |
lineNum = node.lineno |
1036 |
srcLine = self.src[min(lineNum, len(self.src))-1] |
|
1037 |
srcLine = srcLine.replace('\\', '\\\\') |
|
1038 |
srcLine = srcLine.replace('"', '\\"') |
|
1039 |
srcLine = srcLine.replace("'", "\\'") |
|
1040 |
||
1041 |
return self.raw_module_name + ".py, line " \ |
|
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1042 |
+ str(lineNum) + ":"\ |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1043 |
+ "\\n" \ |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1044 |
+ " " + srcLine |
371 | 1045 |
|
1046 |
def _augassign(self, node, current_klass): |
|
1047 |
v = node.node |
|
1048 |
if isinstance(v, ast.Getattr): |
|
1049 |
# XXX HACK! don't allow += on return result of getattr. |
|
1050 |
# TODO: create a temporary variable or something. |
|
1051 |
lhs = self._getattr(v, current_klass, False) |
|
1052 |
else: |
|
1053 |
lhs = self._name(node.node, current_klass) |
|
1054 |
op = node.op |
|
1055 |
rhs = self.expr(node.expr, current_klass) |
|
1056 |
print >>self.output, " " + lhs + " " + op + " " + rhs + ";" |
|
1057 |
||
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1058 |
def _assign(self, node, current_klass, top_level=False): |
371 | 1059 |
if len(node.nodes) != 1: |
1060 |
tempvar = '__temp'+str(node.lineno) |
|
1061 |
tnode = ast.Assign([ast.AssName(tempvar, "OP_ASSIGN", node.lineno)], node.expr, node.lineno) |
|
1062 |
self._assign(tnode, current_klass, top_level) |
|
1063 |
for v in node.nodes: |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1064 |
tnode2 = ast.Assign([v], ast.Name(tempvar, node.lineno), node.lineno) |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1065 |
self._assign(tnode2, current_klass, top_level) |
371 | 1066 |
return |
1067 |
||
1068 |
local_var_names = None |
|
1069 |
if len(self.local_arg_stack) > 0: |
|
1070 |
local_var_names = self.local_arg_stack[-1] |
|
1071 |
||
1072 |
def _lhsFromAttr(v, current_klass): |
|
1073 |
attr_name = v.attrname |
|
1074 |
if isinstance(v.expr, ast.Name): |
|
1075 |
obj = v.expr.name |
|
1076 |
lhs = self._name(v.expr, current_klass) + "." + attr_name |
|
1077 |
elif isinstance(v.expr, ast.Getattr): |
|
1078 |
lhs = self._getattr(v, current_klass) |
|
1079 |
elif isinstance(v.expr, ast.Subscript): |
|
1080 |
lhs = self._subscript(v.expr, current_klass) + "." + attr_name |
|
1081 |
else: |
|
1082 |
raise TranslationError("unsupported type (in _assign)", v.expr) |
|
1083 |
return lhs |
|
1084 |
||
1085 |
def _lhsFromName(v, top_level, current_klass): |
|
1086 |
if top_level: |
|
1087 |
if current_klass: |
|
1088 |
lhs = UU+current_klass.name_ + ".prototype.__class__." \ |
|
1089 |
+ v.name |
|
1090 |
else: |
|
1091 |
self.top_level_vars.add(v.name) |
|
1092 |
vname = self.modpfx() + v.name |
|
1093 |
if not self.modpfx() and v.name not in\ |
|
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1094 |
self.method_imported_globals: |
371 | 1095 |
lhs = "var " + vname |
1096 |
else: |
|
1097 |
lhs = UU + vname |
|
1098 |
self.add_local_arg(v.name) |
|
1099 |
else: |
|
1100 |
if v.name in local_var_names: |
|
1101 |
lhs = v.name |
|
1102 |
elif v.name in self.method_imported_globals: |
|
1103 |
lhs = self.modpfx() + v.name |
|
1104 |
else: |
|
1105 |
lhs = "var " + v.name |
|
1106 |
self.add_local_arg(v.name) |
|
1107 |
return lhs |
|
1108 |
||
1109 |
dbg = 0 |
|
1110 |
v = node.nodes[0] |
|
1111 |
if isinstance(v, ast.AssAttr): |
|
1112 |
lhs = _lhsFromAttr(v, current_klass) |
|
1113 |
if v.flags == "OP_ASSIGN": |
|
1114 |
op = "=" |
|
1115 |
else: |
|
1116 |
raise TranslationError("unsupported flag (in _assign)", v) |
|
1117 |
||
1118 |
elif isinstance(v, ast.AssName): |
|
1119 |
lhs = _lhsFromName(v, top_level, current_klass) |
|
1120 |
if v.flags == "OP_ASSIGN": |
|
1121 |
op = "=" |
|
1122 |
else: |
|
1123 |
raise TranslationError("unsupported flag (in _assign)", v) |
|
1124 |
elif isinstance(v, ast.Subscript): |
|
1125 |
if v.flags == "OP_ASSIGN": |
|
1126 |
obj = self.expr(v.expr, current_klass) |
|
1127 |
if len(v.subs) != 1: |
|
1128 |
raise TranslationError("must have one sub (in _assign)", v) |
|
1129 |
idx = self.expr(v.subs[0], current_klass) |
|
1130 |
value = self.expr(node.expr, current_klass) |
|
1131 |
print >>self.output, " " + obj + ".__setitem__(" + idx + ", " + value + ");" |
|
1132 |
return |
|
1133 |
else: |
|
1134 |
raise TranslationError("unsupported flag (in _assign)", v) |
|
1135 |
elif isinstance(v, (ast.AssList, ast.AssTuple)): |
|
1136 |
uniqueID = self.nextTupleAssignID |
|
1137 |
self.nextTupleAssignID += 1 |
|
1138 |
tempName = "__tupleassign" + str(uniqueID) + "__" |
|
1139 |
print >>self.output, " var " + tempName + " = " + \ |
|
1140 |
self.expr(node.expr, current_klass) + ";" |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1738
diff
changeset
|
1141 |
for index, child in enumerate(v.getChildNodes()): |
371 | 1142 |
rhs = tempName + ".__getitem__(" + str(index) + ")" |
1143 |
||
1144 |
if isinstance(child, ast.AssAttr): |
|
1145 |
lhs = _lhsFromAttr(child, current_klass) |
|
1146 |
elif isinstance(child, ast.AssName): |
|
1147 |
lhs = _lhsFromName(child, top_level, current_klass) |
|
1148 |
elif isinstance(child, ast.Subscript): |
|
1149 |
if child.flags == "OP_ASSIGN": |
|
1150 |
obj = self.expr(child.expr, current_klass) |
|
1151 |
if len(child.subs) != 1: |
|
1152 |
raise TranslationError("must have one sub " + |
|
1153 |
"(in _assign)", child) |
|
1154 |
idx = self.expr(child.subs[0], current_klass) |
|
1155 |
value = self.expr(node.expr, current_klass) |
|
1156 |
print >>self.output, " " + obj + ".__setitem__(" \ |
|
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1157 |
+ idx + ", " + rhs + ");" |
371 | 1158 |
continue |
1159 |
print >>self.output, " " + lhs + " = " + rhs + ";" |
|
1160 |
return |
|
1161 |
else: |
|
1162 |
raise TranslationError("unsupported type (in _assign)", v) |
|
1163 |
||
1164 |
rhs = self.expr(node.expr, current_klass) |
|
1165 |
if dbg: |
|
1166 |
print "b", repr(node.expr), rhs |
|
1167 |
print >>self.output, " " + lhs + " " + op + " " + rhs + ";" |
|
1168 |
||
1169 |
def _discard(self, node, current_klass): |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
1170 |
|
371 | 1171 |
if isinstance(node.expr, ast.CallFunc): |
1172 |
debugStmt = self.debug and not self._isNativeFunc(node) |
|
1173 |
if debugStmt and isinstance(node.expr.node, ast.Name) and \ |
|
1174 |
node.expr.node.name == 'import_wait': |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1175 |
debugStmt = False |
371 | 1176 |
if debugStmt: |
1177 |
st = self.get_line_trace(node) |
|
1178 |
print >>self.output, "sys.addstack('%s');\n" % st |
|
1179 |
if isinstance(node.expr.node, ast.Name) and node.expr.node.name == NATIVE_JS_FUNC_NAME: |
|
1180 |
if len(node.expr.args) != 1: |
|
1181 |
raise TranslationError("native javascript function %s must have one arg" % NATIVE_JS_FUNC_NAME, node.expr) |
|
1182 |
if not isinstance(node.expr.args[0], ast.Const): |
|
1183 |
raise TranslationError("native javascript function %s must have constant arg" % NATIVE_JS_FUNC_NAME, node.expr) |
|
1184 |
raw_js = node.expr.args[0].value |
|
1185 |
print >>self.output, raw_js |
|
1186 |
else: |
|
1187 |
expr = self._callfunc(node.expr, current_klass) |
|
1188 |
print >>self.output, " " + expr + ";" |
|
1189 |
||
1190 |
if debugStmt: |
|
1191 |
print >>self.output, "sys.popstack();\n" |
|
1192 |
||
1193 |
elif isinstance(node.expr, ast.Const): |
|
1737
a39c2918c015
clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
1194 |
if node.expr.value is not None: # Empty statements generate ignore None |
371 | 1195 |
print >>self.output, self._const(node.expr) |
1196 |
else: |
|
1197 |
raise TranslationError("unsupported type (in _discard)", node.expr) |
|
1198 |
||
1199 |
def _if(self, node, current_klass): |
|
1200 |
for i in range(len(node.tests)): |
|
1201 |
test, consequence = node.tests[i] |
|
1202 |
if i == 0: |
|
1203 |
keyword = "if" |
|
1204 |
else: |
|
1205 |
keyword = "else if" |
|
1206 |
||
1207 |
self._if_test(keyword, test, consequence, current_klass) |
|
1208 |
||
1209 |
if node.else_: |
|
1210 |
keyword = "else" |
|
1211 |
test = None |
|
1212 |
consequence = node.else_ |
|
1213 |
||
1214 |
self._if_test(keyword, test, consequence, current_klass) |
|
1215 |
||
1216 |
def _if_test(self, keyword, test, consequence, current_klass): |
|
1217 |
if test: |
|
1218 |
expr = self.expr(test, current_klass) |
|
1219 |
||
1220 |
print >>self.output, " " + keyword + " (pyjslib.bool(" + expr + ")) {" |
|
1221 |
else: |
|
1222 |
print >>self.output, " " + keyword + " {" |
|
1223 |
||
1224 |
if isinstance(consequence, ast.Stmt): |
|
1225 |
for child in consequence.nodes: |
|
1226 |
self._stmt(child, current_klass) |
|
1227 |
else: |
|
1228 |
raise TranslationError("unsupported type (in _if_test)", consequence) |
|
1229 |
||
1230 |
print >>self.output, " }" |
|
1231 |
||
1232 |
def _from(self, node): |
|
1233 |
for name in node.names: |
|
1234 |
# look up "hack" in AppTranslator as to how findFile gets here |
|
1235 |
module_name = node.modname + "." + name[0] |
|
1236 |
try: |
|
1237 |
ff = self.findFile(module_name + ".py") |
|
1238 |
except Exception: |
|
1239 |
ff = None |
|
1240 |
if ff: |
|
1241 |
self.add_imported_module(module_name) |
|
1242 |
else: |
|
1243 |
self.imported_classes[name[0]] = node.modname |
|
1244 |
||
1245 |
def _compare(self, node, current_klass): |
|
1246 |
lhs = self.expr(node.expr, current_klass) |
|
1247 |
||
1248 |
if len(node.ops) != 1: |
|
1249 |
raise TranslationError("only one ops supported (in _compare)", node) |
|
1250 |
||
1251 |
op = node.ops[0][0] |
|
1252 |
rhs_node = node.ops[0][1] |
|
1253 |
rhs = self.expr(rhs_node, current_klass) |
|
1254 |
||
1255 |
if op == "==": |
|
1256 |
return "pyjslib.eq(%s, %s)" % (lhs, rhs) |
|
1257 |
if op == "in": |
|
1258 |
return rhs + ".__contains__(" + lhs + ")" |
|
1259 |
elif op == "not in": |
|
1260 |
return "!" + rhs + ".__contains__(" + lhs + ")" |
|
1261 |
elif op == "is": |
|
1262 |
op = "===" |
|
1263 |
elif op == "is not": |
|
1264 |
op = "!==" |
|
1265 |
||
1266 |
return "(" + lhs + " " + op + " " + rhs + ")" |
|
1267 |
||
1268 |
def _not(self, node, current_klass): |
|
1269 |
expr = self.expr(node.expr, current_klass) |
|
1270 |
||
1271 |
return "!(" + expr + ")" |
|
1272 |
||
1273 |
def _or(self, node, current_klass): |
|
1274 |
expr = "("+(") || (".join([self.expr(child, current_klass) for child in node.nodes]))+')' |
|
1275 |
return expr |
|
1276 |
||
1277 |
def _and(self, node, current_klass): |
|
1278 |
expr = "("+(") && (".join([self.expr(child, current_klass) for child in node.nodes]))+")" |
|
1279 |
return expr |
|
1280 |
||
1281 |
def _for(self, node, current_klass): |
|
1282 |
assign_name = "" |
|
1283 |
assign_tuple = "" |
|
1284 |
||
1285 |
# based on Bob Ippolito's Iteration in Javascript code |
|
1286 |
if isinstance(node.assign, ast.AssName): |
|
1287 |
assign_name = node.assign.name |
|
1288 |
self.add_local_arg(assign_name) |
|
1289 |
if node.assign.flags == "OP_ASSIGN": |
|
1290 |
op = "=" |
|
1291 |
elif isinstance(node.assign, ast.AssTuple): |
|
1292 |
op = "=" |
|
1293 |
i = 0 |
|
1294 |
for child in node.assign: |
|
1295 |
child_name = child.name |
|
1296 |
if assign_name == "": |
|
1297 |
assign_name = "temp_" + child_name |
|
1298 |
self.add_local_arg(child_name) |
|
1299 |
assign_tuple += """ |
|
1300 |
var %(child_name)s %(op)s %(assign_name)s.__getitem__(%(i)i); |
|
1301 |
""" % locals() |
|
1302 |
i += 1 |
|
1303 |
else: |
|
1304 |
raise TranslationError("unsupported type (in _for)", node.assign) |
|
1305 |
||
1306 |
if isinstance(node.list, ast.Name): |
|
1307 |
list_expr = self._name(node.list, current_klass) |
|
1308 |
elif isinstance(node.list, ast.Getattr): |
|
1309 |
list_expr = self._getattr(node.list, current_klass) |
|
1310 |
elif isinstance(node.list, ast.CallFunc): |
|
1311 |
list_expr = self._callfunc(node.list, current_klass) |
|
1312 |
else: |
|
1313 |
raise TranslationError("unsupported type (in _for)", node.list) |
|
1314 |
||
1315 |
lhs = "var " + assign_name |
|
1316 |
iterator_name = "__" + assign_name |
|
1317 |
||
1318 |
print >>self.output, """ |
|
1319 |
var %(iterator_name)s = %(list_expr)s.__iter__(); |
|
1320 |
try { |
|
1321 |
while (true) { |
|
1322 |
%(lhs)s %(op)s %(iterator_name)s.next(); |
|
1323 |
%(assign_tuple)s |
|
1324 |
""" % locals() |
|
1325 |
for node in node.body.nodes: |
|
1326 |
self._stmt(node, current_klass) |
|
1327 |
print >>self.output, """ |
|
1328 |
} |
|
1329 |
} catch (e) { |
|
1330 |
if (e.__name__ != pyjslib.StopIteration.__name__) { |
|
1331 |
throw e; |
|
1332 |
} |
|
1333 |
} |
|
1334 |
""" % locals() |
|
1335 |
||
1336 |
def _while(self, node, current_klass): |
|
1337 |
test = self.expr(node.test, current_klass) |
|
1338 |
print >>self.output, " while (pyjslib.bool(" + test + ")) {" |
|
1339 |
if isinstance(node.body, ast.Stmt): |
|
1340 |
for child in node.body.nodes: |
|
1341 |
self._stmt(child, current_klass) |
|
1342 |
else: |
|
1343 |
raise TranslationError("unsupported type (in _while)", node.body) |
|
1344 |
print >>self.output, " }" |
|
1345 |
||
1346 |
def _const(self, node): |
|
1347 |
if isinstance(node.value, int): |
|
1348 |
return str(node.value) |
|
1349 |
elif isinstance(node.value, float): |
|
1350 |
return str(node.value) |
|
1351 |
elif isinstance(node.value, basestring): |
|
1352 |
v = node.value |
|
1353 |
if isinstance(node.value, unicode): |
|
1354 |
v = v.encode('utf-8') |
|
1738
d2e979738700
clean-up: fix PEP8 E271 multiple spaces after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1737
diff
changeset
|
1355 |
return "String('%s')" % escapejs(v) |
371 | 1356 |
elif node.value is None: |
1357 |
return "null" |
|
1358 |
else: |
|
1359 |
raise TranslationError("unsupported type (in _const)", node) |
|
1360 |
||
1361 |
def _unaryadd(self, node, current_klass): |
|
1362 |
return self.expr(node.expr, current_klass) |
|
1363 |
||
1364 |
def _unarysub(self, node, current_klass): |
|
1365 |
return "-" + self.expr(node.expr, current_klass) |
|
1366 |
||
1367 |
def _add(self, node, current_klass): |
|
1368 |
return self.expr(node.left, current_klass) + " + " + self.expr(node.right, current_klass) |
|
1369 |
||
1370 |
def _sub(self, node, current_klass): |
|
1371 |
return self.expr(node.left, current_klass) + " - " + self.expr(node.right, current_klass) |
|
1372 |
||
1373 |
def _div(self, node, current_klass): |
|
1374 |
return self.expr(node.left, current_klass) + " / " + self.expr(node.right, current_klass) |
|
1375 |
||
1376 |
def _mul(self, node, current_klass): |
|
1377 |
return self.expr(node.left, current_klass) + " * " + self.expr(node.right, current_klass) |
|
1378 |
||
1379 |
def _mod(self, node, current_klass): |
|
1380 |
if isinstance(node.left, ast.Const) and isinstance(node.left.value, StringType): |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1381 |
self.imported_js.add("sprintf.js") # Include the sprintf functionality if it is used |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1382 |
return "sprintf("+self.expr(node.left, current_klass) + ", " + self.expr(node.right, current_klass)+")" |
371 | 1383 |
return self.expr(node.left, current_klass) + " % " + self.expr(node.right, current_klass) |
1384 |
||
1385 |
def _invert(self, node, current_klass): |
|
1386 |
return "~" + self.expr(node.expr, current_klass) |
|
1387 |
||
1388 |
def _bitand(self, node, current_klass): |
|
1389 |
return " & ".join([self.expr(child, current_klass) for child in node.nodes]) |
|
1390 |
||
1391 |
def _bitshiftleft(self, node, current_klass): |
|
1392 |
return self.expr(node.left, current_klass) + " << " + self.expr(node.right, current_klass) |
|
1393 |
||
1394 |
def _bitshiftright(self, node, current_klass): |
|
1395 |
return self.expr(node.left, current_klass) + " >>> " + self.expr(node.right, current_klass) |
|
1396 |
||
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1738
diff
changeset
|
1397 |
def _bitxor(self, node, current_klass): |
371 | 1398 |
return " ^ ".join([self.expr(child, current_klass) for child in node.nodes]) |
1399 |
||
1400 |
def _bitor(self, node, current_klass): |
|
1401 |
return " | ".join([self.expr(child, current_klass) for child in node.nodes]) |
|
1402 |
||
1403 |
def _subscript(self, node, current_klass): |
|
1404 |
if node.flags == "OP_APPLY": |
|
1405 |
if len(node.subs) == 1: |
|
1406 |
return self.expr(node.expr, current_klass) + ".__getitem__(" + self.expr(node.subs[0], current_klass) + ")" |
|
1407 |
else: |
|
1408 |
raise TranslationError("must have one sub (in _subscript)", node) |
|
1409 |
else: |
|
1410 |
raise TranslationError("unsupported flag (in _subscript)", node) |
|
1411 |
||
1412 |
def _subscript_stmt(self, node, current_klass): |
|
1413 |
if node.flags == "OP_DELETE": |
|
1414 |
print >>self.output, " " + self.expr(node.expr, current_klass) + ".__delitem__(" + self.expr(node.subs[0], current_klass) + ");" |
|
1415 |
else: |
|
1416 |
raise TranslationError("unsupported flag (in _subscript)", node) |
|
1417 |
||
1418 |
def _list(self, node, current_klass): |
|
1419 |
return "new pyjslib.List([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])" |
|
1420 |
||
1421 |
def _dict(self, node, current_klass): |
|
1422 |
items = [] |
|
1423 |
for x in node.items: |
|
1424 |
key = self.expr(x[0], current_klass) |
|
1425 |
value = self.expr(x[1], current_klass) |
|
1426 |
items.append("[" + key + ", " + value + "]") |
|
1427 |
return "new pyjslib.Dict([" + ", ".join(items) + "])" |
|
1428 |
||
1429 |
def _tuple(self, node, current_klass): |
|
1430 |
return "new pyjslib.Tuple([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])" |
|
1431 |
||
1432 |
def _lambda(self, node, current_klass): |
|
1433 |
if node.varargs: |
|
1434 |
raise TranslationError("varargs are not supported in Lambdas", node) |
|
1435 |
if node.kwargs: |
|
1436 |
raise TranslationError("kwargs are not supported in Lambdas", node) |
|
1437 |
res = cStringIO.StringIO() |
|
1438 |
arg_names = list(node.argnames) |
|
1439 |
function_args = ", ".join(arg_names) |
|
1440 |
for child in node.getChildNodes(): |
|
1441 |
expr = self.expr(child, None) |
|
1442 |
print >> res, "function (%s){" % function_args |
|
1443 |
self._default_args_handler(node, arg_names, None, |
|
1444 |
output=res) |
|
1445 |
print >> res, 'return %s;}' % expr |
|
1446 |
return res.getvalue() |
|
1447 |
||
1448 |
def _slice(self, node, current_klass): |
|
1449 |
if node.flags == "OP_APPLY": |
|
1450 |
lower = "null" |
|
1451 |
upper = "null" |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
1452 |
if node.lower is not None: |
371 | 1453 |
lower = self.expr(node.lower, current_klass) |
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
1454 |
if node.upper is not None: |
371 | 1455 |
upper = self.expr(node.upper, current_klass) |
1738
d2e979738700
clean-up: fix PEP8 E271 multiple spaces after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1737
diff
changeset
|
1456 |
return "pyjslib.slice(" + self.expr(node.expr, current_klass) + ", " + lower + ", " + upper + ")" |
371 | 1457 |
else: |
1458 |
raise TranslationError("unsupported flag (in _slice)", node) |
|
1459 |
||
1460 |
def _global(self, node, current_klass): |
|
1461 |
for name in node.names: |
|
1462 |
self.method_imported_globals.add(name) |
|
1463 |
||
1464 |
def expr(self, node, current_klass): |
|
1465 |
if isinstance(node, ast.Const): |
|
1466 |
return self._const(node) |
|
1467 |
# @@@ not sure if the parentheses should be here or in individual operator functions - JKT |
|
1468 |
elif isinstance(node, ast.Mul): |
|
1469 |
return " ( " + self._mul(node, current_klass) + " ) " |
|
1470 |
elif isinstance(node, ast.Add): |
|
1471 |
return " ( " + self._add(node, current_klass) + " ) " |
|
1472 |
elif isinstance(node, ast.Sub): |
|
1473 |
return " ( " + self._sub(node, current_klass) + " ) " |
|
1474 |
elif isinstance(node, ast.Div): |
|
1475 |
return " ( " + self._div(node, current_klass) + " ) " |
|
1476 |
elif isinstance(node, ast.Mod): |
|
1477 |
return self._mod(node, current_klass) |
|
1478 |
elif isinstance(node, ast.UnaryAdd): |
|
1479 |
return self._unaryadd(node, current_klass) |
|
1480 |
elif isinstance(node, ast.UnarySub): |
|
1481 |
return self._unarysub(node, current_klass) |
|
1482 |
elif isinstance(node, ast.Not): |
|
1483 |
return self._not(node, current_klass) |
|
1484 |
elif isinstance(node, ast.Or): |
|
1485 |
return self._or(node, current_klass) |
|
1486 |
elif isinstance(node, ast.And): |
|
1487 |
return self._and(node, current_klass) |
|
1488 |
elif isinstance(node, ast.Invert): |
|
1489 |
return self._invert(node, current_klass) |
|
1490 |
elif isinstance(node, ast.Bitand): |
|
1491 |
return "("+self._bitand(node, current_klass)+")" |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1738
diff
changeset
|
1492 |
elif isinstance(node, ast.LeftShift): |
371 | 1493 |
return self._bitshiftleft(node, current_klass) |
1494 |
elif isinstance(node, ast.RightShift): |
|
1495 |
return self._bitshiftright(node, current_klass) |
|
1496 |
elif isinstance(node, ast.Bitxor): |
|
1497 |
return "("+self._bitxor(node, current_klass)+")" |
|
1498 |
elif isinstance(node, ast.Bitor): |
|
1499 |
return "("+self._bitor(node, current_klass)+")" |
|
1500 |
elif isinstance(node, ast.Compare): |
|
1501 |
return self._compare(node, current_klass) |
|
1502 |
elif isinstance(node, ast.CallFunc): |
|
1503 |
return self._callfunc(node, current_klass) |
|
1504 |
elif isinstance(node, ast.Name): |
|
1505 |
return self._name(node, current_klass) |
|
1506 |
elif isinstance(node, ast.Subscript): |
|
1507 |
return self._subscript(node, current_klass) |
|
1508 |
elif isinstance(node, ast.Getattr): |
|
1509 |
return self._getattr(node, current_klass) |
|
1510 |
elif isinstance(node, ast.List): |
|
1511 |
return self._list(node, current_klass) |
|
1512 |
elif isinstance(node, ast.Dict): |
|
1513 |
return self._dict(node, current_klass) |
|
1514 |
elif isinstance(node, ast.Tuple): |
|
1515 |
return self._tuple(node, current_klass) |
|
1516 |
elif isinstance(node, ast.Slice): |
|
1517 |
return self._slice(node, current_klass) |
|
1518 |
elif isinstance(node, ast.Lambda): |
|
1519 |
return self._lambda(node, current_klass) |
|
1520 |
else: |
|
1521 |
raise TranslationError("unsupported type (in expr)", node) |
|
1522 |
||
1523 |
||
1524 |
def translate(file_name, module_name, debug=False): |
|
1525 |
f = file(file_name, "r") |
|
1526 |
src = f.read() |
|
1527 |
f.close() |
|
1528 |
output = cStringIO.StringIO() |
|
1529 |
mod = compiler.parseFile(file_name) |
|
1530 |
t = Translator(module_name, module_name, module_name, src, debug, mod, output) |
|
1531 |
return output.getvalue() |
|
1532 |
||
1533 |
||
1534 |
class PlatformParser: |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1535 |
def __init__(self, platform_dir="", verbose=True): |
371 | 1536 |
self.platform_dir = platform_dir |
1537 |
self.parse_cache = {} |
|
1538 |
self.platform = "" |
|
1539 |
self.verbose = verbose |
|
1540 |
||
1541 |
def setPlatform(self, platform): |
|
1542 |
self.platform = platform |
|
1543 |
||
1544 |
def parseModule(self, module_name, file_name): |
|
1545 |
||
1546 |
importing = False |
|
1775
b45f2768fab1
clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1773
diff
changeset
|
1547 |
if file_name not in self.parse_cache: |
371 | 1548 |
importing = True |
1549 |
mod = compiler.parseFile(file_name) |
|
1550 |
self.parse_cache[file_name] = mod |
|
1551 |
else: |
|
1552 |
mod = self.parse_cache[file_name] |
|
1553 |
||
1554 |
override = False |
|
1555 |
platform_file_name = self.generatePlatformFilename(file_name) |
|
1556 |
if self.platform and os.path.isfile(platform_file_name): |
|
1557 |
mod = copy.deepcopy(mod) |
|
1558 |
mod_override = compiler.parseFile(platform_file_name) |
|
1559 |
self.merge(mod, mod_override) |
|
1560 |
override = True |
|
1561 |
||
1562 |
if self.verbose: |
|
1563 |
if override: |
|
1564 |
print "Importing %s (Platform %s)" % (module_name, self.platform) |
|
1565 |
elif importing: |
|
1566 |
print "Importing %s" % (module_name) |
|
1567 |
||
1568 |
return mod, override |
|
1569 |
||
1570 |
def generatePlatformFilename(self, file_name): |
|
1571 |
(module_name, extension) = os.path.splitext(os.path.basename(file_name)) |
|
1572 |
platform_file_name = module_name + self.platform + extension |
|
1573 |
||
1574 |
return os.path.join(os.path.dirname(file_name), self.platform_dir, platform_file_name) |
|
1575 |
||
1576 |
def merge(self, tree1, tree2): |
|
1577 |
for child in tree2.node: |
|
1578 |
if isinstance(child, ast.Function): |
|
1579 |
self.replaceFunction(tree1, child.name, child) |
|
1580 |
elif isinstance(child, ast.Class): |
|
1581 |
self.replaceClassMethods(tree1, child.name, child) |
|
1582 |
||
1583 |
return tree1 |
|
1584 |
||
1585 |
def replaceFunction(self, tree, function_name, function_node): |
|
1586 |
# find function to replace |
|
1587 |
for child in tree.node: |
|
1588 |
if isinstance(child, ast.Function) and child.name == function_name: |
|
1589 |
self.copyFunction(child, function_node) |
|
1590 |
return |
|
1591 |
raise TranslationError("function not found: " + function_name, function_node) |
|
1592 |
||
1593 |
def replaceClassMethods(self, tree, class_name, class_node): |
|
1594 |
# find class to replace |
|
1595 |
old_class_node = None |
|
1596 |
for child in tree.node: |
|
1597 |
if isinstance(child, ast.Class) and child.name == class_name: |
|
1598 |
old_class_node = child |
|
1599 |
break |
|
1600 |
||
1601 |
if not old_class_node: |
|
1602 |
raise TranslationError("class not found: " + class_name, class_node) |
|
1603 |
||
1604 |
# replace methods |
|
1605 |
for function_node in class_node.code: |
|
1606 |
if isinstance(function_node, ast.Function): |
|
1607 |
found = False |
|
1608 |
for child in old_class_node.code: |
|
1609 |
if isinstance(child, ast.Function) and child.name == function_node.name: |
|
1610 |
found = True |
|
1611 |
self.copyFunction(child, function_node) |
|
1612 |
break |
|
1613 |
||
1614 |
if not found: |
|
1615 |
raise TranslationError("class method not found: " + class_name + "." + function_node.name, function_node) |
|
1616 |
||
1617 |
def copyFunction(self, target, source): |
|
1618 |
target.code = source.code |
|
1619 |
target.argnames = source.argnames |
|
1620 |
target.defaults = source.defaults |
|
1737
a39c2918c015
clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
1621 |
target.doc = source.doc # @@@ not sure we need to do this any more |
371 | 1622 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
1623 |
|
371 | 1624 |
def dotreplace(fname): |
1625 |
path, ext = os.path.splitext(fname) |
|
1626 |
return path.replace(".", "/") + ext |
|
1627 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
1628 |
|
371 | 1629 |
class AppTranslator: |
1630 |
||
1631 |
def __init__(self, library_dirs=[], parser=None, dynamic=False, |
|
1632 |
optimize=False, verbose=True): |
|
1633 |
self.extension = ".py" |
|
1634 |
self.optimize = optimize |
|
1635 |
self.library_modules = [] |
|
1636 |
self.overrides = {} |
|
1637 |
self.library_dirs = path + library_dirs |
|
1638 |
self.dynamic = dynamic |
|
1639 |
self.verbose = verbose |
|
1640 |
||
1641 |
if not parser: |
|
1642 |
self.parser = PlatformParser() |
|
1643 |
else: |
|
1644 |
self.parser = parser |
|
1645 |
||
1646 |
self.parser.dynamic = dynamic |
|
1647 |
||
1648 |
def findFile(self, file_name): |
|
1649 |
if os.path.isfile(file_name): |
|
1650 |
return file_name |
|
1651 |
||
1652 |
for library_dir in self.library_dirs: |
|
1653 |
file_name = dotreplace(file_name) |
|
1654 |
full_file_name = os.path.join( |
|
1655 |
os.path.abspath(os.path.dirname(__file__)), library_dir, file_name) |
|
1656 |
if os.path.isfile(full_file_name): |
|
1657 |
return full_file_name |
|
1658 |
||
1659 |
fnameinit, ext = os.path.splitext(file_name) |
|
1660 |
fnameinit = fnameinit + "/__init__.py" |
|
1661 |
||
1662 |
full_file_name = os.path.join( |
|
1663 |
os.path.abspath(os.path.dirname(__file__)), library_dir, fnameinit) |
|
1664 |
if os.path.isfile(full_file_name): |
|
1665 |
return full_file_name |
|
1666 |
||
1667 |
raise Exception("file not found: " + file_name) |
|
1668 |
||
1669 |
def _translate(self, module_name, is_app=True, debug=False, |
|
1670 |
imported_js=set()): |
|
1671 |
if module_name not in self.library_modules: |
|
1672 |
self.library_modules.append(module_name) |
|
1673 |
||
1674 |
file_name = self.findFile(module_name + self.extension) |
|
1675 |
||
1676 |
output = cStringIO.StringIO() |
|
1677 |
||
1678 |
f = file(file_name, "r") |
|
1679 |
src = f.read() |
|
1680 |
f.close() |
|
1681 |
||
1682 |
mod, override = self.parser.parseModule(module_name, file_name) |
|
1683 |
if override: |
|
1684 |
override_name = "%s.%s" % (self.parser.platform.lower(), |
|
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
1685 |
module_name) |
371 | 1686 |
self.overrides[override_name] = override_name |
1687 |
if is_app: |
|
1688 |
mn = '__main__' |
|
1689 |
else: |
|
1690 |
mn = module_name |
|
1691 |
t = Translator(mn, module_name, module_name, |
|
1692 |
src, debug, mod, output, self.dynamic, self.optimize, |
|
1693 |
self.findFile) |
|
1694 |
||
1695 |
module_str = output.getvalue() |
|
1696 |
imported_js.update(set(t.imported_js)) |
|
1697 |
imported_modules_str = "" |
|
1698 |
for module in t.imported_modules: |
|
1699 |
if module not in self.library_modules: |
|
1700 |
self.library_modules.append(module) |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
1701 |
# imported_js.update(set(t.imported_js)) |
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1776
diff
changeset
|
1702 |
# imported_modules_str += self._translate( |
371 | 1703 |
# module, False, debug=debug, imported_js=imported_js) |
1704 |
||
1705 |
return imported_modules_str + module_str |
|
1706 |
||
1707 |
def translate(self, module_name, is_app=True, debug=False, |
|
1708 |
library_modules=[]): |
|
1709 |
app_code = cStringIO.StringIO() |
|
1710 |
lib_code = cStringIO.StringIO() |
|
1711 |
imported_js = set() |
|
1712 |
self.library_modules = [] |
|
1713 |
self.overrides = {} |
|
1714 |
for library in library_modules: |
|
1715 |
if library.endswith(".js"): |
|
1716 |
imported_js.add(library) |
|
1717 |
continue |
|
1718 |
self.library_modules.append(library) |
|
1719 |
if self.verbose: |
|
1720 |
print 'Including LIB', library |
|
1721 |
print >> lib_code, '\n//\n// BEGIN LIB '+library+'\n//\n' |
|
1722 |
print >> lib_code, self._translate( |
|
1723 |
library, False, debug=debug, imported_js=imported_js) |
|
1724 |
||
1725 |
print >> lib_code, "/* initialize static library */" |
|
1726 |
print >> lib_code, "%s%s();\n" % (UU, library) |
|
1727 |
||
1728 |
print >> lib_code, '\n//\n// END LIB '+library+'\n//\n' |
|
1729 |
if module_name: |
|
1730 |
print >> app_code, self._translate( |
|
1731 |
module_name, is_app, debug=debug, imported_js=imported_js) |
|
1732 |
for js in imported_js: |
|
1757
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1733 |
path = self.findFile(js) |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1734 |
if os.path.isfile(path): |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1735 |
if self.verbose: |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1736 |
print 'Including JS', js |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1737 |
print >> lib_code, '\n//\n// BEGIN JS '+js+'\n//\n' |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1738 |
print >> lib_code, file(path).read() |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1739 |
print >> lib_code, '\n//\n// END JS '+js+'\n//\n' |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1740 |
else: |
0de89da92ee0
clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1756
diff
changeset
|
1741 |
print >>sys.stderr, 'Warning: Unable to find imported javascript:', js |
371 | 1742 |
return lib_code.getvalue(), app_code.getvalue() |
1743 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1746
diff
changeset
|
1744 |
|
371 | 1745 |
usage = """ |
1746 |
usage: %s file_name [module_name] |
|
1747 |
""" |
|
1748 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
1749 |
|
371 | 1750 |
def main(): |
1751 |
import sys |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
1752 |
if len(sys.argv) < 2: |
371 | 1753 |
print >> sys.stderr, usage % sys.argv[0] |
1754 |
sys.exit(1) |
|
1755 |
file_name = os.path.abspath(sys.argv[1]) |
|
1756 |
if not os.path.isfile(file_name): |
|
1757 |
print >> sys.stderr, "File not found %s" % file_name |
|
1758 |
sys.exit(1) |
|
1759 |
if len(sys.argv) > 2: |
|
1760 |
module_name = sys.argv[2] |
|
1761 |
else: |
|
1762 |
module_name = None |
|
1763 |
print translate(file_name, module_name), |
|
1764 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1746
diff
changeset
|
1765 |
|
371 | 1766 |
if __name__ == "__main__": |
1767 |
main() |