Automatically set the height according to the text height.
[pysize.git] / pysize / core / pysize_fs_tree.py
blobe03c68050802144c18c71cf28f50b2f75036aa0c
1 from pysize.core import compute_size
2 from pysize.core.pysize_fs_node import create_node
4 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/231503
5 # By David Eppstein
6 def _breadth_first(tree, children=iter):
7 """Traverse the nodes of a tree in breadth-first order.
8 The first argument should be the tree root; children
9 should be a function taking as argument a tree node and
10 returning an iterator of the node's children.
11 """
12 yield tree
13 last = tree
14 for node in _breadth_first(tree, children):
15 for child in children(node):
16 yield child
17 last = child
18 if last is node:
19 return
21 class pysize_tree:
22 """The entry point to a tree of pysize_node.
23 min_size < 1.0 => ratio to total size."""
24 def __init__(self, paths, max_depth=1, min_size=0.0):
25 self.fullpaths = paths
26 if paths and min_size <= 1.0:
27 min_size *= sum(map(compute_size.slow, paths))
28 self.root = create_node(None, paths, max_depth, min_size)
29 self.height = self.root.compute_height()
31 def get_next_sibling(self, node):
32 """Return the next pysize_node in node's level."""
33 is_next = False
34 for child in _breadth_first(self.root, lambda c: c.children):
35 if is_next:
36 if child.compute_depth() == node.compute_depth():
37 return child
38 return
39 is_next = child is node
41 def get_previous_sibling(self, node):
42 """Return the previous pysize_node in node's level."""
43 prev = None
44 for child in _breadth_first(self.root, lambda c: c.children):
45 if child == node:
46 if prev.compute_depth() == node.compute_depth():
47 return prev
48 return
49 prev = child
51 def get_first_child(self, node):
52 """Return the first pysize_node in node's children."""
53 if node.children:
54 return node.children[0]
56 def get_parent(self, node):
57 """Return the saved parent of node."""
58 return node.parent
60 def _main():
61 tree = pysize_tree(['/usr/share/doc'], 4, 0.1)
62 print 'height', tree.height
64 if __name__ == '__main__':
65 _main()