"Fossies" - the Fresh Open Source Software Archive 
Member "pysize-0.2/pysize/core/pysize_fs_tree.py" (11 Mar 2007, 3417 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14 #
15 # See the COPYING file for license information.
16 #
17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
18
19 from pysize.core import compute_size
20 from pysize.core.pysize_fs_node import create_node
21
22 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/231503
23 # By David Eppstein
24 def _breadth_first(tree, children=iter):
25 """Traverse the nodes of a tree in breadth-first order.
26 The first argument should be the tree root; children
27 should be a function taking as argument a tree node and
28 returning an iterator of the node's children.
29 """
30 yield tree
31 last = tree
32 for node in _breadth_first(tree, children):
33 for child in children(node):
34 yield child
35 last = child
36 if last is node:
37 return
38
39 class pysize_tree(object):
40 """The entry point to a tree of pysize_node.
41 min_size < 1.0 => ratio to total size."""
42 def __init__(self, paths, max_depth, min_size, options):
43 self.fullpaths = paths
44 auto_min_size = min_size <= 1.0
45 estimated_size = compute_size.slow_sum(paths, options.cross_device)
46 if auto_min_size:
47 min_size_ratio = min_size
48 min_size = estimated_size * min_size_ratio
49 self.root = create_node(None, paths, max_depth, min_size, options)
50 if auto_min_size and estimated_size != self.root.size:
51 estimated_size = self.root.size
52 min_size = estimated_size * min_size_ratio
53 self.root = create_node(None, paths, max_depth, min_size, options)
54 self.height = self.root.compute_height()
55
56 def get_next_sibling(self, node):
57 """Return the next pysize_node in node's level."""
58 is_next = False
59 for child in self.breadth_first():
60 if is_next:
61 if child.compute_depth() == node.compute_depth():
62 return child
63 return
64 is_next = child is node
65
66 def get_previous_sibling(self, node):
67 """Return the previous pysize_node in node's level."""
68 prev = None
69 for child in self.breadth_first():
70 if child == node:
71 if prev.compute_depth() == node.compute_depth():
72 return prev
73 return
74 prev = child
75
76 def get_first_child(self, node):
77 """Return the first pysize_node in node's children."""
78 if node.children:
79 return node.children[0]
80
81 def get_parent(self, node):
82 """Return the saved parent of node."""
83 return node.parent
84
85 def breadth_first(self):
86 """Iterate over the nodes in breadth first order."""
87 return _breadth_first(self.root, lambda c: c.children)