#-----------------------------------------------------------------------------
# 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()
|