forked from PythonJS/PythonJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_to_pythonjs.py
More file actions
327 lines (313 loc) · 10.8 KB
/
python_to_pythonjs.py
File metadata and controls
327 lines (313 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from ast import Str
from ast import Expr
from ast import Call
from ast import Name
from ast import While
from ast import Assign
from ast import keyword
from ast import arguments
from ast import TryExcept
from ast import Attribute
from ast import FunctionDef
from ast import NodeTransformer
class PythonToPythonJS(NodeTransformer):
def visit_ClassDef(self, node):
name = Name(node.name, None)
yield Expr(Assign([name], Call(Name('JSObject', None), None, None, None, None)))
yield Expr(Assign([Name('parents', None)], Call(Name('JSArray', Name), None, None, None, None)))
if node.bases:
yield Expr(
Call(
Attribute(
Name('parents', None),
'push',
None
),
node.bases,
None,
None,
None
)
)
for item in node.body:
if isinstance(item, FunctionDef):
item_name = item.name
item.name = closure_name = '%s__%s' % (node.name, item_name)
for i in self.visit(item):
yield i
yield Expr(Assign([Attribute(name, item_name, None)], Name(closure_name, None)))
elif isinstance(item, Assign):
item_name = item.targets[0].id
item.targets[0].id = closure_name = '%s__%s' % (name.id, item_name)
yield self.visit(item)
yield Expr(Assign([Attribute(name, item_name, None)], Name(closure_name, None)))
yield Expr(Assign([name], Call(Name('create_class', None), [Str(node.name), Name('parents', None), Name(name.id, None)], None, None, None)))
def visit_Attribute(self, node):
return Call(Name('get_attribute', None), [self.visit(node.value), Str(node.attr)], None, None, None)
def visit_Expr(self, node): # FIXME: is it useful
return Expr(self.visit(node.value))
def visit_Assign(self, node):
attr = node.targets[0]
if isinstance(attr, Attribute):
return Expr(Call(Name('set_attribute', None), [attr.value, Str(attr.attr), self.visit(node.value)], None, None, None))
else:
return Expr(self.generic_visit(node))
def visit_Call(self, node):
if hasattr(node.func, 'id') and node.func.id in ('JS', 'toString', 'JSObject', 'JSArray', 'var'):
return self.generic_visit(node)
return Call(
Call(
Name('get_attribute', None),
[self.visit(node.func), Str('__call__')],
None,
None,
None,
),
[
Call(
Name('JSArray', None),
map(self.visit, node.args),
None,
None,
None
),
Call(
Name('JSObject', None),
None,
map(lambda x: keyword(Name(x.arg, None), self.visit(x.value)), node.keywords),
None,
None
),
],
None,
None,
None,
)
def visit_FunctionDef(self, node):
# new body is old body processed by PythonToPythonJS
# prepended by the python arguments handling
body = map(self.visit, node.body)
# new pythonjs' python function arguments handling
# create the structure representing the functions arguments
# first create the defaultkwargs JSObject
l = len(node.args.defaults)
kwargsdefault = map(lambda x: keyword(x[0], x[1]), zip(node.args.args[-l:], node.args.defaults))
kwargsdefault = Call(
Name('JSObject', None),
None,
kwargsdefault,
None,
None
)
args = Call(
Name('JSArray', None),
map(lambda x: Str(x.id), node.args.args),
None,
None,
None
)
keywords = list([
keyword(Name('kwargs', None), kwargsdefault),
keyword(Name('args', None), args),
])
if node.args.vararg:
keywords.append(keyword(Name('vararg', None), Str(node.args.vararg)))
if node.args.kwarg:
keywords.append(keyword(Name('varkwarg', None), Str(node.args.kwarg)))
prebody = list()
# create a JS Object to store the value of each parameter
prebody.append(
Expr(
Assign(
[Name('var signature', None)],
Call(
Name('JSObject', None),
None,
keywords,
None,
None
)
)
)
)
# retrieve the actual value for each argument, cf. pythonpythonjs
prebody.append(
Expr(
Assign(
[Name('var arguments', None)],
Call(
Name('get_arguments', None),
[Name('signature', None), Name('args', None), Name('kwargs', None)],
None,
None,
None
)
)
)
)
# then for each argument assign its value
for arg in node.args.args:
prebody.append(
Expr(
Assign(
[Name('var ' + arg.id, None)],
Call(
Name('JS', None),
[Str('arguments["%s"]' % arg.id)],
None,
None,
None
)
)
)
)
if node.args.vararg:
prebody.append(
Expr(
Call(
Name('JS', None),
[Str('var %s = arguments["%s"]' % (node.args.vararg, node.args.vararg))],
None,
None,
None
)
)
)
# turn it into a list
expr = '%s = get_attribute(list, "__call__")(create_array(%s), {});'
expr = expr % (node.args.vararg, node.args.vararg)
prebody.append(Name(expr, None))
if node.args.kwarg:
prebody.append(
Expr(
Call(
Name('JS', None),
[Str('var %s = arguments["%s"]' % (node.args.kwarg, node.args.kwarg))],
None,
None,
None
)
)
)
expr = '%s = get_attribute(dict, "__call__")(create_array(%s), {});'
expr = expr % (node.args.kwarg, node.args.kwarg)
prebody.append(Name(expr, None))
prebody.extend(body)
body = prebody
# process arguments to build python keyword arguments handling
# in pythonjs, python functions takes two parameters args and kwargs
args = arguments([Name('args', None), Name('kwargs', None)], None, None, None)
yield FunctionDef(
node.name,
args,
body,
None
)
for decorator in reversed(node.decorator_list):
yield Assign(
[Name(node.name, None)],
Call(
decorator,
[
Call(
Name('JS', None),
[Str('create_array(%s)' % node.name)],
None,
None,
None
)
],
None,
None,
None
)
)
def visit_For(self, node):
yield Assign(
[Name('var __iterator__', None)],
Call(
Call(Name('get_attribute', None), [self.visit(node.iter), Str('__iter__')], None, None, None),
[
Call(
Name('JSArray', None),
None,
None,
None,
None
),
Call(
Name('JSObject', None),
None,
None,
None,
None
),
],
None,
None,
None
)
)
node.body = map(self.visit, node.body)
node.body.append(
Assign(
[Name('var %s' % node.target.id, None)],
Call(
Call(Name('get_attribute', None), [Name('__iterator__', None), Str('next')], None, None, None),
[
Call(
Name('JSArray', None),
None,
None,
None,
None
),
Call(
Name('JSObject', None),
None,
None,
None,
None
),
],
None,
None,
None
)
)
)
tryexcept_body = [
Assign(
[Name('var %s' % node.target.id, None)],
Call(
Call(Name('get_attribute', None), [Name('__iterator__', None), Str('next')], None, None, None),
[
Call(
Name('JSArray', None),
None,
None,
None,
None
),
Call(
Name('JSObject', None),
None,
None,
None,
None
),
],
None,
None,
None
)
),
While(Name('true', None), node.body, None)
]
yield TryExcept(
tryexcept_body,
[], # FIXME: there is no handlers any exception
# will throw us out the the for loop
# XXX: at least at console.log
[],
)