summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2006-09-22 18:34:17 +0000
committerLennart Poettering <lennart@poettering.net>2006-09-22 18:34:17 +0000
commitbe9285318a41312b68b298fc7c43d42776f13f69 (patch)
tree133c5cfb201e2b5ef84364d4cbd5b9b5aa18f09c
parenta9135dfe8ebb7b0411a868f21f1e4eff09443e17 (diff)
rework tree walking: sort items by size
git-svn-id: file:///home/lennart/svn/public/fring/trunk@10 d0d2c35f-0a1e-0410-abeb-dabff30a67ee
-rw-r--r--src/fringlib/fringui.py1
-rw-r--r--src/fringlib/fringwalker.py135
2 files changed, 68 insertions, 68 deletions
diff --git a/src/fringlib/fringui.py b/src/fringlib/fringui.py
index eb5ff42..592176c 100644
--- a/src/fringlib/fringui.py
+++ b/src/fringlib/fringui.py
@@ -278,6 +278,7 @@ class UI( gtk.Window ):
#print data.name
def __walker_finished(self, widget, data):
+ self.__list_changed(widget, data)
self.__show_busy_cursor(-1)
def __on_resize(self, widget, event):
diff --git a/src/fringlib/fringwalker.py b/src/fringlib/fringwalker.py
index d4237e5..fef7d4e 100644
--- a/src/fringlib/fringwalker.py
+++ b/src/fringlib/fringwalker.py
@@ -1,8 +1,6 @@
-import os, os.path, stat, sys
-
+import os, os.path, stat, sys, time
import gobject, gtk
from threading import Thread
-import time
class sum_list:
def __init__(self, l, name = None):
@@ -21,6 +19,22 @@ class sum_list:
def __str__(self):
return self.name+": "+str(self.data)
+ def sort(self):
+
+ def cmp_fn(a, b):
+ a_dir = isinstance(a[1], sum_list)
+ b_dir = isinstance(b[1], sum_list)
+
+ if a_dir and not b_dir:
+ return 1
+ elif b_dir and not a_dir:
+ return -1
+ elif a_dir:
+ return cmp(a[1].the_sum, b[1].the_sum)
+ else:
+ return cmp(a[1], b[1])
+
+ self.data.sort(cmp_fn)
class FringWalker( gobject.GObject ):
@@ -60,91 +74,76 @@ class FringWalker( gobject.GObject ):
def _parse(self,path):
""" Parse the root directory """
- l = []
- i = 0
- subdirectories = {}
-
- try:
- for fn in os.listdir(path):
-
- if not self.showhidden and fn[0] == '.': continue
-
- try: p = os.path.join(path, fn)
- except: continue
-
- s = os.lstat(p)
-
- if stat.S_ISDIR(s.st_mode):
- subdirectories[i] = (fn,p);
- l.append((fn, s.st_size))
- elif stat.S_ISREG(s.st_mode):
- l.append((fn, s.st_size))
-
- i += 1
-
- except:
- pass
-
- gtk.gdk.threads_enter()
- self.emit("list-changed",sum_list(l, os.path.split(path)[-1]))
- gtk.gdk.threads_leave()
-
- c = 0
- for n in subdirectories:
- c += 1
-
- # give gtk a breath to redraw
- time.sleep(0.05)
-
- # walk subdirectory
- fn,p = subdirectories[n]
- l[n] = (fn, self._build_tree(p));
- if self.stopsignal: return
-
+ def progress_fn(c, l, r):
# emit signals
gtk.gdk.threads_enter()
- self.emit("progress",c,len(subdirectories))
- self.emit("list-changed",sum_list(l, os.path.split(path)[-1]))
+ self.emit("progress", c, l)
+ self.emit("list-changed", r)
gtk.gdk.threads_leave()
+ r = self._build_tree(path, progress_fn)
gtk.gdk.threads_enter()
- self.emit("finished",sum_list(l, os.path.split(path)[-1]))
+ self.emit("finished", r)
gtk.gdk.threads_leave()
- print "finished walking",path
+ print "finished walking", path
-
- def _build_tree(self,path):
+ def _build_tree(self, path, progress_fn = None):
""" Parse directories recursively """
- l = []
+
+ ret = []
+ tmp_dirs = []
+
try:
-
# walk files in directory
- subdirectories = []
for fn in os.listdir(path):
- if self.stopsignal: return sum_list([])
+ if self.stopsignal:
+ return sum_list([])
- if not self.showhidden and fn[0] == '.': continue
+ if not self.showhidden and fn[0] == '.':
+ continue
- try: p = os.path.join(path, fn)
- except: continue
-
- s = os.lstat(p)
+ try:
+ p = os.path.join(path, fn)
+ s = os.lstat(p)
+ except:
+ continue
if stat.S_ISDIR(s.st_mode):
- subdirectories.append( (fn,p) );
+ tmp_dirs.append(fn);
elif stat.S_ISREG(s.st_mode):
- l.append((fn, s.st_size))
-
- # add subdirectories after files
- for fn,p in subdirectories:
- l.append((fn, self._build_tree(p)))
- if self.stopsignal: return sum_list([])
+ ret.append((fn, s.st_size))
+ finally:
+ pass
- except:
+ try:
+ c = 0
+
+ for fn in tmp_dirs:
+ c += 1
+
+ if self.stopsignal:
+ return sum_list([])
+
+ try:
+ p = os.path.join(path, fn)
+ except:
+ continue
+
+ ret.append((fn, self._build_tree(p)))
+
+ if not (progress_fn is None):
+ r = sum_list(ret, os.path.split(path)[-1])
+ r.sort()
+ progress_fn(c, len(tmp_dirs), r)
+
+ finally:
pass
- return sum_list(l, os.path.split(path)[-1])
+ r = sum_list(ret, os.path.split(path)[-1])
+ r.sort()
+
+ return r