Added a per-session class-based handler wrapper.
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 27 May 2011 06:14:32 +0000 (08:14 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 27 May 2011 06:14:32 +0000 (08:14 +0200)
wrw/__init__.py
wrw/util.py

index 5f7aacb..61c6e04 100644 (file)
@@ -1,7 +1,7 @@
 __all__ = ["request", "wsgiwrap", "restart", "cookie", "formdata"]
 
 from req import request
-from util import wsgiwrap
+from util import wsgiwrap, persession
 from dispatch import restart
 import cookie
 from form import formdata
index 22131ea..86836bd 100644 (file)
@@ -1,6 +1,21 @@
-import req, dispatch
+import req, dispatch, session
 
 def wsgiwrap(callable):
     def wrapper(env, startreq):
         return dispatch.handle(req.request(env), startreq, callable)
     return wrapper
+
+def persession(data = None):
+    def dec(callable):
+        def wrapper(req):
+            sess = session.get(req)
+            if callable not in sess:
+                if data is None:
+                    sess[callable] = callable()
+                else:
+                    if data not in sess:
+                        sess[data] = data()
+                    sess[callable] = callable(data)
+            return sess[callable].handle(req)
+        return wrapper
+    return dec