SessionStore.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » SessionStore.py
from Common import *


class SessionStore(Object):
  """A general session store.

  SessionStores are dictionary-like objects used by Application to
  store session state. This class is abstract and it's up to the
  concrete subclass to implement several key methods that determine
  how sessions are stored (such as in memory, on disk or in a
  database).

  Subclasses often encode sessions for storage somewhere. In light
  of that, this class also defines methods encoder(), decoder() and
  setEncoderDecoder(). The encoder and decoder default to the load()
  and dump() functions of the cPickle or pickle module. However,
  using the setEncoderDecoder() method, you can use the functions
  from marshal (if appropriate) or your own encoding scheme.
  Subclasses should use encoder() and decoder() (and not
  pickle.load() and pickle.dump()).

  Subclasses may rely on the attribute self._app to point to the
  application.

  Subclasses should be named SessionFooStore since Application
  expects "Foo" to appear for the "SessionStore" setting and
  automatically prepends Session and appends Store. Currently, you
  will also need to add another import statement in Application.py.
  Search for SessionStore and you'll find the place.

  TO DO

  * Should there be a check-in/check-out strategy for sessions to
    prevent concurrent requests on the same session? If so, that can
    probably be done at this level (as opposed to pushing the burden
    on various subclasses).

  """


  ## Init ##

  def __init__(self, app):
    """ Subclasses must invoke super. """
    Object.__init__(self)
    self._app = app
    try:
      import cPickle as pickle
    except ImportError:
      import pickle
    if hasattr(pickle, 'HIGHEST_PROTOCOL'):
      def dumpWithHighestProtocol(obj, f,
          proto=pickle.HIGHEST_PROTOCOL, dump=pickle.dump):
        return dump(obj, f, proto)
      self._encoder = dumpWithHighestProtocol
    else:
      self._encoder = pickle.dump
    self._decoder = pickle.load


  ## Access ##

  def application(self):
    return self._app


  ## Dictionary-style access ##

  def __len__(self):
    raise AbstractError, self.__class__

  def __getitem__(self, key):
    raise AbstractError, self.__class__

  def __setitem__(self, key, item):
    raise AbstractError, self.__class__

  def __delitem__(self, key):
    """Delete an item.

    Subclasses are responsible for expiring the session as well.
    Something along the lines of:
      sess = self[key]
      if not sess.isExpired():
        sess.expiring()

    """
    raise AbstractError, self.__class__

  def has_key(self, key):
    raise AbstractError, self.__class__

  def keys(self):
    raise AbstractError, self.__class__

  def clear(self):
    raise AbstractError, self.__class__

  def setdefault(self, key, default):
    raise AbstractError, self.__class__


  ## Application support ##

  def storeSession(self, session):
    raise AbstractError, self.__class__

  def storeAllSessions(self):
    raise AbstractError, self.__class__

  def cleanStaleSessions(self, task=None):
    """Clean stale sessions.

    Called by the Application to tell this store to clean out all
    sessions that have exceeded their lifetime.

    """
    curTime = time.time()
    for key in self.keys():
      try:
        sess = self[key]
      except KeyError:
        pass # session was already deleted by some other thread
      else:
        if curTime - sess.lastAccessTime() >= sess.timeout() \
            or sess.timeout() == 0:
          try:
            del self[key]
          except KeyError:
            pass # already deleted by some other thread


  ## Convenience methods ##

  def items(self):
    itms = []
    for k in self.keys():
      try:
        itms.append((k, self[k]))
      except KeyError:
        # since we aren't using a lock here, some keys
        # could be already deleted again during this loop
        pass
    return itms

  def values(self):
    vals = []
    for k in self.keys():
      try:
        vals.append(self[k])
      except KeyError:
        pass
    return vals

  def get(self, key, default=None):
    try:
      return self[key]
    except KeyError:
      return default


  ## Encoder/decoder ##

  def encoder(self):
    return self._encoder

  def decoder(self):
    return self._decoder

  def setEncoderDecoder(self, encoder, decoder):
    self._encoder = encoder
    self._decoder = decoder


  ## As a string ##

  def __repr__(self):
    d = {}
    for key, value in self.items():
      d[key] = value
    return repr(d)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.