diff -r f4d1d5d9ccbb -r 90f2f20367bc web/data/cubicweb.python.js --- a/web/data/cubicweb.python.js Tue Jul 27 12:36:03 2010 +0200 +++ b/web/data/cubicweb.python.js Wed Nov 03 16:38:28 2010 +0100 @@ -1,18 +1,14 @@ -/* +/** * This file contains extensions for standard javascript types * */ ONE_DAY = 86400000; // (in milliseconds) - // ========== DATE EXTENSIONS ========== /// - Date.prototype.equals = function(other) { /* compare with other date ignoring time differences */ - if (this.getYear() == other.getYear() && - this.getMonth() == other.getMonth() && - this.getDate() == other.getDate()) { - return true; + if (this.getYear() == other.getYear() && this.getMonth() == other.getMonth() && this.getDate() == other.getDate()) { + return true; } return false; }; @@ -24,7 +20,7 @@ }; Date.prototype.sub = function(days) { - return this.add(-days); + return this.add( - days); }; Date.prototype.iadd = function(days) { @@ -39,33 +35,37 @@ this.setTime(this.getTime() - (days * ONE_DAY)); }; -/* +/** + * .. function:: Date.prototype.nextMonth() + * * returns the first day of the next month */ Date.prototype.nextMonth = function() { if (this.getMonth() == 11) { - var d =new Date(this.getFullYear()+1, 0, 1); - return d; + var d = new Date(this.getFullYear() + 1, 0, 1); + return d; } else { - var d2 = new Date(this.getFullYear(), this.getMonth()+1, 1); - return d2; + var d2 = new Date(this.getFullYear(), this.getMonth() + 1, 1); + return d2; } }; -/* +/** + * .. function:: Date.prototype.getRealDay() + * * returns the day of week, 0 being monday, 6 being sunday */ Date.prototype.getRealDay = function() { // getDay() returns 0 for Sunday ==> 6 for Saturday - return (this.getDay()+6) % 7; + return (this.getDay() + 6) % 7; }; Date.prototype.strftime = function(fmt) { if (this.toLocaleFormat !== undefined) { // browser dependent - return this.toLocaleFormat(fmt); + return this.toLocaleFormat(fmt); } // XXX implement at least a decent fallback implementation - return this.getFullYear() + '/' + (this.getMonth()+1) + '/' + this.getDate(); + return this.getFullYear() + '/' + (this.getMonth() + 1) + '/' + this.getDate(); }; var _DATE_FORMAT_REGXES = { @@ -74,231 +74,131 @@ 'm': new RegExp('^[0-9]{1,2}'), 'H': new RegExp('^[0-9]{1,2}'), 'M': new RegExp('^[0-9]{1,2}') -} +}; -/* +/** + * .. function:: _parseDate(datestring, format) + * * _parseData does the actual parsing job needed by `strptime` */ function _parseDate(datestring, format) { var skip0 = new RegExp('^0*[0-9]+'); var parsed = {}; - for (var i1=0,i2=0;i1 12) { - return null; - } - // !!! month indexes start at 0 in javascript !!! - date.setMonth(parsed.m - 1); + if (parsed.m < 1 || parsed.m > 12) { + return null; + } + // !!! month indexes start at 0 in javascript !!! + date.setMonth(parsed.m - 1); } if (parsed.d) { - if (parsed.m < 1 || parsed.m > 31) { - return null; - } - date.setDate(parsed.d); + if (parsed.m < 1 || parsed.m > 31) { + return null; + } + date.setDate(parsed.d); } if (parsed.H) { - if (parsed.H < 0 || parsed.H > 23) { - return null; - } - date.setHours(parsed.H); + if (parsed.H < 0 || parsed.H > 23) { + return null; + } + date.setHours(parsed.H); } if (parsed.M) { - if (parsed.M < 0 || parsed.M > 59) { - return null; - } - date.setMinutes(parsed.M); + if (parsed.M < 0 || parsed.M > 59) { + return null; + } + date.setMinutes(parsed.M); } return date; } // ========== END OF DATE EXTENSIONS ========== /// - - - -// ========== ARRAY EXTENSIONS ========== /// -Array.prototype.contains = function(element) { - return findValue(this, element) != -1; -}; - -// ========== END OF ARRAY EXTENSIONS ========== /// - - - // ========== STRING EXTENSIONS ========== // - -/* python-like startsWith method for js strings +/** + * .. function:: String.prototype.startswith(prefix) + * + * python-like startsWith method for js strings * >>> */ -String.prototype.startsWith = function(prefix) { +String.prototype.startswith = function(prefix) { return this.indexOf(prefix) == 0; }; -/* python-like endsWith method for js strings */ -String.prototype.endsWith = function(suffix) { +/** + * .. function:: String.prototype.endswith(suffix) + * + * python-like endsWith method for js strings + */ +String.prototype.endswith = function(suffix) { var startPos = this.length - suffix.length; - if (startPos < 0) { return false; } + if (startPos < 0) { + return false; + } return this.lastIndexOf(suffix, startPos) == startPos; }; -/* python-like strip method for js strings */ +/** + * .. function:: String.prototype.strip() + * + * python-like strip method for js strings + */ String.prototype.strip = function() { return this.replace(/^\s*(.*?)\s*$/, "$1"); }; -/* py-equiv: string in list */ -String.prototype.in_ = function(values) { - return findValue(values, this) != -1; -}; - -/* py-equiv: str.join(list) */ -String.prototype.join = function(args) { - return args.join(this); -}; +// ========= class factories ========= // -/* python-like list builtin - * transforms an iterable in a js sequence - * >>> gen = ifilter(function(x) {return x%2==0}, range(10)) - * >>> s = list(gen) - * [0,2,4,6,8] - */ -function list(iterable) { - var iterator = iter(iterable); - var result = []; - while (true) { - /* iterates until StopIteration occurs */ - try { - result.push(iterator.next()); - } catch (exc) { - if (exc != StopIteration) { throw exc; } - return result; - } - } -} - -/* py-equiv: getattr(obj, attrname, default=None) */ -function getattr(obj, attrname, defaultValue) { - // when not passed, defaultValue === undefined - return obj[attrname] || defaultValue; -} - -/* py-equiv: operator.attrgetter */ -function attrgetter(attrname) { - return function(obj) { return getattr(obj, attrname); }; -} - - -/* returns a subslice of `lst` using `start`/`stop`/`step` - * start, stop might be negative +/** + * .. function:: makeUnboundMethod(meth) * - * >>> sliceList(['a', 'b', 'c', 'd', 'e', 'f'], 2) - * ['c', 'd', 'e', 'f'] - * >>> sliceList(['a', 'b', 'c', 'd', 'e', 'f'], 2, -2) - * ['c', 'd'] - * >>> sliceList(['a', 'b', 'c', 'd', 'e', 'f'], -3) - * ['d', 'e', 'f'] + * transforms a function into an unbound method */ -function sliceList(lst, start, stop, step) { - start = start || 0; - stop = stop || lst.length; - step = step || 1; - if (stop < 0) { - stop = max(lst.length+stop, 0); - } - if (start < 0) { - start = min(lst.length+start, lst.length); - } - var result = []; - for (var i=start; i < stop; i+=step) { - result.push(lst[i]); - } - return result; -} - -/* returns a partial func that calls a mehod on its argument - * py-equiv: return lambda obj: getattr(obj, methname)(*args) - */ -// XXX looks completely unused (candidate for removal) -function methodcaller(methname) { - var args = sliceList(arguments, 1); - return function(obj) { - return obj[methname].apply(obj, args); - }; -} - -/* use MochiKit's listMin / listMax */ -function min() { return listMin(arguments); } -function max() { return listMax(arguments); } - -/* - * >>> d = dict(["x", "y", "z"], [0, 1, 2]) - * >>> d['y'] - * 1 - * >>> d.y - * 1 - */ -function dict(keys, values) { - if (keys.length != values.length) { - throw "got different number of keys and values !"; - } - var newobj = {}; - for(var i=0; i differs from python) var basemeths = {}; var reverseLookup = []; - for(var i=baseclasses.length-1; i >= 0; i--) { - reverseLookup.push(baseclasses[i]); + for (var i = baseclasses.length - 1; i >= 0; i--) { + reverseLookup.push(baseclasses[i]); } - reverseLookup.push({'__dict__' : classdict}); + reverseLookup.push({ + '__dict__': classdict + }); - for(var i=0; i < reverseLookup.length; i++) { - var cls = reverseLookup[i]; - for (prop in cls.__dict__) { - // XXX hack to avoid __init__, __bases__... - if ( !_isAttrSkipped(prop) ) { - basemeths[prop] = cls.__dict__[prop]; - } - } + for (var i = 0; i < reverseLookup.length; i++) { + var cls = reverseLookup[i]; + for (prop in cls.__dict__) { + // XXX hack to avoid __init__, __bases__... + if (!_isAttrSkipped(prop)) { + basemeths[prop] = cls.__dict__[prop]; + } + } } var userctor = basemeths['__init__']; var constructor = makeConstructor(userctor); @@ -371,38 +284,8 @@ constructor.prototype.__class__ = constructor; // make bound / unbound methods for (methname in basemeths) { - attachMethodToClass(constructor, methname, basemeths[methname]); + attachMethodToClass(constructor, methname, basemeths[methname]); } return constructor; } - -// Not really python-like -CubicWeb = {}; -// XXX backward compatibility -Erudi = CubicWeb; -CubicWeb.loaded = []; -CubicWeb.require = function(module) { - if (!CubicWeb.loaded.contains(module)) { - // a CubicWeb.load_javascript(module) function would require a dependency on ajax.js - log(module, ' is required but not loaded'); - } -}; - -CubicWeb.provide = function(module) { - if (!CubicWeb.loaded.contains(module)) { - CubicWeb.loaded.push(module); - } -}; - -jQuery(document).ready(function() { - jQuery(CubicWeb).trigger('server-response', [false, document]); -}); - -// XXX as of 2010-04-07, no known cube uses this -jQuery(CubicWeb).bind('ajax-loaded', function() { - log('[3.7] "ajax-loaded" event is deprecated, use "server-response" instead'); - jQuery(CubicWeb).trigger('server-response', [false, document]); -}); - -CubicWeb.provide('python.js');