Changeset 241
- Timestamp:
- 06/04/05 12:11:00
- Files:
-
- branches/ticket-132/cherrypy/_cpconfig.py (modified) (1 diff)
- branches/ticket-132/cherrypy/lib/session/basesession.py (modified) (3 diffs)
- branches/ticket-132/cherrypy/lib/session/dbmsession.py (modified) (1 diff)
- branches/ticket-132/cherrypy/lib/session/errors.py (modified) (1 diff)
- branches/ticket-132/cherrypy/lib/session/example.conf (modified) (2 diffs)
- branches/ticket-132/cherrypy/lib/session/example.py (moved) (moved from branches/ticket-132/cherrypy/lib/session/session_example.py)
- branches/ticket-132/cherrypy/lib/session/filesession.py (modified) (3 diffs)
- branches/ticket-132/cherrypy/lib/session/ramsession.py (modified) (1 diff)
- branches/ticket-132/cherrypy/lib/session/sessiondict.py (copied) (copied from branches/ticket-132/cherrypy/lib/session/sessionmap.py)
- branches/ticket-132/cherrypy/lib/session/sessionfilter.py (modified) (4 diffs)
- branches/ticket-132/cherrypy/lib/session/sessionmap.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/ticket-132/cherrypy/_cpconfig.py
r239 r241 44 44 'server.threadPool': 0, 45 45 46 'session.new': 'sessionMap',47 'sessionMap.storageType' : 'ram',48 'session.timeout': 60,49 46 'session.cleanUpDelay': 60, 50 47 'session.cookieName': 'CherryPySession', 51 'sessionMap.cookieName': 'CherryPySession',52 48 'session.storageFileDir': '', 49 50 'sessionFilter.on': True, 51 'sessionFilter.new': 'sessionMap', 52 'sessionFilter.timeout': 60, 53 'sessionFilter.storageType' : 'ram', 54 'sessionFilter.cookieName': 'CherryPySession', 55 'sessionFilter.storageFileDir': '.', 53 56 }, 54 57 } branches/ticket-132/cherrypy/lib/session/basesession.py
r240 r241 75 75 return { 76 76 'timestamp' : int(time.time()), 77 'timeout' : cherrypy.cpg.config.get('session .timeout') * 60,77 'timeout' : cherrypy.cpg.config.get('sessionFilter.timeout') * 60, 78 78 'maxAge' : None, 79 79 'lastAccess' : int(time.time()), … … 81 81 } 82 82 83 def register(cls):84 """85 This method will place the configName and session object so86 the configuration system will know that "BaseSession" maps to87 the BaseSession class88 """89 cherrypy.cpg.session.registerSession(cls.configName, cls)90 register=classmethod(register)91 92 83 def newSession(self): 93 84 """ Return a new sessionMap instance """ … … 133 124 134 125 def dropSession(self, sessionKey): 135 self.del eteSession()126 self.delSession() 136 127 """ delete a session from storage """ 128 129 def register(cls): 130 """ 131 This method will place the configName and session object so 132 the configuration system will know that "BaseSession" maps to 133 the BaseSession class 134 """ 135 cherrypy.cpg.session.registerSession(cls.configName, cls) 136 register=classmethod(register) 137 137 138 138 def cleanUpOldSessions(self): branches/ticket-132/cherrypy/lib/session/dbmsession.py
r225 r241 30 30 from basesession import BaseSession 31 31 import cherrypy.cpg 32 import mrow33 32 34 33 import shelve 35 34 35 from errors import * 36 37 import threading 38 import cPickle as pickle 39 36 40 class DBMSession(BaseSession): 37 38 def __init__(self): 41 def __init__(self, sessionName): 39 42 cpg = cherrypy.cpg 43 44 BaseSession.__init__(self, sessionName) 45 40 46 sessionName=cpg.config.get('session.new', None) 41 47 sessionFile=cpg.config.get('%s.dbFile' % sessionName, 'shelfSession.db') 42 48 self.__data = shelve.open(sessionFile, 'c') 43 49 44 __lock = mrow.RWLock() 50 def getSession(self, sessionKey): 51 try: 52 return self.__data[sessionKey] 53 except KeyError: 54 raise SessionNotFoundError 45 55 46 def sessionReader(sessionFunc): 47 """Decorates a function as being a reader of the sessionMap. 48 49 Acquires a read lock on the sessionMap before calling `sessionFunc`, then 50 releases the lock after the call completes. 51 52 `sessionFunc` will also be called with a named argument, `sessionMap`, as a 53 convenience, set to `cpg.request.sessionMap`. 54 55 Example:: 56 57 @cpg.expose 58 @sessionReader 59 def index(self, sessionMap): 60 # Do stuff... 61 """ 62 def _inner(self, *args, **kw): 63 lock = self.__lock.reader() 64 try: 65 return sessionFunc(self, *args, **kw) 66 finally: 67 lock.release() 68 return _inner 69 70 71 def sessionWriter(sessionFunc): 72 """Same as sessionReader, except obtains an exclusive write lock.""" 73 def _inner(self, sessionKey, sessionValue): 74 lock = self.__lock.writer() 75 try: 76 return sessionFunc(self, sessionKey, sessionValue) 77 finally: 78 lock.release() 79 return _inner 80 81 def __getitem__(self, sessionKey): 82 return self.__data[sessionKey] 83 __getitem__ = sessionReader(__getitem__) 84 85 def __setitem__(self, sessionKey, value): 86 self.__data[sessionKey] = value 87 __setitem__ = sessionWriter(__setitem__) 56 def setSession(self, sessionData): 57 self.__data[sessionData.key] = sessionData 88 58 89 def __delitem__(self, sessionKey): 90 lock = self.__lock.writer() 59 def delSession(self, sessionKey): 91 60 try: 92 61 del self.__data[sessionKey] 93 finally:94 lock.release()62 except KeyError: 63 raise SessionNotFoundError 95 64 96 65 def cleanUpOldSessions(self): branches/ticket-132/cherrypy/lib/session/errors.py
r194 r241 32 32 """ Base type for session exceptions. """ 33 33 def __init__(self, *args, **kwds): 34 print args35 print kwds36 34 Exception.__init__(self, *args, **kwds) 37 35 branches/ticket-132/cherrypy/lib/session/example.conf
r239 r241 5 5 [/admin] 6 6 session.new='adminSession' 7 sessionFilter.new='adminSession' 7 8 adminSession.storageType='ram' 8 9 adminSession.maxAge=5 … … 10 11 [/forum] 11 12 session.new='forumSession' 13 sessionFilter.new='forumSession' 12 14 forumSession.storageType='ram' branches/ticket-132/cherrypy/lib/session/filesession.py
r208 r241 35 35 from sessionmap import SessionMapClass 36 36 37 from errors import * 38 37 39 class FileSession(BaseSession): 38 40 39 __lock = mrow.RWLock() 40 41 def sessionReader(sessionFunc): 42 """Decorates a function as being a reader of the sessionMap. 43 44 Acquires a read lock on the sessionMap before calling `sessionFunc`, then 45 releases the lock after the call completes. 46 47 `sessionFunc` will also be called with a named argument, `sessionMap`, as a 48 convenience, set to `cpg.request.sessionMap`. 49 50 """ 51 def _inner(self, *args, **kw): 52 lock = self.__lock.reader() 53 try: 54 return sessionFunc(self, *args, **kw) 55 finally: 56 lock.release() 57 return _inner 58 59 60 def sessionWriter(sessionFunc): 61 """Same as sessionReader, except obtains an exclusive write lock.""" 62 def _inner(self, sessionKey, sessionValue): 63 lock = self.__lock.writer() 64 try: 65 return sessionFunc(self, sessionKey, sessionValue) 66 finally: 67 lock.release() 68 return _inner 69 70 def __getitem__(self, sessionKey): 71 sessionStorageFileDir = cherrypy.cpg.config.get('session.storageFileDir') 41 def __init__(self, sessionName): 42 BaseSession.__init__(self, sessionName) 43 44 def getSession(self, sessionKey): 45 sessionStorageFileDir = cherrypy.cpg.config.get('sessionFilter.storageFileDir', 'lib/session/') 72 46 fname = os.path.join(sessionStorageFileDir, sessionKey) 73 47 if os.path.exists(fname): … … 76 50 f.close() 77 51 return sessionData 78 else: return None79 __getitem__ = sessionReader(__getitem__)52 else: 53 raise SessionNotFoundError 80 54 81 def __setitem__(self, sessionKey, sessionData):82 sessionStorageFileDir = cherrypy.cpg.config.get('session .storageFileDir')55 def setSession(self, sessionData): 56 sessionStorageFileDir = cherrypy.cpg.config.get('sessionFilter.storageFileDir') 83 57 84 fname=os.path.join(sessionStorageFileDir, sessionKey)58 fname=os.path.join(sessionStorageFileDir, sessionData.key) 85 59 f = open(fname,"wb") 86 60 pickle.dump(sessionData, f) 87 61 f.close() 88 __setitem__ = sessionWriter(__setitem__)89 62 90 def __delitem__(self, sessionKey): 91 sessionStorageFileDir = cherrypy.cpg.config.get('session.storageFileDir') 92 lock = self.__lock.writer() 93 try: 94 del self.__data[sessionKey] 95 finally: 96 lock.release() 63 def delSession(self, sessionKey): 64 sessionStorageFileDir = cherrypy.cpg.config.get('sessionFilter.storageFileDir') 97 65 98 66 def cleanUpOldSessions(self): … … 113 81 except: 114 82 print 'exception cleaning' 115 pass116 branches/ticket-132/cherrypy/lib/session/ramsession.py
r240 r241 39 39 self.__data = {} 40 40 41 __lock = mrow.RWLock()42 43 def sessionReader(sessionFunc):44 """Decorates a function as being a reader of the sessionMap.45 46 Acquires a read lock on the sessionMap before calling `sessionFunc`, then47 releases the lock after the call completes.48 49 `sessionFunc` will also be called with a named argument, `sessionMap`, as a50 convenience, set to `cpg.request.sessionMap`.51 52 Example::53 54 @cpg.expose55 @sessionReader56 def index(self, sessionMap):57 # Do stuff...58 """59 def _inner(self, *args, **kw):60 lock = self.__lock.reader()61 try:62 return sessionFunc(self, *args, **kw)63 finally:64 lock.release()65 return _inner66 67 68 def sessionWriter(sessionFunc):69 """Same as sessionReader, except obtains an exclusive write lock."""70 def _inner(self, sessionKey, sessionValue):71 lock = self.__lock.writer()72 try:73 return sessionFunc(self, sessionKey, sessionValue)74 finally:75 lock.release()76 return _inner77 78 41 def getSession(self, sessionKey): 79 42 try: 80 43 return self.__data[sessionKey] 81 except :44 except KeyError: 82 45 raise SessionNotFoundError 83 getSession = sessionReader(getSession)84 46 85 47 def setSession(self, sessionData): 86 48 self.__data[sessionData.key] = sessionData 87 #saveSession = sessionWriter(saveSession)88 49 89 def __delitem__(self, sessionKey): 90 lock = self.__lock.writer() 50 def delSession(self, sessionKey): 91 51 try: 92 52 del self.__data[sessionKey] 93 finally:94 lock.release()53 except KeyError: 54 raise SessionNotFoundError 95 55 96 56 def cleanUpOldSessions(self): branches/ticket-132/cherrypy/lib/session/sessionfilter.py
r240 r241 14 14 sessions = {} 15 15 16 path = cpg.config.get('session .new', returnSection = True )17 cpg.config.getAll('session.new')16 path = cpg.config.get('sessionFilter.new', returnSection = True ) 17 18 18 paths=os.path.split(path) 19 19 20 sessionNames = cpg.config.getAll('session .new')20 sessionNames = cpg.config.getAll('sessionFilter.new') 21 21 for sessionPath in sessionNames: 22 22 sessionName = sessionNames[sessionPath] … … 75 75 sessionKeys= {} 76 76 77 sessions = cpg.config.getAll('session .new')77 sessions = cpg.config.getAll('sessionFilter.new') 78 78 for sessionPath in sessions: 79 79 sessionName = sessions[sessionPath] … … 113 113 114 114 def afterRequestHeader(self): 115 self.__initSessions() 115 cpg = cherrypy.cpg 116 if cpg.config.get('sessionFilter.on', False): 117 self.__initSessions() 116 118 117 119 class SessionOutputFilter(BaseOutputFilter): … … 130 132 131 133 def beforeResponse(self): 132 self.__saveSessions() 134 cpg = cherrypy.cpg 135 if cpg.config.get('sessionFilter.on', True): 136 self.__saveSessions() branches/ticket-132/cherrypy/lib/session/sessionmap.py
r240 r241 32 32 from errors import SessionImmutableError 33 33 34 import threading 35 34 36 class SessionMapClass(dict): 35 37 """ … … 54 56 55 57 def __init__(self, sessionData = {}, sessionAttributes = {}): 56 self.__sessionAttributes = {} 58 self._sessionAttributes = {} 59 self._lock = threading.RLock() 57 60 self.threadCount = 0 58 61 self.update(sessionData) 59 62 if sessionAttributes: 60 self._ _sessionAttributes.update(sessionAttributes)63 self._sessionAttributes.update(sessionAttributes) 61 64 else: 62 65 sub={ … … 68 71 } 69 72 for key in sub: 70 self.__sessionAttributes[key] = sessionData.pop(sub[key]) 73 try: 74 self._sessionAttributes[key] = sessionData.pop(sub[key]) 75 except: 76 pass 71 77 72 def __getattr__(self, attr): 78 def __getattribute__(self, attr): 79 dict.__getattribute__(self, '_lock').acquire() 73 80 try: 74 return self.__sessionAttributes[attr]81 return dict.__getattribute__(self, '_sessionAttributes')[attr] 75 82 except KeyError: 76 raise AttributeError 83 return dict.__getattribute__(self, attr) 84 dict.__getattribute__(self, '_lock').release() 77 85 78 def __setattr__(self, attr, value): 86 def __setattribute__(self, attr, value): 87 dict.__getattribute__(self, '_lock').acquire() 79 88 if attr in ['key', 'timestamp']: 80 89 raise SessionImmutableError(attr) 81 90 elif attr in ['timeout', 'maxAge', 'lastAccess']: 82 self.__sessionData[attr] = value91 dict._sessionAttributes[attr] = value 83 92 else: 84 93 dict.__setattr__(self, attr, value) 94 dict.__getattribute__(self, '_lock').release() 95 96 def __getstate__(self): 97 stateDict = self.__dict__.copy() 98 stateDict['_lock'] = None 99 100 return stateDict 101 102 def __setstate__(self, stateDict): 103 self.__dict__.update(stateDict) 104 self.__dict__['_lock'] = threading.RLock() 105 self.__dict__['threadCount'] = 0 85 106 86 107 def flatten(self): … … 94 115 95 116 d = self.copy() 96 for attribute in self._ _sessionAttributes:97 d[sub[attribute]] = self._ _sessionAttributes[attribute]117 for attribute in self._sessionAttributes: 118 d[sub[attribute]] = self._sessionAttributes[attribute] 98 119 return d 120

