annotate HashTree.py @ 266:bd41c278b88a new_pro_struct

add web calendar link.
author casties
date Wed, 27 Aug 2014 14:28:27 +0200
parents 7288e7729960
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
1 import logging
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
2
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
3 class HashTreeNode:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
4 """Node of a HashTree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
5 Has a value and keeps children in a dict indexed by key."""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
6 key = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
7 value = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
8 children = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
9
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
10 def __init__(self, key=None, value=None, children=None):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
11 """create node"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
12 self.key = key
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
13 self.value = value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
14 self.children = children
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
15
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
16
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
17 def setValue(self, val, append=True, unique=False):
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
18 """set or append to the value"""
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
19 if (self.value is not None) or append:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
20 if isinstance(self.value, list):
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
21 # old value is list
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
22 if unique and val in self.value:
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
23 logging.debug("setValue: list HAS SAME: %s"%repr(val))
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
24 return
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
25
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
26 self.value.append(val)
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
27
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
28 elif self.value is not None:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
29 # old value is scalar
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
30 if unique and val == self.value:
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
31 logging.debug("setValue: scalar IS SAME: %s"%repr(val))
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
32 return
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
33
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
34 self.value = [self.value, val]
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
35
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
36 else:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
37 # old value is None
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
38 self.value = val
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
39
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
40 else:
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
41 # old value is None
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
42 self.value = val
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
43
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
44
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
45 def getValue(self, onlyFirst=True, asList=False):
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
46 """get the (first) value"""
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
47 if isinstance(self.value, list):
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
48 if onlyFirst and not asList:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
49 return self.value[0]
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
50 else:
229
d4216a848547 fixed problem that reading the project tree changes it.
casties
parents: 228
diff changeset
51 return self.value[:]
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
52 else:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
53 if asList:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
54 if self.value is None:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
55 return []
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
56 else:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
57 return [self.value]
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
58 else:
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
59 return self.value
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
60
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
61
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
62 def getNode(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
63 """return node under key"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
64 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
65 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
66 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
67 return self.children.get(key, None)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
68
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
69
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
70 def addNode(self, node):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
71 """add child node using key from node"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
72 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
73 self.children = {node.key : node}
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
74 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
75 self.children[node.key] = node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
76
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
77
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
78 def getSubtreeAsList(self, depthFirst=True):
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
79 """Return the subtree as flattened list sorted by key."""
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
80 if depthFirst:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
81 return self.getSubtreeAsListDepthFirst()
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
82 else:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
83 return self.getSubtreeAsListBreadthFirst()
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
84
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
85
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
86 def getSubtreeAsListDepthFirst(self):
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
87 """Return the subtree as flattened list, depth first, sorted by key."""
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
88 if self.children is None:
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
89 if self.value is None:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
90 return []
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
91 else:
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
92 return self.getValue(asList=True)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
93
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
94 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
95 if self.value is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
96 sub = []
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
97 else:
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
98 sub = self.getValue(asList=True)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
99
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
100 for k in sorted(self.children.keys()):
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
101 sub.extend(self.children.get(k).getSubtreeAsListDepthFirst())
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
102
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
103 return sub
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
104
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
105
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
106 def getSubtreeAsListBreadthFirst(self):
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
107 """Return the subtree as flattened list, breadth first, sorted by key."""
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
108 q = [self]
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
109 sub = []
115
014efa0923be removed limit from testing in getSubtreeAsList.
casties
parents: 114
diff changeset
110 while len(q) > 0:
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
111 node = q.pop(0)
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
112 if node.value is not None:
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
113 sub.extend(node.getValue(asList=True))
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
114
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
115 if node.children is not None:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
116 clist = sorted(node.children.values(), key=lambda x:x.key)
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
117 q.extend(clist)
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
118
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
119 return sub
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
120
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
121
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
122 def getSubtreeAsText(self):
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
123 """Return whole tree as text. Depth first."""
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
124 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
125 return "(%s:%s)"%(self.key, self.value)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
126 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
127 sub = "(%s:%s):["%(self.key, self.value)
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
128 for k in sorted(self.children.keys()):
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
129 sub += self.children.get(k).getSubtreeAsText()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
130 sub += ", "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
131
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
132 return sub + "] "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
133
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
134
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
135 class HashTree:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
136 """Tree using dictionaries"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
137
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
138 # root HashTreeNode
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
139 root = None
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
140 # separator by which key strings are split into parts
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
141 keySeparator = '.'
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
142 # function applied to key parts
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
143 keyFn = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
144
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
145 def __init__(self, keySeparator='.', keyFn=None):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
146 """Create a HashTree.
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
147
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
148 @param keySeparator string by which key strings are split into parts
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
149 @param keyFn function applied to key parts (e.g. int if key parts are numbers)
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
150 """
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
151 self.root = HashTreeNode()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
152 self.keySeparator = keySeparator
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
153 self.keyFn = keyFn
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
154
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
155
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
156 def _splitkey(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
157 """Return a list of key parts"""
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
158 if isinstance(key, basestring):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
159 # its a string - split
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
160 keys = key.split(self.keySeparator)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
161 if self.keyFn is not None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
162 keys = [self.keyFn(k) for k in keys]
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
163
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
164 elif isinstance(key, list):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
165 # its a list - keep
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
166 keys = key
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
167
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
168 else:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
169 # not a list - wrap in list
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
170 keys = [key]
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
171
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
172 return keys
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
173
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
174
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
175 def getNode(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
176 """Return node under key from the tree.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
177
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
178 key can be sequence of key string or a single string using keySeparator.
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
179 If key is None, returns root node.
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
180 """
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
181 if key is None:
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
182 return self.root
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
183
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
184 keys = self._splitkey(key)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
185
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
186 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
187 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
188 node = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
189 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
190 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
191
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
192 return node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
193
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
194
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
195 def get(self, key, onlyFirst=True):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
196 """Return value under key from the tree.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
197
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
198 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
199 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
200 node = self.getNode(key)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
201 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
202 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
203
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
204 return node.getValue(onlyFirst=onlyFirst)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
205
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
206
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
207 def add(self, key, value):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
208 """Add value under key to the tree.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
209
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
210 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
211 """
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
212 keys = self._splitkey(key)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
213 node = self.root
257
7288e7729960 added projectType field to MPIWGProjects.
casties
parents: 229
diff changeset
214 #logging.debug("hashtree.add: keys=%s value=%s"%(repr(keys), repr(value)))
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
215 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
216 nextnode = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
217 if nextnode is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
218 nextnode = HashTreeNode(key=k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
219 node.addNode(nextnode)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
220
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
221 node = nextnode
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
222
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
223 node.setValue(value)
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
224
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
225
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
226 def getChildrenOf(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
227 """Return the list of child (values) of the node under key."""
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
228 node = self.getNode(key)
97
7b96a85552aa fix bugs in project editing.
casties
parents: 52
diff changeset
229 if getattr(node, 'children', None) is None:
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
230 return []
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
231 else:
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
232 # sort children by key
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
233 childlist = sorted(node.children.items(), key=lambda x:x[0])
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
234 # and return the values
228
afc96bc56817 also show multiple projects with the same number (including none) in the tree.
casties
parents: 115
diff changeset
235 return [n.getValue() for k, n in childlist if n.value is not None]
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
236
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
237
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
238 def getAncestorsOf(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
239 """Return the list of ancestor (values) of the node under key.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
240
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
241 Order: root element first."""
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
242 keys = self._splitkey(key)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
243 node = self.root
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
244 ancestors = []
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
245 for k in keys:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
246 if node.value is not None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
247 ancestors.append(node.value)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
248
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
249 node = node.getNode(k)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
250 if node is None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
251 return ancestors
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
252
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
253 return ancestors
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
254
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
255