Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 241

Show
Ignore:
Timestamp:
06/04/05 12:11:00
Author:
mikerobi
Message:

major cleanups

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/ticket-132/cherrypy/_cpconfig.py

    r239 r241  
    4444            'server.threadPool': 0, 
    4545 
    46             'session.new': 'sessionMap', 
    47             'sessionMap.storageType' : 'ram', 
    48             'session.timeout': 60, 
    4946            'session.cleanUpDelay': 60, 
    5047            'session.cookieName': 'CherryPySession', 
    51             'sessionMap.cookieName': 'CherryPySession', 
    5248            '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': '.', 
    5356        }, 
    5457    } 
  • branches/ticket-132/cherrypy/lib/session/basesession.py

    r240 r241  
    7575      return {  
    7676               'timestamp'  : int(time.time()), 
    77                'timeout'    : cherrypy.cpg.config.get('session.timeout') * 60, 
     77               'timeout'    : cherrypy.cpg.config.get('sessionFilter.timeout') * 60, 
    7878               'maxAge'     : None, 
    7979               'lastAccess' : int(time.time()), 
     
    8181             } 
    8282        
    83     def register(cls): 
    84         """ 
    85         This method will place the configName and session object so 
    86         the configuration system will know that "BaseSession" maps to 
    87         the BaseSession class 
    88         """ 
    89         cherrypy.cpg.session.registerSession(cls.configName, cls) 
    90     register=classmethod(register) 
    91  
    9283    def newSession(self): 
    9384        """ Return a new sessionMap instance """ 
     
    133124 
    134125    def dropSession(self, sessionKey): 
    135         self.deleteSession() 
     126        self.delSession() 
    136127        """ 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) 
    137137 
    138138    def cleanUpOldSessions(self): 
  • branches/ticket-132/cherrypy/lib/session/dbmsession.py

    r225 r241  
    3030from basesession import BaseSession 
    3131import cherrypy.cpg 
    32 import mrow 
    3332 
    3433import shelve 
    3534 
     35from errors import * 
     36 
     37import threading 
     38import cPickle as pickle 
     39 
    3640class DBMSession(BaseSession): 
    37      
    38     def __init__(self): 
     41    def __init__(self, sessionName): 
    3942        cpg = cherrypy.cpg 
     43 
     44        BaseSession.__init__(self, sessionName) 
     45         
    4046        sessionName=cpg.config.get('session.new', None) 
    4147        sessionFile=cpg.config.get('%s.dbFile' % sessionName, 'shelfSession.db') 
    4248        self.__data = shelve.open(sessionFile, 'c') 
    4349 
    44     __lock = mrow.RWLock() 
     50    def getSession(self, sessionKey): 
     51        try: 
     52            return self.__data[sessionKey] 
     53        except KeyError: 
     54            raise SessionNotFoundError 
    4555     
    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 
    8858 
    89     def __delitem__(self, sessionKey): 
    90         lock = self.__lock.writer() 
     59    def delSession(self, sessionKey): 
    9160        try: 
    9261            del self.__data[sessionKey] 
    93         finally
    94             lock.release() 
     62        except KeyError
     63            raise SessionNotFoundError 
    9564     
    9665    def cleanUpOldSessions(self): 
  • branches/ticket-132/cherrypy/lib/session/errors.py

    r194 r241  
    3232    """ Base type for session exceptions. """ 
    3333    def __init__(self, *args, **kwds): 
    34         print args 
    35         print kwds 
    3634        Exception.__init__(self, *args, **kwds) 
    3735 
  • branches/ticket-132/cherrypy/lib/session/example.conf

    r239 r241  
    55[/admin] 
    66session.new='adminSession' 
     7sessionFilter.new='adminSession' 
    78adminSession.storageType='ram' 
    89adminSession.maxAge=5 
     
    1011[/forum] 
    1112session.new='forumSession' 
     13sessionFilter.new='forumSession' 
    1214forumSession.storageType='ram' 
  • branches/ticket-132/cherrypy/lib/session/filesession.py

    r208 r241  
    3535from sessionmap import SessionMapClass 
    3636 
     37from errors import * 
     38 
    3739class FileSession(BaseSession): 
    3840   
    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/') 
    7246        fname = os.path.join(sessionStorageFileDir, sessionKey) 
    7347        if os.path.exists(fname): 
     
    7650            f.close() 
    7751            return sessionData 
    78         else: return None 
    79     __getitem__ = sessionReader(__getitem__) 
     52        else: 
     53            raise SessionNotFoundError 
    8054     
    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') 
    8357     
    84         fname=os.path.join(sessionStorageFileDir,sessionKey) 
     58        fname=os.path.join(sessionStorageFileDir, sessionData.key) 
    8559        f = open(fname,"wb") 
    8660        pickle.dump(sessionData, f) 
    8761        f.close() 
    88     __setitem__ = sessionWriter(__setitem__) 
    8962 
    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') 
    9765     
    9866    def cleanUpOldSessions(self): 
     
    11381                except: 
    11482                    print 'exception cleaning' 
    115                     pass 
    116  
  • branches/ticket-132/cherrypy/lib/session/ramsession.py

    r240 r241  
    3939        self.__data = {} 
    4040 
    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`, then 
    47         releases the lock after the call completes. 
    48          
    49         `sessionFunc` will also be called with a named argument, `sessionMap`, as a 
    50         convenience, set to `cpg.request.sessionMap`. 
    51      
    52         Example:: 
    53      
    54             @cpg.expose 
    55             @sessionReader 
    56             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 _inner 
    66      
    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 _inner 
    77      
    7841    def getSession(self, sessionKey): 
    7942        try: 
    8043            return self.__data[sessionKey] 
    81         except
     44        except KeyError
    8245            raise SessionNotFoundError 
    83     getSession = sessionReader(getSession) 
    8446     
    8547    def setSession(self, sessionData): 
    8648        self.__data[sessionData.key] = sessionData 
    87     #saveSession = sessionWriter(saveSession) 
    8849 
    89     def __delitem__(self, sessionKey): 
    90         lock = self.__lock.writer() 
     50    def delSession(self, sessionKey): 
    9151        try: 
    9252            del self.__data[sessionKey] 
    93         finally
    94             lock.release() 
     53        except KeyError
     54            raise SessionNotFoundError 
    9555     
    9656    def cleanUpOldSessions(self): 
  • branches/ticket-132/cherrypy/lib/session/sessionfilter.py

    r240 r241  
    1414    sessions = {} 
    1515     
    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     
    1818    paths=os.path.split(path) 
    1919     
    20     sessionNames = cpg.config.getAll('session.new') 
     20    sessionNames = cpg.config.getAll('sessionFilter.new') 
    2121    for sessionPath in sessionNames: 
    2222        sessionName = sessionNames[sessionPath] 
     
    7575        sessionKeys= {} 
    7676         
    77         sessions = cpg.config.getAll('session.new') 
     77        sessions = cpg.config.getAll('sessionFilter.new') 
    7878        for sessionPath in sessions: 
    7979            sessionName = sessions[sessionPath] 
     
    113113 
    114114    def afterRequestHeader(self): 
    115        self.__initSessions() 
     115        cpg = cherrypy.cpg 
     116        if cpg.config.get('sessionFilter.on', False): 
     117            self.__initSessions() 
    116118     
    117119class SessionOutputFilter(BaseOutputFilter): 
     
    130132 
    131133    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  
    3232from errors import SessionImmutableError 
    3333 
     34import threading 
     35 
    3436class SessionMapClass(dict): 
    3537    """ 
     
    5456 
    5557    def __init__(self, sessionData = {}, sessionAttributes = {}): 
    56         self.__sessionAttributes = {} 
     58        self._sessionAttributes = {} 
     59        self._lock = threading.RLock() 
    5760        self.threadCount = 0 
    5861        self.update(sessionData) 
    5962        if sessionAttributes: 
    60             self.__sessionAttributes.update(sessionAttributes) 
     63            self._sessionAttributes.update(sessionAttributes) 
    6164        else: 
    6265          sub={ 
     
    6871              } 
    6972          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 
    7177 
    72     def __getattr__(self, attr): 
     78    def __getattribute__(self, attr): 
     79        dict.__getattribute__(self, '_lock').acquire() 
    7380        try: 
    74           return self.__sessionAttributes[attr] 
     81            return dict.__getattribute__(self, '_sessionAttributes')[attr] 
    7582        except KeyError: 
    76             raise AttributeError 
     83            return dict.__getattribute__(self, attr) 
     84        dict.__getattribute__(self, '_lock').release() 
    7785 
    78     def __setattr__(self, attr, value): 
     86    def __setattribute__(self, attr, value): 
     87        dict.__getattribute__(self, '_lock').acquire() 
    7988        if attr in ['key', 'timestamp']: 
    8089            raise SessionImmutableError(attr) 
    8190        elif attr in ['timeout', 'maxAge', 'lastAccess']: 
    82             self.__sessionData[attr] = value 
     91            dict._sessionAttributes[attr] = value 
    8392        else: 
    8493            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 
    85106 
    86107    def flatten(self): 
     
    94115              
    95116        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] 
    98119        return d 
     120 

Hosted by WebFaction

Log in as guest/cpguest to create tickets