Handle cookie attributes more flexibly.
[wrw.git] / wrw / session.py
index 428c93d..19bdb6b 100644 (file)
@@ -70,6 +70,9 @@ class session(object):
             self.__dict__[k] = v
         # The proper lock is set by the thawer
 
+    def __repr__(self):
+        return "<session %s>" % self.id
+
 class db(object):
     def __init__(self, backdb = None, cookiename = "wrwsess", path = "/"):
         self.live = {}
@@ -154,15 +157,17 @@ class db(object):
                 else:
                     raise Exception("Illegal session entry: " + repr(entry[1]))
 
-    def fetch(self, req):
-        now = int(time.time())
-        sessid = cookie.get(req, self.cookiename)
-        new = False
+    def checkclean(self):
         with self.lock:
             if self.cthread is None:
                 self.cthread = threading.Thread(target = self.cleanloop)
                 self.cthread.setDaemon(True)
                 self.cthread.start()
+
+    def fetch(self, req):
+        now = int(time.time())
+        sessid = cookie.get(req, self.cookiename)
+        new = False
         try:
             if sessid is None:
                 raise KeyError()
@@ -174,13 +179,14 @@ class db(object):
         def ckfreeze(req):
             if sess.dirty():
                 if new:
-                    cookie.add(req, self.cookiename, sess.id, self.path)
+                    cookie.add(req, self.cookiename, sess.id, path=self.path)
                     with self.lock:
                         self.live[sess.id] = [sess.lock, sess]
                 try:
                     self.freeze(sess)
                 except:
                     pass
+                self.checkclean()
         req.oncommit(ckfreeze)
         return sess