X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=blobdiff_plain;f=wrw%2Futil.py;h=7209ae42d9d96bc39f1b01594b49d1d4afb3db2b;hp=f70de0da7ee82077a27b32141aeb5a2b1aae37ed;hb=6a9037cbbdde6ac644683096f8b0b0e53a678b3e;hpb=8d8d1f703090499006efeb51299b8a267b00aa09 diff --git a/wrw/util.py b/wrw/util.py index f70de0d..7209ae4 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -7,42 +7,42 @@ def wsgiwrap(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): + spec = inspect.getargspec(callable) def wrapper(req): - data = form.formdata(req) - spec = inspect.getargspec(callable) + try: + data = form.formdata(req) + except IOError: + raise resp.httperror(400, "Invalid request", "Form data was incomplete") args = dict(data.items()) args["req"] = req if not spec.keywords: for arg in list(args): if arg not in spec.args: del args[arg] - for i in range(len(spec.args) - len(spec.defaults)): + for i in range(len(spec.args) - (len(spec.defaults) if spec.defaults else 0)): 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 == "": + if "__root__" in self.dir: + return self.dir["__root__"](req) raise resp.redirect(req.uriname + "/") if req.pathinfo[:1] != "/": raise resp.notfound() @@ -53,12 +53,23 @@ def funplex(*funs, **nfuns): else: p = p.partition("/")[0] bi = len(p) + 1 - if p in dir: - return dir[p](req.shift(bi)) + if p in self.dir: + sreq = req.shift(bi) + sreq.selfpath = req.pathinfo[1:] + return self.dir[p](sreq) raise resp.notfound() - return handler -def persession(data = None): + 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): def wrapper(req): sess = session.get(req) @@ -106,9 +117,19 @@ def pregen(callable): wrapper.__wrapped__ = callable return wrapper +def stringwrap(charset): + def dec(callable): + @pregen + def wrapper(*args, **kwargs): + for string in callable(*args, **kwargs): + yield string.encode(charset) + wrapper.__wrapped__ = callable + return wrapper + return dec + class sessiondata(object): @classmethod - def get(cls, req, create = True): + def get(cls, req, create=True): sess = cls.sessdb().get(req) with sess.lock: try: @@ -253,6 +274,6 @@ class specdirty(sessiondata, metaclass=specclass): def datecheck(req, mtime): if "If-Modified-Since" in req.ihead: rtime = proto.phttpdate(req.ihead["If-Modified-Since"]) - if rtime >= math.floor(mtime): + if rtime is not None and rtime >= math.floor(mtime): raise resp.unmodified() req.ohead["Last-Modified"] = proto.httpdate(mtime)