Author: jamest Date: 2005-09-11 19:01:05 -0500 (Sun, 11 Sep 2005) New Revision: 7896
Modified: trunk/gnue-common/src/apps/GDebug.py trunk/gnue-common/src/definitions/GObjects.py Log: added findChildNamed to GObj made gEnter and gDebug return True so they can be used behind an assert Modified: trunk/gnue-common/src/apps/GDebug.py =================================================================== --- trunk/gnue-common/src/apps/GDebug.py 2005-09-09 17:42:27 UTC (rev 7895) +++ trunk/gnue-common/src/apps/GDebug.py 2005-09-12 00:01:05 UTC (rev 7896) @@ -104,10 +104,11 @@ # ----------------------------------------------------------------------------- def __noDebug (level, message, dropToDebugger = False): - pass + return True + def __noEnter (level = 1): - pass + return True def __noLeave (level = 1, *result): if len (result): @@ -180,6 +181,7 @@ @param dropToDebugger: if set to True, Python's interactive debugger will be switched to trace mode. This requires that setDebugger has been called before. + @return: Always true so it can be filtered out via assert """ if level in _DEBUG_LEVELS : @@ -200,6 +202,7 @@ __dumpMessage (level, file, message, dropToDebugger) + return True # ----------------------------------------------------------------------------- # Add a function-signature to the debug output @@ -213,10 +216,11 @@ gEnter is intended to be called at the begin of a function. @param level: the debug-level the message will be logged in + @return: Always true so it can be filtered out via assert """ if not level in _DEBUG_LEVELS: - return + return True # Get the caller's frame frame = sys._getframe (1) @@ -261,6 +265,8 @@ # Make sure to release the reference to the frame object. This keeps # garbage collection doing a fine job :) del frame + + return True # ----------------------------------------------------------------------------- Modified: trunk/gnue-common/src/definitions/GObjects.py =================================================================== --- trunk/gnue-common/src/definitions/GObjects.py 2005-09-09 17:42:27 UTC (rev 7895) +++ trunk/gnue-common/src/definitions/GObjects.py 2005-09-12 00:01:05 UTC (rev 7896) @@ -22,11 +22,10 @@ # GObjects.py # # DESCRIPTION: -# Base class for GNUe objects which can be represented as XML -# -# NOTES: -# -import sys +""" +Base class for GNUe objects which can be represented as XML +""" +__revision__ = "$Id$" from xml.sax import saxutils from gnue.common.apps import GDebug @@ -35,7 +34,6 @@ from gnue.common.definitions.GParserHelpers import GContent, ParserObj from gnue.common.formatting import GTypecast from gnue.common.logic.GTriggerCore import GTriggerCore -import types # # Class GObj @@ -111,7 +109,7 @@ for phase in range(iterations): self._phaseInit(phase) - def _phaseInit(self,phase): + def _phaseInit(self, phase): """ Used internally by phaseInit to walk through the object tree initializing objects. @@ -178,8 +176,8 @@ The function passed to the tree walker to extract the length of the _inits list. Used by maxInits. """ - if hasattr(object,'_inits'): - self._initCount = max(self._initCount,len(object._inits)) + if hasattr(object, '_inits'): + self._initCount = max(self._initCount, len(object._inits)) def getChildrenAsContent(self): @@ -213,7 +211,7 @@ to properly indent the tree. @type indent: int """ - print ' ' * indent + `self._type`,self + print ' ' * indent + `self._type`, self for child in self._children: child.showTree(indent + 2) @@ -245,11 +243,11 @@ @rtype: string """ if stripPrefixes == None: - return string.lower(string.replace(self._type[2:],'_','-')) + return string.lower(string.replace(self._type[2:], '_', '-')) for prefix in stripPrefixes: if prefix == self._type[:len(prefix)]: - return string.lower(string.replace(self._type[len(prefix):],'_','-')) - return string.lower(string.replace(self._type,'_','-')) + return string.lower(string.replace(self._type[len(prefix):], '_', '-')) + return string.lower(string.replace(self._type, '_', '-')) def dumpXML(self, lookupDict, treeDump=None, gap=" ", xmlnamespaces={}, textEncoding='<locale>', stripPrefixes=None): @@ -291,12 +289,12 @@ try: if self._xmlchildnamespaces: for abbrev in self._xmlchildnamespaces.keys(): - xmlnsdef += ' xmlns:%s="%s"' % (abbrev,self._xmlchildnamespaces[abbrev]) + xmlnsdef += ' xmlns:%s="%s"' % (abbrev, self._xmlchildnamespaces[abbrev]) except AttributeError: pass xmlEntity = self.getXmlTag(stripPrefixes) - xmlString = "%s<%s%s%s" % (gap[:-2],xmlns, xmlEntity, xmlnsdef) + xmlString = "%s<%s%s%s" % (gap[:-2], xmlns, xmlEntity, xmlnsdef) indent = len(xmlString) pos = indent @@ -306,7 +304,7 @@ # Make 'name' first if 'name' in attrs: attrs.pop(attrs.index('name')) - attrs.insert(0,'name') + attrs.insert(0, 'name') for attribute in attrs: # skip keys beginning with _ @@ -330,14 +328,14 @@ addl = ' %s="N"' % (attribute) elif typecast == GTypecast.names: if type(val) == types.StringType: - #addl = ' %s="%s"' % (attribute, string.join(val.decode(textEncoding),',')) - addl = ' %s="%s"' % (attribute, string.join(unicode(val,textEncoding),',')) + #addl = ' %s="%s"' % (attribute, string.join(val.decode(textEncoding), ',')) + addl = ' %s="%s"' % (attribute, string.join(unicode(val, textEncoding), ',')) else: - addl = ' %s="%s"' % (attribute, string.join(val,',')) + addl = ' %s="%s"' % (attribute, string.join(val, ',')) else: if type(val) == types.StringType: #addl = ' %s="%s"' % (attribute, saxutils.escape('%s' % val.decode(textEncoding))) - addl = ' %s="%s"' % (attribute, saxutils.escape('%s' % unicode(val,textEncoding))) + addl = ' %s="%s"' % (attribute, saxutils.escape('%s' % unicode(val, textEncoding))) else: addl = ' %s="%s"' % (attribute, saxutils.escape('%s' % val)) if len(addl) + pos > 78: @@ -349,14 +347,14 @@ if attribute.find('__') > 0 and attribute.split('__')[0] in xmlnamespaces.keys(): if val != None: if type(val) == types.StringType: - addl = ' %s="%s"' % (attribute.replace('__',':'), saxutils.escape('%s' % unicode(val,textEncoding))) + addl = ' %s="%s"' % (attribute.replace('__', ':'), saxutils.escape('%s' % unicode(val, textEncoding))) else: try: if val == int(val): val = int(val) except: pass - addl = ' %s="%s"' % (attribute.replace('__',':'), saxutils.escape('%s' % val)) + addl = ' %s="%s"' % (attribute.replace('__', ':'), saxutils.escape('%s' % val)) if len(addl) + pos > 78: xmlString += "\n" + " " * indent + addl pos = indent @@ -367,7 +365,7 @@ if len(self._children): hasContent = 0 for child in self._children: - hasContent = hasContent or isinstance(child,GContent) + hasContent = hasContent or isinstance(child, GContent) if hasContent: xmlString += ">" else: @@ -375,13 +373,13 @@ if treeDump: for child in self._children: - xmlString += child.dumpXML(lookupDict, 1,gap+" ", + xmlString += child.dumpXML(lookupDict, 1, gap+" ", textEncoding=textEncoding, xmlnamespaces=xmlnamespaces, stripPrefixes=stripPrefixes) if hasContent: xmlString += "</%s%s>\n" % (xmlns, xmlEntity) else: - xmlString += "%s</%s%s>\n" % (gap[:-2], xmlns,xmlEntity) + xmlString += "%s</%s%s>\n" % (gap[:-2], xmlns, xmlEntity) else: xmlString += "/>\n" return xmlString @@ -397,7 +395,7 @@ if isinstance(child, GObj): child.walk(function, *args, **parms) - def findParentOfType(self,type, includeSelf=1): + def findParentOfType(self, parentType, includeSelf=1): """ Moves upward though the parents of an object till it finds the parent of the specified type @@ -410,55 +408,74 @@ while 1: if parentObject == None: return None - elif parentObject._type == type: + elif parentObject._type == parentType: return parentObject # If passed a type of NONE it finds the top object in the tree - if not type and not parentObject.getParent (): + if not parentType and not parentObject.getParent (): return parentObject parentObject = parentObject.getParent () + + def findChildNamed(self, name, childType = None): + """ + Moves downward though the children of an object till + it finds the child with the specified name. + + @param name: The name to search for + @param childType: The type of object to search for, if None then any type + will match. + @return: The child with the matching name, or None if child not found + """ - def findChildOfType(self, type, includeSelf=1, allowAllChildren=0): + for child in self._children: + if child.name == name: + if childType is None or child._type == childType: + return child + + return None + + + def findChildOfType(self, childType, includeSelf=1, allowAllChildren=0): """ Moves downward though the children of an object till it finds the child of the specified type """ - if includeSelf and self._type == type: + if includeSelf and self._type == childType: return self for child in self._children: - if child._type == type: + if child._type == childType: return child if allowAllChildren: for child in self._children: - o = child.findChildOfType(type,0, 1) + o = child.findChildOfType(childType, 0, 1) if o: return o return None - def findChildrenOfType(self, type, includeSelf=1, allowAllChildren=0): + def findChildrenOfType(self, childType, includeSelf=1, allowAllChildren=0): """ find all children of a specific type """ rs = [] - if includeSelf and self._type == type: + if includeSelf and self._type == childType: rs += [self] for child in self._children: - if child._type == type: + if child._type == childType: rs += [child] if allowAllChildren: for child in self._children: try: - rs += child.findChildrenOfType(type,0, 1) + rs += child.findChildrenOfType(childType, 0, 1) except AttributeError: pass # not all children will be GObj and support that function @@ -470,9 +487,9 @@ Return a useful description of the object. Currently used by GNUe Designer. """ - if hasattr(self,'_description'): + if hasattr(self, '_description'): return self._description - elif hasattr(self,'name'): + elif hasattr(self, 'name'): return self.name + " (%s)" % self._type[2:] else: return self._type[2:] + " (%s)" % self._type[2:] @@ -481,10 +498,10 @@ # Hooks - I haven't a clue yet # =========================================================================== def __getitem__(self, key): - return self.__dict__[key.replace(':','__')] + return self.__dict__[key.replace(':', '__')] def __setitem__(self, key, value): - return self._setItemHook(key.replace(':','__'),value) + return self._setItemHook(key.replace(':', '__'), value) def _setItemHook(self, key, value): self.__dict__[key] = value @@ -517,7 +534,7 @@ def _buildObject(self): newObj = self.getClass()(None) for attr, value in self.__dict__.items(): - if attr not in ('_buildObject','getClass') and attr[:2] != '__': + if attr not in ('_buildObject', 'getClass') and attr[:2] != '__': newObj.__dict__[attr] = value parent = self.getParent () _______________________________________________ Commit-gnue mailing list Commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue