diff -r 31e63e25b4cc -r 64beb9e9c749 svgui/pyjs/lib/pyjslib.py --- a/svgui/pyjs/lib/pyjslib.py Mon Aug 21 20:17:19 2017 +0000 +++ b/svgui/pyjs/lib/pyjslib.py Mon Aug 21 23:22:58 2017 +0300 @@ -19,8 +19,9 @@ # must declare import _before_ importing sys + def import_module(path, parent_module, module_name, dynamic=1, async=False): - """ + """ """ JS(""" @@ -38,7 +39,7 @@ } var override_name = sys.platform + "." + module_name; - if (((sys.overrides != null) && + if (((sys.overrides != null) && (sys.overrides.has_key(override_name)))) { cache_file = sys.overrides.__getitem__(override_name) ; @@ -67,7 +68,7 @@ module_load_request[module_name] = 1; } - /* following a load, this first executes the script + /* following a load, this first executes the script * "preparation" function MODULENAME_loaded_fn() * and then sets up the loaded module in the namespace * of the parent. @@ -105,6 +106,7 @@ """) + JS(""" function import_wait(proceed_fn, parent_mod, dynamic) { @@ -184,11 +186,14 @@ } """) + class Object: pass + object = Object + class Modload: def __init__(self, path, app_modlist, app_imported_fn, dynamic, @@ -196,15 +201,15 @@ self.app_modlist = app_modlist self.app_imported_fn = app_imported_fn self.path = path - self.idx = 0; + self.idx = 0 self.dynamic = dynamic self.parent_mod = parent_mod def next(self): - + for i in range(len(self.app_modlist[self.idx])): app = self.app_modlist[self.idx][i] - import_module(self.path, self.parent_mod, app, self.dynamic, True); + import_module(self.path, self.parent_mod, app, self.dynamic, True) self.idx += 1 if self.idx >= len(self.app_modlist): @@ -212,18 +217,24 @@ else: import_wait(getattr(self, "next"), self.parent_mod, self.dynamic) + def get_module(module_name): ev = "__mod = %s;" % module_name JS("pyjs_eval(ev);") return __mod + def preload_app_modules(path, app_modnames, app_imported_fn, dynamic, parent_mod=None): loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod) loader.next() -import sys + +# as comment on line 20 says +# import sys should be below +import sys # noqa + class BaseException: @@ -242,32 +253,37 @@ def toString(self): return str(self) + class Exception(BaseException): - name = "Exception" + class TypeError(BaseException): name = "TypeError" + class StandardError(Exception): name = "StandardError" + class LookupError(StandardError): name = "LookupError" def toString(self): return self.name + ": " + self.args[0] + class KeyError(LookupError): name = "KeyError" + class AttributeError(StandardError): - name = "AttributeError" def toString(self): return "AttributeError: %s of %s" % (self.args[1], self.args[0]) + JS(""" pyjslib.StopIteration = function () { }; pyjslib.StopIteration.prototype = new Error(); @@ -407,6 +423,7 @@ """) + class Class: def __init__(self, name): self.name = name @@ -414,7 +431,8 @@ def __str___(self): return self.name -def eq(a,b): + +def eq(a, b): JS(""" if (pyjslib.hasattr(a, "__cmp__")) { return a.__cmp__(b) == 0; @@ -424,7 +442,8 @@ return a == b; """) -def cmp(a,b): + +def cmp(a, b): if hasattr(a, "__cmp__"): return a.__cmp__(b) elif hasattr(b, "__cmp__"): @@ -436,6 +455,7 @@ else: return 0 + def bool(v): # this needs to stay in native code without any dependencies here, # because this is used by if and while, we need to prevent @@ -456,6 +476,7 @@ return Boolean(v); """) + class List: def __init__(self, data=None): JS(""" @@ -511,7 +532,7 @@ def insert(self, index, value): JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") - def pop(self, index = -1): + def pop(self, index=-1): JS(""" if (index<0) index = this.l.length + index; var a = this.l[index]; @@ -580,15 +601,15 @@ global cmp compareFunc = cmp if keyFunc and reverse: - def thisSort1(a,b): + def thisSort1(a, b): return -compareFunc(keyFunc(a), keyFunc(b)) self.l.sort(thisSort1) elif keyFunc: - def thisSort2(a,b): + def thisSort2(a, b): return compareFunc(keyFunc(a), keyFunc(b)) self.l.sort(thisSort2) elif reverse: - def thisSort3(a,b): + def thisSort3(a, b): return -compareFunc(a, b) self.l.sort(thisSort3) else: @@ -603,8 +624,10 @@ def __str__(self): return repr(self) + list = List + class Tuple: def __init__(self, data=None): JS(""" @@ -660,7 +683,7 @@ def insert(self, index, value): JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""") - def pop(self, index = -1): + def pop(self, index=-1): JS(""" if (index<0) index = this.l.length + index; var a = this.l[index]; @@ -729,15 +752,15 @@ global cmp compareFunc = cmp if keyFunc and reverse: - def thisSort1(a,b): + def thisSort1(a, b): return -compareFunc(keyFunc(a), keyFunc(b)) self.l.sort(thisSort1) elif keyFunc: - def thisSort2(a,b): + def thisSort2(a, b): return compareFunc(keyFunc(a), keyFunc(b)) self.l.sort(thisSort2) elif reverse: - def thisSort3(a,b): + def thisSort3(a, b): return -compareFunc(a, b) self.l.sort(thisSort3) else: @@ -752,6 +775,7 @@ def __str__(self): return repr(self) + tuple = Tuple @@ -866,22 +890,22 @@ return self.__iter__() def itervalues(self): - return self.values().__iter__(); + return self.values().__iter__() def iteritems(self): - return self.items().__iter__(); + return self.items().__iter__() def setdefault(self, key, default_value): - if not self.has_key(key): + if key not in self: self[key] = default_value def get(self, key, default_=None): - if not self.has_key(key): + if key not in self: return default_ return self[key] def update(self, d): - for k,v in d.iteritems(): + for k, v in d.iteritems(): self[k] = v def getObject(self): @@ -897,9 +921,12 @@ def __str__(self): return repr(self) + dict = Dict # taken from mochikit: range( [start,] stop[, step] ) + + def range(): JS(""" var start = 0; @@ -930,6 +957,7 @@ } """) + def slice(object, lower, upper): JS(""" if (pyjslib.isString(object)) { @@ -948,6 +976,7 @@ return null; """) + def str(text): JS(""" if (pyjslib.hasattr(text,"__str__")) { @@ -956,6 +985,7 @@ return String(text); """) + def ord(x): if(isString(x) and len(x) is 1): JS(""" @@ -967,11 +997,13 @@ """) return None + def chr(x): JS(""" return String.fromCharCode(x) """) + def is_basetype(x): JS(""" var t = typeof(x); @@ -983,6 +1015,7 @@ ; """) + def get_pyjs_classtype(x): JS(""" if (pyjslib.hasattr(x, "__class__")) @@ -992,6 +1025,7 @@ return null; """) + def repr(x): """ Return the string representation of 'x'. """ @@ -1088,16 +1122,19 @@ return "<" + constructor + " object>"; """) + def float(text): JS(""" return parseFloat(text); """) + def int(text, radix=0): JS(""" return parseInt(text, radix); """) + def len(object): JS(""" if (object==null) return 0; @@ -1105,11 +1142,12 @@ return object.length; """) + def isinstance(object_, classinfo): if pyjslib.isUndefined(object_): return False if not pyjslib.isObject(object_): - + return False if _isinstance(classinfo, Tuple): for ci in classinfo: @@ -1119,6 +1157,7 @@ else: return _isinstance(object_, classinfo) + def _isinstance(object_, classinfo): if not pyjslib.isObject(object_): return False @@ -1130,6 +1169,7 @@ return false; """) + def getattr(obj, name, default_): JS(""" if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){ @@ -1151,6 +1191,7 @@ return fnwrap; """) + def setattr(obj, name, value): JS(""" if (!pyjslib.isObject(obj)) return null; @@ -1159,6 +1200,7 @@ """) + def hasattr(obj, name): JS(""" if (!pyjslib.isObject(obj)) return false; @@ -1167,6 +1209,7 @@ return true; """) + def dir(obj): JS(""" var properties=new pyjslib.List(); @@ -1174,6 +1217,7 @@ return properties; """) + def filter(obj, method, sequence=None): # object context is LOST when a method is passed, hence object must be passed separately # to emulate python behaviour, should generate this code inline rather than as a function call @@ -1240,6 +1284,7 @@ next_hash_id = 0 + def hash(obj): JS(""" if (obj == null) return null; @@ -1259,41 +1304,49 @@ return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a); """) + def isFunction(a): JS(""" return typeof a == 'function'; """) + def isString(a): JS(""" return typeof a == 'string'; """) + def isNull(a): JS(""" return typeof a == 'object' && !a; """) + def isArray(a): JS(""" return pyjslib.isObject(a) && a.constructor == Array; """) + def isUndefined(a): JS(""" return typeof a == 'undefined'; """) + def isIteratable(a): JS(""" return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__); """) + def isNumber(a): JS(""" return typeof a == 'number' && isFinite(a); """) + def toJSObjects(x): """ Convert the pyjs pythonic List and Dict objects into javascript Object and Array @@ -1337,6 +1390,7 @@ """) return x + def printFunc(objs): JS(""" if ($wnd.console==undefined) return; @@ -1348,6 +1402,7 @@ console.debug(s) """) + def type(clsname, bases=None, methods=None): """ creates a class, derived from bases, with methods and variables """ @@ -1362,4 +1417,3 @@ if bases: JS("bss = bases.l;") JS(" return pyjs_type(clsname, bss, mths); ") -