4 from core
import chdir_browsing
5 from core
import compute_size
8 """The parent class of all displayed nodes, as these nodes are displayed on
9 screen, there should be few instances."""
10 def __init__(self
, parent
, basename
):
11 self
.rectangle
= (0, 0, 0, 0)
14 self
.basename
= basename
15 self
.size
= compute_size
.slow(basename
)
17 def compute_height(self
):
20 def compute_depth(self
):
28 def minimum_real_size(self
):
30 for child
in self
.get_children():
31 child_min_real_size
= child
.minimum_real_size()
32 if child_min_real_size
>= 0 and child_min_real_size
< res
:
33 res
= child_min_real_size
40 """Does the node correspond to a path that actually exists?"""
43 def get_children(self
):
44 """Return the children for a directory."""
47 def get_fullpath(self
):
48 """Return the fullpath of the node, using its parent node."""
49 fullpath
= self
.basename
52 fullpath
= os
.path
.join(parent
.basename
, fullpath
)
53 parent
= parent
.parent
57 """Return the name that should be displayed for this node."""
61 """The iterator is a depth first traversal."""
63 for child
in self
.get_children():
67 def contains_point(self
, p
):
68 """Is the point p in the graphical representation of this node?"""
69 (x0
, x1
, y0
, y1
) = self
.rectangle
70 return x0
< p
.x
and p
.x
< x1
and y0
< p
.y
and p
.y
< y1
72 class _pysize_node_remaining(_pysize_node
):
73 """The sum of a directory's children that are too small to be drawn."""
74 def __init__(self
, parent
, size
):
75 _pysize_node
.__init
__(self
, parent
, None)
76 self
.basename
= '..///..'
80 """This node does not actually exists, it is an aggregate."""
83 def minimum_real_size(self
):
86 class _pysize_node_dir(_pysize_node
):
88 def __init__(self
, parent
, basename
, max_depth
, min_size
):
89 _pysize_node
.__init
__(self
, parent
, basename
)
94 cookie
= chdir_browsing
.init(basename
)
96 for child
in chdir_browsing
.listdir(cookie
):
97 node
= create_node(self
, child
, max_depth
- 1, min_size
)
99 self
.children
.append(node
)
100 children_size
+= node
.size
102 remaining_size
+= node
.size
104 chdir_browsing
.finalize(cookie
)
105 self
.children
.sort(key
=lambda t
: t
.size
, reverse
=True)
106 self
.size
= max(children_size
, self
.size
)
107 if remaining_size
> min_size
:
108 rem
= _pysize_node_remaining(self
, remaining_size
)
109 self
.children
.append(rem
)
111 def compute_height(self
):
113 children_height
= max([c
.compute_height() for c
in self
.children
])
114 return children_height
+ 1
120 def get_children(self
):
124 return self
.basename
+ '/'
126 class _pysize_node_file(_pysize_node
):
128 def __init__(self
, parent
, basename
):
129 _pysize_node
.__init
__(self
, parent
, basename
)
131 class _pysize_node_hardlink(_pysize_node_file
):
132 """A hardlink, the canonical one, or a link"""
133 def __init__(self
, parent
, basename
):
134 _pysize_node_file
.__init
__(self
, parent
, basename
)
136 def create_node(parent
, basename
, max_depth
, min_size
):
137 """Return a pysize_node for parent/basename traversing up to max_depth
138 levels and only taking into account elements bigger than min_size."""
140 return _pysize_node_remaining(parent
, 0)
141 size
= compute_size
.slow(basename
)
143 node
= _pysize_node_remaining(parent
, size
)
145 st
= os
.lstat(basename
)
146 if stat
.S_ISDIR(st
.st_mode
):
147 node
= _pysize_node_dir(parent
, basename
, max_depth
, min_size
)
148 elif st
.st_nlink
> 1:
149 node
= _pysize_node_hardlink(parent
, basename
)
151 node
= _pysize_node_file(parent
, basename
)