From 525d7938bc668f6079f0fd00a0baad75b1aebe24 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Tue, 23 Apr 2013 05:52:54 +0200 Subject: [PATCH] Added a mod_python-style (but better ^^) function multiplexer. --- wrw/__init__.py | 2 +- wrw/util.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/wrw/__init__.py b/wrw/__init__.py index 8fcb72e..95ff4cd 100644 --- a/wrw/__init__.py +++ b/wrw/__init__.py @@ -1,7 +1,7 @@ __all__ = ["wsgiwrap", "restart", "cookie", "formdata"] import proto -from util import wsgiwrap, formparams, persession, sessiondata, autodirty, manudirty, specdirty +from util import wsgiwrap, formparams, funplex, persession, sessiondata, autodirty, manudirty, specdirty from dispatch import restart import cookie from form import formdata diff --git a/wrw/util.py b/wrw/util.py index 64b820f..cfc2c8c 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -22,6 +22,27 @@ def formparams(callable): return callable(**args) return wrapper +def funplex(*funs, **nfuns): + dir = {} + dir.update(((fun.__name__, fun) for fun in funs)) + dir.update(nfuns) + def handler(req): + if req.pathinfo == "": + raise resp.redirect(req.uriname + "/") + if req.pathinfo[:1] != "/": + raise resp.notfound() + p = req.pathinfo[1:] + if p == "": + p = "__index__" + bi = 1 + else: + p = p.partition("/")[0] + bi = len(p) + 1 + if p in dir: + return dir[p](req.shift(bi)) + raise resp.notfound() + return handler + def persession(data = None): def dec(callable): def wrapper(req): -- 2.11.0