summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringwalker.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/fringlib/fringwalker.py')
-rw-r--r--src/fringlib/fringwalker.py97
1 files changed, 40 insertions, 57 deletions
diff --git a/src/fringlib/fringwalker.py b/src/fringlib/fringwalker.py
index d897123..cede5af 100644
--- a/src/fringlib/fringwalker.py
+++ b/src/fringlib/fringwalker.py
@@ -7,25 +7,38 @@ import gobject, gtk
from gnomevfs import *
try: import fringtools
-except: print 'Error: Could not find "fringtools" extension module'
+except: pass
-WALKER_CLASSIC = 0
-WALKER_GNOMEVFS = 1
-WALKER_CPP = 2
+class SumList:
+
+ def __init__(self, name, children, size):
+ self.name = name
+ self.children = children
+ self.size = size
+
+ def __getitem__(self, key):
+ if key == 0: return self.name
+ if key == 1: return self.children
+ if key == 2: return self.size
+
+ def __len__(self):
+ return 3
-def print_tree( treearray, tab=0 ):
+
+def print_tree( sumlist, tab=0 ):
""" An utility function to print out a tree array """
- fn, data, size = treearray
- print " "*tab,"%s (%i)"%(fn,size)
- if not data: return
+ print " "*tab,"%s (%i)"%(sumlist.name,sumlist.size)
+ if not sumlist.children: return
if tab > 1: return
- for e in data:
+ for e in sumlist.children:
print_tree(e,tab+1)
+
def treearray_cmp_by_size( b, a ):
return cmp(a[2], b[2]) # compare sizes
+
def treearray_cmp_fn( b, a ):
""" a and b are tuples describing a directory tree. Compare first by directory
status, then by size, and finally by name. """
@@ -66,9 +79,9 @@ class FringWalker( gobject.GObject ):
self.thread = None
self.showhidden = False
- def walk(self,uri,method=WALKER_GNOMEVFS):
+ def walk(self,uri):
self.stop()
- self.thread = WalkThread(self,uri,self.showhidden,method)
+ self.thread = WalkThread(self,uri,self.showhidden)
self.thread.start()
def stop(self):
@@ -97,14 +110,13 @@ class WalkThread( threading.Thread ):
""" A separate class for the thread. """
- def __init__(self, master, uri, showhidden, method=WALKER_CLASSIC):
+ def __init__(self, master, uri, showhidden):
""" Parameters: A FringWalker instance, a string with the path and a bool """
threading.Thread.__init__(self)
self.stopsignal = False
self.master = master
self.uri = URI(uri)
self.showhidden = showhidden
- self.method = method
self.max_recursion_sort = 3
@@ -114,24 +126,6 @@ class WalkThread( threading.Thread ):
f = posixpath.split( f )[-1]
return f
- def build_tree_python(self, path):
- l = []
- total = 0
- for fn in os.listdir(path):
- if self.stopsignal: return (None,None,0)
- 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):
- sub = self.build_tree_python(p)
- l.append( sub )
- total += sub[2]
- elif stat.S_ISREG(s.st_mode):
- l.append( (fn, None, s.st_size) )
- total += s.st_size
- return (os.path.split(path)[-1], l, total)
-
def build_tree_gnomevfs(self, uri, recursionlvl=0):
@@ -156,14 +150,14 @@ class WalkThread( threading.Thread ):
l.append( sub )
total += sub[2]
else:
- l.append( (d.name, None, d.size) )
+ l.append( SumList(d.name, None, d.size) )
total += d.size
except StopIteration: pass
if recursionlvl <= self.max_recursion_sort:
l.sort(treearray_cmp_by_size)
- return (self.__uri_tail(uri), l, total)
+ return SumList(self.__uri_tail(uri), l, total)
def run(self):
@@ -172,9 +166,6 @@ class WalkThread( threading.Thread ):
# write some debug information
starttime = time.time()
print "start walking",self.uri,
- if self.method == WALKER_CPP: print "(using c++ extension)"
- elif self.method == WALKER_GNOMEVFS: print "(using python and gnomevfs)"
- else: print "(using classic python)"
# scan root directory first (using gnomevfs)
try: h = DirectoryHandle(self.uri)
@@ -197,47 +188,39 @@ class WalkThread( threading.Thread ):
if d.type == 2: # directory
subdirectories.append( d.name );
else:
- l.append( (d.name, None, d.size) )
+ l.append( SumList(d.name, None, d.size) )
total += d.size
except StopIteration: pass
# emit an intermediate version to fill up the screen while waiting
- self.master._progress_fn(self,
- 0, len(subdirectories),
- (self.__uri_tail(self.uri), l, total))
+ sumlist = SumList(self.__uri_tail(self.uri), l, total)
+ self.master._progress_fn(self, 0, len(subdirectories), sumlist)
- # now walk the subdirectories with the faster extension function
+ # walk the subdirectories
c = 0
-
for directory in subdirectories:
c += 1
- if self.method == WALKER_CPP:
- path = get_local_path_from_uri(str(self.uri))+os.sep+directory
- sub = fringtools.build_tree(path, self.showhidden)
- elif self.method == WALKER_GNOMEVFS:
- uri = self.uri
- sub = self.build_tree_gnomevfs(uri.append_path(directory))
- else:
- path = get_local_path_from_uri(str(self.uri))+os.sep+directory
- sub = self.build_tree_python(path)
+ uri = self.uri
+ sub = self.build_tree_gnomevfs(uri.append_path(directory))
if self.stopsignal: return
- total += sub[2]
- l.append( (directory,sub[1],sub[2]) );
+ total += sub.size
+ sub.name = directory
+ l.append( sub );
l.sort(treearray_cmp_fn)
# emit an intermediate version after each directory
- self.master._progress_fn(self,
- c, len(subdirectories),
- (self.__uri_tail(self.uri), l, total))
+ sumlist = SumList(self.__uri_tail(self.uri), l, total)
+ self.master._progress_fn(self, c, len(subdirectories), sumlist)
l.sort(treearray_cmp_fn)
# emit final signal
- self.master._finished_fn(self,(self.__uri_tail(self.uri), l, total))
+ sumlist = SumList(self.__uri_tail(self.uri), l, total)
+ self.master._finished_fn(self,sumlist)
print "finished walking",self.uri,"(time=%s)"%round(time.time()-starttime,2)