Merge branch 'master' into python3
[wrw.git] / wrw / session.py
index 19bdb6b..e7d8581 100644 (file)
@@ -1,5 +1,5 @@
 import threading, time, pickle, random, os
-import cookie, env
+from . import cookie, env
 
 __all__ = ["db", "get"]
 
@@ -11,12 +11,12 @@ 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
 
 class session(object):
-    def __init__(self, lock, expire = 86400 * 7):
+    def __init__(self, lock, expire=86400 * 7):
         self.id = hexencode(gennonce(16))
         self.dict = {}
         self.lock = lock
@@ -39,7 +39,7 @@ class session(object):
     def __getitem__(self, key):
         return self.dict[key]
 
-    def get(self, key, default = None):
+    def get(self, key, default=None):
         return self.dict.get(key, default)
 
     def __setitem__(self, key, value):
@@ -74,7 +74,7 @@ class session(object):
         return "<session %s>" % self.id
 
 class db(object):
-    def __init__(self, backdb = None, cookiename = "wrwsess", path = "/"):
+    def __init__(self, backdb=None, cookiename="wrwsess", path="/"):
         self.live = {}
         self.cookiename = cookiename
         self.path = path
@@ -86,7 +86,7 @@ class db(object):
     def clean(self):
         now = int(time.time())
         with self.lock:
-            clist = self.live.keys()
+            clist = list(self.live.keys())
         for sessid in clist:
             with self.lock:
                 try:
@@ -164,6 +164,14 @@ class db(object):
                 self.cthread.setDaemon(True)
                 self.cthread.start()
 
+    def mksession(self, req):
+        return session(threading.RLock())
+
+    def mkcookie(self, req, sess):
+        cookie.add(req, self.cookiename, sess.id,
+                   path=self.path,
+                   expires=cookie.cdate(time.time() + sess.expire))
+
     def fetch(self, req):
         now = int(time.time())
         sessid = cookie.get(req, self.cookiename)
@@ -173,13 +181,13 @@ class db(object):
                 raise KeyError()
             sess = self._fetch(sessid)
         except KeyError:
-            sess = session(threading.RLock())
+            sess = self.mksession(req)
             new = True
 
         def ckfreeze(req):
             if sess.dirty():
                 if new:
-                    cookie.add(req, self.cookiename, sess.id, path=self.path)
+                    self.mkcookie(req, sess)
                     with self.lock:
                         self.live[sess.id] = [sess.lock, sess]
                 try:
@@ -196,7 +204,7 @@ class db(object):
         data = self.backdb[sessid]
         try:
             return pickle.loads(data)
-        except Exception, e:
+        except:
             raise KeyError()
 
     def freeze(self, sess):
@@ -227,7 +235,7 @@ class dirback(object):
         with open(os.path.join(self.path, key), "w") as out:
             out.write(value)
 
-default = env.var(db(backdb = 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.val.get(req)