Mercurial > farol > farolluz
comparison farolluz/producttree.py @ 46:1b7f3f4f1238
Add the possibility to batch-import trees
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Tue, 30 Dec 2014 12:28:06 +0100 |
parents | b87f2a6e613a |
children | 652f59fbea3a |
comparison
equal
deleted
inserted
replaced
45:075519975933 | 46:1b7f3f4f1238 |
---|---|
139 yield (i, relationship) | 139 yield (i, relationship) |
140 | 140 |
141 def nbProducts(self): | 141 def nbProducts(self): |
142 """ Amount of 'raw' Products """ | 142 """ Amount of 'raw' Products """ |
143 return len([p for p in self._products if p._parent is self]) | 143 return len([p for p in self._products if p._parent is self]) |
144 | |
145 def importTree(self, tree): | |
146 """ tree is a list of tuple (type, value) like the one generated by | |
147 getTree() """ | |
148 if len(tree) == 0: | |
149 return self | |
150 found = None | |
151 for branch in self._branches: | |
152 if branch.getTree() == tree[:1]: | |
153 found = branch | |
154 if found is None: | |
155 found = CVRFProductBranch(tree[0][0], tree[0][1], self) | |
156 return found.importTree(tree, 1) | |
144 | 157 |
145 def validate(self): | 158 def validate(self): |
146 for branch in self._branches: | 159 for branch in self._branches: |
147 branch.validate() | 160 branch.validate() |
148 productids = set() | 161 productids = set() |
244 if self.isRoot(): | 257 if self.isRoot(): |
245 parent._branches.append(self) | 258 parent._branches.append(self) |
246 else: | 259 else: |
247 parent._childs.append(self) | 260 parent._childs.append(self) |
248 | 261 |
262 def importTree(self, tree, index): | |
263 """ tree is a list of tuple (type, value) like the one generated by | |
264 getTree() """ | |
265 if len(tree) == index: | |
266 return self | |
267 found = None | |
268 for branch in self._childs: | |
269 if branch.getTree() == tree[:index + 1]: | |
270 found = branch | |
271 if found is None: | |
272 found = CVRFProductBranch(tree[index][0], tree[index][1], self) | |
273 return found.importTree(tree, index + 1) | |
249 | 274 |
250 def validate(self): | 275 def validate(self): |
251 if not self._type: | 276 if not self._type: |
252 raise ValidationError('A Branch must have a Type') | 277 raise ValidationError('A Branch must have a Type') |
253 if self._type not in self.TYPES: | 278 if self._type not in self.TYPES: |