author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Wed, 11 Oct 2017 11:54:55 +0300 | |
changeset 1867 | 418777c1fbc7 |
parent 1865 | 0bd5b3099144 |
child 1870 | 4d070115b552 |
permissions | -rw-r--r-- |
371 | 1 |
# Copyright 2006 James Tauber and contributors |
2 |
# |
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 |
# you may not use this file except in compliance with the License. |
|
5 |
# You may obtain a copy of the License at |
|
6 |
# |
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
8 |
# |
|
9 |
# Unless required by applicable law or agreed to in writing, software |
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 |
# See the License for the specific language governing permissions and |
|
13 |
# limitations under the License. |
|
14 |
||
15 |
||
16 |
# iteration from Bob Ippolito's Iteration in JavaScript |
|
17 |
||
18 |
from __pyjamas__ import JS |
|
19 |
||
20 |
# must declare import _before_ importing sys |
|
21 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
22 |
|
371 | 23 |
def import_module(path, parent_module, module_name, dynamic=1, async=False): |
24 |
||
25 |
JS(""" |
|
26 |
var cache_file; |
|
27 |
||
28 |
if (module_name == "sys" || module_name == 'pyjslib') |
|
29 |
{ |
|
30 |
/*module_load_request[module_name] = 1;*/ |
|
31 |
return; |
|
32 |
} |
|
33 |
||
34 |
if (path == null) |
|
35 |
{ |
|
36 |
path = './'; |
|
37 |
} |
|
38 |
||
39 |
var override_name = sys.platform + "." + module_name; |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
40 |
if (((sys.overrides != null) && |
371 | 41 |
(sys.overrides.has_key(override_name)))) |
42 |
{ |
|
43 |
cache_file = sys.overrides.__getitem__(override_name) ; |
|
44 |
} |
|
45 |
else |
|
46 |
{ |
|
47 |
cache_file = module_name ; |
|
48 |
} |
|
49 |
||
50 |
cache_file = (path + cache_file + '.cache.js' ) ; |
|
51 |
||
52 |
//alert("cache " + cache_file + " " + module_name + " " + parent_module); |
|
53 |
||
54 |
/* already loaded? */ |
|
55 |
if (module_load_request[module_name]) |
|
56 |
{ |
|
57 |
if (module_load_request[module_name] >= 3 && parent_module != null) |
|
58 |
{ |
|
59 |
//onload_fn = parent_module + '.' + module_name + ' = ' + module_name + ';'; |
|
60 |
//pyjs_eval(onload_fn); /* set up the parent-module namespace */ |
|
61 |
} |
|
62 |
return; |
|
63 |
} |
|
64 |
if (typeof (module_load_request[module_name]) == 'undefined') |
|
65 |
{ |
|
66 |
module_load_request[module_name] = 1; |
|
67 |
} |
|
68 |
||
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
69 |
/* following a load, this first executes the script |
371 | 70 |
* "preparation" function MODULENAME_loaded_fn() |
71 |
* and then sets up the loaded module in the namespace |
|
72 |
* of the parent. |
|
73 |
*/ |
|
74 |
||
75 |
onload_fn = ''; // module_name + "_loaded_fn();" |
|
76 |
||
77 |
if (parent_module != null) |
|
78 |
{ |
|
79 |
//onload_fn += parent_module + '.' + module_name + ' = ' + module_name + ';'; |
|
80 |
/*pmod = parent_module + '.' + module_name; |
|
81 |
onload_fn += 'alert("' + pmod + '"+' + pmod+');';*/ |
|
82 |
} |
|
83 |
||
84 |
||
85 |
if (dynamic) |
|
86 |
{ |
|
87 |
/* this one tacks the script onto the end of the DOM |
|
88 |
*/ |
|
89 |
||
90 |
pyjs_load_script(cache_file, onload_fn, async); |
|
91 |
||
92 |
/* this one actually RUNS the script (eval) into the page. |
|
93 |
my feeling is that this would be better for non-async |
|
94 |
but i can't get it to work entirely yet. |
|
95 |
*/ |
|
96 |
/*pyjs_ajax_eval(cache_file, onload_fn, async);*/ |
|
97 |
} |
|
98 |
else |
|
99 |
{ |
|
100 |
if (module_name != "pyjslib" && |
|
101 |
module_name != "sys") |
|
102 |
pyjs_eval(onload_fn); |
|
103 |
} |
|
104 |
||
105 |
""") |
|
106 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
107 |
|
371 | 108 |
JS(""" |
109 |
function import_wait(proceed_fn, parent_mod, dynamic) { |
|
110 |
||
111 |
var data = ''; |
|
112 |
var element = $doc.createElement("div"); |
|
113 |
$doc.body.appendChild(element); |
|
114 |
function write_dom(txt) { |
|
115 |
element.innerHTML = txt + '<br />'; |
|
116 |
} |
|
117 |
||
118 |
var timeoutperiod = 1; |
|
119 |
if (dynamic) |
|
120 |
var timeoutperiod = 1; |
|
121 |
||
122 |
var wait = function() { |
|
123 |
||
124 |
var status = ''; |
|
125 |
for (l in module_load_request) |
|
126 |
{ |
|
127 |
var m = module_load_request[l]; |
|
128 |
if (l == "sys" || l == 'pyjslib') |
|
129 |
continue; |
|
130 |
status += l + m + " "; |
|
131 |
} |
|
132 |
||
133 |
//write_dom( " import wait " + wait_count + " " + status + " parent_mod " + parent_mod); |
|
134 |
wait_count += 1; |
|
135 |
||
136 |
if (status == '') |
|
137 |
{ |
|
138 |
setTimeout(wait, timeoutperiod); |
|
139 |
return; |
|
140 |
} |
|
141 |
||
142 |
for (l in module_load_request) |
|
143 |
{ |
|
144 |
var m = module_load_request[l]; |
|
145 |
if (l == "sys" || l == 'pyjslib') |
|
146 |
{ |
|
147 |
module_load_request[l] = 4; |
|
148 |
continue; |
|
149 |
} |
|
150 |
if ((parent_mod != null) && (l == parent_mod)) |
|
151 |
{ |
|
152 |
if (m == 1) |
|
153 |
{ |
|
154 |
setTimeout(wait, timeoutperiod); |
|
155 |
return; |
|
156 |
} |
|
157 |
if (m == 2) |
|
158 |
{ |
|
159 |
/* cheat and move app on to next stage */ |
|
160 |
module_load_request[l] = 3; |
|
161 |
} |
|
162 |
} |
|
163 |
if (m == 1 || m == 2) |
|
164 |
{ |
|
165 |
setTimeout(wait, timeoutperiod); |
|
166 |
return; |
|
167 |
} |
|
168 |
if (m == 3) |
|
169 |
{ |
|
170 |
//alert("waited for module " + l + ": loaded"); |
|
171 |
module_load_request[l] = 4; |
|
172 |
mod_fn = modules[l]; |
|
173 |
} |
|
174 |
} |
|
175 |
//alert("module wait done"); |
|
176 |
||
177 |
if (proceed_fn.importDone) |
|
178 |
proceed_fn.importDone(proceed_fn); |
|
179 |
else |
|
180 |
proceed_fn(); |
|
181 |
} |
|
182 |
||
183 |
wait(); |
|
184 |
} |
|
185 |
""") |
|
186 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
187 |
|
1851
1b8b5324506c
fix pylint warning '(old-style-class) Old-style class defined.'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset
|
188 |
# pylint: disable=C1001 |
371 | 189 |
class Object: |
190 |
pass |
|
191 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
192 |
|
371 | 193 |
object = Object |
194 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
195 |
|
371 | 196 |
class Modload: |
197 |
||
198 |
def __init__(self, path, app_modlist, app_imported_fn, dynamic, |
|
199 |
parent_mod): |
|
200 |
self.app_modlist = app_modlist |
|
201 |
self.app_imported_fn = app_imported_fn |
|
202 |
self.path = path |
|
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
203 |
self.idx = 0 |
371 | 204 |
self.dynamic = dynamic |
205 |
self.parent_mod = parent_mod |
|
206 |
||
207 |
def next(self): |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
208 |
|
371 | 209 |
for i in range(len(self.app_modlist[self.idx])): |
210 |
app = self.app_modlist[self.idx][i] |
|
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
211 |
import_module(self.path, self.parent_mod, app, self.dynamic, True) |
371 | 212 |
self.idx += 1 |
213 |
||
214 |
if self.idx >= len(self.app_modlist): |
|
215 |
import_wait(self.app_imported_fn, self.parent_mod, self.dynamic) |
|
216 |
else: |
|
217 |
import_wait(getattr(self, "next"), self.parent_mod, self.dynamic) |
|
218 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
219 |
|
371 | 220 |
def get_module(module_name): |
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1841
diff
changeset
|
221 |
_ev = "__mod = %s;" % module_name |
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1841
diff
changeset
|
222 |
JS("pyjs_eval(_ev);") |
371 | 223 |
return __mod |
224 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
225 |
|
371 | 226 |
def preload_app_modules(path, app_modnames, app_imported_fn, dynamic, |
227 |
parent_mod=None): |
|
228 |
||
229 |
loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod) |
|
230 |
loader.next() |
|
231 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
232 |
|
1783
3311eea28d56
clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset
|
233 |
# as comment on line 20 says |
3311eea28d56
clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset
|
234 |
# import sys should be below |
1859
1df77c809257
fix pylint warning '(wrong-import-position) Import "import X" should be placed at the top of the module'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1854
diff
changeset
|
235 |
import sys # noqa # pylint: disable=wrong-import-order,unused-import,wrong-import-position |
371 | 236 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
237 |
|
371 | 238 |
class BaseException: |
239 |
||
240 |
name = "BaseException" |
|
241 |
||
242 |
def __init__(self, *args): |
|
243 |
self.args = args |
|
244 |
||
245 |
def __str__(self): |
|
246 |
if len(self.args) is 0: |
|
247 |
return '' |
|
248 |
elif len(self.args) is 1: |
|
249 |
return repr(self.args[0]) |
|
250 |
return repr(self.args) |
|
251 |
||
252 |
def toString(self): |
|
253 |
return str(self) |
|
254 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
255 |
|
371 | 256 |
class Exception(BaseException): |
257 |
name = "Exception" |
|
258 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
259 |
|
371 | 260 |
class TypeError(BaseException): |
261 |
name = "TypeError" |
|
262 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
263 |
|
371 | 264 |
class StandardError(Exception): |
265 |
name = "StandardError" |
|
266 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
267 |
|
371 | 268 |
class LookupError(StandardError): |
269 |
name = "LookupError" |
|
270 |
||
271 |
def toString(self): |
|
272 |
return self.name + ": " + self.args[0] |
|
273 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
274 |
|
371 | 275 |
class KeyError(LookupError): |
276 |
name = "KeyError" |
|
277 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
278 |
|
371 | 279 |
class AttributeError(StandardError): |
280 |
name = "AttributeError" |
|
281 |
||
282 |
def toString(self): |
|
283 |
return "AttributeError: %s of %s" % (self.args[1], self.args[0]) |
|
284 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
285 |
|
371 | 286 |
JS(""" |
287 |
pyjslib.StopIteration = function () { }; |
|
288 |
pyjslib.StopIteration.prototype = new Error(); |
|
289 |
pyjslib.StopIteration.name = 'StopIteration'; |
|
290 |
pyjslib.StopIteration.message = 'StopIteration'; |
|
291 |
||
292 |
pyjslib.String_find = function(sub, start, end) { |
|
293 |
var pos=this.indexOf(sub, start); |
|
294 |
if (pyjslib.isUndefined(end)) return pos; |
|
295 |
||
296 |
if (pos + sub.length>end) return -1; |
|
297 |
return pos; |
|
298 |
} |
|
299 |
||
300 |
pyjslib.String_join = function(data) { |
|
301 |
var text=""; |
|
302 |
||
303 |
if (pyjslib.isArray(data)) { |
|
304 |
return data.join(this); |
|
305 |
} |
|
306 |
else if (pyjslib.isIteratable(data)) { |
|
307 |
var iter=data.__iter__(); |
|
308 |
try { |
|
309 |
text+=iter.next(); |
|
310 |
while (true) { |
|
311 |
var item=iter.next(); |
|
312 |
text+=this + item; |
|
313 |
} |
|
314 |
} |
|
315 |
catch (e) { |
|
316 |
if (e != pyjslib.StopIteration) throw e; |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
return text; |
|
321 |
} |
|
322 |
||
323 |
pyjslib.String_isdigit = function() { |
|
324 |
return (this.match(/^\d+$/g) != null); |
|
325 |
} |
|
326 |
||
327 |
pyjslib.String_replace = function(old, replace, count) { |
|
328 |
var do_max=false; |
|
329 |
var start=0; |
|
330 |
var new_str=""; |
|
331 |
var pos=0; |
|
332 |
||
333 |
if (!pyjslib.isString(old)) return this.__replace(old, replace); |
|
334 |
if (!pyjslib.isUndefined(count)) do_max=true; |
|
335 |
||
336 |
while (start<this.length) { |
|
337 |
if (do_max && !count--) break; |
|
338 |
||
339 |
pos=this.indexOf(old, start); |
|
340 |
if (pos<0) break; |
|
341 |
||
342 |
new_str+=this.substring(start, pos) + replace; |
|
343 |
start=pos+old.length; |
|
344 |
} |
|
345 |
if (start<this.length) new_str+=this.substring(start); |
|
346 |
||
347 |
return new_str; |
|
348 |
} |
|
349 |
||
350 |
pyjslib.String_split = function(sep, maxsplit) { |
|
351 |
var items=new pyjslib.List(); |
|
352 |
var do_max=false; |
|
353 |
var subject=this; |
|
354 |
var start=0; |
|
355 |
var pos=0; |
|
356 |
||
357 |
if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) { |
|
358 |
sep=" "; |
|
359 |
subject=subject.strip(); |
|
360 |
subject=subject.replace(/\s+/g, sep); |
|
361 |
} |
|
362 |
else if (!pyjslib.isUndefined(maxsplit)) do_max=true; |
|
363 |
||
364 |
if (subject.length == 0) { |
|
365 |
return items; |
|
366 |
} |
|
367 |
||
368 |
while (start<subject.length) { |
|
369 |
if (do_max && !maxsplit--) break; |
|
370 |
||
371 |
pos=subject.indexOf(sep, start); |
|
372 |
if (pos<0) break; |
|
373 |
||
374 |
items.append(subject.substring(start, pos)); |
|
375 |
start=pos+sep.length; |
|
376 |
} |
|
377 |
if (start<=subject.length) items.append(subject.substring(start)); |
|
378 |
||
379 |
return items; |
|
380 |
} |
|
381 |
||
382 |
pyjslib.String___iter__ = function() { |
|
383 |
var i = 0; |
|
384 |
var s = this; |
|
385 |
return { |
|
386 |
'next': function() { |
|
387 |
if (i >= s.length) { |
|
388 |
throw pyjslib.StopIteration; |
|
389 |
} |
|
390 |
return s.substring(i++, i, 1); |
|
391 |
}, |
|
392 |
'__iter__': function() { |
|
393 |
return this; |
|
394 |
} |
|
395 |
}; |
|
396 |
} |
|
397 |
||
398 |
pyjslib.String_strip = function(chars) { |
|
399 |
return this.lstrip(chars).rstrip(chars); |
|
400 |
} |
|
401 |
||
402 |
pyjslib.String_lstrip = function(chars) { |
|
403 |
if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, ""); |
|
404 |
||
405 |
return this.replace(new RegExp("^[" + chars + "]+"), ""); |
|
406 |
} |
|
407 |
||
408 |
pyjslib.String_rstrip = function(chars) { |
|
409 |
if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, ""); |
|
410 |
||
411 |
return this.replace(new RegExp("[" + chars + "]+$"), ""); |
|
412 |
} |
|
413 |
||
414 |
pyjslib.String_startswith = function(prefix, start) { |
|
415 |
if (pyjslib.isUndefined(start)) start = 0; |
|
416 |
||
417 |
if (this.substring(start, prefix.length) == prefix) return true; |
|
418 |
return false; |
|
419 |
} |
|
420 |
||
421 |
pyjslib.abs = Math.abs; |
|
422 |
||
423 |
""") |
|
424 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
425 |
|
371 | 426 |
class Class: |
427 |
def __init__(self, name): |
|
428 |
self.name = name |
|
429 |
||
430 |
def __str___(self): |
|
431 |
return self.name |
|
432 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
433 |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
434 |
def eq(a, b): |
371 | 435 |
JS(""" |
436 |
if (pyjslib.hasattr(a, "__cmp__")) { |
|
437 |
return a.__cmp__(b) == 0; |
|
438 |
} else if (pyjslib.hasattr(b, "__cmp__")) { |
|
439 |
return b.__cmp__(a) == 0; |
|
440 |
} |
|
441 |
return a == b; |
|
442 |
""") |
|
443 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
444 |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
445 |
def cmp(a, b): |
371 | 446 |
if hasattr(a, "__cmp__"): |
447 |
return a.__cmp__(b) |
|
448 |
elif hasattr(b, "__cmp__"): |
|
449 |
return -b.__cmp__(a) |
|
450 |
if a > b: |
|
451 |
return 1 |
|
452 |
elif b > a: |
|
453 |
return -1 |
|
454 |
else: |
|
455 |
return 0 |
|
456 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
457 |
|
371 | 458 |
def bool(v): |
459 |
# this needs to stay in native code without any dependencies here, |
|
460 |
# because this is used by if and while, we need to prevent |
|
461 |
# recursion |
|
462 |
JS(""" |
|
463 |
if (!v) return false; |
|
464 |
switch(typeof v){ |
|
465 |
case 'boolean': |
|
466 |
return v; |
|
467 |
case 'object': |
|
468 |
if (v.__nonzero__){ |
|
469 |
return v.__nonzero__(); |
|
470 |
}else if (v.__len__){ |
|
471 |
return v.__len__()>0; |
|
472 |
} |
|
473 |
return true; |
|
474 |
} |
|
475 |
return Boolean(v); |
|
476 |
""") |
|
477 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
478 |
|
371 | 479 |
class List: |
480 |
def __init__(self, data=None): |
|
481 |
JS(""" |
|
482 |
this.l = []; |
|
483 |
this.extend(data); |
|
484 |
""") |
|
485 |
||
486 |
def append(self, item): |
|
487 |
JS(""" this.l[this.l.length] = item;""") |
|
488 |
||
489 |
def extend(self, data): |
|
490 |
JS(""" |
|
491 |
if (pyjslib.isArray(data)) { |
|
492 |
n = this.l.length; |
|
493 |
for (var i=0; i < data.length; i++) { |
|
494 |
this.l[n+i]=data[i]; |
|
495 |
} |
|
496 |
} |
|
497 |
else if (pyjslib.isIteratable(data)) { |
|
498 |
var iter=data.__iter__(); |
|
499 |
var i=this.l.length; |
|
500 |
try { |
|
501 |
while (true) { |
|
502 |
var item=iter.next(); |
|
503 |
this.l[i++]=item; |
|
504 |
} |
|
505 |
} |
|
506 |
catch (e) { |
|
507 |
if (e != pyjslib.StopIteration) throw e; |
|
508 |
} |
|
509 |
} |
|
510 |
""") |
|
511 |
||
512 |
def remove(self, value): |
|
513 |
JS(""" |
|
514 |
var index=this.index(value); |
|
515 |
if (index<0) return false; |
|
516 |
this.l.splice(index, 1); |
|
517 |
return true; |
|
518 |
""") |
|
519 |
||
520 |
def index(self, value, start=0): |
|
521 |
JS(""" |
|
522 |
var length=this.l.length; |
|
523 |
for (var i=start; i<length; i++) { |
|
524 |
if (this.l[i]==value) { |
|
525 |
return i; |
|
526 |
} |
|
527 |
} |
|
528 |
return -1; |
|
529 |
""") |
|
530 |
||
531 |
def insert(self, index, value): |
|
532 |
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
|
533 |
||
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
534 |
def pop(self, index=-1): |
371 | 535 |
JS(""" |
536 |
if (index<0) index = this.l.length + index; |
|
537 |
var a = this.l[index]; |
|
538 |
this.l.splice(index, 1); |
|
539 |
return a; |
|
540 |
""") |
|
541 |
||
542 |
def __cmp__(self, l): |
|
543 |
if not isinstance(l, List): |
|
544 |
return -1 |
|
545 |
ll = len(self) - len(l) |
|
546 |
if ll != 0: |
|
547 |
return ll |
|
548 |
for x in range(len(l)): |
|
549 |
ll = cmp(self.__getitem__(x), l[x]) |
|
550 |
if ll != 0: |
|
551 |
return ll |
|
552 |
return 0 |
|
553 |
||
554 |
def slice(self, lower, upper): |
|
555 |
JS(""" |
|
556 |
if (upper==null) return pyjslib.List(this.l.slice(lower)); |
|
557 |
return pyjslib.List(this.l.slice(lower, upper)); |
|
558 |
""") |
|
559 |
||
560 |
def __getitem__(self, index): |
|
561 |
JS(""" |
|
562 |
if (index<0) index = this.l.length + index; |
|
563 |
return this.l[index]; |
|
564 |
""") |
|
565 |
||
566 |
def __setitem__(self, index, value): |
|
567 |
JS(""" this.l[index]=value;""") |
|
568 |
||
569 |
def __delitem__(self, index): |
|
570 |
JS(""" this.l.splice(index, 1);""") |
|
571 |
||
572 |
def __len__(self): |
|
573 |
JS(""" return this.l.length;""") |
|
574 |
||
575 |
def __contains__(self, value): |
|
576 |
return self.index(value) >= 0 |
|
577 |
||
578 |
def __iter__(self): |
|
579 |
JS(""" |
|
580 |
var i = 0; |
|
581 |
var l = this.l; |
|
582 |
return { |
|
583 |
'next': function() { |
|
584 |
if (i >= l.length) { |
|
585 |
throw pyjslib.StopIteration; |
|
586 |
} |
|
587 |
return l[i++]; |
|
588 |
}, |
|
589 |
'__iter__': function() { |
|
590 |
return this; |
|
591 |
} |
|
592 |
}; |
|
593 |
""") |
|
594 |
||
595 |
def reverse(self): |
|
596 |
JS(""" this.l.reverse();""") |
|
597 |
||
598 |
def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
|
599 |
if not compareFunc: |
|
600 |
compareFunc = cmp |
|
601 |
if keyFunc and reverse: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
602 |
def thisSort1(a, b): |
371 | 603 |
return -compareFunc(keyFunc(a), keyFunc(b)) |
604 |
self.l.sort(thisSort1) |
|
605 |
elif keyFunc: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
606 |
def thisSort2(a, b): |
371 | 607 |
return compareFunc(keyFunc(a), keyFunc(b)) |
608 |
self.l.sort(thisSort2) |
|
609 |
elif reverse: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
610 |
def thisSort3(a, b): |
371 | 611 |
return -compareFunc(a, b) |
612 |
self.l.sort(thisSort3) |
|
613 |
else: |
|
614 |
self.l.sort(compareFunc) |
|
615 |
||
616 |
def getArray(self): |
|
617 |
""" |
|
618 |
Access the javascript Array that is used internally by this list |
|
619 |
""" |
|
620 |
return self.l |
|
621 |
||
622 |
def __str__(self): |
|
623 |
return repr(self) |
|
624 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
625 |
|
371 | 626 |
list = List |
627 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
628 |
|
371 | 629 |
class Tuple: |
630 |
def __init__(self, data=None): |
|
631 |
JS(""" |
|
632 |
this.l = []; |
|
633 |
this.extend(data); |
|
634 |
""") |
|
635 |
||
636 |
def append(self, item): |
|
637 |
JS(""" this.l[this.l.length] = item;""") |
|
638 |
||
639 |
def extend(self, data): |
|
640 |
JS(""" |
|
641 |
if (pyjslib.isArray(data)) { |
|
642 |
n = this.l.length; |
|
643 |
for (var i=0; i < data.length; i++) { |
|
644 |
this.l[n+i]=data[i]; |
|
645 |
} |
|
646 |
} |
|
647 |
else if (pyjslib.isIteratable(data)) { |
|
648 |
var iter=data.__iter__(); |
|
649 |
var i=this.l.length; |
|
650 |
try { |
|
651 |
while (true) { |
|
652 |
var item=iter.next(); |
|
653 |
this.l[i++]=item; |
|
654 |
} |
|
655 |
} |
|
656 |
catch (e) { |
|
657 |
if (e != pyjslib.StopIteration) throw e; |
|
658 |
} |
|
659 |
} |
|
660 |
""") |
|
661 |
||
662 |
def remove(self, value): |
|
663 |
JS(""" |
|
664 |
var index=this.index(value); |
|
665 |
if (index<0) return false; |
|
666 |
this.l.splice(index, 1); |
|
667 |
return true; |
|
668 |
""") |
|
669 |
||
670 |
def index(self, value, start=0): |
|
671 |
JS(""" |
|
672 |
var length=this.l.length; |
|
673 |
for (var i=start; i<length; i++) { |
|
674 |
if (this.l[i]==value) { |
|
675 |
return i; |
|
676 |
} |
|
677 |
} |
|
678 |
return -1; |
|
679 |
""") |
|
680 |
||
681 |
def insert(self, index, value): |
|
682 |
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
|
683 |
||
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
684 |
def pop(self, index=-1): |
371 | 685 |
JS(""" |
686 |
if (index<0) index = this.l.length + index; |
|
687 |
var a = this.l[index]; |
|
688 |
this.l.splice(index, 1); |
|
689 |
return a; |
|
690 |
""") |
|
691 |
||
692 |
def __cmp__(self, l): |
|
693 |
if not isinstance(l, Tuple): |
|
694 |
return -1 |
|
695 |
ll = len(self) - len(l) |
|
696 |
if ll != 0: |
|
697 |
return ll |
|
698 |
for x in range(len(l)): |
|
699 |
ll = cmp(self.__getitem__(x), l[x]) |
|
700 |
if ll != 0: |
|
701 |
return ll |
|
702 |
return 0 |
|
703 |
||
704 |
def slice(self, lower, upper): |
|
705 |
JS(""" |
|
706 |
if (upper==null) return pyjslib.Tuple(this.l.slice(lower)); |
|
707 |
return pyjslib.Tuple(this.l.slice(lower, upper)); |
|
708 |
""") |
|
709 |
||
710 |
def __getitem__(self, index): |
|
711 |
JS(""" |
|
712 |
if (index<0) index = this.l.length + index; |
|
713 |
return this.l[index]; |
|
714 |
""") |
|
715 |
||
716 |
def __setitem__(self, index, value): |
|
717 |
JS(""" this.l[index]=value;""") |
|
718 |
||
719 |
def __delitem__(self, index): |
|
720 |
JS(""" this.l.splice(index, 1);""") |
|
721 |
||
722 |
def __len__(self): |
|
723 |
JS(""" return this.l.length;""") |
|
724 |
||
725 |
def __contains__(self, value): |
|
726 |
return self.index(value) >= 0 |
|
727 |
||
728 |
def __iter__(self): |
|
729 |
JS(""" |
|
730 |
var i = 0; |
|
731 |
var l = this.l; |
|
732 |
return { |
|
733 |
'next': function() { |
|
734 |
if (i >= l.length) { |
|
735 |
throw pyjslib.StopIteration; |
|
736 |
} |
|
737 |
return l[i++]; |
|
738 |
}, |
|
739 |
'__iter__': function() { |
|
740 |
return this; |
|
741 |
} |
|
742 |
}; |
|
743 |
""") |
|
744 |
||
745 |
def reverse(self): |
|
746 |
JS(""" this.l.reverse();""") |
|
747 |
||
748 |
def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
|
749 |
if not compareFunc: |
|
750 |
compareFunc = cmp |
|
751 |
if keyFunc and reverse: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
752 |
def thisSort1(a, b): |
371 | 753 |
return -compareFunc(keyFunc(a), keyFunc(b)) |
754 |
self.l.sort(thisSort1) |
|
755 |
elif keyFunc: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
756 |
def thisSort2(a, b): |
371 | 757 |
return compareFunc(keyFunc(a), keyFunc(b)) |
758 |
self.l.sort(thisSort2) |
|
759 |
elif reverse: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
760 |
def thisSort3(a, b): |
371 | 761 |
return -compareFunc(a, b) |
762 |
self.l.sort(thisSort3) |
|
763 |
else: |
|
764 |
self.l.sort(compareFunc) |
|
765 |
||
766 |
def getArray(self): |
|
767 |
""" |
|
768 |
Access the javascript Array that is used internally by this list |
|
769 |
""" |
|
770 |
return self.l |
|
771 |
||
772 |
def __str__(self): |
|
773 |
return repr(self) |
|
774 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
775 |
|
371 | 776 |
tuple = Tuple |
777 |
||
778 |
||
779 |
class Dict: |
|
780 |
def __init__(self, data=None): |
|
781 |
JS(""" |
|
782 |
this.d = {}; |
|
783 |
||
784 |
if (pyjslib.isArray(data)) { |
|
785 |
for (var i in data) { |
|
786 |
var item=data[i]; |
|
787 |
this.__setitem__(item[0], item[1]); |
|
788 |
//var sKey=pyjslib.hash(item[0]); |
|
789 |
//this.d[sKey]=item[1]; |
|
790 |
} |
|
791 |
} |
|
792 |
else if (pyjslib.isIteratable(data)) { |
|
793 |
var iter=data.__iter__(); |
|
794 |
try { |
|
795 |
while (true) { |
|
796 |
var item=iter.next(); |
|
797 |
this.__setitem__(item.__getitem__(0), item.__getitem__(1)); |
|
798 |
} |
|
799 |
} |
|
800 |
catch (e) { |
|
801 |
if (e != pyjslib.StopIteration) throw e; |
|
802 |
} |
|
803 |
} |
|
804 |
else if (pyjslib.isObject(data)) { |
|
805 |
for (var key in data) { |
|
806 |
this.__setitem__(key, data[key]); |
|
807 |
} |
|
808 |
} |
|
809 |
""") |
|
810 |
||
811 |
def __setitem__(self, key, value): |
|
812 |
JS(""" |
|
813 |
var sKey = pyjslib.hash(key); |
|
814 |
this.d[sKey]=[key, value]; |
|
815 |
""") |
|
816 |
||
817 |
def __getitem__(self, key): |
|
818 |
JS(""" |
|
819 |
var sKey = pyjslib.hash(key); |
|
820 |
var value=this.d[sKey]; |
|
821 |
if (pyjslib.isUndefined(value)){ |
|
822 |
throw pyjslib.KeyError(key); |
|
823 |
} |
|
824 |
return value[1]; |
|
825 |
""") |
|
826 |
||
827 |
def __nonzero__(self): |
|
828 |
JS(""" |
|
829 |
for (var i in this.d){ |
|
830 |
return true; |
|
831 |
} |
|
832 |
return false; |
|
833 |
""") |
|
834 |
||
835 |
def __len__(self): |
|
836 |
JS(""" |
|
837 |
var size=0; |
|
838 |
for (var i in this.d) size++; |
|
839 |
return size; |
|
840 |
""") |
|
841 |
||
842 |
def has_key(self, key): |
|
843 |
return self.__contains__(key) |
|
844 |
||
845 |
def __delitem__(self, key): |
|
846 |
JS(""" |
|
847 |
var sKey = pyjslib.hash(key); |
|
848 |
delete this.d[sKey]; |
|
849 |
""") |
|
850 |
||
851 |
def __contains__(self, key): |
|
852 |
JS(""" |
|
853 |
var sKey = pyjslib.hash(key); |
|
854 |
return (pyjslib.isUndefined(this.d[sKey])) ? false : true; |
|
855 |
""") |
|
856 |
||
857 |
def keys(self): |
|
858 |
JS(""" |
|
859 |
var keys=new pyjslib.List(); |
|
860 |
for (var key in this.d) { |
|
861 |
keys.append(this.d[key][0]); |
|
862 |
} |
|
863 |
return keys; |
|
864 |
""") |
|
865 |
||
866 |
def values(self): |
|
867 |
JS(""" |
|
868 |
var values=new pyjslib.List(); |
|
869 |
for (var key in this.d) values.append(this.d[key][1]); |
|
870 |
return values; |
|
871 |
""") |
|
872 |
||
873 |
def items(self): |
|
874 |
JS(""" |
|
875 |
var items = new pyjslib.List(); |
|
876 |
for (var key in this.d) { |
|
877 |
var kv = this.d[key]; |
|
878 |
items.append(new pyjslib.List(kv)) |
|
879 |
} |
|
880 |
return items; |
|
881 |
""") |
|
882 |
||
883 |
def __iter__(self): |
|
884 |
return self.keys().__iter__() |
|
885 |
||
886 |
def iterkeys(self): |
|
887 |
return self.__iter__() |
|
888 |
||
889 |
def itervalues(self): |
|
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
890 |
return self.values().__iter__() |
371 | 891 |
|
892 |
def iteritems(self): |
|
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
893 |
return self.items().__iter__() |
371 | 894 |
|
895 |
def setdefault(self, key, default_value): |
|
1775
b45f2768fab1
clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
896 |
if key not in self: |
371 | 897 |
self[key] = default_value |
898 |
||
899 |
def get(self, key, default_=None): |
|
1775
b45f2768fab1
clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset
|
900 |
if key not in self: |
371 | 901 |
return default_ |
902 |
return self[key] |
|
903 |
||
904 |
def update(self, d): |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
905 |
for k, v in d.iteritems(): |
371 | 906 |
self[k] = v |
907 |
||
908 |
def getObject(self): |
|
909 |
""" |
|
910 |
Return the javascript Object which this class uses to store |
|
911 |
dictionary keys and values |
|
912 |
""" |
|
913 |
return self.d |
|
914 |
||
915 |
def copy(self): |
|
916 |
return Dict(self.items()) |
|
917 |
||
918 |
def __str__(self): |
|
919 |
return repr(self) |
|
920 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
921 |
|
371 | 922 |
dict = Dict |
923 |
||
924 |
# taken from mochikit: range( [start,] stop[, step] ) |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
925 |
|
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
926 |
|
371 | 927 |
def range(): |
928 |
JS(""" |
|
929 |
var start = 0; |
|
930 |
var stop = 0; |
|
931 |
var step = 1; |
|
932 |
||
933 |
if (arguments.length == 2) { |
|
934 |
start = arguments[0]; |
|
935 |
stop = arguments[1]; |
|
936 |
} |
|
937 |
else if (arguments.length == 3) { |
|
938 |
start = arguments[0]; |
|
939 |
stop = arguments[1]; |
|
940 |
step = arguments[2]; |
|
941 |
} |
|
942 |
else if (arguments.length>0) stop = arguments[0]; |
|
943 |
||
944 |
return { |
|
945 |
'next': function() { |
|
946 |
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) throw pyjslib.StopIteration; |
|
947 |
var rval = start; |
|
948 |
start += step; |
|
949 |
return rval; |
|
950 |
}, |
|
951 |
'__iter__': function() { |
|
952 |
return this; |
|
953 |
} |
|
954 |
} |
|
955 |
""") |
|
956 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
957 |
|
371 | 958 |
def slice(object, lower, upper): |
959 |
JS(""" |
|
960 |
if (pyjslib.isString(object)) { |
|
961 |
if (lower < 0) { |
|
962 |
lower = object.length + lower; |
|
963 |
} |
|
964 |
if (upper < 0) { |
|
965 |
upper = object.length + upper; |
|
966 |
} |
|
967 |
if (pyjslib.isNull(upper)) upper=object.length; |
|
968 |
return object.substring(lower, upper); |
|
969 |
} |
|
970 |
if (pyjslib.isObject(object) && object.slice) |
|
971 |
return object.slice(lower, upper); |
|
972 |
||
973 |
return null; |
|
974 |
""") |
|
975 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
976 |
|
371 | 977 |
def str(text): |
978 |
JS(""" |
|
979 |
if (pyjslib.hasattr(text,"__str__")) { |
|
980 |
return text.__str__(); |
|
981 |
} |
|
982 |
return String(text); |
|
983 |
""") |
|
984 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
985 |
|
371 | 986 |
def ord(x): |
1828
396da88d7b5c
fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
987 |
if isString(x) and len(x) is 1: |
371 | 988 |
JS(""" |
989 |
return x.charCodeAt(0); |
|
990 |
""") |
|
991 |
else: |
|
992 |
JS(""" |
|
993 |
throw pyjslib.TypeError(); |
|
994 |
""") |
|
995 |
return None |
|
996 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
997 |
|
371 | 998 |
def chr(x): |
999 |
JS(""" |
|
1000 |
return String.fromCharCode(x) |
|
1001 |
""") |
|
1002 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1003 |
|
371 | 1004 |
def is_basetype(x): |
1005 |
JS(""" |
|
1006 |
var t = typeof(x); |
|
1007 |
return t == 'boolean' || |
|
1008 |
t == 'function' || |
|
1009 |
t == 'number' || |
|
1010 |
t == 'string' || |
|
1011 |
t == 'undefined' |
|
1012 |
; |
|
1013 |
""") |
|
1014 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1015 |
|
371 | 1016 |
def get_pyjs_classtype(x): |
1017 |
JS(""" |
|
1018 |
if (pyjslib.hasattr(x, "__class__")) |
|
1019 |
if (pyjslib.hasattr(x.__class__, "__new__")) |
|
1020 |
var src = x.__class__.__name__; |
|
1021 |
return src; |
|
1022 |
return null; |
|
1023 |
""") |
|
1024 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1025 |
|
371 | 1026 |
def repr(x): |
1027 |
""" Return the string representation of 'x'. |
|
1028 |
""" |
|
1029 |
JS(""" |
|
1030 |
if (x === null) |
|
1031 |
return "null"; |
|
1032 |
||
1033 |
if (x === undefined) |
|
1034 |
return "undefined"; |
|
1035 |
||
1036 |
var t = typeof(x); |
|
1037 |
||
1038 |
//alert("repr typeof " + t + " : " + x); |
|
1039 |
||
1040 |
if (t == "boolean") |
|
1041 |
return x.toString(); |
|
1042 |
||
1043 |
if (t == "function") |
|
1044 |
return "<function " + x.toString() + ">"; |
|
1045 |
||
1046 |
if (t == "number") |
|
1047 |
return x.toString(); |
|
1048 |
||
1049 |
if (t == "string") { |
|
1050 |
if (x.indexOf("'") == -1) |
|
1051 |
return "'" + x + "'"; |
|
1052 |
if (x.indexOf('"') == -1) |
|
1053 |
return '"' + x + '"'; |
|
1054 |
var s = x.replace(new RegExp('"', "g"), '\\\\"'); |
|
1055 |
return '"' + s + '"'; |
|
1056 |
}; |
|
1057 |
||
1058 |
if (t == "undefined") |
|
1059 |
return "undefined"; |
|
1060 |
||
1061 |
// If we get here, x is an object. See if it's a Pyjamas class. |
|
1062 |
||
1063 |
if (!pyjslib.hasattr(x, "__init__")) |
|
1064 |
return "<" + x.toString() + ">"; |
|
1065 |
||
1066 |
// Handle the common Pyjamas data types. |
|
1067 |
||
1068 |
var constructor = "UNKNOWN"; |
|
1069 |
||
1070 |
constructor = pyjslib.get_pyjs_classtype(x); |
|
1071 |
||
1072 |
//alert("repr constructor: " + constructor); |
|
1073 |
||
1074 |
if (constructor == "Tuple") { |
|
1075 |
var contents = x.getArray(); |
|
1076 |
var s = "("; |
|
1077 |
for (var i=0; i < contents.length; i++) { |
|
1078 |
s += pyjslib.repr(contents[i]); |
|
1079 |
if (i < contents.length - 1) |
|
1080 |
s += ", "; |
|
1081 |
}; |
|
1082 |
s += ")" |
|
1083 |
return s; |
|
1084 |
}; |
|
1085 |
||
1086 |
if (constructor == "List") { |
|
1087 |
var contents = x.getArray(); |
|
1088 |
var s = "["; |
|
1089 |
for (var i=0; i < contents.length; i++) { |
|
1090 |
s += pyjslib.repr(contents[i]); |
|
1091 |
if (i < contents.length - 1) |
|
1092 |
s += ", "; |
|
1093 |
}; |
|
1094 |
s += "]" |
|
1095 |
return s; |
|
1096 |
}; |
|
1097 |
||
1098 |
if (constructor == "Dict") { |
|
1099 |
var keys = new Array(); |
|
1100 |
for (var key in x.d) |
|
1101 |
keys.push(key); |
|
1102 |
||
1103 |
var s = "{"; |
|
1104 |
for (var i=0; i<keys.length; i++) { |
|
1105 |
var key = keys[i] |
|
1106 |
s += pyjslib.repr(key) + ": " + pyjslib.repr(x.d[key]); |
|
1107 |
if (i < keys.length-1) |
|
1108 |
s += ", " |
|
1109 |
}; |
|
1110 |
s += "}"; |
|
1111 |
return s; |
|
1112 |
}; |
|
1113 |
||
1114 |
// If we get here, the class isn't one we know -> return the class name. |
|
1115 |
// Note that we replace underscores with dots so that the name will |
|
1116 |
// (hopefully!) look like the original Python name. |
|
1117 |
||
1118 |
//var s = constructor.replace(new RegExp('_', "g"), '.'); |
|
1119 |
return "<" + constructor + " object>"; |
|
1120 |
""") |
|
1121 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1122 |
|
371 | 1123 |
def float(text): |
1124 |
JS(""" |
|
1125 |
return parseFloat(text); |
|
1126 |
""") |
|
1127 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1128 |
|
371 | 1129 |
def int(text, radix=0): |
1130 |
JS(""" |
|
1131 |
return parseInt(text, radix); |
|
1132 |
""") |
|
1133 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1134 |
|
371 | 1135 |
def len(object): |
1136 |
JS(""" |
|
1137 |
if (object==null) return 0; |
|
1138 |
if (pyjslib.isObject(object) && object.__len__) return object.__len__(); |
|
1139 |
return object.length; |
|
1140 |
""") |
|
1141 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1142 |
|
371 | 1143 |
def isinstance(object_, classinfo): |
1144 |
if pyjslib.isUndefined(object_): |
|
1145 |
return False |
|
1146 |
if not pyjslib.isObject(object_): |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
1147 |
|
371 | 1148 |
return False |
1149 |
if _isinstance(classinfo, Tuple): |
|
1150 |
for ci in classinfo: |
|
1151 |
if isinstance(object_, ci): |
|
1152 |
return True |
|
1153 |
return False |
|
1154 |
else: |
|
1155 |
return _isinstance(object_, classinfo) |
|
1156 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1157 |
|
371 | 1158 |
def _isinstance(object_, classinfo): |
1159 |
if not pyjslib.isObject(object_): |
|
1160 |
return False |
|
1161 |
JS(""" |
|
1162 |
if (object_.__class__){ |
|
1163 |
var res = object_ instanceof classinfo.constructor; |
|
1164 |
return res; |
|
1165 |
} |
|
1166 |
return false; |
|
1167 |
""") |
|
1168 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1169 |
|
1865
0bd5b3099144
fix pylint warning "(no-value-for-parameter) No value for argument 'X' in function call"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1859
diff
changeset
|
1170 |
def getattr(obj, name, default_=None): |
371 | 1171 |
JS(""" |
1172 |
if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){ |
|
1173 |
if (pyjslib.isUndefined(default_)){ |
|
1174 |
throw pyjslib.AttributeError(obj, name); |
|
1175 |
}else{ |
|
1176 |
return default_; |
|
1177 |
} |
|
1178 |
} |
|
1179 |
if (!pyjslib.isFunction(obj[name])) return obj[name]; |
|
1180 |
var fnwrap = function() { |
|
1181 |
var args = []; |
|
1182 |
for (var i = 0; i < arguments.length; i++) { |
|
1183 |
args.push(arguments[i]); |
|
1184 |
} |
|
1185 |
return obj[name].apply(obj,args); |
|
1186 |
} |
|
1187 |
fnwrap.__name__ = name; |
|
1188 |
return fnwrap; |
|
1189 |
""") |
|
1190 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1191 |
|
371 | 1192 |
def setattr(obj, name, value): |
1193 |
JS(""" |
|
1194 |
if (!pyjslib.isObject(obj)) return null; |
|
1195 |
||
1196 |
obj[name] = value; |
|
1197 |
||
1198 |
""") |
|
1199 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1200 |
|
371 | 1201 |
def hasattr(obj, name): |
1202 |
JS(""" |
|
1203 |
if (!pyjslib.isObject(obj)) return false; |
|
1204 |
if (pyjslib.isUndefined(obj[name])) return false; |
|
1205 |
||
1206 |
return true; |
|
1207 |
""") |
|
1208 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1209 |
|
371 | 1210 |
def dir(obj): |
1211 |
JS(""" |
|
1212 |
var properties=new pyjslib.List(); |
|
1213 |
for (property in obj) properties.append(property); |
|
1214 |
return properties; |
|
1215 |
""") |
|
1216 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1217 |
|
371 | 1218 |
def filter(obj, method, sequence=None): |
1219 |
# object context is LOST when a method is passed, hence object must be passed separately |
|
1220 |
# to emulate python behaviour, should generate this code inline rather than as a function call |
|
1221 |
items = [] |
|
1222 |
if sequence is None: |
|
1223 |
sequence = method |
|
1224 |
method = obj |
|
1225 |
||
1226 |
for item in sequence: |
|
1227 |
if method(item): |
|
1228 |
items.append(item) |
|
1229 |
else: |
|
1230 |
for item in sequence: |
|
1231 |
if method.call(obj, item): |
|
1232 |
items.append(item) |
|
1233 |
||
1234 |
return items |
|
1235 |
||
1236 |
||
1237 |
def map(obj, method, sequence=None): |
|
1238 |
items = [] |
|
1239 |
||
1240 |
if sequence is None: |
|
1241 |
sequence = method |
|
1242 |
method = obj |
|
1243 |
||
1244 |
for item in sequence: |
|
1245 |
items.append(method(item)) |
|
1246 |
else: |
|
1247 |
for item in sequence: |
|
1248 |
items.append(method.call(obj, item)) |
|
1249 |
||
1250 |
return items |
|
1251 |
||
1252 |
||
1253 |
def enumerate(sequence): |
|
1254 |
enumeration = [] |
|
1255 |
nextIndex = 0 |
|
1256 |
for item in sequence: |
|
1257 |
enumeration.append([nextIndex, item]) |
|
1258 |
nextIndex = nextIndex + 1 |
|
1259 |
return enumeration |
|
1260 |
||
1261 |
||
1262 |
def min(*sequence): |
|
1263 |
minValue = None |
|
1264 |
for item in sequence: |
|
1265 |
if minValue is None: |
|
1266 |
minValue = item |
|
1267 |
elif item < minValue: |
|
1268 |
minValue = item |
|
1269 |
return minValue |
|
1270 |
||
1271 |
||
1272 |
def max(*sequence): |
|
1273 |
maxValue = None |
|
1274 |
for item in sequence: |
|
1275 |
if maxValue is None: |
|
1276 |
maxValue = item |
|
1277 |
elif item > maxValue: |
|
1278 |
maxValue = item |
|
1279 |
return maxValue |
|
1280 |
||
1281 |
||
1282 |
next_hash_id = 0 |
|
1283 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1284 |
|
371 | 1285 |
def hash(obj): |
1286 |
JS(""" |
|
1287 |
if (obj == null) return null; |
|
1288 |
||
1289 |
if (obj.$H) return obj.$H; |
|
1290 |
if (obj.__hash__) return obj.__hash__(); |
|
1291 |
if (obj.constructor == String || obj.constructor == Number || obj.constructor == Date) return obj; |
|
1292 |
||
1293 |
obj.$H = ++pyjslib.next_hash_id; |
|
1294 |
return obj.$H; |
|
1295 |
""") |
|
1296 |
||
1297 |
||
1298 |
# type functions from Douglas Crockford's Remedial Javascript: http://www.crockford.com/javascript/remedial.html |
|
1299 |
def isObject(a): |
|
1300 |
JS(""" |
|
1301 |
return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a); |
|
1302 |
""") |
|
1303 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1304 |
|
371 | 1305 |
def isFunction(a): |
1306 |
JS(""" |
|
1307 |
return typeof a == 'function'; |
|
1308 |
""") |
|
1309 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1310 |
|
371 | 1311 |
def isString(a): |
1312 |
JS(""" |
|
1313 |
return typeof a == 'string'; |
|
1314 |
""") |
|
1315 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1316 |
|
371 | 1317 |
def isNull(a): |
1318 |
JS(""" |
|
1319 |
return typeof a == 'object' && !a; |
|
1320 |
""") |
|
1321 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1322 |
|
371 | 1323 |
def isArray(a): |
1324 |
JS(""" |
|
1325 |
return pyjslib.isObject(a) && a.constructor == Array; |
|
1326 |
""") |
|
1327 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1328 |
|
371 | 1329 |
def isUndefined(a): |
1330 |
JS(""" |
|
1331 |
return typeof a == 'undefined'; |
|
1332 |
""") |
|
1333 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1334 |
|
371 | 1335 |
def isIteratable(a): |
1336 |
JS(""" |
|
1337 |
return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__); |
|
1338 |
""") |
|
1339 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1340 |
|
371 | 1341 |
def isNumber(a): |
1342 |
JS(""" |
|
1343 |
return typeof a == 'number' && isFinite(a); |
|
1344 |
""") |
|
1345 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1346 |
|
371 | 1347 |
def toJSObjects(x): |
1348 |
""" |
|
1349 |
Convert the pyjs pythonic List and Dict objects into javascript Object and Array |
|
1350 |
objects, recursively. |
|
1351 |
""" |
|
1352 |
if isArray(x): |
|
1353 |
JS(""" |
|
1354 |
var result = []; |
|
1355 |
for(var k=0; k < x.length; k++) { |
|
1356 |
var v = x[k]; |
|
1357 |
var tv = pyjslib.toJSObjects(v); |
|
1358 |
result.push(tv); |
|
1359 |
} |
|
1360 |
return result; |
|
1361 |
""") |
|
1362 |
if isObject(x): |
|
1363 |
if isinstance(x, Dict): |
|
1364 |
JS(""" |
|
1365 |
var o = x.getObject(); |
|
1366 |
var result = {}; |
|
1367 |
for (var i in o) { |
|
1368 |
result[o[i][0].toString()] = o[i][1]; |
|
1369 |
} |
|
1370 |
return pyjslib.toJSObjects(result) |
|
1371 |
""") |
|
1372 |
elif isinstance(x, List): |
|
1373 |
return toJSObjects(x.l) |
|
1374 |
elif hasattr(x, '__class__'): |
|
1375 |
# we do not have a special implementation for custom |
|
1376 |
# classes, just pass it on |
|
1377 |
return x |
|
1378 |
if isObject(x): |
|
1379 |
JS(""" |
|
1380 |
var result = {}; |
|
1381 |
for(var k in x) { |
|
1382 |
var v = x[k]; |
|
1383 |
var tv = pyjslib.toJSObjects(v) |
|
1384 |
result[k] = tv; |
|
1385 |
} |
|
1386 |
return result; |
|
1387 |
""") |
|
1388 |
return x |
|
1389 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1390 |
|
371 | 1391 |
def printFunc(objs): |
1392 |
JS(""" |
|
1393 |
if ($wnd.console==undefined) return; |
|
1394 |
var s = ""; |
|
1395 |
for(var i=0; i < objs.length; i++) { |
|
1396 |
if(s != "") s += " "; |
|
1397 |
s += objs[i]; |
|
1398 |
} |
|
1399 |
console.debug(s) |
|
1400 |
""") |
|
1401 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1402 |
|
371 | 1403 |
def type(clsname, bases=None, methods=None): |
1404 |
""" creates a class, derived from bases, with methods and variables |
|
1405 |
""" |
|
1406 |
||
1407 |
JS(" var mths = {}; ") |
|
1408 |
if methods: |
|
1409 |
for k in methods.keys(): |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1841
diff
changeset
|
1410 |
_mth = methods[k] |
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1841
diff
changeset
|
1411 |
JS(" mths[k] = _mth; ") |
371 | 1412 |
|
1413 |
JS(" var bss = null; ") |
|
1414 |
if bases: |
|
1415 |
JS("bss = bases.l;") |
|
1416 |
JS(" return pyjs_type(clsname, bss, mths); ") |