author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Tue, 12 Sep 2017 18:32:13 +0300 | |
changeset 1801 | 58ff55053518 |
parent 1783 | 3311eea28d56 |
child 1828 | 396da88d7b5c |
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 |
|
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
|
234 |
# 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
|
235 |
# import sys should be below |
3311eea28d56
clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset
|
236 |
import sys # noqa |
371 | 237 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
238 |
|
371 | 239 |
class BaseException: |
240 |
||
241 |
name = "BaseException" |
|
242 |
||
243 |
def __init__(self, *args): |
|
244 |
self.args = args |
|
245 |
||
246 |
def __str__(self): |
|
247 |
if len(self.args) is 0: |
|
248 |
return '' |
|
249 |
elif len(self.args) is 1: |
|
250 |
return repr(self.args[0]) |
|
251 |
return repr(self.args) |
|
252 |
||
253 |
def toString(self): |
|
254 |
return str(self) |
|
255 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
256 |
|
371 | 257 |
class Exception(BaseException): |
258 |
name = "Exception" |
|
259 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
260 |
|
371 | 261 |
class TypeError(BaseException): |
262 |
name = "TypeError" |
|
263 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
264 |
|
371 | 265 |
class StandardError(Exception): |
266 |
name = "StandardError" |
|
267 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
268 |
|
371 | 269 |
class LookupError(StandardError): |
270 |
name = "LookupError" |
|
271 |
||
272 |
def toString(self): |
|
273 |
return self.name + ": " + self.args[0] |
|
274 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
275 |
|
371 | 276 |
class KeyError(LookupError): |
277 |
name = "KeyError" |
|
278 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
279 |
|
371 | 280 |
class AttributeError(StandardError): |
281 |
name = "AttributeError" |
|
282 |
||
283 |
def toString(self): |
|
284 |
return "AttributeError: %s of %s" % (self.args[1], self.args[0]) |
|
285 |
||
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
|
286 |
|
371 | 287 |
JS(""" |
288 |
pyjslib.StopIteration = function () { }; |
|
289 |
pyjslib.StopIteration.prototype = new Error(); |
|
290 |
pyjslib.StopIteration.name = 'StopIteration'; |
|
291 |
pyjslib.StopIteration.message = 'StopIteration'; |
|
292 |
||
293 |
pyjslib.String_find = function(sub, start, end) { |
|
294 |
var pos=this.indexOf(sub, start); |
|
295 |
if (pyjslib.isUndefined(end)) return pos; |
|
296 |
||
297 |
if (pos + sub.length>end) return -1; |
|
298 |
return pos; |
|
299 |
} |
|
300 |
||
301 |
pyjslib.String_join = function(data) { |
|
302 |
var text=""; |
|
303 |
||
304 |
if (pyjslib.isArray(data)) { |
|
305 |
return data.join(this); |
|
306 |
} |
|
307 |
else if (pyjslib.isIteratable(data)) { |
|
308 |
var iter=data.__iter__(); |
|
309 |
try { |
|
310 |
text+=iter.next(); |
|
311 |
while (true) { |
|
312 |
var item=iter.next(); |
|
313 |
text+=this + item; |
|
314 |
} |
|
315 |
} |
|
316 |
catch (e) { |
|
317 |
if (e != pyjslib.StopIteration) throw e; |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
return text; |
|
322 |
} |
|
323 |
||
324 |
pyjslib.String_isdigit = function() { |
|
325 |
return (this.match(/^\d+$/g) != null); |
|
326 |
} |
|
327 |
||
328 |
pyjslib.String_replace = function(old, replace, count) { |
|
329 |
var do_max=false; |
|
330 |
var start=0; |
|
331 |
var new_str=""; |
|
332 |
var pos=0; |
|
333 |
||
334 |
if (!pyjslib.isString(old)) return this.__replace(old, replace); |
|
335 |
if (!pyjslib.isUndefined(count)) do_max=true; |
|
336 |
||
337 |
while (start<this.length) { |
|
338 |
if (do_max && !count--) break; |
|
339 |
||
340 |
pos=this.indexOf(old, start); |
|
341 |
if (pos<0) break; |
|
342 |
||
343 |
new_str+=this.substring(start, pos) + replace; |
|
344 |
start=pos+old.length; |
|
345 |
} |
|
346 |
if (start<this.length) new_str+=this.substring(start); |
|
347 |
||
348 |
return new_str; |
|
349 |
} |
|
350 |
||
351 |
pyjslib.String_split = function(sep, maxsplit) { |
|
352 |
var items=new pyjslib.List(); |
|
353 |
var do_max=false; |
|
354 |
var subject=this; |
|
355 |
var start=0; |
|
356 |
var pos=0; |
|
357 |
||
358 |
if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) { |
|
359 |
sep=" "; |
|
360 |
subject=subject.strip(); |
|
361 |
subject=subject.replace(/\s+/g, sep); |
|
362 |
} |
|
363 |
else if (!pyjslib.isUndefined(maxsplit)) do_max=true; |
|
364 |
||
365 |
if (subject.length == 0) { |
|
366 |
return items; |
|
367 |
} |
|
368 |
||
369 |
while (start<subject.length) { |
|
370 |
if (do_max && !maxsplit--) break; |
|
371 |
||
372 |
pos=subject.indexOf(sep, start); |
|
373 |
if (pos<0) break; |
|
374 |
||
375 |
items.append(subject.substring(start, pos)); |
|
376 |
start=pos+sep.length; |
|
377 |
} |
|
378 |
if (start<=subject.length) items.append(subject.substring(start)); |
|
379 |
||
380 |
return items; |
|
381 |
} |
|
382 |
||
383 |
pyjslib.String___iter__ = function() { |
|
384 |
var i = 0; |
|
385 |
var s = this; |
|
386 |
return { |
|
387 |
'next': function() { |
|
388 |
if (i >= s.length) { |
|
389 |
throw pyjslib.StopIteration; |
|
390 |
} |
|
391 |
return s.substring(i++, i, 1); |
|
392 |
}, |
|
393 |
'__iter__': function() { |
|
394 |
return this; |
|
395 |
} |
|
396 |
}; |
|
397 |
} |
|
398 |
||
399 |
pyjslib.String_strip = function(chars) { |
|
400 |
return this.lstrip(chars).rstrip(chars); |
|
401 |
} |
|
402 |
||
403 |
pyjslib.String_lstrip = function(chars) { |
|
404 |
if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, ""); |
|
405 |
||
406 |
return this.replace(new RegExp("^[" + chars + "]+"), ""); |
|
407 |
} |
|
408 |
||
409 |
pyjslib.String_rstrip = function(chars) { |
|
410 |
if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, ""); |
|
411 |
||
412 |
return this.replace(new RegExp("[" + chars + "]+$"), ""); |
|
413 |
} |
|
414 |
||
415 |
pyjslib.String_startswith = function(prefix, start) { |
|
416 |
if (pyjslib.isUndefined(start)) start = 0; |
|
417 |
||
418 |
if (this.substring(start, prefix.length) == prefix) return true; |
|
419 |
return false; |
|
420 |
} |
|
421 |
||
422 |
pyjslib.abs = Math.abs; |
|
423 |
||
424 |
""") |
|
425 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
426 |
|
371 | 427 |
class Class: |
428 |
def __init__(self, name): |
|
429 |
self.name = name |
|
430 |
||
431 |
def __str___(self): |
|
432 |
return self.name |
|
433 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
434 |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
435 |
def eq(a, b): |
371 | 436 |
JS(""" |
437 |
if (pyjslib.hasattr(a, "__cmp__")) { |
|
438 |
return a.__cmp__(b) == 0; |
|
439 |
} else if (pyjslib.hasattr(b, "__cmp__")) { |
|
440 |
return b.__cmp__(a) == 0; |
|
441 |
} |
|
442 |
return a == b; |
|
443 |
""") |
|
444 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
445 |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
446 |
def cmp(a, b): |
371 | 447 |
if hasattr(a, "__cmp__"): |
448 |
return a.__cmp__(b) |
|
449 |
elif hasattr(b, "__cmp__"): |
|
450 |
return -b.__cmp__(a) |
|
451 |
if a > b: |
|
452 |
return 1 |
|
453 |
elif b > a: |
|
454 |
return -1 |
|
455 |
else: |
|
456 |
return 0 |
|
457 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
458 |
|
371 | 459 |
def bool(v): |
460 |
# this needs to stay in native code without any dependencies here, |
|
461 |
# because this is used by if and while, we need to prevent |
|
462 |
# recursion |
|
463 |
JS(""" |
|
464 |
if (!v) return false; |
|
465 |
switch(typeof v){ |
|
466 |
case 'boolean': |
|
467 |
return v; |
|
468 |
case 'object': |
|
469 |
if (v.__nonzero__){ |
|
470 |
return v.__nonzero__(); |
|
471 |
}else if (v.__len__){ |
|
472 |
return v.__len__()>0; |
|
473 |
} |
|
474 |
return true; |
|
475 |
} |
|
476 |
return Boolean(v); |
|
477 |
""") |
|
478 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
479 |
|
371 | 480 |
class List: |
481 |
def __init__(self, data=None): |
|
482 |
JS(""" |
|
483 |
this.l = []; |
|
484 |
this.extend(data); |
|
485 |
""") |
|
486 |
||
487 |
def append(self, item): |
|
488 |
JS(""" this.l[this.l.length] = item;""") |
|
489 |
||
490 |
def extend(self, data): |
|
491 |
JS(""" |
|
492 |
if (pyjslib.isArray(data)) { |
|
493 |
n = this.l.length; |
|
494 |
for (var i=0; i < data.length; i++) { |
|
495 |
this.l[n+i]=data[i]; |
|
496 |
} |
|
497 |
} |
|
498 |
else if (pyjslib.isIteratable(data)) { |
|
499 |
var iter=data.__iter__(); |
|
500 |
var i=this.l.length; |
|
501 |
try { |
|
502 |
while (true) { |
|
503 |
var item=iter.next(); |
|
504 |
this.l[i++]=item; |
|
505 |
} |
|
506 |
} |
|
507 |
catch (e) { |
|
508 |
if (e != pyjslib.StopIteration) throw e; |
|
509 |
} |
|
510 |
} |
|
511 |
""") |
|
512 |
||
513 |
def remove(self, value): |
|
514 |
JS(""" |
|
515 |
var index=this.index(value); |
|
516 |
if (index<0) return false; |
|
517 |
this.l.splice(index, 1); |
|
518 |
return true; |
|
519 |
""") |
|
520 |
||
521 |
def index(self, value, start=0): |
|
522 |
JS(""" |
|
523 |
var length=this.l.length; |
|
524 |
for (var i=start; i<length; i++) { |
|
525 |
if (this.l[i]==value) { |
|
526 |
return i; |
|
527 |
} |
|
528 |
} |
|
529 |
return -1; |
|
530 |
""") |
|
531 |
||
532 |
def insert(self, index, value): |
|
533 |
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
|
534 |
||
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
535 |
def pop(self, index=-1): |
371 | 536 |
JS(""" |
537 |
if (index<0) index = this.l.length + index; |
|
538 |
var a = this.l[index]; |
|
539 |
this.l.splice(index, 1); |
|
540 |
return a; |
|
541 |
""") |
|
542 |
||
543 |
def __cmp__(self, l): |
|
544 |
if not isinstance(l, List): |
|
545 |
return -1 |
|
546 |
ll = len(self) - len(l) |
|
547 |
if ll != 0: |
|
548 |
return ll |
|
549 |
for x in range(len(l)): |
|
550 |
ll = cmp(self.__getitem__(x), l[x]) |
|
551 |
if ll != 0: |
|
552 |
return ll |
|
553 |
return 0 |
|
554 |
||
555 |
def slice(self, lower, upper): |
|
556 |
JS(""" |
|
557 |
if (upper==null) return pyjslib.List(this.l.slice(lower)); |
|
558 |
return pyjslib.List(this.l.slice(lower, upper)); |
|
559 |
""") |
|
560 |
||
561 |
def __getitem__(self, index): |
|
562 |
JS(""" |
|
563 |
if (index<0) index = this.l.length + index; |
|
564 |
return this.l[index]; |
|
565 |
""") |
|
566 |
||
567 |
def __setitem__(self, index, value): |
|
568 |
JS(""" this.l[index]=value;""") |
|
569 |
||
570 |
def __delitem__(self, index): |
|
571 |
JS(""" this.l.splice(index, 1);""") |
|
572 |
||
573 |
def __len__(self): |
|
574 |
JS(""" return this.l.length;""") |
|
575 |
||
576 |
def __contains__(self, value): |
|
577 |
return self.index(value) >= 0 |
|
578 |
||
579 |
def __iter__(self): |
|
580 |
JS(""" |
|
581 |
var i = 0; |
|
582 |
var l = this.l; |
|
583 |
return { |
|
584 |
'next': function() { |
|
585 |
if (i >= l.length) { |
|
586 |
throw pyjslib.StopIteration; |
|
587 |
} |
|
588 |
return l[i++]; |
|
589 |
}, |
|
590 |
'__iter__': function() { |
|
591 |
return this; |
|
592 |
} |
|
593 |
}; |
|
594 |
""") |
|
595 |
||
596 |
def reverse(self): |
|
597 |
JS(""" this.l.reverse();""") |
|
598 |
||
599 |
def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
|
600 |
if not compareFunc: |
|
601 |
global cmp |
|
602 |
compareFunc = cmp |
|
603 |
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
|
604 |
def thisSort1(a, b): |
371 | 605 |
return -compareFunc(keyFunc(a), keyFunc(b)) |
606 |
self.l.sort(thisSort1) |
|
607 |
elif keyFunc: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
608 |
def thisSort2(a, b): |
371 | 609 |
return compareFunc(keyFunc(a), keyFunc(b)) |
610 |
self.l.sort(thisSort2) |
|
611 |
elif reverse: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
612 |
def thisSort3(a, b): |
371 | 613 |
return -compareFunc(a, b) |
614 |
self.l.sort(thisSort3) |
|
615 |
else: |
|
616 |
self.l.sort(compareFunc) |
|
617 |
||
618 |
def getArray(self): |
|
619 |
""" |
|
620 |
Access the javascript Array that is used internally by this list |
|
621 |
""" |
|
622 |
return self.l |
|
623 |
||
624 |
def __str__(self): |
|
625 |
return repr(self) |
|
626 |
||
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
|
627 |
|
371 | 628 |
list = List |
629 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
630 |
|
371 | 631 |
class Tuple: |
632 |
def __init__(self, data=None): |
|
633 |
JS(""" |
|
634 |
this.l = []; |
|
635 |
this.extend(data); |
|
636 |
""") |
|
637 |
||
638 |
def append(self, item): |
|
639 |
JS(""" this.l[this.l.length] = item;""") |
|
640 |
||
641 |
def extend(self, data): |
|
642 |
JS(""" |
|
643 |
if (pyjslib.isArray(data)) { |
|
644 |
n = this.l.length; |
|
645 |
for (var i=0; i < data.length; i++) { |
|
646 |
this.l[n+i]=data[i]; |
|
647 |
} |
|
648 |
} |
|
649 |
else if (pyjslib.isIteratable(data)) { |
|
650 |
var iter=data.__iter__(); |
|
651 |
var i=this.l.length; |
|
652 |
try { |
|
653 |
while (true) { |
|
654 |
var item=iter.next(); |
|
655 |
this.l[i++]=item; |
|
656 |
} |
|
657 |
} |
|
658 |
catch (e) { |
|
659 |
if (e != pyjslib.StopIteration) throw e; |
|
660 |
} |
|
661 |
} |
|
662 |
""") |
|
663 |
||
664 |
def remove(self, value): |
|
665 |
JS(""" |
|
666 |
var index=this.index(value); |
|
667 |
if (index<0) return false; |
|
668 |
this.l.splice(index, 1); |
|
669 |
return true; |
|
670 |
""") |
|
671 |
||
672 |
def index(self, value, start=0): |
|
673 |
JS(""" |
|
674 |
var length=this.l.length; |
|
675 |
for (var i=start; i<length; i++) { |
|
676 |
if (this.l[i]==value) { |
|
677 |
return i; |
|
678 |
} |
|
679 |
} |
|
680 |
return -1; |
|
681 |
""") |
|
682 |
||
683 |
def insert(self, index, value): |
|
684 |
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
|
685 |
||
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
686 |
def pop(self, index=-1): |
371 | 687 |
JS(""" |
688 |
if (index<0) index = this.l.length + index; |
|
689 |
var a = this.l[index]; |
|
690 |
this.l.splice(index, 1); |
|
691 |
return a; |
|
692 |
""") |
|
693 |
||
694 |
def __cmp__(self, l): |
|
695 |
if not isinstance(l, Tuple): |
|
696 |
return -1 |
|
697 |
ll = len(self) - len(l) |
|
698 |
if ll != 0: |
|
699 |
return ll |
|
700 |
for x in range(len(l)): |
|
701 |
ll = cmp(self.__getitem__(x), l[x]) |
|
702 |
if ll != 0: |
|
703 |
return ll |
|
704 |
return 0 |
|
705 |
||
706 |
def slice(self, lower, upper): |
|
707 |
JS(""" |
|
708 |
if (upper==null) return pyjslib.Tuple(this.l.slice(lower)); |
|
709 |
return pyjslib.Tuple(this.l.slice(lower, upper)); |
|
710 |
""") |
|
711 |
||
712 |
def __getitem__(self, index): |
|
713 |
JS(""" |
|
714 |
if (index<0) index = this.l.length + index; |
|
715 |
return this.l[index]; |
|
716 |
""") |
|
717 |
||
718 |
def __setitem__(self, index, value): |
|
719 |
JS(""" this.l[index]=value;""") |
|
720 |
||
721 |
def __delitem__(self, index): |
|
722 |
JS(""" this.l.splice(index, 1);""") |
|
723 |
||
724 |
def __len__(self): |
|
725 |
JS(""" return this.l.length;""") |
|
726 |
||
727 |
def __contains__(self, value): |
|
728 |
return self.index(value) >= 0 |
|
729 |
||
730 |
def __iter__(self): |
|
731 |
JS(""" |
|
732 |
var i = 0; |
|
733 |
var l = this.l; |
|
734 |
return { |
|
735 |
'next': function() { |
|
736 |
if (i >= l.length) { |
|
737 |
throw pyjslib.StopIteration; |
|
738 |
} |
|
739 |
return l[i++]; |
|
740 |
}, |
|
741 |
'__iter__': function() { |
|
742 |
return this; |
|
743 |
} |
|
744 |
}; |
|
745 |
""") |
|
746 |
||
747 |
def reverse(self): |
|
748 |
JS(""" this.l.reverse();""") |
|
749 |
||
750 |
def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
|
751 |
if not compareFunc: |
|
752 |
global cmp |
|
753 |
compareFunc = cmp |
|
754 |
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
|
755 |
def thisSort1(a, b): |
371 | 756 |
return -compareFunc(keyFunc(a), keyFunc(b)) |
757 |
self.l.sort(thisSort1) |
|
758 |
elif keyFunc: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
759 |
def thisSort2(a, b): |
371 | 760 |
return compareFunc(keyFunc(a), keyFunc(b)) |
761 |
self.l.sort(thisSort2) |
|
762 |
elif reverse: |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
763 |
def thisSort3(a, b): |
371 | 764 |
return -compareFunc(a, b) |
765 |
self.l.sort(thisSort3) |
|
766 |
else: |
|
767 |
self.l.sort(compareFunc) |
|
768 |
||
769 |
def getArray(self): |
|
770 |
""" |
|
771 |
Access the javascript Array that is used internally by this list |
|
772 |
""" |
|
773 |
return self.l |
|
774 |
||
775 |
def __str__(self): |
|
776 |
return repr(self) |
|
777 |
||
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
|
778 |
|
371 | 779 |
tuple = Tuple |
780 |
||
781 |
||
782 |
class Dict: |
|
783 |
def __init__(self, data=None): |
|
784 |
JS(""" |
|
785 |
this.d = {}; |
|
786 |
||
787 |
if (pyjslib.isArray(data)) { |
|
788 |
for (var i in data) { |
|
789 |
var item=data[i]; |
|
790 |
this.__setitem__(item[0], item[1]); |
|
791 |
//var sKey=pyjslib.hash(item[0]); |
|
792 |
//this.d[sKey]=item[1]; |
|
793 |
} |
|
794 |
} |
|
795 |
else if (pyjslib.isIteratable(data)) { |
|
796 |
var iter=data.__iter__(); |
|
797 |
try { |
|
798 |
while (true) { |
|
799 |
var item=iter.next(); |
|
800 |
this.__setitem__(item.__getitem__(0), item.__getitem__(1)); |
|
801 |
} |
|
802 |
} |
|
803 |
catch (e) { |
|
804 |
if (e != pyjslib.StopIteration) throw e; |
|
805 |
} |
|
806 |
} |
|
807 |
else if (pyjslib.isObject(data)) { |
|
808 |
for (var key in data) { |
|
809 |
this.__setitem__(key, data[key]); |
|
810 |
} |
|
811 |
} |
|
812 |
""") |
|
813 |
||
814 |
def __setitem__(self, key, value): |
|
815 |
JS(""" |
|
816 |
var sKey = pyjslib.hash(key); |
|
817 |
this.d[sKey]=[key, value]; |
|
818 |
""") |
|
819 |
||
820 |
def __getitem__(self, key): |
|
821 |
JS(""" |
|
822 |
var sKey = pyjslib.hash(key); |
|
823 |
var value=this.d[sKey]; |
|
824 |
if (pyjslib.isUndefined(value)){ |
|
825 |
throw pyjslib.KeyError(key); |
|
826 |
} |
|
827 |
return value[1]; |
|
828 |
""") |
|
829 |
||
830 |
def __nonzero__(self): |
|
831 |
JS(""" |
|
832 |
for (var i in this.d){ |
|
833 |
return true; |
|
834 |
} |
|
835 |
return false; |
|
836 |
""") |
|
837 |
||
838 |
def __len__(self): |
|
839 |
JS(""" |
|
840 |
var size=0; |
|
841 |
for (var i in this.d) size++; |
|
842 |
return size; |
|
843 |
""") |
|
844 |
||
845 |
def has_key(self, key): |
|
846 |
return self.__contains__(key) |
|
847 |
||
848 |
def __delitem__(self, key): |
|
849 |
JS(""" |
|
850 |
var sKey = pyjslib.hash(key); |
|
851 |
delete this.d[sKey]; |
|
852 |
""") |
|
853 |
||
854 |
def __contains__(self, key): |
|
855 |
JS(""" |
|
856 |
var sKey = pyjslib.hash(key); |
|
857 |
return (pyjslib.isUndefined(this.d[sKey])) ? false : true; |
|
858 |
""") |
|
859 |
||
860 |
def keys(self): |
|
861 |
JS(""" |
|
862 |
var keys=new pyjslib.List(); |
|
863 |
for (var key in this.d) { |
|
864 |
keys.append(this.d[key][0]); |
|
865 |
} |
|
866 |
return keys; |
|
867 |
""") |
|
868 |
||
869 |
def values(self): |
|
870 |
JS(""" |
|
871 |
var values=new pyjslib.List(); |
|
872 |
for (var key in this.d) values.append(this.d[key][1]); |
|
873 |
return values; |
|
874 |
""") |
|
875 |
||
876 |
def items(self): |
|
877 |
JS(""" |
|
878 |
var items = new pyjslib.List(); |
|
879 |
for (var key in this.d) { |
|
880 |
var kv = this.d[key]; |
|
881 |
items.append(new pyjslib.List(kv)) |
|
882 |
} |
|
883 |
return items; |
|
884 |
""") |
|
885 |
||
886 |
def __iter__(self): |
|
887 |
return self.keys().__iter__() |
|
888 |
||
889 |
def iterkeys(self): |
|
890 |
return self.__iter__() |
|
891 |
||
892 |
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
|
893 |
return self.values().__iter__() |
371 | 894 |
|
895 |
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
|
896 |
return self.items().__iter__() |
371 | 897 |
|
898 |
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
|
899 |
if key not in self: |
371 | 900 |
self[key] = default_value |
901 |
||
902 |
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
|
903 |
if key not in self: |
371 | 904 |
return default_ |
905 |
return self[key] |
|
906 |
||
907 |
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
|
908 |
for k, v in d.iteritems(): |
371 | 909 |
self[k] = v |
910 |
||
911 |
def getObject(self): |
|
912 |
""" |
|
913 |
Return the javascript Object which this class uses to store |
|
914 |
dictionary keys and values |
|
915 |
""" |
|
916 |
return self.d |
|
917 |
||
918 |
def copy(self): |
|
919 |
return Dict(self.items()) |
|
920 |
||
921 |
def __str__(self): |
|
922 |
return repr(self) |
|
923 |
||
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
|
924 |
|
371 | 925 |
dict = Dict |
926 |
||
927 |
# 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
|
928 |
|
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
929 |
|
371 | 930 |
def range(): |
931 |
JS(""" |
|
932 |
var start = 0; |
|
933 |
var stop = 0; |
|
934 |
var step = 1; |
|
935 |
||
936 |
if (arguments.length == 2) { |
|
937 |
start = arguments[0]; |
|
938 |
stop = arguments[1]; |
|
939 |
} |
|
940 |
else if (arguments.length == 3) { |
|
941 |
start = arguments[0]; |
|
942 |
stop = arguments[1]; |
|
943 |
step = arguments[2]; |
|
944 |
} |
|
945 |
else if (arguments.length>0) stop = arguments[0]; |
|
946 |
||
947 |
return { |
|
948 |
'next': function() { |
|
949 |
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) throw pyjslib.StopIteration; |
|
950 |
var rval = start; |
|
951 |
start += step; |
|
952 |
return rval; |
|
953 |
}, |
|
954 |
'__iter__': function() { |
|
955 |
return this; |
|
956 |
} |
|
957 |
} |
|
958 |
""") |
|
959 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
960 |
|
371 | 961 |
def slice(object, lower, upper): |
962 |
JS(""" |
|
963 |
if (pyjslib.isString(object)) { |
|
964 |
if (lower < 0) { |
|
965 |
lower = object.length + lower; |
|
966 |
} |
|
967 |
if (upper < 0) { |
|
968 |
upper = object.length + upper; |
|
969 |
} |
|
970 |
if (pyjslib.isNull(upper)) upper=object.length; |
|
971 |
return object.substring(lower, upper); |
|
972 |
} |
|
973 |
if (pyjslib.isObject(object) && object.slice) |
|
974 |
return object.slice(lower, upper); |
|
975 |
||
976 |
return null; |
|
977 |
""") |
|
978 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
979 |
|
371 | 980 |
def str(text): |
981 |
JS(""" |
|
982 |
if (pyjslib.hasattr(text,"__str__")) { |
|
983 |
return text.__str__(); |
|
984 |
} |
|
985 |
return String(text); |
|
986 |
""") |
|
987 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
988 |
|
371 | 989 |
def ord(x): |
990 |
if(isString(x) and len(x) is 1): |
|
991 |
JS(""" |
|
992 |
return x.charCodeAt(0); |
|
993 |
""") |
|
994 |
else: |
|
995 |
JS(""" |
|
996 |
throw pyjslib.TypeError(); |
|
997 |
""") |
|
998 |
return None |
|
999 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1000 |
|
371 | 1001 |
def chr(x): |
1002 |
JS(""" |
|
1003 |
return String.fromCharCode(x) |
|
1004 |
""") |
|
1005 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1006 |
|
371 | 1007 |
def is_basetype(x): |
1008 |
JS(""" |
|
1009 |
var t = typeof(x); |
|
1010 |
return t == 'boolean' || |
|
1011 |
t == 'function' || |
|
1012 |
t == 'number' || |
|
1013 |
t == 'string' || |
|
1014 |
t == 'undefined' |
|
1015 |
; |
|
1016 |
""") |
|
1017 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1018 |
|
371 | 1019 |
def get_pyjs_classtype(x): |
1020 |
JS(""" |
|
1021 |
if (pyjslib.hasattr(x, "__class__")) |
|
1022 |
if (pyjslib.hasattr(x.__class__, "__new__")) |
|
1023 |
var src = x.__class__.__name__; |
|
1024 |
return src; |
|
1025 |
return null; |
|
1026 |
""") |
|
1027 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1028 |
|
371 | 1029 |
def repr(x): |
1030 |
""" Return the string representation of 'x'. |
|
1031 |
""" |
|
1032 |
JS(""" |
|
1033 |
if (x === null) |
|
1034 |
return "null"; |
|
1035 |
||
1036 |
if (x === undefined) |
|
1037 |
return "undefined"; |
|
1038 |
||
1039 |
var t = typeof(x); |
|
1040 |
||
1041 |
//alert("repr typeof " + t + " : " + x); |
|
1042 |
||
1043 |
if (t == "boolean") |
|
1044 |
return x.toString(); |
|
1045 |
||
1046 |
if (t == "function") |
|
1047 |
return "<function " + x.toString() + ">"; |
|
1048 |
||
1049 |
if (t == "number") |
|
1050 |
return x.toString(); |
|
1051 |
||
1052 |
if (t == "string") { |
|
1053 |
if (x.indexOf("'") == -1) |
|
1054 |
return "'" + x + "'"; |
|
1055 |
if (x.indexOf('"') == -1) |
|
1056 |
return '"' + x + '"'; |
|
1057 |
var s = x.replace(new RegExp('"', "g"), '\\\\"'); |
|
1058 |
return '"' + s + '"'; |
|
1059 |
}; |
|
1060 |
||
1061 |
if (t == "undefined") |
|
1062 |
return "undefined"; |
|
1063 |
||
1064 |
// If we get here, x is an object. See if it's a Pyjamas class. |
|
1065 |
||
1066 |
if (!pyjslib.hasattr(x, "__init__")) |
|
1067 |
return "<" + x.toString() + ">"; |
|
1068 |
||
1069 |
// Handle the common Pyjamas data types. |
|
1070 |
||
1071 |
var constructor = "UNKNOWN"; |
|
1072 |
||
1073 |
constructor = pyjslib.get_pyjs_classtype(x); |
|
1074 |
||
1075 |
//alert("repr constructor: " + constructor); |
|
1076 |
||
1077 |
if (constructor == "Tuple") { |
|
1078 |
var contents = x.getArray(); |
|
1079 |
var s = "("; |
|
1080 |
for (var i=0; i < contents.length; i++) { |
|
1081 |
s += pyjslib.repr(contents[i]); |
|
1082 |
if (i < contents.length - 1) |
|
1083 |
s += ", "; |
|
1084 |
}; |
|
1085 |
s += ")" |
|
1086 |
return s; |
|
1087 |
}; |
|
1088 |
||
1089 |
if (constructor == "List") { |
|
1090 |
var contents = x.getArray(); |
|
1091 |
var s = "["; |
|
1092 |
for (var i=0; i < contents.length; i++) { |
|
1093 |
s += pyjslib.repr(contents[i]); |
|
1094 |
if (i < contents.length - 1) |
|
1095 |
s += ", "; |
|
1096 |
}; |
|
1097 |
s += "]" |
|
1098 |
return s; |
|
1099 |
}; |
|
1100 |
||
1101 |
if (constructor == "Dict") { |
|
1102 |
var keys = new Array(); |
|
1103 |
for (var key in x.d) |
|
1104 |
keys.push(key); |
|
1105 |
||
1106 |
var s = "{"; |
|
1107 |
for (var i=0; i<keys.length; i++) { |
|
1108 |
var key = keys[i] |
|
1109 |
s += pyjslib.repr(key) + ": " + pyjslib.repr(x.d[key]); |
|
1110 |
if (i < keys.length-1) |
|
1111 |
s += ", " |
|
1112 |
}; |
|
1113 |
s += "}"; |
|
1114 |
return s; |
|
1115 |
}; |
|
1116 |
||
1117 |
// If we get here, the class isn't one we know -> return the class name. |
|
1118 |
// Note that we replace underscores with dots so that the name will |
|
1119 |
// (hopefully!) look like the original Python name. |
|
1120 |
||
1121 |
//var s = constructor.replace(new RegExp('_', "g"), '.'); |
|
1122 |
return "<" + constructor + " object>"; |
|
1123 |
""") |
|
1124 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1125 |
|
371 | 1126 |
def float(text): |
1127 |
JS(""" |
|
1128 |
return parseFloat(text); |
|
1129 |
""") |
|
1130 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1131 |
|
371 | 1132 |
def int(text, radix=0): |
1133 |
JS(""" |
|
1134 |
return parseInt(text, radix); |
|
1135 |
""") |
|
1136 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1137 |
|
371 | 1138 |
def len(object): |
1139 |
JS(""" |
|
1140 |
if (object==null) return 0; |
|
1141 |
if (pyjslib.isObject(object) && object.__len__) return object.__len__(); |
|
1142 |
return object.length; |
|
1143 |
""") |
|
1144 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1145 |
|
371 | 1146 |
def isinstance(object_, classinfo): |
1147 |
if pyjslib.isUndefined(object_): |
|
1148 |
return False |
|
1149 |
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
|
1150 |
|
371 | 1151 |
return False |
1152 |
if _isinstance(classinfo, Tuple): |
|
1153 |
for ci in classinfo: |
|
1154 |
if isinstance(object_, ci): |
|
1155 |
return True |
|
1156 |
return False |
|
1157 |
else: |
|
1158 |
return _isinstance(object_, classinfo) |
|
1159 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1160 |
|
371 | 1161 |
def _isinstance(object_, classinfo): |
1162 |
if not pyjslib.isObject(object_): |
|
1163 |
return False |
|
1164 |
JS(""" |
|
1165 |
if (object_.__class__){ |
|
1166 |
var res = object_ instanceof classinfo.constructor; |
|
1167 |
return res; |
|
1168 |
} |
|
1169 |
return false; |
|
1170 |
""") |
|
1171 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1172 |
|
371 | 1173 |
def getattr(obj, name, default_): |
1174 |
JS(""" |
|
1175 |
if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){ |
|
1176 |
if (pyjslib.isUndefined(default_)){ |
|
1177 |
throw pyjslib.AttributeError(obj, name); |
|
1178 |
}else{ |
|
1179 |
return default_; |
|
1180 |
} |
|
1181 |
} |
|
1182 |
if (!pyjslib.isFunction(obj[name])) return obj[name]; |
|
1183 |
var fnwrap = function() { |
|
1184 |
var args = []; |
|
1185 |
for (var i = 0; i < arguments.length; i++) { |
|
1186 |
args.push(arguments[i]); |
|
1187 |
} |
|
1188 |
return obj[name].apply(obj,args); |
|
1189 |
} |
|
1190 |
fnwrap.__name__ = name; |
|
1191 |
return fnwrap; |
|
1192 |
""") |
|
1193 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1194 |
|
371 | 1195 |
def setattr(obj, name, value): |
1196 |
JS(""" |
|
1197 |
if (!pyjslib.isObject(obj)) return null; |
|
1198 |
||
1199 |
obj[name] = value; |
|
1200 |
||
1201 |
""") |
|
1202 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1203 |
|
371 | 1204 |
def hasattr(obj, name): |
1205 |
JS(""" |
|
1206 |
if (!pyjslib.isObject(obj)) return false; |
|
1207 |
if (pyjslib.isUndefined(obj[name])) return false; |
|
1208 |
||
1209 |
return true; |
|
1210 |
""") |
|
1211 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1212 |
|
371 | 1213 |
def dir(obj): |
1214 |
JS(""" |
|
1215 |
var properties=new pyjslib.List(); |
|
1216 |
for (property in obj) properties.append(property); |
|
1217 |
return properties; |
|
1218 |
""") |
|
1219 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1220 |
|
371 | 1221 |
def filter(obj, method, sequence=None): |
1222 |
# object context is LOST when a method is passed, hence object must be passed separately |
|
1223 |
# to emulate python behaviour, should generate this code inline rather than as a function call |
|
1224 |
items = [] |
|
1225 |
if sequence is None: |
|
1226 |
sequence = method |
|
1227 |
method = obj |
|
1228 |
||
1229 |
for item in sequence: |
|
1230 |
if method(item): |
|
1231 |
items.append(item) |
|
1232 |
else: |
|
1233 |
for item in sequence: |
|
1234 |
if method.call(obj, item): |
|
1235 |
items.append(item) |
|
1236 |
||
1237 |
return items |
|
1238 |
||
1239 |
||
1240 |
def map(obj, method, sequence=None): |
|
1241 |
items = [] |
|
1242 |
||
1243 |
if sequence is None: |
|
1244 |
sequence = method |
|
1245 |
method = obj |
|
1246 |
||
1247 |
for item in sequence: |
|
1248 |
items.append(method(item)) |
|
1249 |
else: |
|
1250 |
for item in sequence: |
|
1251 |
items.append(method.call(obj, item)) |
|
1252 |
||
1253 |
return items |
|
1254 |
||
1255 |
||
1256 |
def enumerate(sequence): |
|
1257 |
enumeration = [] |
|
1258 |
nextIndex = 0 |
|
1259 |
for item in sequence: |
|
1260 |
enumeration.append([nextIndex, item]) |
|
1261 |
nextIndex = nextIndex + 1 |
|
1262 |
return enumeration |
|
1263 |
||
1264 |
||
1265 |
def min(*sequence): |
|
1266 |
minValue = None |
|
1267 |
for item in sequence: |
|
1268 |
if minValue is None: |
|
1269 |
minValue = item |
|
1270 |
elif item < minValue: |
|
1271 |
minValue = item |
|
1272 |
return minValue |
|
1273 |
||
1274 |
||
1275 |
def max(*sequence): |
|
1276 |
maxValue = None |
|
1277 |
for item in sequence: |
|
1278 |
if maxValue is None: |
|
1279 |
maxValue = item |
|
1280 |
elif item > maxValue: |
|
1281 |
maxValue = item |
|
1282 |
return maxValue |
|
1283 |
||
1284 |
||
1285 |
next_hash_id = 0 |
|
1286 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1287 |
|
371 | 1288 |
def hash(obj): |
1289 |
JS(""" |
|
1290 |
if (obj == null) return null; |
|
1291 |
||
1292 |
if (obj.$H) return obj.$H; |
|
1293 |
if (obj.__hash__) return obj.__hash__(); |
|
1294 |
if (obj.constructor == String || obj.constructor == Number || obj.constructor == Date) return obj; |
|
1295 |
||
1296 |
obj.$H = ++pyjslib.next_hash_id; |
|
1297 |
return obj.$H; |
|
1298 |
""") |
|
1299 |
||
1300 |
||
1301 |
# type functions from Douglas Crockford's Remedial Javascript: http://www.crockford.com/javascript/remedial.html |
|
1302 |
def isObject(a): |
|
1303 |
JS(""" |
|
1304 |
return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a); |
|
1305 |
""") |
|
1306 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1307 |
|
371 | 1308 |
def isFunction(a): |
1309 |
JS(""" |
|
1310 |
return typeof a == 'function'; |
|
1311 |
""") |
|
1312 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1313 |
|
371 | 1314 |
def isString(a): |
1315 |
JS(""" |
|
1316 |
return typeof a == 'string'; |
|
1317 |
""") |
|
1318 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1319 |
|
371 | 1320 |
def isNull(a): |
1321 |
JS(""" |
|
1322 |
return typeof a == 'object' && !a; |
|
1323 |
""") |
|
1324 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1325 |
|
371 | 1326 |
def isArray(a): |
1327 |
JS(""" |
|
1328 |
return pyjslib.isObject(a) && a.constructor == Array; |
|
1329 |
""") |
|
1330 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1331 |
|
371 | 1332 |
def isUndefined(a): |
1333 |
JS(""" |
|
1334 |
return typeof a == 'undefined'; |
|
1335 |
""") |
|
1336 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1337 |
|
371 | 1338 |
def isIteratable(a): |
1339 |
JS(""" |
|
1340 |
return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__); |
|
1341 |
""") |
|
1342 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1343 |
|
371 | 1344 |
def isNumber(a): |
1345 |
JS(""" |
|
1346 |
return typeof a == 'number' && isFinite(a); |
|
1347 |
""") |
|
1348 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1349 |
|
371 | 1350 |
def toJSObjects(x): |
1351 |
""" |
|
1352 |
Convert the pyjs pythonic List and Dict objects into javascript Object and Array |
|
1353 |
objects, recursively. |
|
1354 |
""" |
|
1355 |
if isArray(x): |
|
1356 |
JS(""" |
|
1357 |
var result = []; |
|
1358 |
for(var k=0; k < x.length; k++) { |
|
1359 |
var v = x[k]; |
|
1360 |
var tv = pyjslib.toJSObjects(v); |
|
1361 |
result.push(tv); |
|
1362 |
} |
|
1363 |
return result; |
|
1364 |
""") |
|
1365 |
if isObject(x): |
|
1366 |
if isinstance(x, Dict): |
|
1367 |
JS(""" |
|
1368 |
var o = x.getObject(); |
|
1369 |
var result = {}; |
|
1370 |
for (var i in o) { |
|
1371 |
result[o[i][0].toString()] = o[i][1]; |
|
1372 |
} |
|
1373 |
return pyjslib.toJSObjects(result) |
|
1374 |
""") |
|
1375 |
elif isinstance(x, List): |
|
1376 |
return toJSObjects(x.l) |
|
1377 |
elif hasattr(x, '__class__'): |
|
1378 |
# we do not have a special implementation for custom |
|
1379 |
# classes, just pass it on |
|
1380 |
return x |
|
1381 |
if isObject(x): |
|
1382 |
JS(""" |
|
1383 |
var result = {}; |
|
1384 |
for(var k in x) { |
|
1385 |
var v = x[k]; |
|
1386 |
var tv = pyjslib.toJSObjects(v) |
|
1387 |
result[k] = tv; |
|
1388 |
} |
|
1389 |
return result; |
|
1390 |
""") |
|
1391 |
return x |
|
1392 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1393 |
|
371 | 1394 |
def printFunc(objs): |
1395 |
JS(""" |
|
1396 |
if ($wnd.console==undefined) return; |
|
1397 |
var s = ""; |
|
1398 |
for(var i=0; i < objs.length; i++) { |
|
1399 |
if(s != "") s += " "; |
|
1400 |
s += objs[i]; |
|
1401 |
} |
|
1402 |
console.debug(s) |
|
1403 |
""") |
|
1404 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1405 |
|
371 | 1406 |
def type(clsname, bases=None, methods=None): |
1407 |
""" creates a class, derived from bases, with methods and variables |
|
1408 |
""" |
|
1409 |
||
1410 |
JS(" var mths = {}; ") |
|
1411 |
if methods: |
|
1412 |
for k in methods.keys(): |
|
1413 |
mth = methods[k] |
|
1414 |
JS(" mths[k] = mth; ") |
|
1415 |
||
1416 |
JS(" var bss = null; ") |
|
1417 |
if bases: |
|
1418 |
JS("bss = bases.l;") |
|
1419 |
JS(" return pyjs_type(clsname, bss, mths); ") |