diff -r c02818d7e29f -r 7e61baa047f0 svgui/pyjs/lib/pyjslib.py --- a/svgui/pyjs/lib/pyjslib.py Mon Aug 14 22:30:41 2017 +0300 +++ b/svgui/pyjs/lib/pyjslib.py Mon Aug 14 23:27:15 2017 +0300 @@ -19,6 +19,7 @@ # must declare import _before_ importing sys + def import_module(path, parent_module, module_name, dynamic=1, async=False): """ """ @@ -184,11 +185,13 @@ } """) + class Object: pass object = Object + class Modload: def __init__(self, path, app_modlist, app_imported_fn, dynamic, @@ -212,11 +215,13 @@ 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): @@ -225,6 +230,7 @@ import sys + class BaseException: name = "BaseException" @@ -242,27 +248,31 @@ 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): @@ -407,6 +417,7 @@ """) + class Class: def __init__(self, name): self.name = name @@ -414,6 +425,7 @@ def __str___(self): return self.name + def eq(a,b): JS(""" if (pyjslib.hasattr(a, "__cmp__")) { @@ -424,6 +436,7 @@ return a == b; """) + def cmp(a,b): if hasattr(a, "__cmp__"): return a.__cmp__(b) @@ -436,6 +449,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 +470,7 @@ return Boolean(v); """) + class List: def __init__(self, data=None): JS(""" @@ -605,6 +620,7 @@ list = List + class Tuple: def __init__(self, data=None): JS(""" @@ -900,6 +916,8 @@ dict = Dict # taken from mochikit: range( [start,] stop[, step] ) + + def range(): JS(""" var start = 0; @@ -930,6 +948,7 @@ } """) + def slice(object, lower, upper): JS(""" if (pyjslib.isString(object)) { @@ -948,6 +967,7 @@ return null; """) + def str(text): JS(""" if (pyjslib.hasattr(text,"__str__")) { @@ -956,6 +976,7 @@ return String(text); """) + def ord(x): if(isString(x) and len(x) is 1): JS(""" @@ -967,11 +988,13 @@ """) return None + def chr(x): JS(""" return String.fromCharCode(x) """) + def is_basetype(x): JS(""" var t = typeof(x); @@ -983,6 +1006,7 @@ ; """) + def get_pyjs_classtype(x): JS(""" if (pyjslib.hasattr(x, "__class__")) @@ -992,6 +1016,7 @@ return null; """) + def repr(x): """ Return the string representation of 'x'. """ @@ -1088,16 +1113,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,6 +1133,7 @@ return object.length; """) + def isinstance(object_, classinfo): if pyjslib.isUndefined(object_): return False @@ -1119,6 +1148,7 @@ else: return _isinstance(object_, classinfo) + def _isinstance(object_, classinfo): if not pyjslib.isObject(object_): return False @@ -1130,6 +1160,7 @@ return false; """) + def getattr(obj, name, default_): JS(""" if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){ @@ -1151,6 +1182,7 @@ return fnwrap; """) + def setattr(obj, name, value): JS(""" if (!pyjslib.isObject(obj)) return null; @@ -1159,6 +1191,7 @@ """) + def hasattr(obj, name): JS(""" if (!pyjslib.isObject(obj)) return false; @@ -1167,6 +1200,7 @@ return true; """) + def dir(obj): JS(""" var properties=new pyjslib.List(); @@ -1174,6 +1208,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 +1275,7 @@ next_hash_id = 0 + def hash(obj): JS(""" if (obj == null) return null; @@ -1259,41 +1295,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 +1381,7 @@ """) return x + def printFunc(objs): JS(""" if ($wnd.console==undefined) return; @@ -1348,6 +1393,7 @@ console.debug(s) """) + def type(clsname, bases=None, methods=None): """ creates a class, derived from bases, with methods and variables """