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