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

Changeset 568

Show
Ignore:
Timestamp:
08/28/05 12:57:01
Author:
mikerobi
Message:

sessionFilter: more cleanups, completely rewrote the localdict class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/filter/sessionfilter/__init__.py

    r567 r568  
    1010    'sessionFilter.cleanUpDelay': 60, 
    1111    'sessionFilter.storageType' : 'ram', 
    12     'sessionFilter.cookiePrefix': 'CherryPySession', 
     12    'sessionFilter.cookieName': 'CherryPySession', 
     13    'sessionFilter.cookiePath'  : '/', 
    1314    'sessionFilter.storagePath': '.sessiondata', 
    1415    'sessionFilter.dbFile': 'sessionData.db', 
     
    4748    """ 
    4849 
    49     def __init__(self, sessionName = 'default', sessionPath = '/'): 
     50    def __init__(self): 
    5051        """ Initilizes the session filter and creates cherrypy.sessions  """ 
    5152 
     
    5354         
    5455        cherrypy.config.update(_sessionDefaults, override = False) 
    55         self.sessionManager = self.__newSessionManager(sessionName, sessionPath) 
    5656         
    57     def __newSessionManager(self, sessionName, sessionPath): 
    58         """ 
    59         Takes the name of a new session and its configuration path. 
    60         Returns a storageAdaptor instance maching the configured storage type. 
    61         If the storage type is not built in, it tries to use sessionFilter.storageAadaptors. 
    62         If the storage type still can not be found, an exception is raised. 
    63         """ 
    6457        # look up the storage type or return the default 
    65         storageType = cherrypy.config.get('sessionFilter.%s.storageType' % sessionName, None) 
    66         if not storageType: 
    67             storageType = cherrypy.config.get('sessionFilter.storageType') 
     58        storageType = cherrypy.config.get('sessionFilter.storageType', None) 
    6859         
    6960        # try to initilize a built in session 
     
    8172                raise SessionBadStorageTypeError(storageType) 
    8273         
    83         return storageAdaptor(sessionName, sessionPath
     74        self.sessionManager = storageAdaptor(
    8475     
    8576     
    86     def __loadSessions(self): 
     77    def __loadSession(self): 
     78        """ loads the session or creates a new one """ 
     79        sessionManager = self.sessionManager 
    8780         
    88         sessionManager = self.sessionManager 
    89         sessionName = sessionManager.name 
    90          
    91         if not cherrypy.config.get('sessionFilter.%s.on' % sessionName, False): 
    92             return 
    93  
    94         cookieName = sessionManager.cookieName 
     81        cookieName = cherrypy.config.get('sessionFilter.cookieName') 
    9582         
    9683        try: 
     
    9885        except KeyError: 
    9986            sessionKey = sessionManager.createSession() 
    100             self.saveSessionDictKey(sessionKey)  
    101             sessionManager.loadSession(sessionKey) 
     87            self.sessionManager.loadSession(sessionKey) 
    10288 
    10389        try: 
    104             sessionManager.loadSession(sessionKey) 
     90            self.sessionManager.loadSession(sessionKey) 
    10591        except SessionNotFoundError: 
    10692            sessionKey = sessionManager.createSession() 
    107             self.saveSessionDictKey(sessionKey)   
    108             sessionManager.loadSession(sessionKey) 
     93            self.sessionManager.loadSession(sessionKey) 
     94         
     95        # currently we have to keep setting the cookie to update the timeout value 
     96        self.setSessionCookie(sessionKey)   
    10997 
    110     def saveSessionDictKey(self, sessionKey): 
     98    def setSessionCookie(self, sessionKey): 
    11199        """  
    112100        Sets the session key in a cookie.  
     
    115103        sessionManager = self.sessionManager 
    116104         
    117         sessionName = sessionManager.name 
    118         cookieName  = sessionManager.cookieName 
    119          
    120          
    121         # if we do not have a manually defined cookie path use path where the session 
    122         # manager was defined 
     105        cookieName  = cherrypy.config.get('sessionFilter.cookieName') 
    123106         
    124107        cookiePath = cherrypy.config.get('sessionFilter.cookiePath') 
    125         if not cookiePath: 
    126             cookiePath = sessionManager.path 
    127108 
    128109        timeout = cherrypy.config.get('sessionFilter.timeout') 
     
    144125    def __saveSessions(self): 
    145126        try: 
    146             sessionData = getattr(cherrypy.session, self.sessionManager.name) 
     127            #sessionData = getattr(cherrypy.session, 'default') 
     128            sessionData = cherrypy.session._getDict() 
    147129         
    148130            self.sessionManager.commitCache(sessionData.key) 
     
    156138        if (not cherrypy.config.get('staticFilter.on', False) 
    157139            and cherrypy.config.get('sessionFilter.on')): 
    158            self.__loadSessions() 
     140           self.__loadSession() 
    159141 
    160142    def beforeFinalize(self): 
  • trunk/cherrypy/lib/filter/sessionfilter/anydbadaptor.py

    r567 r568  
    4343    noCache = False 
    4444     
    45     def __init__(self, sessionName, sessionPath): 
    46         BaseAdaptor.__init__(self, sessionName, sessionPath
     45    def __init__(self): 
     46        BaseAdaptor.__init__(self
    4747         
    4848        # we must make sure the db file is unique 
    49         dbFile = cherrypy.config.get('sessionFilter.%s.dbFile', None
    50         if not dbFile: 
    51             defaultFile = '%s.db' % sessionName 
     49        dbFile = cherrypy.config.get('sessionFilter.dbFile'
     50  
     51        storagePath = cherrypy.config.get('sessionFilter.storagePath') 
    5252             
    53             storagePath = cherrypy.config.get('sessionFilter.storagePath') 
    54              
    55             dbFile = os.path.join(storagePath, defaultFile) 
     53        dbFile = os.path.join(storagePath, dbFile) 
     54         
    5655        self.__data = shelve.open(dbFile, 'c') 
    5756     
  • trunk/cherrypy/lib/filter/sessionfilter/baseadaptor.py

    r567 r568  
    7171    # internally by the sessionFilter 
    7272         
    73     def __init__(self, sessionName, sessionPath): 
     73    def __init__(self): 
    7474        """ 
    7575        Create the session caceh and set the session name.  Make if you write 
     
    7979         
    8080        self.__sessionCache = {} 
    81         self.name = sessionName 
    8281         
    8382        #set the path 
    84         self.path = sessionPath 
    85  
    8683        cleanUpDelay = cherrypy.config.get('sessionFilter.cleanUpDelay') 
    8784        timeMultiple = cherrypy.config.get('sessionFilter.timeMultiple') 
    8885 
    8986        self.nextCleanUp = time.time()+cleanUpDelay * timeMultiple 
    90  
    91         # find the cookie name 
    92         cookiePrefix = cherrypy.config.get('sessionFilter.cookiePrefix') 
    93          
    94         self.cookieName = '%s_%s' % (cookiePrefix, sessionName) 
    9587 
    9688        try: 
     
    120112        session = self.getSessionDict(sessionKey) 
    121113 
    122         session.cookieName = self.cookieName 
    123114        session.lastAccess = time.time() 
    124115 
    125         setattr(cherrypy.session, self.name, session) 
     116        #setattr(cherrypy.session, 'default', session) 
     117        cherrypy.session._setDict(session) 
    126118     
    127119    def createSession(self): 
  • trunk/cherrypy/lib/filter/sessionfilter/fileadaptor.py

    r567 r568  
    4242    noCache = False 
    4343     
    44     def __init__(self, sessionName, sessionPath): 
    45         BaseAdaptor.__init__(self, sessionName, sessionPath
     44    def __init__(self): 
     45        BaseAdaptor.__init__(self
    4646        self.__fileLock = mrow.MROWLock()  
    4747 
     
    5353        storagePath = cherrypy.config.get('sessionFilter.storagePath') 
    5454 
    55         fileName = '%s-%s' % (self.name, sessionKey) 
     55        fileName = 'sessionFile-' + sessionKey 
    5656        filePath = os.path.join(storagePath, fileName) 
    5757         
     
    7272        storagePath = cherrypy.config.get('sessionFilter.storagePath') 
    7373 
    74         fileName = '%s-%s' % (self.name, sessionData.key) 
     74        fileName = 'sessionFile-' + sessionData.key 
    7575        filePath = os.path.join(storagePath, fileName) 
    7676 
     
    9292                try: 
    9393                    prefix, sessionKey = fileName.split('-') 
    94                     if prefix == self.name
     94                    if prefix == 'sessionFile'
    9595                        session = self._getSessionDict(sessionKey) 
    9696                        if session.expired(): 
     
    109109            count = 0 
    110110            for fileName in sessionFileList: 
    111                 if fileName.startswith(self.name + '-'): 
     111                if fileName.startswith('sessionFile-'): 
    112112                    count += 1 
    113113         
  • trunk/cherrypy/lib/filter/sessionfilter/localdict.py

    r548 r568  
    2727""" 
    2828 
    29  
    3029try: 
    3130    from threading import local 
     
    3332    from cherrypy._cpthreadinglocal import local 
    3433 
    35 class LocalDict(local)
    36     def __getitem__(self, key): 
    37         return self.default[key] 
     34class LocalDict
     35    def __init__(self): 
     36        self.localObj = local() 
    3837     
    39     def __setitem__(self, key, value): 
    40         self.default[key] = value 
     38    def __getattr__(self, attr): 
     39        return getattr(self.localObj.sessionDict, attr) 
     40         
     41    def _setDict(self, sessionDict): 
     42        self.localObj.sessionDict = sessionDict 
    4143 
    42     def __delitem__(self, key): 
    43         del self.default[key] 
     44    def _getDict(self): 
     45        return self.localObj.sessionDict 
    4446 
    45     def get(self, key, value = None): 
    46         return self.default.get(key, value) 
    47  
    48     def setdefault(self, key, default): 
    49         return self.default.setdefault(key, default) 
    50  
    51     def __iter__(self): 
    52         return iter(self.default) 
    53  
    54     def __setattr__(self, attr, value): 
    55         local.__setattr__(self, attr, value) 
    56         ''' 
    57         else: 
    58             local.__setattr__(self, attr, value) 
    59             return 
    60             raise TypeError('%s is not a session dict.' % attr) 
    61         ''' 
    62      
    63     def __getattribute__(self, attr): 
    64         return local.__getattribute__(self, attr) 
  • trunk/cherrypy/lib/filter/sessionfilter/ramadaptor.py

    r566 r568  
    3535class RamAdaptor(BaseAdaptor): 
    3636 
    37     def __init__(self, sessionName, sessionPath): 
    38         BaseAdaptor.__init__(self, sessionName, sessionPath
     37    def __init__(self): 
     38        BaseAdaptor.__init__(self
    3939        self.__data = {} 
    4040     

Hosted by WebFaction

Log in as guest/cpguest to create tickets