def main(schema=None, output_dir=None, config_path=None): """ Validate the schema file and output directory parameters. Build a tree from the schema file. Walk the tree, calling the registered callbacks on each node. """ validator = Validator(schema, output_dir=output_dir) if not validator.validate(): click.echo(validator.error['msg']) sys.exit(1) directory_tree = Tree( indent_size = validator.indent_size, output_dir = output_dir, base_path = os.path.abspath(os.curdir) ) directory_tree.load_data(schema) directory_tree.build_tree() callbacks = [ pprint_node ] if config_path: process_hooks = make_config_processor(config_path) callbacks.append(process_hooks) directory_tree.walk(callbacks=callbacks)
class treeTest(unittest.TestCase): def setUp(self): n = Node(5, parent=None) self.tree = Tree(n) self.tree.add_child(parent=5, child=4) self.tree.add_child(parent=5, child=3) self.tree.add_child(parent=4, child=2) def test_get_element(self): self.assertEqual( self.tree.get_element(5, start=self.tree.root).data, 5) # def test_find(self): # self.assertTrue(self.tree.find(3)) def test_count_nodes(self): self.assertEqual(self.tree.count_nodes(), 4) def test_height(self): self.assertEqual(self.tree.height(self.tree.root), 3) def test_walk(self): self.assertEqual(self.tree.walk(self.tree.root), [])
def show(self, result): by_product = {} by_id = {} class TreeBug(dict): def __init__(self, b): for i in b: self[i] = b[i] def __str__(self): res = "" if 'notmybug' in self: res = '\033[33m' res += str(self['id']) + " " res += str(self['priority']) + " " res += str(self['estimated_time']) + " " res += str(self['status'][:3]) + " " res += str(self['severity'][:3]) + " " res += str(self['summary'].encode('utf-8')) + " " res += str(self['blocks']) + " " creat = datetime.strptime(str(self['creation_time']).split('T')[0], "%Y%m%d").date() creat += timedelta(days=severity_importance[self['severity']]) if self['severity'] != "enhancement": res += str((creat - date.today()).days) if 'notmybug' in self: res += '\033[0m' #res += str(self['blocklist']) + " " return res def equals(self, b): return self['id'] == b['id'] def is_above(self, b): return self.is_parent_of(b) def is_parent_of(self, b): return self['id'] in b['blocklist'] def update(self, b): pass class Walker(object): def __init__(self): self.parents = [] def _update_parents(self, bug): while len(self.parents): parent = self.parents[-1] self.parents = self.parents[:-1] if parent.is_parent_of(bug): self.parents.append(parent) break class TreePrinter(Walker): def __call__(self, bug): self._update_parents(bug) print '{} {}'.format(' ' * len(self.parents), str(bug)) self.parents.append(bug) return True; class TreeEstimator(Walker): def __call__(self, bug): self._update_parents(bug) for parent in self.parents: parent['estimated_time'] += bug['estimated_time'] self.parents.append(bug) return True; mybugs = [] for bug in result: mybugs.append(TreeBug(bug)) result = None for bug in mybugs: by_product.setdefault(bug['product'], []) by_product[bug['product']].append(bug) by_id[bug['id']] = bug for bug in mybugs: res = [] self.addblock(by_id, bug, res) bug['blocklist'] = res for product in by_product: print product bugtree = Tree() #by_product[product] = sorted(by_product[product], cmp=bugcmp) for bug in sorted(by_product[product], cmp=bugcmp): bugtree.insert(bug) bugtree.walk(TreeEstimator()) bugtree.walk(TreePrinter()) print
def test_walk(self): t = Tree("t", "T") observed = list(t.walk()) expected = ['t'] self.assertEqual(observed, expected)
def test_insert(self): t = Tree("t", "T") t.insert('u', "U") observed = list(t.walk()) expected = ['t', 'u'] self.assertEqual(observed, expected)
if state[i] == -1: indices.append(i) for index in indices: newstate = deepcopy(state) if (numFreeSpots % 2) == 0: newstate[index] = 'X' else: newstate[index] = 'O' newnode = tree.addNode(node, newstate) if newnode.depth < maxdepth: recurse(newnode) recurse(tree.root) constructTree(4) n = tree.walk(0, tree.root) print(n) """ tree = Tree() tree.addNode(tree.root, 9) tree.addNode(tree.root, 10) tree.addNode(tree.root, 11) tree.addNode(tree.root.children[0], 4) tree.addNode(tree.root.children[0], 6) tree.addNode(tree.root.children[1], 8) tree.addNode(tree.root.children[1], 1) tree.addNode(tree.root.children[2], 7) tree.addNode(tree.root.children[2], 90) tree.addNode(tree.root.children[2], 66) tree.addNode(tree.root.children[0].children[1], 78) tree.addNode(tree.root.children[0].children[1], 99)
def test_tree_init(self): t = Tree("D", 4) self.assertEqual(list(t.walk()), ['D'])
def test_tree_both_lr(self): 'Test tree with both L and R nodes. Also no data passed in.' t = Tree('D') for c in 'FGCAEB': t.insert(c) self.assertEqual(list(t.walk()), ['A', 'B', 'C', 'D', 'E', 'F', 'G'])
def test_tree_no_left(self): t = Tree('D') for c in 'FGE': t.insert(c) self.assertEqual(list(t.walk()), ['D', 'E', 'F', 'G'])
def test_tree_no_right(self): t = Tree('D', 4) for c, i in (('C', 3), ('A', 1), ('B', 2)): t.insert(c, i) self.assertEqual(list(t.walk()), ['A', 'B', 'C', 'D'])