Merge branch 'master' into python3
[wrw.git] / wrw / session.py
index 2abe650..832387e 100644 (file)
@@ -1,5 +1,5 @@
 import threading, time, pickle, random, os
-import cookie
+from . import cookie, env
 
 __all__ = ["db", "get"]
 
@@ -11,7 +11,7 @@ def hexencode(str):
 
 def gennonce(length):
     nonce = ""
-    for i in xrange(length):
+    for i in range(length):
         nonce += chr(random.randint(0, 255))
     return nonce
 
@@ -71,19 +71,20 @@ class session(object):
         self.lock = threading.Lock()
 
 class db(object):
-    def __init__(self, cookiename = "wrwsess", path = "/"):
+    def __init__(self, backdb = None, cookiename = "wrwsess", path = "/"):
         self.live = {}
         self.cookiename = cookiename
         self.path = path
         self.lock = threading.Lock()
         self.cthread = None
         self.freezetime = 3600
+        self.backdb = backdb
 
     def clean(self):
         now = int(time.time())
         with self.lock:
             dlist = []
-            for sess in self.live.itervalues():
+            for sess in self.live.values():
                 if sess.atime + self.freezetime < now:
                     try:
                         if sess.dirty():
@@ -129,14 +130,15 @@ class db(object):
                 sess.atime = now
             except KeyError:
                 sess = session()
-                self.live[sess.id] = sess
                 new = True
 
         def ckfreeze(req):
             if sess.dirty():
+                if new:
+                    cookie.add(req, self.cookiename, sess.id, self.path)
+                    with self.lock:
+                        self.live[sess.id] = sess
                 try:
-                    if new:
-                        cookie.add(req, self.cookiename, sess.id, self.path)
                     self.freeze(sess)
                 except:
                     pass
@@ -144,30 +146,23 @@ class db(object):
         return sess
 
     def thaw(self, sessid):
-        raise KeyError()
-
-    def freeze(self, sess):
-        raise TypeError()
-
-    def get(self, req):
-        return req.item(self.fetch)
-
-class backeddb(db):
-    def __init__(self, backdb, *args, **kw):
-        super(backeddb, self).__init__(*args, **kw)
-        self.backdb = backdb
-
-    def thaw(self, sessid):
+        if self.backdb is None:
+            raise KeyError()
         data = self.backdb[sessid]
         try:
             return pickle.loads(data)
-        except Exception, e:
+        except:
             raise KeyError()
 
     def freeze(self, sess):
-        self.backdb[sess.id] = pickle.dumps(sess)
+        if self.backdb is None:
+            raise TypeError()
+        self.backdb[sess.id] = pickle.dumps(sess, -1)
         sess.frozen()
 
+    def get(self, req):
+        return req.item(self.fetch)
+
 class dirback(object):
     def __init__(self, path):
         self.path = path
@@ -185,7 +180,7 @@ class dirback(object):
         with open(os.path.join(self.path, key), "w") as out:
             out.write(value)
 
-default = backeddb(dirback(os.path.join("/tmp", "wrwsess-" + str(os.getuid()))))
+default = env.var(db(backdb = dirback(os.path.join("/tmp", "wrwsess-" + str(os.getuid())))))
 
 def get(req):
-    return default.get(req)
+    return default.val.get(req)