X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsession.py;h=0a17c6d602120098f1fa35057fa4ba0bce131bdf;hb=920b69c320cb0dc83eec0c8b4397c7a5b80b7d5b;hp=3b2b2b29cc98dfbf435544ffb842e3af9b05d32c;hpb=b9e22c33901b627684450411477f6c459fc302da;p=wrw.git diff --git a/wrw/session.py b/wrw/session.py index 3b2b2b2..0a17c6d 100644 --- a/wrw/session.py +++ b/wrw/session.py @@ -1,5 +1,5 @@ import threading, time, pickle, random, os -import cookie, env +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 @@ -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: @@ -157,33 +157,44 @@ 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 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) + new = False try: if sessid is None: 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, self.path) + self.mkcookie(req, sess) with self.lock: self.live[sess.id] = [sess.lock, sess] try: self.freeze(sess) except: pass + self.checkclean() req.oncommit(ckfreeze) return sess @@ -193,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):