summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrederic Back <fredericback@gmail.com>2006-10-03 10:33:15 +0000
committerFrederic Back <fredericback@gmail.com>2006-10-03 10:33:15 +0000
commit9f5926851fab9e3e5129a62efd1044214691b531 (patch)
treed74a422369d1ee347e0efd33416f8e351c612d05
parentd93cf5bd93c4a51f9938164f3879ea6e3f630a9c (diff)
* Cleaned the walker, moved some functions back to the
sumlist. git-svn-id: file:///home/lennart/svn/public/fring/trunk@61 d0d2c35f-0a1e-0410-abeb-dabff30a67ee
-rw-r--r--src/fringlib/fringdata.py58
-rw-r--r--src/fringlib/fringwalker.py167
2 files changed, 98 insertions, 127 deletions
diff --git a/src/fringlib/fringdata.py b/src/fringlib/fringdata.py
new file mode 100644
index 0000000..a9bea69
--- /dev/null
+++ b/src/fringlib/fringdata.py
@@ -0,0 +1,58 @@
+import gc
+
+#==================================================================================
+class SumList:
+
+ def __init__(self, name, children, size):
+ self.name = name
+ self.children = children
+ if self.children is None: self.children = []
+ self.size = size
+
+ def append_child(self, sumlist):
+ self.children.append(sumlist)
+ self.size += sumlist.size
+
+ def printout(self, tab=0 ):
+ print " "*tab,"%s (%i)"%(sumlist.name,sumlist.size)
+ if not sumlist.children: return
+ if tab > 1: return
+ for e in sumlist.children:
+ e.printout(tab+1)
+
+ def sort(self):
+ self.children.sort(self.__cmp_fn)
+
+ def sort_by_size(self):
+ self.children.sort(self.__cmp_by_size)
+
+ def __cmp_by_size(self, b, a ):
+ return cmp(a[2], b[2]) # compare sizes
+
+ def __cmp_fn(self, b, a ):
+ """ a and b are tuples describing a directory tree. Compare first by directory
+ status, then by size, and finally by name. """
+
+ a_dir = a[1] != None
+ b_dir = b[1] != None
+
+ if a_dir and not b_dir:
+ return 1
+ elif b_dir and not a_dir:
+ return -1
+ elif a_dir and b_dir:
+ return cmp(a[2], b[2]) # compare sizes
+ else:
+ return cmp(a[0], b[0]) # compare names
+
+ 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
+
+
+
+
diff --git a/src/fringlib/fringwalker.py b/src/fringlib/fringwalker.py
index af761e8..28cda4b 100644
--- a/src/fringlib/fringwalker.py
+++ b/src/fringlib/fringwalker.py
@@ -1,70 +1,14 @@
-import os, os.path, stat, sys
+import os, sys
import threading
-import posixpath # instead of os.path for gnomevfs operations
import time
-
import gobject, gtk
+import posixpath
from gnomevfs import *
-
-try: import fringtools
-except: pass
-
-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( sumlist, tab=0 ):
- """ An utility function to print out a tree array """
-
- print " "*tab,"%s (%i)"%(sumlist.name,sumlist.size)
- if not sumlist.children: return
- if tab > 1: return
- 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. """
-
- a_dir = a[1] != None
- b_dir = b[1] != None
-
- if a_dir and not b_dir:
- return 1
- elif b_dir and not a_dir:
- return -1
- elif a_dir and b_dir:
- return cmp(a[2], b[2]) # compare sizes
- else:
- return cmp(a[0], b[0]) # compare names
-
+from fringdata import *
class FringWalker( gobject.GObject ):
- """ Manages requests for walking directories
-
- Directory entries are represented by tuples of the following form:
- (name, contents, size)
-
- name: the file name of the directory
- contents: a list of entries within the entry. If None, the entry is not a directory
- size: the size of the entry.
+ """ Manages requests for walking folder hierarchies.
+ Folder data is stored in SumList objects.
"""
__gsignals__ = {
@@ -107,8 +51,9 @@ class FringWalker( gobject.GObject ):
class WalkThread( threading.Thread ):
- """ A separate class for the thread. """
-
+ """ A separate class for the thread.
+ Aggregated by FringWalker.
+ """
def __init__(self, master, uri, showhidden):
""" Parameters: A FringWalker instance, a string with the path and a bool """
@@ -120,110 +65,78 @@ class WalkThread( threading.Thread ):
self.max_recursion_sort = 3
- def __uri_tail(self, uri):
- """ Return the tail (the filename) of a gnomevfs uri """
- f = format_uri_for_display(str(uri))
- f = posixpath.split( f )[-1]
- return f
-
-
def build_tree_gnomevfs(self, uri, recursionlvl=0):
+ result = SumList(self.__uri_tail(uri), None, 0)
- try: h = DirectoryHandle(uri)
- except InvalidURIError:
- print uri,"is not a valid uri, skipped"
- return (str(uri), None, 0)
- except NotFoundError:
- print uri,"not found, skipped"
- return (str(uri), None, 0)
+ h = self.__open_directory(uri)
+ if h is None: return result
- l = []
- total = 0
try:
while True:
- if self.stopsignal: return (None,None,0)
+ if self.stopsignal: return result
d = h.next()
if not self.showhidden and d.name[0] == ".": continue
-
if d.name == "." or d.name == "..": continue
-
- if d.type == 2: # directory
- sub = self.build_tree_gnomevfs(uri.append_path(d.name),recursionlvl+1)
- l.append( sub )
- total += sub[2]
+ if d.type == 2:
+ result.append_child( self.build_tree_gnomevfs(uri.append_path(d.name),recursionlvl+1) )
else:
- 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 SumList(self.__uri_tail(uri), l, total)
+ result.append_child( SumList(d.name, None, d.size) )
+ except StopIteration: pass
+ if recursionlvl <= self.max_recursion_sort: result.sort_by_size()
+ return result
def run(self):
""" Parse the root directory """
+ self.result = SumList(self.__uri_tail(self.uri), None, 0)
+
# write some debug information
starttime = time.time()
print "start walking",self.uri
# scan root directory first (using gnomevfs)
- try: h = DirectoryHandle(self.uri)
- except InvalidURIError:
- print uri,"is not a valid uri, skipped"
- return (str(self.uri), None, 0)
- except NotFoundError:
- print uri,"not found, skipped"
- return (str(self.uri), None, 0)
-
+ h = self.__open_directory(self.uri)
+ if h is None: return
subdirectories = []
- l = []
- total = 0
try:
while True:
if self.stopsignal: return
d = h.next()
if not self.showhidden and d.name[0] == ".": continue
if d.name == "." or d.name == "..": continue
-
- if d.type == 2: # directory
- subdirectories.append( d.name );
- else:
- l.append( SumList(d.name, None, d.size) )
- total += d.size
+ if d.type == 2: subdirectories.append( d.name );
+ else: self.result.append_child( SumList(d.name, None, d.size) )
except StopIteration: pass
# emit an intermediate version to fill up the screen while waiting
- sumlist = SumList(self.__uri_tail(self.uri), l, total)
- self.master._progress_fn(self, 0, len(subdirectories), sumlist)
+ self.master._progress_fn(self, 0, len(subdirectories), self.result)
# walk the subdirectories
c = 0
for directory in subdirectories:
c += 1
- uri = self.uri
- sub = self.build_tree_gnomevfs(uri.append_path(directory))
-
+ self.result.append_child( self.build_tree_gnomevfs(self.uri.append_path(directory)) )
if self.stopsignal: return
-
- total += sub.size
- sub.name = directory
- l.append( sub );
-
- l.sort(treearray_cmp_fn)
+ self.result.sort()
# emit an intermediate version after each directory
- sumlist = SumList(self.__uri_tail(self.uri), l, total)
- self.master._progress_fn(self, c, len(subdirectories), sumlist)
-
+ self.master._progress_fn(self, c, len(subdirectories), self.result)
- l.sort(treearray_cmp_fn)
+ self.result.sort()
# emit final signal
- sumlist = SumList(self.__uri_tail(self.uri), l, total)
- self.master._finished_fn(self,sumlist)
+ self.master._finished_fn(self,self.result)
print "finished walking",self.uri,"(time=%s)"%round(time.time()-starttime,2)
+ def __uri_tail(self, uri):
+ """ Return the tail (the filename) of a gnomevfs uri """
+ f = format_uri_for_display(str(uri))
+ f = posixpath.split( f )[-1]
+ return f
+
+ def __open_directory(self, uri):
+ try: return DirectoryHandle(uri)
+ except InvalidURIError: print uri,"is not a valid uri, skipped"
+ except NotFoundError: print uri,"not found, skipped"