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

Changeset 395

Show
Ignore:
Timestamp:
06/30/05 17:23:19
Author:
mikerobi
Message:

sessionFilter cleanups

Files:

Legend:

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

    r390 r395  
    5050response = local() 
    5151 
    52 # Create as sessions object for accessing session data 
    53 sessions = local() 
    54  
    5552# Create threadData object as a thread-specific all-purpose storage 
    5653threadData = local() 
    57  
    5854 
    5955# decorator function for exposing methods 
  • trunk/cherrypy/config.py

    r394 r395  
    5858    'sessionFilter.on' : False, 
    5959    'sessionFilter.sessionList' : ['default'], 
     60    'sessionFIlter.storageAdaptors' : {}, 
    6061    'sessionFilter.default.on': True, 
    6162    'sessionFilter.default.timeout': 60, 
  • trunk/cherrypy/lib/filter/sessionfilter/__init__.py

    r392 r395  
    2424 
    2525 
    26 # this function gets all active sessions based on the path 
    27 def _getSessions(): 
    28     sessions = [] 
    29      
    30     sessionLists = cherrypy.config.getAll('sessionFilter.sessionList') 
    31      
    32     # loop across all paths with session listts 
    33     for sessionPath, sessionList in sessionLists.iteritems(): 
    34          
    35         # if it isn't a list make it one 
    36         if not isinstance(sessionList,list): 
    37             sessionList=[sessionList] 
    38          
    39         for index in xrange(len(sessionList)): 
    40              
    41             session = sessionList[index] 
    42              
    43             # check if the session is a string, if it is 
    44             # try and match it to a storage adaptor and replace 
    45             # the string in the list with the initilized adaptor 
    46             if isinstance(session, str): 
    47                 sessionName = session 
    48                 # if the session is off skip to the next session in the list 
    49                 if not cherrypy.config.get('sessionFilter.%s.on' % sessionName, True): 
    50                     continue 
    51                  
    52                 # look up the storage type or return the default 
    53                 storageType = sessionconfig.retrieve('storageType', sessionName) 
    54                  
    55                 # try to initilize a built in session 
    56                 try: 
    57                     sessionManager = _sessionTypes[storageType](sessionName) 
    58                 except KeyError: 
    59                     # the storageType is not built in 
    60                     try: 
    61                         sessionManger = cherrypy._cputil.getSpecialAttribute(storageType)(sessionName) 
    62                     except cherrypy.InternalError: 
    63                         # it is not a built in session and the adaptor has not been 
    64                         # set as an attribute in the CherryPy tree 
    65                         raise SessionBadStorageTypeError(storageType) 
    66                          
    67                  
    68                 # we need to remember the path 
    69                 sessionManager.path = sessionPath 
    70  
    71                 # the session is born clean 
    72                 sessionManager.lastCleanUp = time.time() 
    73                  
    74                 # replace the entry in the session list 
    75                 sessionList[index] = sessionManager 
    76                  
    77                 # put then new session list back in the config map 
    78                 cherrypy.config.update({sessionPath: {'sessionFilter.sessionList' : sessionList} }) 
    79             else: 
    80                 sessionManager = session 
    81              
    82             sessions.append(sessionManager) 
    83      
    84     return sessions 
    85  
    8626class SessionFilter: 
    8727    """ 
     
    8929    """ 
    9030 
     31    def __init__(self): 
     32        """ Initilizes the session filter and creates cherrypy.sessions  """ 
     33 
     34        try: 
     35            from threading import local 
     36        except ImportError: 
     37            from cherrypy._cpthreadinglocal import local 
     38 
     39        # Create as sessions object for accessing session data 
     40        cherrypy.sessions = local() 
     41 
     42 
     43    def __newSessionManager(self, sessionName, sessionPath): 
     44        """  
     45        Takes the name of a new session and its configuration path. 
     46        Returns a storageAdaptor instance maching the configured storage type. 
     47        If the storage type is not built in, it tries to use sessionFilter.storageAadaptors. 
     48        If the storage type still can not be found, an exception is raised. 
     49        """ 
     50        # look up the storage type or return the default 
     51        storageType = sessionconfig.retrieve('storageType', sessionName) 
     52         
     53        # try to initilize a built in session 
     54        try: 
     55            storageAdaptor = _sessionTypes[storageType] 
     56        except KeyError: 
     57            # the storageType is not built in 
     58             
     59            # check for custom storage adaptors 
     60            adaptors = cpg.config.get('sessionFilter.storageAdaptors') 
     61            try: 
     62                storageAdaptor = adaptors[storageType] 
     63            except cherrypy.InternalError: 
     64                # we couldn't find the session 
     65                raise SessionBadStorageTypeError(storageType) 
     66         
     67        return storageAdaptor(sessionName, sessionPath)         
     68         
     69    # this function gets all active sessions based on the path 
     70    def __getSessions(self): 
     71        """ 
     72        Returns a list containing instances of all active sessions for the current request path. 
     73        """ 
     74        sessions = [] 
     75         
     76        sessionLists = cherrypy.config.getAll('sessionFilter.sessionList') 
     77         
     78        # loop across all paths with session listts 
     79        for sessionPath, sessionList in sessionLists.iteritems(): 
     80             
     81            # if it isn't a list make it one 
     82            if not isinstance(sessionList,list): 
     83                sessionList=[sessionList] 
     84             
     85            for index in xrange(len(sessionList)): 
     86                 
     87                session = sessionList[index] 
     88                 
     89                # check if the session is a string, if it is 
     90                # try and match it to a storage adaptor and replace 
     91                # the string in the list with the initilized adaptor 
     92                if isinstance(session, str): 
     93                    sessionName = session 
     94     
     95                    # if the session is off skip to the next session in the list 
     96                    if not cherrypy.config.get('sessionFilter.%s.on' % session, True): 
     97                        continue 
     98                    sessionManager = self.__newSessionManager(session, sessionPath) 
     99                     
     100                    # now we replace the string name with the instanced storage class 
     101                    # thanks to references this will directly change the configMap 
     102                    sessionList[index] = sessionManager 
     103                     
     104                else: 
     105                    sessionManager = session 
     106                 
     107                sessions.append(sessionManager) 
     108         
     109        return sessions 
     110 
     111 
     112 
    91113    def __initSessions(self): 
    92114         
     
    94116        sessionKeys = self.getSessionKeys() 
    95117         
    96         for sessionManager in _getSessions(): 
     118        for sessionManager in self.__getSessions(): 
    97119            sessionName = sessionManager.sessionName 
    98120            sessionKey = sessionKeys.get(sessionName, None) 
     
    113135        sessionKeys = {} 
    114136         
    115         for sessionManager in _getSessions(): 
     137        for sessionManager in self.__getSessions(): 
    116138            sessionName = sessionManager.sessionName 
    117139             
     
    146168    def __saveSessions(self): 
    147169         
    148         for sessionManager in _getSessions(): 
     170        for sessionManager in self.__getSessions(): 
    149171            sessionName = sessionManager.sessionName 
    150172             
  • trunk/cherrypy/lib/filter/sessionfilter/anydbadaptor.py

    r392 r395  
    4242    noCache = False 
    4343     
    44     def __init__(self, sessionName): 
    45         BaseSession.__init__(self, sessionName
     44    def __init__(self, sessionName, sessionPath): 
     45        BaseSession.__init__(self, sessionName, sessionPath
    4646         
    4747        # we must make sure the db file is unique 
  • trunk/cherrypy/lib/filter/sessionfilter/basesession.py

    r382 r395  
    7777        return newKey 
    7878     
    79     def __init__(self, sessionName): 
     79    def __init__(self, sessionName, sessionPath): 
    8080        """ 
    8181        Create the session caceh and set the session name.  Make if you write 
     
    8686        self.__sessionCache = {} 
    8787        self.sessionName = sessionName 
     88         
     89        #set the path 
     90        self.path = sessionPath 
     91         
     92 
     93        # the session is born clean 
     94        self.lastCleanUp = time.time() 
    8895     
    8996     
  • trunk/cherrypy/lib/filter/sessionfilter/fileadaptor.py

    r392 r395  
    4242    noCache = False 
    4343     
    44     def __init__(self, sessionName): 
    45         BaseSession.__init__(self, sessionName
     44    def __init__(self, sessionName, sessionPath): 
     45        BaseSession.__init__(self, sessionName, sessionPath
    4646        self.__fileLock = threading.RLock() 
    4747 
  • trunk/cherrypy/lib/filter/sessionfilter/ramadaptor.py

    r392 r395  
    3434class RamSession(BaseSession): 
    3535 
    36     def __init__(self, sessionName): 
    37         BaseSession.__init__(self, sessionName
     36    def __init__(self, sessionName, sessionPath): 
     37        BaseSession.__init__(self, sessionName, sessionPath
    3838        self.__data = {} 
    3939     
  • trunk/cherrypy/lib/filter/sessionfilter/sqlobjectadaptor.py

    r394 r395  
    8989class SQLObjectSession(BaseSession): 
    9090     
    91     def __init__(self, sessionName): 
    92         BaseSession.__init__(self, sessionName
     91    def __init__(self, sessionName, sessionPath): 
     92        BaseSession.__init__(self, sessionName, sessionPath
    9393         
    9494        dbClassName = cherrypy.config.get('%s.dbClassName' % sessionName) 

Hosted by WebFaction

Log in as guest/cpguest to create tickets