/usr/lib/python2.7/dist-packages/PyMca/Object3D/ObjectTree.py is in pymca 4.7.4+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | """
Provides the class 'ObjectTree'.
"""
class ObjectTree:
"""
Implements a simple tree of objects with a _name attribute.
Number of children is illimited.
Recursive structure
"""
def __init__(self, item, name=None):
"""
Init an empty tree.
PARAMETERS :
item : the object of the tree
name : the name of the object.
If ignored it will be set to object.name()
or to Unnamed.
"""
self.root = [item]
if name is None:
if hasattr(item, "name"):
name = item.name()
else:
name = "Unnamed"
self.__name = name
def name(self):
"""
Return its name.
"""
#if type(self.root[0]) == type(''):
# return self.root[0]
#return self.root[0]._name
return self.__name
def addChild(self, item, name=None):
"""
Add a child to the current tree or sub-tree.
PARAMETERS :
item : the item of the child tree
RETURNS :
the child
"""
child = ObjectTree(item, name=name)
self.root.append(child)
return child
def addChildTree(self, childTree):
"""
Add a child tree to the tree.
PARAMETERS :
childTree : the child tree to add
"""
self.root.append(childTree)
def childList(self):
"""
Compute the list of children.
"""
return self.root[1:]
def childNumber(self):
"""
Compute the number of children.
"""
return len(self.root) - 1
def delDirectChild(self, name):
"""
Remove a direct child.
PARAMETERS :
name : the name of the child to remove
RETURNS :
1 if deleted, else 0
"""
nbChild = self.childNumber()+1
for i in range(1,nbChild):
if self.root[i].name() == str(name):
del self.root[i]
return 1
return 0
def delChild(self, name):
"""
Remove a child in the tree.
PARAMETERS :
name : the name of the child to remove
RETURNS :
1 if deleted, else 0
"""
nbChild = self.childNumber()+1
for i in range(1,nbChild):
if self.root[i].name() == str(name):
del self.root[i]
return 1
for child in self.childList():
child.delChild(name)
def erase(self):
"""
Full erase of the tree.
Only the name stays.
"""
del self.root[1:]
def getList(self):
"""
Compute a plane list of the elements.
RETURNS :
this list of names.
"""
result = [self.name()]
childList = self.childList()
for child in childList:
result += child.getList()
return result
def find(self, childName):
"""
Find a child - or sub-child, given its name.
PARAMETERS :
childName : the name of the child to find
RETURNS :
the child, or 'None' if not found
"""
if self.name() == str(childName):
return self
for child in self.childList():
f = child.find(childName)
if f!=None:
return f
return None
def parent(self, childName):
"""
Return the parent of the child in the tree.
PARAMETERS :
childName : the name of the child to find the parent
RETURNS :
the parent, or 'None' if not found
"""
nbChild = self.childNumber()+1
for i in range(1,nbChild):
if self.root[i].name() == str(childName):
return self
for child in self.childList():
parent = child.parent(childName)
if parent!=None:
return parent
return None
def getLine(self, childName):
"""
Extract the line of parents from root to child.
PARAMETERS :
childName : the name of the child to extract the line
RETURNS :
a list of name, from root to child, or [] if not found
"""
name = self.name()
if name == str(childName):
return [name]
for child in self.childList():
line = child.getLine(childName)
if line!=[]:
return [name] + line
return []
def __str__(self):
"""
Return a printable string representing the tree.
This is only a convinience mask for 'self.str2()'.
"""
return "*" + self.str2(1)
def str2(self, indent):
"""
Return a printable string representing the tree.
PARAMETERS :
indent : the number of space to indent the tree string
"""
result = "-> " + self.name() + "\n"
newIndent = indent+3
for subTree in self.childList():
result += newIndent * " " + subTree.str2(newIndent)
return result
def html(self, fileName, openMode="w"):
"""
Similar to str, but in a HTML file .
"""
ffile = open(fileName, openMode)
self.htmlTable(ffile)
ffile.close()
def htmlTable(self, ffile):
"""
Create table here.
"""
ffile.write("<table width=100%>\n")
name = self.name()
nb = self.childNumber()
if nb <= 0:
ffile.write("<tr><td bgcolor=skyblue><center><b>%s</b></center></td></tr>\n" %name)
else:
ffile.write("<tr><td colspan=%d bgcolor=skyblue><center><b>%s</b></center></td></tr>\n" %(nb, name))
for child in self.childList():
ffile.write("<td valign=top>\n")
child.htmlTable(ffile)
ffile.write("</td>\n")
ffile.write("</table>\n")
if __name__ == "__main__":
import Object3DQt as qt
import Object3DBase
app = qt.QApplication([])
o0 = Object3DBase.Object3D("DummyObject0")
o1 = Object3DBase.Object3D("DummyObject1")
o01 = Object3DBase.Object3D("DummyObject01")
w = ObjectTree('__Scene__', name='root')
t0=w.addChild(o0)
t1=w.addChild(o1)
if 0:
t0.addChild(o01)
else:
#append DummyObject01 to DummyObject0'
tree = w.find("DummyObject0")
tree.addChild(o01)
print w
print "LIST"
print w.getList()
print "Now I am going to paste DummyObject0 into DummyObject1"
t0 = w.find("DummyObject0")
w.delChild("DummyObject0")
t1=w.find("DummyObject1")
t1.addChildTree(t0)
print w
print "LIST"
print w.getList()
|