65 if (typeof (module_load_request[module_name]) == 'undefined') |
66 if (typeof (module_load_request[module_name]) == 'undefined') |
66 { |
67 { |
67 module_load_request[module_name] = 1; |
68 module_load_request[module_name] = 1; |
68 } |
69 } |
69 |
70 |
70 /* following a load, this first executes the script |
71 /* following a load, this first executes the script |
71 * "preparation" function MODULENAME_loaded_fn() |
72 * "preparation" function MODULENAME_loaded_fn() |
72 * and then sets up the loaded module in the namespace |
73 * and then sets up the loaded module in the namespace |
73 * of the parent. |
74 * of the parent. |
74 */ |
75 */ |
75 |
76 |
182 |
184 |
183 wait(); |
185 wait(); |
184 } |
186 } |
185 """) |
187 """) |
186 |
188 |
|
189 |
187 class Object: |
190 class Object: |
188 pass |
191 pass |
189 |
192 |
|
193 |
190 object = Object |
194 object = Object |
|
195 |
191 |
196 |
192 class Modload: |
197 class Modload: |
193 |
198 |
194 def __init__(self, path, app_modlist, app_imported_fn, dynamic, |
199 def __init__(self, path, app_modlist, app_imported_fn, dynamic, |
195 parent_mod): |
200 parent_mod): |
196 self.app_modlist = app_modlist |
201 self.app_modlist = app_modlist |
197 self.app_imported_fn = app_imported_fn |
202 self.app_imported_fn = app_imported_fn |
198 self.path = path |
203 self.path = path |
199 self.idx = 0; |
204 self.idx = 0 |
200 self.dynamic = dynamic |
205 self.dynamic = dynamic |
201 self.parent_mod = parent_mod |
206 self.parent_mod = parent_mod |
202 |
207 |
203 def next(self): |
208 def next(self): |
204 |
209 |
205 for i in range(len(self.app_modlist[self.idx])): |
210 for i in range(len(self.app_modlist[self.idx])): |
206 app = self.app_modlist[self.idx][i] |
211 app = self.app_modlist[self.idx][i] |
207 import_module(self.path, self.parent_mod, app, self.dynamic, True); |
212 import_module(self.path, self.parent_mod, app, self.dynamic, True) |
208 self.idx += 1 |
213 self.idx += 1 |
209 |
214 |
210 if self.idx >= len(self.app_modlist): |
215 if self.idx >= len(self.app_modlist): |
211 import_wait(self.app_imported_fn, self.parent_mod, self.dynamic) |
216 import_wait(self.app_imported_fn, self.parent_mod, self.dynamic) |
212 else: |
217 else: |
213 import_wait(getattr(self, "next"), self.parent_mod, self.dynamic) |
218 import_wait(getattr(self, "next"), self.parent_mod, self.dynamic) |
214 |
219 |
|
220 |
215 def get_module(module_name): |
221 def get_module(module_name): |
216 ev = "__mod = %s;" % module_name |
222 ev = "__mod = %s;" % module_name |
217 JS("pyjs_eval(ev);") |
223 JS("pyjs_eval(ev);") |
218 return __mod |
224 return __mod |
219 |
225 |
|
226 |
220 def preload_app_modules(path, app_modnames, app_imported_fn, dynamic, |
227 def preload_app_modules(path, app_modnames, app_imported_fn, dynamic, |
221 parent_mod=None): |
228 parent_mod=None): |
222 |
229 |
223 loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod) |
230 loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod) |
224 loader.next() |
231 loader.next() |
225 |
232 |
226 import sys |
233 |
|
234 # as comment on line 20 says |
|
235 # import sys should be below |
|
236 import sys # noqa |
|
237 |
227 |
238 |
228 class BaseException: |
239 class BaseException: |
229 |
240 |
230 name = "BaseException" |
241 name = "BaseException" |
231 |
242 |
240 return repr(self.args) |
251 return repr(self.args) |
241 |
252 |
242 def toString(self): |
253 def toString(self): |
243 return str(self) |
254 return str(self) |
244 |
255 |
|
256 |
245 class Exception(BaseException): |
257 class Exception(BaseException): |
246 |
|
247 name = "Exception" |
258 name = "Exception" |
|
259 |
248 |
260 |
249 class TypeError(BaseException): |
261 class TypeError(BaseException): |
250 name = "TypeError" |
262 name = "TypeError" |
251 |
263 |
|
264 |
252 class StandardError(Exception): |
265 class StandardError(Exception): |
253 name = "StandardError" |
266 name = "StandardError" |
254 |
267 |
|
268 |
255 class LookupError(StandardError): |
269 class LookupError(StandardError): |
256 name = "LookupError" |
270 name = "LookupError" |
257 |
271 |
258 def toString(self): |
272 def toString(self): |
259 return self.name + ": " + self.args[0] |
273 return self.name + ": " + self.args[0] |
260 |
274 |
|
275 |
261 class KeyError(LookupError): |
276 class KeyError(LookupError): |
262 name = "KeyError" |
277 name = "KeyError" |
263 |
278 |
|
279 |
264 class AttributeError(StandardError): |
280 class AttributeError(StandardError): |
265 |
|
266 name = "AttributeError" |
281 name = "AttributeError" |
267 |
282 |
268 def toString(self): |
283 def toString(self): |
269 return "AttributeError: %s of %s" % (self.args[1], self.args[0]) |
284 return "AttributeError: %s of %s" % (self.args[1], self.args[0]) |
|
285 |
270 |
286 |
271 JS(""" |
287 JS(""" |
272 pyjslib.StopIteration = function () { }; |
288 pyjslib.StopIteration = function () { }; |
273 pyjslib.StopIteration.prototype = new Error(); |
289 pyjslib.StopIteration.prototype = new Error(); |
274 pyjslib.StopIteration.name = 'StopIteration'; |
290 pyjslib.StopIteration.name = 'StopIteration'; |
405 |
421 |
406 pyjslib.abs = Math.abs; |
422 pyjslib.abs = Math.abs; |
407 |
423 |
408 """) |
424 """) |
409 |
425 |
|
426 |
410 class Class: |
427 class Class: |
411 def __init__(self, name): |
428 def __init__(self, name): |
412 self.name = name |
429 self.name = name |
413 |
430 |
414 def __str___(self): |
431 def __str___(self): |
415 return self.name |
432 return self.name |
416 |
433 |
417 def eq(a,b): |
434 |
|
435 def eq(a, b): |
418 JS(""" |
436 JS(""" |
419 if (pyjslib.hasattr(a, "__cmp__")) { |
437 if (pyjslib.hasattr(a, "__cmp__")) { |
420 return a.__cmp__(b) == 0; |
438 return a.__cmp__(b) == 0; |
421 } else if (pyjslib.hasattr(b, "__cmp__")) { |
439 } else if (pyjslib.hasattr(b, "__cmp__")) { |
422 return b.__cmp__(a) == 0; |
440 return b.__cmp__(a) == 0; |
423 } |
441 } |
424 return a == b; |
442 return a == b; |
425 """) |
443 """) |
426 |
444 |
427 def cmp(a,b): |
445 |
|
446 def cmp(a, b): |
428 if hasattr(a, "__cmp__"): |
447 if hasattr(a, "__cmp__"): |
429 return a.__cmp__(b) |
448 return a.__cmp__(b) |
430 elif hasattr(b, "__cmp__"): |
449 elif hasattr(b, "__cmp__"): |
431 return -b.__cmp__(a) |
450 return -b.__cmp__(a) |
432 if a > b: |
451 if a > b: |
509 """) |
530 """) |
510 |
531 |
511 def insert(self, index, value): |
532 def insert(self, index, value): |
512 JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
533 JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
513 |
534 |
514 def pop(self, index = -1): |
535 def pop(self, index=-1): |
515 JS(""" |
536 JS(""" |
516 if (index<0) index = this.l.length + index; |
537 if (index<0) index = this.l.length + index; |
517 var a = this.l[index]; |
538 var a = this.l[index]; |
518 this.l.splice(index, 1); |
539 this.l.splice(index, 1); |
519 return a; |
540 return a; |
578 def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
599 def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
579 if not compareFunc: |
600 if not compareFunc: |
580 global cmp |
601 global cmp |
581 compareFunc = cmp |
602 compareFunc = cmp |
582 if keyFunc and reverse: |
603 if keyFunc and reverse: |
583 def thisSort1(a,b): |
604 def thisSort1(a, b): |
584 return -compareFunc(keyFunc(a), keyFunc(b)) |
605 return -compareFunc(keyFunc(a), keyFunc(b)) |
585 self.l.sort(thisSort1) |
606 self.l.sort(thisSort1) |
586 elif keyFunc: |
607 elif keyFunc: |
587 def thisSort2(a,b): |
608 def thisSort2(a, b): |
588 return compareFunc(keyFunc(a), keyFunc(b)) |
609 return compareFunc(keyFunc(a), keyFunc(b)) |
589 self.l.sort(thisSort2) |
610 self.l.sort(thisSort2) |
590 elif reverse: |
611 elif reverse: |
591 def thisSort3(a,b): |
612 def thisSort3(a, b): |
592 return -compareFunc(a, b) |
613 return -compareFunc(a, b) |
593 self.l.sort(thisSort3) |
614 self.l.sort(thisSort3) |
594 else: |
615 else: |
595 self.l.sort(compareFunc) |
616 self.l.sort(compareFunc) |
596 |
617 |
658 """) |
681 """) |
659 |
682 |
660 def insert(self, index, value): |
683 def insert(self, index, value): |
661 JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
684 JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") |
662 |
685 |
663 def pop(self, index = -1): |
686 def pop(self, index=-1): |
664 JS(""" |
687 JS(""" |
665 if (index<0) index = this.l.length + index; |
688 if (index<0) index = this.l.length + index; |
666 var a = this.l[index]; |
689 var a = this.l[index]; |
667 this.l.splice(index, 1); |
690 this.l.splice(index, 1); |
668 return a; |
691 return a; |
727 def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
750 def sort(self, compareFunc=None, keyFunc=None, reverse=False): |
728 if not compareFunc: |
751 if not compareFunc: |
729 global cmp |
752 global cmp |
730 compareFunc = cmp |
753 compareFunc = cmp |
731 if keyFunc and reverse: |
754 if keyFunc and reverse: |
732 def thisSort1(a,b): |
755 def thisSort1(a, b): |
733 return -compareFunc(keyFunc(a), keyFunc(b)) |
756 return -compareFunc(keyFunc(a), keyFunc(b)) |
734 self.l.sort(thisSort1) |
757 self.l.sort(thisSort1) |
735 elif keyFunc: |
758 elif keyFunc: |
736 def thisSort2(a,b): |
759 def thisSort2(a, b): |
737 return compareFunc(keyFunc(a), keyFunc(b)) |
760 return compareFunc(keyFunc(a), keyFunc(b)) |
738 self.l.sort(thisSort2) |
761 self.l.sort(thisSort2) |
739 elif reverse: |
762 elif reverse: |
740 def thisSort3(a,b): |
763 def thisSort3(a, b): |
741 return -compareFunc(a, b) |
764 return -compareFunc(a, b) |
742 self.l.sort(thisSort3) |
765 self.l.sort(thisSort3) |
743 else: |
766 else: |
744 self.l.sort(compareFunc) |
767 self.l.sort(compareFunc) |
745 |
768 |
864 |
888 |
865 def iterkeys(self): |
889 def iterkeys(self): |
866 return self.__iter__() |
890 return self.__iter__() |
867 |
891 |
868 def itervalues(self): |
892 def itervalues(self): |
869 return self.values().__iter__(); |
893 return self.values().__iter__() |
870 |
894 |
871 def iteritems(self): |
895 def iteritems(self): |
872 return self.items().__iter__(); |
896 return self.items().__iter__() |
873 |
897 |
874 def setdefault(self, key, default_value): |
898 def setdefault(self, key, default_value): |
875 if not self.has_key(key): |
899 if key not in self: |
876 self[key] = default_value |
900 self[key] = default_value |
877 |
901 |
878 def get(self, key, default_=None): |
902 def get(self, key, default_=None): |
879 if not self.has_key(key): |
903 if key not in self: |
880 return default_ |
904 return default_ |
881 return self[key] |
905 return self[key] |
882 |
906 |
883 def update(self, d): |
907 def update(self, d): |
884 for k,v in d.iteritems(): |
908 for k, v in d.iteritems(): |
885 self[k] = v |
909 self[k] = v |
886 |
910 |
887 def getObject(self): |
911 def getObject(self): |
888 """ |
912 """ |
889 Return the javascript Object which this class uses to store |
913 Return the javascript Object which this class uses to store |
946 return object.slice(lower, upper); |
974 return object.slice(lower, upper); |
947 |
975 |
948 return null; |
976 return null; |
949 """) |
977 """) |
950 |
978 |
|
979 |
951 def str(text): |
980 def str(text): |
952 JS(""" |
981 JS(""" |
953 if (pyjslib.hasattr(text,"__str__")) { |
982 if (pyjslib.hasattr(text,"__str__")) { |
954 return text.__str__(); |
983 return text.__str__(); |
955 } |
984 } |
956 return String(text); |
985 return String(text); |
957 """) |
986 """) |
958 |
987 |
|
988 |
959 def ord(x): |
989 def ord(x): |
960 if(isString(x) and len(x) is 1): |
990 if(isString(x) and len(x) is 1): |
961 JS(""" |
991 JS(""" |
962 return x.charCodeAt(0); |
992 return x.charCodeAt(0); |
963 """) |
993 """) |
981 t == 'string' || |
1013 t == 'string' || |
982 t == 'undefined' |
1014 t == 'undefined' |
983 ; |
1015 ; |
984 """) |
1016 """) |
985 |
1017 |
|
1018 |
986 def get_pyjs_classtype(x): |
1019 def get_pyjs_classtype(x): |
987 JS(""" |
1020 JS(""" |
988 if (pyjslib.hasattr(x, "__class__")) |
1021 if (pyjslib.hasattr(x, "__class__")) |
989 if (pyjslib.hasattr(x.__class__, "__new__")) |
1022 if (pyjslib.hasattr(x.__class__, "__new__")) |
990 var src = x.__class__.__name__; |
1023 var src = x.__class__.__name__; |
991 return src; |
1024 return src; |
992 return null; |
1025 return null; |
993 """) |
1026 """) |
|
1027 |
994 |
1028 |
995 def repr(x): |
1029 def repr(x): |
996 """ Return the string representation of 'x'. |
1030 """ Return the string representation of 'x'. |
997 """ |
1031 """ |
998 JS(""" |
1032 JS(""" |
1086 |
1120 |
1087 //var s = constructor.replace(new RegExp('_', "g"), '.'); |
1121 //var s = constructor.replace(new RegExp('_', "g"), '.'); |
1088 return "<" + constructor + " object>"; |
1122 return "<" + constructor + " object>"; |
1089 """) |
1123 """) |
1090 |
1124 |
|
1125 |
1091 def float(text): |
1126 def float(text): |
1092 JS(""" |
1127 JS(""" |
1093 return parseFloat(text); |
1128 return parseFloat(text); |
1094 """) |
1129 """) |
1095 |
1130 |
|
1131 |
1096 def int(text, radix=0): |
1132 def int(text, radix=0): |
1097 JS(""" |
1133 JS(""" |
1098 return parseInt(text, radix); |
1134 return parseInt(text, radix); |
1099 """) |
1135 """) |
|
1136 |
1100 |
1137 |
1101 def len(object): |
1138 def len(object): |
1102 JS(""" |
1139 JS(""" |
1103 if (object==null) return 0; |
1140 if (object==null) return 0; |
1104 if (pyjslib.isObject(object) && object.__len__) return object.__len__(); |
1141 if (pyjslib.isObject(object) && object.__len__) return object.__len__(); |
1105 return object.length; |
1142 return object.length; |
1106 """) |
1143 """) |
|
1144 |
1107 |
1145 |
1108 def isinstance(object_, classinfo): |
1146 def isinstance(object_, classinfo): |
1109 if pyjslib.isUndefined(object_): |
1147 if pyjslib.isUndefined(object_): |
1110 return False |
1148 return False |
1111 if not pyjslib.isObject(object_): |
1149 if not pyjslib.isObject(object_): |
1112 |
1150 |
1113 return False |
1151 return False |
1114 if _isinstance(classinfo, Tuple): |
1152 if _isinstance(classinfo, Tuple): |
1115 for ci in classinfo: |
1153 for ci in classinfo: |
1116 if isinstance(object_, ci): |
1154 if isinstance(object_, ci): |
1117 return True |
1155 return True |
1118 return False |
1156 return False |
1119 else: |
1157 else: |
1120 return _isinstance(object_, classinfo) |
1158 return _isinstance(object_, classinfo) |
1121 |
1159 |
|
1160 |
1122 def _isinstance(object_, classinfo): |
1161 def _isinstance(object_, classinfo): |
1123 if not pyjslib.isObject(object_): |
1162 if not pyjslib.isObject(object_): |
1124 return False |
1163 return False |
1125 JS(""" |
1164 JS(""" |
1126 if (object_.__class__){ |
1165 if (object_.__class__){ |
1127 var res = object_ instanceof classinfo.constructor; |
1166 var res = object_ instanceof classinfo.constructor; |
1128 return res; |
1167 return res; |
1129 } |
1168 } |
1130 return false; |
1169 return false; |
1131 """) |
1170 """) |
|
1171 |
1132 |
1172 |
1133 def getattr(obj, name, default_): |
1173 def getattr(obj, name, default_): |
1134 JS(""" |
1174 JS(""" |
1135 if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){ |
1175 if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){ |
1136 if (pyjslib.isUndefined(default_)){ |
1176 if (pyjslib.isUndefined(default_)){ |
1149 } |
1189 } |
1150 fnwrap.__name__ = name; |
1190 fnwrap.__name__ = name; |
1151 return fnwrap; |
1191 return fnwrap; |
1152 """) |
1192 """) |
1153 |
1193 |
|
1194 |
1154 def setattr(obj, name, value): |
1195 def setattr(obj, name, value): |
1155 JS(""" |
1196 JS(""" |
1156 if (!pyjslib.isObject(obj)) return null; |
1197 if (!pyjslib.isObject(obj)) return null; |
1157 |
1198 |
1158 obj[name] = value; |
1199 obj[name] = value; |
1159 |
1200 |
1160 """) |
1201 """) |
|
1202 |
1161 |
1203 |
1162 def hasattr(obj, name): |
1204 def hasattr(obj, name): |
1163 JS(""" |
1205 JS(""" |
1164 if (!pyjslib.isObject(obj)) return false; |
1206 if (!pyjslib.isObject(obj)) return false; |
1165 if (pyjslib.isUndefined(obj[name])) return false; |
1207 if (pyjslib.isUndefined(obj[name])) return false; |
1166 |
1208 |
1167 return true; |
1209 return true; |
1168 """) |
1210 """) |
|
1211 |
1169 |
1212 |
1170 def dir(obj): |
1213 def dir(obj): |
1171 JS(""" |
1214 JS(""" |
1172 var properties=new pyjslib.List(); |
1215 var properties=new pyjslib.List(); |
1173 for (property in obj) properties.append(property); |
1216 for (property in obj) properties.append(property); |
1174 return properties; |
1217 return properties; |
1175 """) |
1218 """) |
|
1219 |
1176 |
1220 |
1177 def filter(obj, method, sequence=None): |
1221 def filter(obj, method, sequence=None): |
1178 # object context is LOST when a method is passed, hence object must be passed separately |
1222 # object context is LOST when a method is passed, hence object must be passed separately |
1179 # to emulate python behaviour, should generate this code inline rather than as a function call |
1223 # to emulate python behaviour, should generate this code inline rather than as a function call |
1180 items = [] |
1224 items = [] |
1257 def isObject(a): |
1302 def isObject(a): |
1258 JS(""" |
1303 JS(""" |
1259 return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a); |
1304 return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a); |
1260 """) |
1305 """) |
1261 |
1306 |
|
1307 |
1262 def isFunction(a): |
1308 def isFunction(a): |
1263 JS(""" |
1309 JS(""" |
1264 return typeof a == 'function'; |
1310 return typeof a == 'function'; |
1265 """) |
1311 """) |
1266 |
1312 |
|
1313 |
1267 def isString(a): |
1314 def isString(a): |
1268 JS(""" |
1315 JS(""" |
1269 return typeof a == 'string'; |
1316 return typeof a == 'string'; |
1270 """) |
1317 """) |
1271 |
1318 |
|
1319 |
1272 def isNull(a): |
1320 def isNull(a): |
1273 JS(""" |
1321 JS(""" |
1274 return typeof a == 'object' && !a; |
1322 return typeof a == 'object' && !a; |
1275 """) |
1323 """) |
1276 |
1324 |
|
1325 |
1277 def isArray(a): |
1326 def isArray(a): |
1278 JS(""" |
1327 JS(""" |
1279 return pyjslib.isObject(a) && a.constructor == Array; |
1328 return pyjslib.isObject(a) && a.constructor == Array; |
1280 """) |
1329 """) |
1281 |
1330 |
|
1331 |
1282 def isUndefined(a): |
1332 def isUndefined(a): |
1283 JS(""" |
1333 JS(""" |
1284 return typeof a == 'undefined'; |
1334 return typeof a == 'undefined'; |
1285 """) |
1335 """) |
1286 |
1336 |
|
1337 |
1287 def isIteratable(a): |
1338 def isIteratable(a): |
1288 JS(""" |
1339 JS(""" |
1289 return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__); |
1340 return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__); |
1290 """) |
1341 """) |
1291 |
1342 |
|
1343 |
1292 def isNumber(a): |
1344 def isNumber(a): |
1293 JS(""" |
1345 JS(""" |
1294 return typeof a == 'number' && isFinite(a); |
1346 return typeof a == 'number' && isFinite(a); |
1295 """) |
1347 """) |
|
1348 |
1296 |
1349 |
1297 def toJSObjects(x): |
1350 def toJSObjects(x): |
1298 """ |
1351 """ |
1299 Convert the pyjs pythonic List and Dict objects into javascript Object and Array |
1352 Convert the pyjs pythonic List and Dict objects into javascript Object and Array |
1300 objects, recursively. |
1353 objects, recursively. |