X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Futil.py;h=759c2957af7574be16cf4e23767452c10f0e4481;hb=612eb9f52dd4bc45b16bdcfcecbedec567df3155;hp=e601be39aeef1fc7634cfb29427e9f96ba8f095d;hpb=0f18b7748229b3b899cbc9b7b320d10cd5a668ed;p=wrw.git diff --git a/wrw/util.py b/wrw/util.py index e601be3..759c295 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -24,11 +24,19 @@ def formparams(callable): wrapper.__wrapped__ = callable return wrapper -def funplex(*funs, **nfuns): - dir = {} - dir.update(((fun.__name__, fun) for fun in funs)) - dir.update(nfuns) - def handler(req): +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 + + def __call__(self, req): if req.pathinfo == "": raise resp.redirect(req.uriname + "/") if req.pathinfo[:1] != "/": @@ -40,10 +48,19 @@ 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: + 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):