我在Jython中创建了一个类,它在顶层接受一个对象,然后递归遍历所有子对象(来自外部系统),有效地给我一个嵌入的对象树。从这里,我可以使用类似的技术列出子项的相关属性,只需使用递归。
但是,我现在需要一种从调用程序遍历这个树的方法,在那里我可以使用for循环,它会递归遍历所有的子节点,而我无法理解这一点! / p>
请指点什么?
代码:
class DepotGroup():
def __init__(self, jli, groupName, groupFQPath, groupID, parentID=0):
self.logger = clog.CoBaLogging("Class:DepotGroup")
self._jli = jli
self.name = groupName
self.fullyQualifiedPath = groupFQPath
self.ID = groupID
self.parentID = parentID
# Create and populate the children[] list
self.children = []
self._FindChildren()
# Now do the same for Depot Objects
self.depotObjects = []
self._FindDepotObjects()
return
def AddChild(self, childGroupName, childGroupFQPath, childGroupID):
child = DepotGroup(self._jli, childGroupName, childGroupFQPath, childGroupID, self.ID)
self.children.append(child)
return
def _FindChildren(self):
(ok, childList) = self._jli.cliCall("Group", "findAllByParentGroup", DEPOT_GROUP, self.ID)
if not ok:
raise EnvironmentError("BLCLI call 'Group.findAllByParentGroup()' failed")
for child in childList:
(ok, groupPath) = self._jli.cliCall("Group", "getStringPathToGroup", child.groupId)
if not ok:
raise EnvironmentError("BLCLI call 'Group.getStringPathToGroup()' failed")
self.AddChild(child.name, groupPath[5:], child.groupId)
return
def _FindDepotObjects(self):
(ok, objList) = self._jli.cliCall("DepotObject", "findAllByGroup", self.ID, False)
if not ok:
raise EnvironmentError("BLCLI call 'DepotObject.findAllByGroup()' failed")
for depotObj in objList:
self.depotObjects.append(DepotObject(depotObj.name, depotObj.objectId, depotObj.DBKey, depotObj.objectTypeId))
return
def __list(self, indent=""):
if indent=="":
print "%s" % self.fullyQualifiedPath
else:
print "%s/%s" % (indent, self.name)
childIndent = "%s " % indent
for child in self.children:
child.__list(childIndent)
for depotObj in self.depotObjects:
flag = ""
if depotObj.IsBLPackage():
flag = "*"
print "%s - %s %s" % (indent, depotObj.name, flag)
if indent=="":
print "\n\n[Note: '*' indicates a BLPackages]\n"
return
def list(self):
self.__list()
return
def __repr__(self):
thisNode = {'Name': self.name, 'FQPath': self.fullyQualifiedPath, 'GroupID': self.ID, 'Children': []}
for child in self.children:
thisNode['Children'].append(child)
return repr(thisNode)
def __str__(self):
retVal = ""
retVal += "Name : %s" % self.name
retVal += "\nFQPath : %s" % self.fullyQualifiedPath
retVal += "\nGroup ID : %s" % str(self.ID)
retVal += "\n# Children : %d" % len(self.children)
return retVal
def __len__(self):
totalLen = len(self.children)
for child in self.children:
totalLen += len(child)
return totalLen
class DepotObject():
def __init__(self, objName, objID, objDBKey, objType):
self.name = objName
self.ID = objID
self.DBKey = objDBKey
self.type = objType
def IsBLPackage(self):
return (self.type == BLPACKAGE)