EditingContextSessioning.py :  » Database » Modeling-Framework » Modeling-0.9 » Modeling » utilities » 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 » Database » Modeling Framework 
Modeling Framework » Modeling 0.9 » Modeling » utilities » EditingContextSessioning.py
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
EditingContextSessioning provides a set of functions that can be used to bind
to an existing sessioning mechanism so that each existing session has its own
EditingContext.

Typical usage:

  - a session creates its EditingContext, either explicitly by instanciating
    one and registering it with registerEditingContext(), or lazily by simply
    calling getEditingContext() with 'create=1' when needed.

  - The session calls getEditingContext() to retrieve its own EC.

  - When the session is about to be destroyed, it calls
    unregisterEditingContext()
  
The session_id parameter required by all three methods must be a valid key
object for python dictionary.

MT-status: MT-safe

CVS: $Id: EditingContextSessioning.py 932 2004-07-20 06:21:57Z sbigaret $
"""

__version__='$Revision: 932 $'[11:-2]

from Modeling.EditingContext import EditingContext
from Modeling.logging import debug

## you should *NOT* directly access to module's variable
## '__editingContextPerSession' since it is considered private (MT-safety)
from threading import RLock
__editingContextPerSession={}
editingContextPerSession_lock=RLock()
lock=editingContextPerSession_lock.acquire
unlock=editingContextPerSession_lock.release

def registerEditingContext(session_id, editingContext):
  """
  Associates 'editingContext' to 'session_id'. Any previously registered
  editing context for this session_id is silently dropped.
  """
  lock()
  debug('Registering EC %s for session_id: %s'%(editingContext,session_id))
  try:
    __editingContextPerSession[session_id]=editingContext
  finally:
    unlock()

def getEditingContext(session_id, create=1):
  """
  Returns the EditingContext registered for a given session

  Parameters:

    session_id -- the identifier of the current session

    create (default: true) -- tells what to do when no EditingContext js
      registered for 'session_id': if true, the method creates, registers and
      returns a new EditingContext; otherwise, the method returns None

  """
  lock()
  try:
    ec=__editingContextPerSession.get(session_id, None)
    if not ec and create:
      ec=EditingContext()
      registerEditingContext(session_id, ec)
    return ec
  finally:
    unlock()

def unregisterEditingContext(session_id, dispose=1):
  """
  Unregisters the EditingContext corresponding to 'session_id'. It silently
  returns if no EditingContext was registered for 'session_id'.

  Parameters:

    session_id -- the identifier of the current session

    dispose (default: true) -- if true, call dispose() on the unregistered
      EditingContext. If false, do nothing, just wait for the garbage
      collector to trigger it via EditingContext.__del__
    
  """
  lock()
  try:
    ec=__editingContextPerSession.get(session_id, None)
    if ec:
      debug('Unregistering EC %s for session_id: %s'%(ec,session_id))
      del __editingContextPerSession[session_id]
      if dispose:
        ec.dispose()
  finally:
    unlock()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.