Top

session3.store.ShelveSessionStore module

Store sessions in a 'shelve' database.

"""
Store sessions in a 'shelve' database.
"""

import shelve
from session3.store.SessionStore import SessionStore

class ShelveSessionStore(SessionStore):
    """
    Open a 'shelve' dictionary with the given filename, and store sessions
    in it.

    Shelve is not thread safe or multiprocess safe.  See the "Restrictions"
    section for the shelve module in the Python Library Reference for 
    information about file locking.
    """

    is_multiprocess_safe = False  # DBM isn't process safe.
    is_thread_safe = False        # Don't know about this...
    
    def __init__(self, filename):
        """
        __init__ takes the filename to use as the shelve store.
        """
        self.filename = filename

    def open(self):
        """
        Open the shelve store file.
        """
        return shelve.open(self.filename, 'c')

    def load_session(self, id, default=None):
        """
        Load the session from the shelf.
        """
        
        db = self.open()
        return db.get(id, default)

    def delete_session(self, session):
        """
        Delete the given session from the shelf.
        """
        db = self.open()
        del db[session.id]

    def save_session(self, session):
        """
        Save the session to the shelf.
        """
        db = self.open()
        db[session.id] = session

Classes

class ShelveSessionStore

Open a 'shelve' dictionary with the given filename, and store sessions in it.

Shelve is not thread safe or multiprocess safe. See the "Restrictions" section for the shelve module in the Python Library Reference for information about file locking.

class ShelveSessionStore(SessionStore):
    """
    Open a 'shelve' dictionary with the given filename, and store sessions
    in it.

    Shelve is not thread safe or multiprocess safe.  See the "Restrictions"
    section for the shelve module in the Python Library Reference for 
    information about file locking.
    """

    is_multiprocess_safe = False  # DBM isn't process safe.
    is_thread_safe = False        # Don't know about this...
    
    def __init__(self, filename):
        """
        __init__ takes the filename to use as the shelve store.
        """
        self.filename = filename

    def open(self):
        """
        Open the shelve store file.
        """
        return shelve.open(self.filename, 'c')

    def load_session(self, id, default=None):
        """
        Load the session from the shelf.
        """
        
        db = self.open()
        return db.get(id, default)

    def delete_session(self, session):
        """
        Delete the given session from the shelf.
        """
        db = self.open()
        del db[session.id]

    def save_session(self, session):
        """
        Save the session to the shelf.
        """
        db = self.open()
        db[session.id] = session

Ancestors (in MRO)

Class variables

var is_multiprocess_safe

var is_thread_safe

Static methods

def __init__(

self, filename)

init takes the filename to use as the shelve store.

def __init__(self, filename):
    """
    __init__ takes the filename to use as the shelve store.
    """
    self.filename = filename

def delete_old_sessions(

self, minutes)

Delete all sessions that have not been modified for N minutes. The default implementation does nothing, meaning the store cannot delete old sessions.

This method is never called by the session manager. It's for your application maintenance program; e.g., a daily cron job.

def delete_old_sessions(self, minutes):
    """
    Delete all sessions that have not been modified for N minutes.  The
    default implementation does nothing, meaning the store cannot delete
    old sessions.
    This method is never called by the session manager.  It's for your
    application maintenance program; e.g., a daily cron job.
    """
    pass

def delete_session(

self, session)

Delete the given session from the shelf.

def delete_session(self, session):
    """
    Delete the given session from the shelf.
    """
    db = self.open()
    del db[session.id]

def has_session(

self, id)

Return true if the session exists in the store, else false.

def has_session(self, id):
    """Return true if the session exists in the store, else false."""
    return self.load_session(id, None)

def iter_sessions(

self)

Return an iterable of (id, session) for all sessions in the store.

This method is never called by the session manager; it's for admin applications that want to browse the sessions.

def iter_sessions(self):
    """
    Return an iterable of (id, session) for all sessions in the store.
    This method is never called by the session manager; it's for admin
    applications that want to browse the sessions.
    """
    raise NotImplementedError()

def load_session(

self, id, default=None)

Load the session from the shelf.

def load_session(self, id, default=None):
    """
    Load the session from the shelf.
    """
    
    db = self.open()
    return db.get(id, default)

def open(

self)

Open the shelve store file.

def open(self):
    """
    Open the shelve store file.
    """
    return shelve.open(self.filename, 'c')

def save_session(

self, session)

Save the session to the shelf.

def save_session(self, session):
    """
    Save the session to the shelf.
    """
    db = self.open()
    db[session.id] = session

def setup(

self)

Initialize the session store; e.g., create required database tables. If a previous store exists, overwrite it or raise an error. The default implmenetation does nothing, meaning no setup is necessary.

This method is never called by the session manager; it's for your application setup program.

def setup(self):
    """
    Initialize the session store; e.g., create required database tables.
    If a previous store exists, overwrite it or raise an error.  The
    default implmenetation does nothing, meaning no setup is necessary.
    This method is never called by the session manager; it's for your
    application setup program.
    """
    pass

Instance variables

var filename