# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# 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.
#-----------------------------------------------------------------------------
"""
ClassDescription
!!! Abstract class !!!
Class description fills the gap between a model and informations needed at
run-time, such as ...
Automatically registered when a model is loaded/instanciated.
Module's functions are the one a ZModelizationTool should be able to answer.
(or something else?)
CVS information
$Id: ClassDescription.py 932 2004-07-20 06:21:57Z sbigaret $
"""
__version__='$Revision: 932 $'[11:-2]
from logging import info,warn
from delegation import DelegateWrapper
from utils import staticmethod,cache_simple_methods
# interfaces
from interfaces.ClassDescription import ClassDescriptionInterface
from interfaces.ClassDescriptionDelegate import ClassDescriptionDelegate
# Integer constants
DELETE_NULLIFY='nullify'
DELETE_DENY='deny'
DELETE_CASCADE='cascade'
DELETE_NOACTION='noaction' # careful here...
# For backward compatibility, used in Relationship.setDeleteRule()
old_delete_rules= { 0: DELETE_NULLIFY,
1: DELETE_DENY,
2: DELETE_CASCADE,
3: DELETE_NOACTION,
}
# Notification
ClassDescriptionNeededForEntityNameNotification='ClassDescriptionNeededForEntityName'
#
# Module methods: cache for ClassDescription
#
from NotificationFramework import NotificationCenter
from threading import RLock
classDescriptionsCache_global_lock=RLock()
lock=classDescriptionsCache_global_lock.acquire
unlock=classDescriptionsCache_global_lock.release
# PRIVATE: do not access directly
__classDescriptionCache__={}
def classDescriptionForName(name):
"""
Returns the ClassDescription corresponding to name
If the requested class description cannot be found, this method posts a
'ClassDescriptionNeededForEntityNameNotification' so that listeners can
register the apropriate class description --in a standard confioguration,
the default ModelSet listens to that notification and registers the class
descriptions each time they are needed.
"""
lock()
try:
_cd=__classDescriptionCache__.get(name, None)
if _cd:
return _cd
# Okay, can someone provide it?
NC=NotificationCenter
NC.postNotification(ClassDescriptionNeededForEntityNameNotification,
name)
_cd=__classDescriptionCache__.get(name, None)
return _cd
finally:
unlock()
def classDescriptionForObject(anObject):
"""
Returns the ClassDescription associated to the object 'anObject'
"""
lock()
try:
name=anObject.entityName()
return classDescriptionForName(name)
finally:
unlock()
def registeredNames():
"Returns the registered names"
lock()
try:
return __classDescriptionCache__.keys()
finally:
unlock()
def registerClassDescription(aClassDescription, name):
"""
Assigns the supplied classDescription to 'name'.
The supplied ClassDescription's simple methods are not cached, unless
MDL_ENABLE_SIMPLE_METHOD_CACHE is set to any non-empty string. See
utils.cache_simple_methods() for details.
"""
lock()
try:
cache_simple_methods(aClassDescription, ['rootClassDescription'])
__classDescriptionCache__[name]=aClassDescription
finally:
unlock()
def invalidateClassDescriptionCache():
"Simply empties the set of held class description"
lock()
try:
#info('invalidateClassDescriptionCache', 'cache cleared')
__classDescriptionCache__.clear()
finally:
unlock()
##
## ClassDescription
##
from Modeling.utils import base_object
class ClassDescription(base_object):
"""
This class is an *abstract* class ; all methods defined in
ClassDescriptionInterface raises and should be overridden in concrete
subclasses.
Please refer to ClassDescriptionInterface for documentation.
"""
__implements__=(ClassDescriptionInterface,)
# Static methods
#
classDescriptionForName=staticmethod(classDescriptionForName)
#
classDescriptionForObject=staticmethod(classDescriptionForObject)
#
registeredNames=staticmethod(registeredNames)
#
registerClassDescription=staticmethod(registerClassDescription)
#
invalidateClassDescriptionCache=staticmethod(invalidateClassDescriptionCache)
_delegate=None
def adaptorName(self):
abstract()
def attributesKeys(self):
abstract()
def awakeObjectFromInsertion(self, object, editingContext):
"""
__TBD Currently does nothing.
Subclasses overriding this method should call this method before proceeding
"""
warn('not implemented yet: does nothing')
return
def awakeObjectFromFetch(self, object, editingContext):
"""
__TBD Currently does nothing.
Subclasses overriding this method should call this method before proceeding
"""
warn('not implemented yet: does nothing')
return
def classDescriptionForDestinationKey(self, aKey):
abstract()
def createInstanceWithEditingContext(self, editingContext):
abstract()
def delegate(self):
"See interfaces.ClassDescription for details"
if self._delegate:
return self._delegate.delegateObject()
return None
def deleteRuleForRelationshipKey(self, aKey):
abstract()
def displayNameForKey(self, aKey):
abstract()
def entityName(self):
abstract()
def foreignKeys(self):
abstract()
def inverseForRelationshipKey(self, aKey):
abstract()
def primaryKeys(self):
abstract()
def propagateDeleteForObject(self, anObject, editingContext):
abstract()
def propagateInsertionForObject(self, anObject, editingContext):
"""
Examines the object's relationships and searches for related objects not
inserted in 'editingContext', then inserts these objects into
editingContextif they are not already registered.
This is normally called when an EditingContext gets the
processRecentChanges() message.
Subclasses must override this method without calling it.
"""
abstract()
def registerProvider(self, aProvider):
abstract()
def setDelegate(self, aDelegate):
"See interfaces.ClassDescription for details"
self._delegate=DelegateWrapper(ClassDescriptionInterface,
aDelegate)
def toManyRelationshipKeys(self):
abstract()
def toOneRelationshipKeys(self):
abstract()
def userPresentableDescriptionForObject(self, anObject):
abstract()
def validateObjectForDelete(self, anObject):
abstract()
def validateObjectForSave(self, anObject):
abstract()
def validateValueForKey(self, anObject, aValue, aKey):
abstract()
def XMLDescriptionForObject(self, anObject):
abstract()
def abstract():
raise NotImplementedError, "ClassDescription is an abstract class ; concrete subclasses should override this method"
|