# HG changeset patch # User paulb # Date 1132521489 0 # Node ID 015723207074956d6b5f74ae082cd438980c4856 # Parent 559ad9fdc01b4ad4b943b4a1932e81475ed4cc2a [project @ 2005-11-20 21:18:09 by paulb] Changed the SessionStore to use DirectoryRepository. diff -r 559ad9fdc01b -r 015723207074 WebStack/Helpers/Session.py --- a/WebStack/Helpers/Session.py Sun Nov 20 21:17:57 2005 +0000 +++ b/WebStack/Helpers/Session.py Sun Nov 20 21:18:09 2005 +0000 @@ -21,46 +21,37 @@ """ import shelve -import os -import time import random import sys +from WebStack.Repositories.Directory import DirectoryRepository class SessionStore: "A class representing a session store." - def __init__(self, trans, session_directory, session_cookie_name="SID", concurrent=1, delay=1): + def __init__(self, trans, session_directory, session_cookie_name="SID", delay=1): """ Initialise the session store, specifying the transaction 'trans' within - which all session access will occur, a base 'session_directory', the + which all session access will occur, a base 'session_directory', and the optional 'session_cookie_name' where the session identifier is held for - each user, and specifying using the optional 'concurrent' parameter - whether concurrent access within the framework might occur (1) or - whether the framework queues accesses at some other level (0). The - optional 'delay' argument specifies the time in seconds between each + each user. + + The optional 'delay' argument specifies the time in seconds between each poll of the session file when that file is found to be locked for - editing. + editing. (This is part of the underlying repository's behaviour.) """ self.trans = trans - self.session_directory = session_directory self.session_cookie_name = session_cookie_name - self.concurrent = concurrent - self.delay = delay + self.repository = DirectoryRepository(session_directory, delay=delay) # Internal state. self.store = None - self.store_filename, self.edit_filename = None, None + self.current_session_id = None self.to_expire = None - # Attempt to create a session directory if it does not exist. - - if not os.path.exists(self.session_directory): - os.mkdir(self.session_directory) - def close(self): "Close the store, tidying up files and filenames." @@ -68,12 +59,9 @@ if self.store is not None: self.store.close() self.store = None - if self.edit_filename is not None: - try: - os.rename(self.edit_filename, self.store_filename) - except OSError: - pass - self.edit_filename, self.store_filename = None, None + if self.current_session_id is not None: + self.repository.unlock(self.current_session_id) + self.current_session_id = None # Handle expiry appropriately. @@ -95,26 +83,9 @@ def _expire_session(self, session_id): - """ - Expire the session with the given 'session_id'. Note that in concurrent - session stores, this operation will block if another execution context - is editing the session. - """ + "Expire the session with the given 'session_id'." - filename = os.path.join(self.session_directory, session_id) - if self.concurrent: - while 1: - try: - os.unlink(filename) - except OSError: - time.sleep(self.delay) - else: - break - else: - try: - os.unlink(filename) - except OSError: - pass + del self.repository[session_id] def get_session(self, create): @@ -149,39 +120,13 @@ Returns a dictionary-like object representing the session. """ - filename = os.path.join(self.session_directory, session_id) - - # Enforce locking. - - if self.concurrent: - - # Where the session is present (possibly being edited)... - - if os.path.exists(filename) or os.path.exists(filename + ".edit"): - while 1: - try: - os.rename(filename, filename + ".edit") - except OSError: - time.sleep(self.delay) - else: - break - - # Where no session is present and none should be created, return. - - elif not create: - return None - - self.store_filename = filename - filename = filename + ".edit" - self.edit_filename = filename - - # For non-concurrent situations, return if no session exists and none - # should be created. - - elif not os.path.exists(filename) and not create: + try: + store_filename = self.repository.lock(session_id, create=create, opener=shelve.open) + self.current_session_id = session_id + except KeyError: return None - self.store = shelve.open(filename) + self.store = shelve.open(store_filename) return Wrapper(self.store) def _get_session_identifier(self):