From: Fredrik Tolf Date: Tue, 4 Jun 2013 13:24:01 +0000 (+0200) Subject: Merge branch 'master' into python3 X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=2a5a8ce70a0107b7bfd5bd3459ae4aae3a7558e2;hp=-c;p=wrw.git Merge branch 'master' into python3 Conflicts: wrw/util.py --- 2a5a8ce70a0107b7bfd5bd3459ae4aae3a7558e2 diff --combined wrw/dispatch.py index 768d3f4,1dedbe6..5276892 --- a/wrw/dispatch.py +++ b/wrw/dispatch.py @@@ -1,5 -1,5 +1,5 @@@ import sys, traceback -import env, req, proto +from . import env, req, proto __all__ = ["restart"] @@@ -17,13 -17,14 +17,14 @@@ def mangle(result) return [str(result)] def defaulterror(req, excinfo): - import resp + from . import resp traceback.print_exception(*excinfo) raise resp.httperror(500) def wraphandler(handler, excinfo): def wrapped(req): return handler(req, excinfo) + wrapped.__wrapped__ = handler return wrapped errorhandler = env.var(defaulterror) @@@ -36,9 -37,9 +37,9 @@@ def handle(req, startreq, handler) try: resp = handler(req) break - except restart, i: + except restart as i: handler = i.handle - except Exception, i: + except Exception as i: if eh is None: raise handler = wraphandler(eh, sys.exc_info()) diff --combined wrw/resp.py index b62ce0e,159126a..69635f8 --- a/wrw/resp.py +++ b/wrw/resp.py @@@ -1,5 -1,5 +1,5 @@@ -import dispatch, proto, env -from sp import xhtml +from . import dispatch, proto, env +from .sp import xhtml h = xhtml.cons() __all__ = ["skeleton", "skelfor", "setskel", "usererror"] @@@ -28,7 -28,7 +28,7 @@@ def setskel(req, skel) class usererror(dispatch.restart): def __init__(self, message, *detail): - super(usererror, self).__init__() + super().__init__() self.message = message self.detail = detail @@@ -36,9 -36,9 +36,9 @@@ return skelfor(req).error(req, self.message, *self.detail) class message(dispatch.restart): - def __init__(self, msg, *detail): - super(message, self).__init__() - self.message = msg + def __init__(self, message, *detail): + super().__init__() + self.message = message self.detail = detail def handle(self, req): @@@ -50,20 -50,20 +50,20 @@@ class httperror(usererror) message = proto.statusinfo[status][0] if detail is None: detail = (proto.statusinfo[status][1],) - super(httperror, self).__init__(message, *detail) + super().__init__(message, *detail) self.status = status def handle(self, req): req.status(self.status, self.message) - return super(httperror, self).handle(req) + return super().handle(req) class notfound(httperror): def __init__(self): - return super(notfound, self).__init__(404) + return super().__init__(404) class redirect(dispatch.restart): def __init__(self, url, status = 303): - super(redirect, self).__init__() + super().__init__() self.url = url self.status = status @@@ -71,3 -71,9 +71,9 @@@ req.status(self.status, "Redirect") req.ohead["Location"] = proto.appendurl(proto.requrl(req), self.url) return [] + + class unmodified(dispatch.restart): + def handle(self, req): + req.status(304, "Not Modified") + req.ohead["Content-Length"] = "0" + return [] diff --combined wrw/util.py index eb27683,4306e9b..f70de0d --- a/wrw/util.py +++ b/wrw/util.py @@@ -1,20 -1,12 +1,21 @@@ - import inspect - from . import req, dispatch, session, form, resp + 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): return dispatch.handleenv(env, startreq, callable) + 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) @@@ -25,15 -17,20 +26,20 @@@ 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): + def unwrap(fun): + while hasattr(fun, "__wrapped__"): + fun = fun.__wrapped__ + return fun dir = {} - dir.update(((fun.__name__, fun) for fun in funs)) + dir.update(((unwrap(fun).__name__, fun) for fun in funs)) dir.update(nfuns) def handler(req): if req.pathinfo == "": @@@ -64,6 -61,7 +70,7 @@@ def persession(data = None) sess[data] = data() sess[callable] = callable(data) return sess[callable].handle(req) + wrapper.__wrapped__ = callable return wrapper return dec @@@ -74,12 -72,12 +81,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 @@@ -96,6 -94,7 +103,7 @@@ def pregen(callable): def wrapper(*args, **kwargs): return preiter(callable(*args, **kwargs)) + wrapper.__wrapped__ = callable return wrapper class sessiondata(object): @@@ -119,7 -118,7 +127,7 @@@ 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 @@@ -131,18 -130,18 +139,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): @@@ -185,7 -184,7 +193,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__: @@@ -197,7 -196,8 +205,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): @@@ -205,7 -205,7 +213,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__() @@@ -241,3 -241,10 +249,10 @@@ ss[i] = specslot.unbound else: ss[i] = val + + def datecheck(req, mtime): + if "If-Modified-Since" in req.ihead: + rtime = proto.phttpdate(req.ihead["If-Modified-Since"]) + if rtime >= math.floor(mtime): + raise resp.unmodified() + req.ohead["Last-Modified"] = proto.httpdate(mtime)