# Copyright 2006 James Tauber and contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=too-many-function-args,undefined-variable,no-absolute-import,assign-to-new-keyword,nonzero-method,next-method-called,next-method-defined
# iteration from Bob Ippolito's Iteration in JavaScript
from __pyjamas__ import JS
# must declare import _before_ importing sys
def import_module(path, parent_module, module_name, dynamic=1, async=False):
if (module_name == "sys" || module_name == 'pyjslib')
/*module_load_request[module_name] = 1;*/
var override_name = sys.platform + "." + module_name;
if (((sys.overrides != null) &&
(sys.overrides.has_key(override_name))))
cache_file = sys.overrides.__getitem__(override_name) ;
cache_file = module_name ;
cache_file = (path + cache_file + '.cache.js' ) ;
//alert("cache " + cache_file + " " + module_name + " " + parent_module);
if (module_load_request[module_name])
if (module_load_request[module_name] >= 3 && parent_module != null)
//onload_fn = parent_module + '.' + module_name + ' = ' + module_name + ';';
//pyjs_eval(onload_fn); /* set up the parent-module namespace */
if (typeof (module_load_request[module_name]) == 'undefined')
module_load_request[module_name] = 1;
/* following a load, this first executes the script
* "preparation" function MODULENAME_loaded_fn()
* and then sets up the loaded module in the namespace
onload_fn = ''; // module_name + "_loaded_fn();"
if (parent_module != null)
//onload_fn += parent_module + '.' + module_name + ' = ' + module_name + ';';
/*pmod = parent_module + '.' + module_name;
onload_fn += 'alert("' + pmod + '"+' + pmod+');';*/
/* this one tacks the script onto the end of the DOM
pyjs_load_script(cache_file, onload_fn, async);
/* this one actually RUNS the script (eval) into the page.
my feeling is that this would be better for non-async
but i can't get it to work entirely yet.
/*pyjs_ajax_eval(cache_file, onload_fn, async);*/
if (module_name != "pyjslib" &&
function import_wait(proceed_fn, parent_mod, dynamic) {
var element = $doc.createElement("div");
$doc.body.appendChild(element);
function write_dom(txt) {
element.innerHTML = txt + '<br />';
for (l in module_load_request)
var m = module_load_request[l];
if (l == "sys" || l == 'pyjslib')
//write_dom( " import wait " + wait_count + " " + status + " parent_mod " + parent_mod);
setTimeout(wait, timeoutperiod);
for (l in module_load_request)
var m = module_load_request[l];
if (l == "sys" || l == 'pyjslib')
module_load_request[l] = 4;
if ((parent_mod != null) && (l == parent_mod))
setTimeout(wait, timeoutperiod);
/* cheat and move app on to next stage */
module_load_request[l] = 3;
setTimeout(wait, timeoutperiod);
//alert("waited for module " + l + ": loaded");
module_load_request[l] = 4;
//alert("module wait done");
if (proceed_fn.importDone)
proceed_fn.importDone(proceed_fn);
# pylint: disable=old-style-class
def __init__(self, path, app_modlist, app_imported_fn, dynamic,
self.app_modlist = app_modlist
self.app_imported_fn = app_imported_fn
self.parent_mod = parent_mod
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)
if self.idx >= len(self.app_modlist):
import_wait(self.app_imported_fn, self.parent_mod, self.dynamic)
import_wait(getattr(self, "next"), self.parent_mod, self.dynamic)
def get_module(module_name):
_ev = "__mod = %s;" % module_name
def preload_app_modules(path, app_modnames, app_imported_fn, dynamic,
loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod)
# as comment on line 20 says
# import sys should be below
import sys # noqa # pylint: disable=wrong-import-order,unused-import,wrong-import-position
def __init__(self, *args):
elif len(self.args) is 1:
return repr(self.args[0])
class Exception(BaseException):
class TypeError(BaseException):
class StandardError(Exception):
class LookupError(StandardError):
return self.name + ": " + self.args[0]
class KeyError(LookupError):
class AttributeError(StandardError):
return "AttributeError: %s of %s" % (self.args[1], self.args[0])
pyjslib.StopIteration = function () { };
pyjslib.StopIteration.prototype = new Error();
pyjslib.StopIteration.name = 'StopIteration';
pyjslib.StopIteration.message = 'StopIteration';
pyjslib.String_find = function(sub, start, end) {
var pos=this.indexOf(sub, start);
if (pyjslib.isUndefined(end)) return pos;
if (pos + sub.length>end) return -1;
pyjslib.String_join = function(data) {
if (pyjslib.isArray(data)) {
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
if (e != pyjslib.StopIteration) throw e;
pyjslib.String_isdigit = function() {
return (this.match(/^\d+$/g) != null);
pyjslib.String_replace = function(old, replace, count) {
if (!pyjslib.isString(old)) return this.__replace(old, replace);
if (!pyjslib.isUndefined(count)) do_max=true;
while (start<this.length) {
if (do_max && !count--) break;
pos=this.indexOf(old, start);
new_str+=this.substring(start, pos) + replace;
if (start<this.length) new_str+=this.substring(start);
pyjslib.String_split = function(sep, maxsplit) {
var items=new pyjslib.List();
if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) {
subject=subject.replace(/\s+/g, sep);
else if (!pyjslib.isUndefined(maxsplit)) do_max=true;
if (subject.length == 0) {
while (start<subject.length) {
if (do_max && !maxsplit--) break;
pos=subject.indexOf(sep, start);
items.append(subject.substring(start, pos));
if (start<=subject.length) items.append(subject.substring(start));
pyjslib.String___iter__ = function() {
throw pyjslib.StopIteration;
return s.substring(i++, i, 1);
pyjslib.String_strip = function(chars) {
return this.lstrip(chars).rstrip(chars);
pyjslib.String_lstrip = function(chars) {
if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, "");
return this.replace(new RegExp("^[" + chars + "]+"), "");
pyjslib.String_rstrip = function(chars) {
if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, "");
return this.replace(new RegExp("[" + chars + "]+$"), "");
pyjslib.String_startswith = function(prefix, start) {
if (pyjslib.isUndefined(start)) start = 0;
if (this.substring(start, prefix.length) == prefix) return true;
def __init__(self, name):
if (pyjslib.hasattr(a, "__cmp__")) {
return a.__cmp__(b) == 0;
} else if (pyjslib.hasattr(b, "__cmp__")) {
return b.__cmp__(a) == 0;
if hasattr(a, "__cmp__"):
elif hasattr(b, "__cmp__"):
# this needs to stay in native code without any dependencies here,
# because this is used by if and while, we need to prevent
def __init__(self, data=None):
JS(""" this.l[this.l.length] = item;""")
if (pyjslib.isArray(data)) {
for (var i=0; i < data.length; i++) {
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
if (e != pyjslib.StopIteration) throw e;
var index=this.index(value);
if (index<0) return false;
def index(self, value, start=0):
var length=this.l.length;
for (var i=start; i<length; i++) {
def insert(self, index, value):
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""")
if (index<0) index = this.l.length + index;
if not isinstance(l, List):
ll = cmp(self.__getitem__(x), l[x])
def slice(self, lower, upper):
if (upper==null) return pyjslib.List(this.l.slice(lower));
return pyjslib.List(this.l.slice(lower, upper));
def __getitem__(self, index):
if (index<0) index = this.l.length + index;
def __setitem__(self, index, value):
JS(""" this.l[index]=value;""")
def __delitem__(self, index):
JS(""" this.l.splice(index, 1);""")
JS(""" return this.l.length;""")
def __contains__(self, value):
return self.index(value) >= 0
throw pyjslib.StopIteration;
JS(""" this.l.reverse();""")
def sort(self, compareFunc=None, keyFunc=None, reverse=False):
return -compareFunc(keyFunc(a), keyFunc(b))
return compareFunc(keyFunc(a), keyFunc(b))
return -compareFunc(a, b)
Access the javascript Array that is used internally by this list
def __init__(self, data=None):
JS(""" this.l[this.l.length] = item;""")
if (pyjslib.isArray(data)) {
for (var i=0; i < data.length; i++) {
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
if (e != pyjslib.StopIteration) throw e;
var index=this.index(value);
if (index<0) return false;
def index(self, value, start=0):
var length=this.l.length;
for (var i=start; i<length; i++) {
def insert(self, index, value):
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""")
if (index<0) index = this.l.length + index;
if not isinstance(l, Tuple):
ll = cmp(self.__getitem__(x), l[x])
def slice(self, lower, upper):
if (upper==null) return pyjslib.Tuple(this.l.slice(lower));
return pyjslib.Tuple(this.l.slice(lower, upper));
def __getitem__(self, index):
if (index<0) index = this.l.length + index;
def __setitem__(self, index, value):
JS(""" this.l[index]=value;""")
def __delitem__(self, index):
JS(""" this.l.splice(index, 1);""")
JS(""" return this.l.length;""")
def __contains__(self, value):
return self.index(value) >= 0
throw pyjslib.StopIteration;
JS(""" this.l.reverse();""")
def sort(self, compareFunc=None, keyFunc=None, reverse=False):
return -compareFunc(keyFunc(a), keyFunc(b))
return compareFunc(keyFunc(a), keyFunc(b))
return -compareFunc(a, b)
Access the javascript Array that is used internally by this list
def __init__(self, data=None):
if (pyjslib.isArray(data)) {
this.__setitem__(item[0], item[1]);
//var sKey=pyjslib.hash(item[0]);
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
this.__setitem__(item.__getitem__(0), item.__getitem__(1));
if (e != pyjslib.StopIteration) throw e;
else if (pyjslib.isObject(data)) {
this.__setitem__(key, data[key]);
def __setitem__(self, key, value):
var sKey = pyjslib.hash(key);
this.d[sKey]=[key, value];
def __getitem__(self, key):
var sKey = pyjslib.hash(key);
if (pyjslib.isUndefined(value)){
throw pyjslib.KeyError(key);
for (var i in this.d) size++;
return self.__contains__(key)
def __delitem__(self, key):
var sKey = pyjslib.hash(key);
def __contains__(self, key):
var sKey = pyjslib.hash(key);
return (pyjslib.isUndefined(this.d[sKey])) ? false : true;
var keys=new pyjslib.List();
for (var key in this.d) {
keys.append(this.d[key][0]);
var values=new pyjslib.List();
for (var key in this.d) values.append(this.d[key][1]);
var items = new pyjslib.List();
for (var key in this.d) {
items.append(new pyjslib.List(kv))
return self.keys().__iter__()
return self.values().__iter__()
return self.items().__iter__()
def setdefault(self, key, default_value):
self[key] = default_value
def get(self, key, default_=None):
for k, v in d.iteritems():
Return the javascript Object which this class uses to store
dictionary keys and values
return Dict(self.items())
# taken from mochikit: range( [start,] stop[, step] )
if (arguments.length == 2) {
else if (arguments.length == 3) {
else if (arguments.length>0) stop = arguments[0];
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) throw pyjslib.StopIteration;
def slice(object, lower, upper):
if (pyjslib.isString(object)) {
lower = object.length + lower;
upper = object.length + upper;
if (pyjslib.isNull(upper)) upper=object.length;
return object.substring(lower, upper);
if (pyjslib.isObject(object) && object.slice)
return object.slice(lower, upper);
if (pyjslib.hasattr(text,"__str__")) {
if isString(x) and len(x) is 1:
throw pyjslib.TypeError();
return String.fromCharCode(x)
def get_pyjs_classtype(x):
if (pyjslib.hasattr(x, "__class__"))
if (pyjslib.hasattr(x.__class__, "__new__"))
var src = x.__class__.__name__;
""" Return the string representation of 'x'.
//alert("repr typeof " + t + " : " + x);
return "<function " + x.toString() + ">";
if (x.indexOf("'") == -1)
if (x.indexOf('"') == -1)
var s = x.replace(new RegExp('"', "g"), '\\\\"');
// If we get here, x is an object. See if it's a Pyjamas class.
if (!pyjslib.hasattr(x, "__init__"))
return "<" + x.toString() + ">";
// Handle the common Pyjamas data types.
var constructor = "UNKNOWN";
constructor = pyjslib.get_pyjs_classtype(x);
//alert("repr constructor: " + constructor);
if (constructor == "Tuple") {
var contents = x.getArray();
for (var i=0; i < contents.length; i++) {
s += pyjslib.repr(contents[i]);
if (i < contents.length - 1)
if (constructor == "List") {
var contents = x.getArray();
for (var i=0; i < contents.length; i++) {
s += pyjslib.repr(contents[i]);
if (i < contents.length - 1)
if (constructor == "Dict") {
for (var i=0; i<keys.length; i++) {
s += pyjslib.repr(key) + ": " + pyjslib.repr(x.d[key]);
// If we get here, the class isn't one we know -> return the class name.
// Note that we replace underscores with dots so that the name will
// (hopefully!) look like the original Python name.
//var s = constructor.replace(new RegExp('_', "g"), '.');
return "<" + constructor + " object>";
return parseInt(text, radix);
if (object==null) return 0;
if (pyjslib.isObject(object) && object.__len__) return object.__len__();
def isinstance(object_, classinfo):
if pyjslib.isUndefined(object_):
if not pyjslib.isObject(object_):
if _isinstance(classinfo, Tuple):
if isinstance(object_, ci):
return _isinstance(object_, classinfo)
def _isinstance(object_, classinfo):
if not pyjslib.isObject(object_):
var res = object_ instanceof classinfo.constructor;
def getattr(obj, name, default_=None):
if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){
if (pyjslib.isUndefined(default_)){
throw pyjslib.AttributeError(obj, name);
if (!pyjslib.isFunction(obj[name])) return obj[name];
var fnwrap = function() {
for (var i = 0; i < arguments.length; i++) {
return obj[name].apply(obj,args);
def setattr(obj, name, value):
if (!pyjslib.isObject(obj)) return null;
if (!pyjslib.isObject(obj)) return false;
if (pyjslib.isUndefined(obj[name])) return false;
var properties=new pyjslib.List();
for (property in obj) properties.append(property);
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
if method.call(obj, item):
def map(obj, method, sequence=None):
items.append(method(item))
items.append(method.call(obj, item))
enumeration.append([nextIndex, item])
nextIndex = nextIndex + 1
if (obj == null) return null;
if (obj.$H) return obj.$H;
if (obj.__hash__) return obj.__hash__();
if (obj.constructor == String || obj.constructor == Number || obj.constructor == Date) return obj;
obj.$H = ++pyjslib.next_hash_id;
# type functions from Douglas Crockford's Remedial Javascript: http://www.crockford.com/javascript/remedial.html
return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a);
return typeof a == 'function';
return typeof a == 'string';
return typeof a == 'object' && !a;
return pyjslib.isObject(a) && a.constructor == Array;
return typeof a == 'undefined';
return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__);
return typeof a == 'number' && isFinite(a);
Convert the pyjs pythonic List and Dict objects into javascript Object and Array
for(var k=0; k < x.length; k++) {
var tv = pyjslib.toJSObjects(v);
result[o[i][0].toString()] = o[i][1];
return pyjslib.toJSObjects(result)
elif isinstance(x, List):
elif hasattr(x, '__class__'):
# we do not have a special implementation for custom
# classes, just pass it on
var tv = pyjslib.toJSObjects(v)
if ($wnd.console==undefined) return;
for(var i=0; i < objs.length; i++) {
def type(clsname, bases=None, methods=None):
""" creates a class, derived from bases, with methods and variables
JS(" return pyjs_type(clsname, bss, mths); ")