Update of /cvsroot/webware/Webware/MiddleKit/Core
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23539/Core
Modified Files:
Klass.py ListAttr.py
Log Message:
- revamped Core.ListAttr class:
- new error check: the target class actually exists in the model
- new error check: the back ref attr actually exists in the target class
- refinement: when back ref attr is automatic it can be "Foo" or "foo"
- refinement: when back ref attr is automatic, it is computed once and
cached
Index: Klass.py
===================================================================
RCS file: /cvsroot/webware/Webware/MiddleKit/Core/Klass.py,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** Klass.py 12 Mar 2004 23:46:11 -0000 1.22
--- Klass.py 24 Mar 2004 07:46:27 -0000 1.23
***************
*** 24,27 ****
--- 24,28 ----
self._pyClass = 0 # 0 means never computed. None would mean computed, but not found.
self._backObjRefAttrs = None
+ self._allAttrs = None
if dict is not None:
***************
*** 75,79 ****
def _makeAllAttrs(self):
"""
! Makes two list attributes accessible via methods:
allAttrs - every attr of the klass including inherited and derived attributes
allDataAttrs - every attr of the klass including inherited, but NOT derived
--- 76,80 ----
def _makeAllAttrs(self):
"""
! Makes list attributes accessible via methods for the following:
allAttrs - every attr of the klass including inherited and derived attributes
allDataAttrs - every attr of the klass including inherited, but NOT derived
***************
*** 81,85 ****
--- 82,91 ----
...and a dictionary attribute used by lookupAttr().
+
+ Does nothing if called extra times.
"""
+ if self._allAttrs is not None:
+ return
+
klass = self
klasses = []
***************
*** 204,207 ****
--- 210,216 ----
def lookupAttr(self, name, default=NoDefault):
+ if self._allAttrs is None:
+ # happens sometimes during awakeFromRead()
+ self._makeAllAttrs()
if default is NoDefault:
return self._allAttrsByName[name]
Index: ListAttr.py
===================================================================
RCS file: /cvsroot/webware/Webware/MiddleKit/Core/ListAttr.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** ListAttr.py 9 Mar 2004 19:31:09 -0000 1.5
--- ListAttr.py 24 Mar 2004 07:46:27 -0000 1.6
***************
*** 11,24 ****
Attr.__init__(self, dict)
self._className = dict['Type'].split()[-1]
! if dict.has_key('BackRefAttr'):
! self._backRefAttr = dict['BackRefAttr']
! else:
! self._backRefAttr = None
!
! # @@ 2000-11-25 ce: check that the class really exists
! if self.get('Max') is not None:
! self['Max'] = int(self['Max'])
if self.get('Min') is not None:
self['Min'] = int(self['Min'])
def className(self):
--- 11,19 ----
Attr.__init__(self, dict)
self._className = dict['Type'].split()[-1]
! self._backRefAttr = None # init'ed in awakeFromRead()
if self.get('Min') is not None:
self['Min'] = int(self['Min'])
+ if self.get('Max') is not None:
+ self['Max'] = int(self['Max'])
def className(self):
***************
*** 27,39 ****
def backRefAttrName(self):
! ''' Returns the name of the back-reference attribute in the referenced
! class. It is necessary to be able to override the default back ref
to create data structures like trees, in which a Middle object might
! reference a parent and multiple children, all of the same class as
itself.
! '''
! if self._backRefAttr:
! return self._backRefAttr
else:
! classname = self.klass().name()
! return classname[0].lower() + classname[1:]
--- 22,62 ----
def backRefAttrName(self):
! """
! Returns the name of the back-reference attribute in the referenced
! class. It is necessary to be able to override the default back ref
to create data structures like trees, in which a Middle object might
! reference a parent and multiple children, all of the same class as
itself.
! """
! assert self._backRefAttr is not None
! return self._backRefAttr
!
! def awakeFromRead(self):
! """
! Check that the target class and backRefAttr actually exist.
! """
! # Check that for "list of Foo", Foo actually exists. And,
! # Compute self._targetKlass.
! from Model import ModelError
! self._targetKlass = self.model().klass(self.className(), None)
! if not self._targetKlass:
! raise ModelError, 'class %s: attr %s: cannot locate target class %s for this list.' % (
! self.klass().name(), self.name(), self.className())
!
! # Compute self._backRefAttr.
! if self.has_key('BackRefAttr'):
! self._backRefAttr = self['BackRefAttr']
else:
! className = self.klass().name()
! attr = self._targetKlass.lookupAttr(className, None)
! if attr is None:
! className = className[0].lower() + className[1:]
! attr = self._targetKlass.lookupAttr(className, None)
! if attr is None:
! pass # error?
! self._backRefAttr = className
!
! # Check that the backRefAttr, whether explicit or implicit, exists in the target class.
! if self._targetKlass.lookupAttr(self.backRefAttrName(), None) is None:
! raise ModelError, 'class %s: attr %s: cannot locate backref attr %s.%s for this list.' % (
! self.klass().name(), self.name(), self.className(), self.backRefAttrName())
|