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