Added a mod_python-style (but better ^^) function multiplexer.
authorFredrik Tolf <fredrik@dolda2000.com>
Tue, 23 Apr 2013 03:52:54 +0000 (05:52 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Tue, 23 Apr 2013 03:56:01 +0000 (05:56 +0200)
wrw/__init__.py
wrw/util.py

index 8fcb72e..95ff4cd 100644 (file)
@@ -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
index 64b820f..cfc2c8c 100644 (file)
@@ -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):