From: Fredrik Tolf Date: Tue, 4 Jun 2013 14:12:15 +0000 (+0200) Subject: Merge branch 'master' into python3 X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=ca3dce3812e3fc65f4c021aa76ee1430b47ad3af;hp=-c;p=wrw.git Merge branch 'master' into python3 --- ca3dce3812e3fc65f4c021aa76ee1430b47ad3af diff --combined wrw/util.py index f70de0d,759c295..ed32cc6 --- a/wrw/util.py +++ b/wrw/util.py @@@ -1,5 -1,5 +1,5 @@@ import inspect, math -import req, dispatch, session, form, resp, proto +from . import req, dispatch, session, form, resp, proto def wsgiwrap(callable): def wrapper(env, startreq): @@@ -7,15 -7,6 +7,15 @@@ wrapper.__wrapped__ = callable return wrapper +def stringwrap(charset): + def dec(callable): + def wrapper(*args, **kwargs): + bk = callable(*args, **kwargs) + for string in bk: + yield string.encode(charset) + return wrapper + return dec + def formparams(callable): def wrapper(req): data = form.formdata(req) @@@ -26,22 -17,26 +26,26 @@@ for arg in list(args): if arg not in spec.args: del args[arg] - for i in xrange(len(spec.args) - len(spec.defaults)): + for i in range(len(spec.args) - len(spec.defaults)): if spec.args[i] not in args: raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied.")) return callable(**args) wrapper.__wrapped__ = callable return wrapper - def funplex(*funs, **nfuns): + class funplex(object): + def __init__(self, *funs, **nfuns): + self.dir = {} + self.dir.update(((self.unwrap(fun).__name__, fun) for fun in funs)) + self.dir.update(nfuns) + + @staticmethod def unwrap(fun): while hasattr(fun, "__wrapped__"): fun = fun.__wrapped__ return fun - dir = {} - dir.update(((unwrap(fun).__name__, fun) for fun in funs)) - dir.update(nfuns) - def handler(req): + + def __call__(self, req): if req.pathinfo == "": raise resp.redirect(req.uriname + "/") if req.pathinfo[:1] != "/": @@@ -53,10 -48,19 +57,19 @@@ else: p = p.partition("/")[0] bi = len(p) + 1 - if p in dir: - return dir[p](req.shift(bi)) + if p in self.dir: + return self.dir[p](req.shift(bi)) raise resp.notfound() - return handler + + def add(self, fun): + self.dir[self.unwrap(fun).__name__] = fun + return fun + + def name(self, name): + def dec(fun): + self.dir[name] = fun + return fun + return dec def persession(data = None): def dec(callable): @@@ -81,12 -85,12 +94,12 @@@ class preiter(object) self.bk = real self.bki = iter(real) self._next = None - self.next() + self.__next__() def __iter__(self): return self - def next(self): + def __next__(self): if self._next is self.end: raise StopIteration() ret = self._next @@@ -127,7 -131,7 +140,7 @@@ class sessiondata(object) class autodirty(sessiondata): @classmethod def get(cls, req): - ret = super(autodirty, cls).get(req) + ret = super().get(req) if "_is_dirty" not in ret.__dict__: ret.__dict__["_is_dirty"] = False return ret @@@ -139,18 -143,18 +152,18 @@@ return self._is_dirty def __setattr__(self, name, value): - super(autodirty, self).__setattr__(name, value) + super().__setattr__(name, value) if "_is_dirty" in self.__dict__: self.__dict__["_is_dirty"] = True def __delattr__(self, name): - super(autodirty, self).__delattr__(name, value) + super().__delattr__(name, value) if "_is_dirty" in self.__dict__: self.__dict__["_is_dirty"] = True class manudirty(object): def __init__(self, *args, **kwargs): - super(manudirty, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.__dirty = False def sessfrozen(self): @@@ -193,7 -197,7 +206,7 @@@ class specslot(object) class specclass(type): def __init__(self, name, bases, tdict): - super(specclass, self).__init__(name, bases, tdict) + super().__init__(name, bases, tdict) sslots = set() dslots = set() for cls in self.__mro__: @@@ -205,7 -209,8 +218,7 @@@ for i, slot in enumerate(self.__sslots_a__): setattr(self, slot, specslot(slot, i, slot in dslots)) -class specdirty(sessiondata): - __metaclass__ = specclass +class specdirty(sessiondata, metaclass=specclass): __slots__ = ["session", "__sslots__", "_is_dirty"] def __specinit__(self): @@@ -213,7 -218,7 +226,7 @@@ @staticmethod def __new__(cls, req, sess): - self = super(specdirty, cls).__new__(cls) + self = super().__new__(cls) self.session = sess self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__) self.__specinit__()