# -*- 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.
#-----------------------------------------------------------------------------
"""
KeyValueCoding API
CVS information
$Id: KeyValueCoding.py 932 2004-07-20 06:21:57Z sbigaret $
"""
__version__='$Revision: 932 $'[11:-2]
try:
from Interface import Base
except:
class Base:
pass
def valueForKeyPath(object, keypath):
"""
order: setKey(), _setKey(), key, _key
Might be overriden to perform additional transformations before
returning the object
If parameter 'object' is a list or a tuple (either at the first call or
during the traversal of the keypath), 'valueForKeyPath' is applied to each
item of the list.
"""
def takeValueForKeyPath(object, value, keypath):
"""
order: setKey(), _setKey(), key, _key
Might be overriden to perform additional transformations before
returning the object
If, during traversal of keypath, a sequence is met, the method raises
'ValueError'.
"""
class KeyValueCodingInterface(Base):
"""
Mix-in class providing a common API to access or assign values to
object's properties.
It defined three kinds of methods:
- the so-called "public" methods: 'valueForKey()' and 'takeValueForKey()',
which search for getters/setters before looking at attributes.
- the so-called "private" methods: 'storedValueForKey()' and
'takeStoredValueForKey()', which search getters or setters in a different
way (see their docstrings dor details). These methods are mainly used
within the framework when the data stream goes from some database to the
real objects (bottom-up stream)
- error handling methods: 'handleQueryWithUnboundKey()' and
'handleAssignementForUnboundKey()'.
"""
def valueForKey(self, key):
"""
order: getKey(), key(), _getKey(), _key(), key, _key
Might be overriden to perform additional transformations before
returning the object
"""
def takeValueForKey(self, value, key):
"""
order: setKey(), _setKey(), key, _key
Might be overriden to perform additional transformations before
returning the object
Might call 'handleTakeValueForUnboundKey()'...
"""
def takeValueForKeyPath(self, value, key):
"-"
return takeValueForKeyPath(self, value, key)
def storedValueForKey(self, key):
"""
order: _getKey(), _key(), key, _key, getKey(), key()
Might be overriden to perform additional transformations before
returning the object
"""
def takeStoredValueForKey(self, value, key):
"""
order: _setKey(), _key, key, setKey()
Might be overriden to perform additional transformations before
returning the object
Might call 'handleTakeStoredValueForUnboundKey()'...
"""
def handleQueryWithUnboundKey(self, key):
"""
Raises AttributeError. Override this method if you want to make any
appropriate processing when an key is not found ; overriding methods
should 'raise AttributeError, key' if the custom process of getting the
value for the specified key fails.
"""
def handleTakeValueForUnboundKey(self, value, key):
"""
Raises AttributeError. Override this method if you want to make any
appropriate processing when an key appears not to be settable using
'takeValueForKey()'; overriding methods should 'raise AttributeError, key'
if the custom process of setting value for the specified key fails.
"""
def handleTakeStoredValueForUnboundKey(self, value, key):
"""
Raises AttributeError. Override this method if you want to make any
appropriate processing when an key appears not to be settable using
'takeStoredValueForKey()'; overriding methods should 'raise
AttributeError, key' if the custom process of setting value for the
specified key fails.
"""
# KVC extension
def takeStoredValuesFromDictionary(self, dictionary):
"""
Invokes takeStoredValueForKey() for each key, value in the dictionary.
Example::
object.takeStoredValueForKey({'attr1': 'value1','attr2': 'value2'})
"""
self.takeStoredValueForKey(dictionary[key], key)
def takeValuesFromDictionary(self, dictionary):
"""
Invokes takeValueForKey() for each key, value in the dictionary.
Example::
object.takeValueForKey({'attr1': 'value1','attr2': 'value2'})
"""
def valuesForKeys(self, keys):
"""
Returns the list of values assigned to attributes specified in keys. This
method uses valueForKey()
Example::
>>> [o.valuesForKeys(('firstName', 'lastName'))
... for o in ec.fetch('Employee', isDeep=1)]
[['Jeanne', 'Cleese'], ['John Jr.', 'Cleese'], ['John', 'Cleese']]
"""
## DEPRECATED alias
def setValueForKey(self, value, key):
"DEPERECATED -- An alias for 'takeValueForKey()'"
def setValueForKeyPath(self, value, keypath):
"DEPERECATED -- An alias for 'takeValueForKey()'"
def setStoredValueForKey(self, value, key):
"DEPERECATED -- An alias for 'takeStoredValueForKey()'"
|