From 0c3f3ae7ed3feb336a50e5ead8089e35f09e996a Mon Sep 17 00:00:00 2001 From: Frederic Back Date: Wed, 27 Sep 2006 17:37:53 +0000 Subject: * Walker now uses gnomevfs git-svn-id: file:///home/lennart/svn/public/fring/branches/c_walker@36 d0d2c35f-0a1e-0410-abeb-dabff30a67ee --- src/fringlib/fringwalker.py | 99 +++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/src/fringlib/fringwalker.py b/src/fringlib/fringwalker.py index 364b997..02d2af6 100644 --- a/src/fringlib/fringwalker.py +++ b/src/fringlib/fringwalker.py @@ -66,9 +66,6 @@ class FringWalker( gobject.GObject ): def walk(self,uri,method=WALKER_GNOMEVFS): self.stop() - - print "walkwalk",uri - self.thread = WalkThread(self,uri,self.showhidden,method) self.thread.start() @@ -103,26 +100,27 @@ class WalkThread( threading.Thread ): threading.Thread.__init__(self) self.stopsignal = False self.master = master - self.uri = uri + self.uri = URI(uri) self.showhidden = showhidden self.method = method - print "open",uri - self.path = get_local_path_from_uri( str(uri) ) - print "local",self.path - + 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_python(self, path, showhidden): + def build_tree_python(self, path): l = [] total = 0 for fn in os.listdir(path): if self.stopsignal: return (None,None,0) - if not 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) if stat.S_ISDIR(s.st_mode): - sub = self.build_tree_python(p,showhidden) + sub = self.build_tree_python(p) l.append( sub ) total += sub[2] elif stat.S_ISREG(s.st_mode): @@ -130,13 +128,9 @@ class WalkThread( threading.Thread ): total += s.st_size return (os.path.split(path)[-1], l, total) - def __uri_tail(self, uri): - """ Return the filename in a gnomevfs uri """ - f = format_uri_for_display(str(uri)) - f = posixpath.split( f )[-1] - return f - def build_tree_gnomevfs(self, uri, showhidden): + + def build_tree_gnomevfs(self, uri): try: h = DirectoryHandle(uri) except InvalidURIError: @@ -153,10 +147,10 @@ class WalkThread( threading.Thread ): while True: if self.stopsignal: return (None,None,0) d = h.next() - if not showhidden and d.name[0] == ".": continue + if not self.showhidden and d.name[0] == ".": continue if d.type == 2: # directory - sub = self.build_tree_gnomevfs(uri.append_path(d.name),showhidden) + sub = self.build_tree_gnomevfs(uri.append_path(d.name)) l.append( sub ) total += sub[2] else: @@ -170,75 +164,74 @@ class WalkThread( threading.Thread ): def run(self): """ Parse the root directory """ + # write some debug information starttime = time.time() - print "start walking",self.path, + 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)" - l = [] - i = 0 + # 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) + subdirectories = [] + l = [] total = 0 - - # make a first run for the root directory + d = h.next() try: - for fn in os.listdir(self.path): - - if self.stopsignal: return None - - if not self.showhidden and fn[0] == '.': continue - - try: p = os.path.join(self.path, fn) - except: continue - - s = os.lstat(p) - - if stat.S_ISDIR(s.st_mode): - subdirectories.append( (fn,p) ); - elif stat.S_ISREG(s.st_mode): - l.append((fn, None, s.st_size)) - - total += s.st_size - i += 1 + while True: + if self.stopsignal: return (None,None,0) + d = h.next() + if not self.showhidden and d.name[0] == ".": continue - except OSError: - pass + if d.type == 2: # directory + subdirectories.append( d.name ); + else: + l.append( (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), - (os.path.split(self.path)[-1], l, total)) + (self.__uri_tail(self.uri), l, total)) # now walk the subdirectories with the faster extension function c = 0 - for fn, p in subdirectories: + for directory in subdirectories: c += 1 if self.method == WALKER_CPP: - sub = fringtools.build_tree(p, self.showhidden) + sub = fringtools.build_tree(self.uri+os.sep+directory, self.showhidden) elif self.method == WALKER_GNOMEVFS: - sub = self.build_tree_gnomevfs(URI("file://"+p), self.showhidden) + uri = self.uri + sub = self.build_tree_gnomevfs(uri.append_path(directory)) else: - sub = self.build_tree_python(p, self.showhidden) + sub = self.build_tree_python(self.uri+os.sep+directory) if self.stopsignal: return None total += sub[2] - l.append( (fn,sub[1],sub[2]) ); + l.append( (directory,sub[1],sub[2]) ); l.sort(treearray_cmp_fn) # emit an intermediate version after each directory self.master._progress_fn(self, c, len(subdirectories), - (os.path.split(self.path)[-1], l, total)) + (self.__uri_tail(self.uri), l, total)) l.sort(treearray_cmp_fn) # emit final signal - self.master._finished_fn(self,(os.path.split(self.path)[-1], l, total)) - print "finished walking",self.path,"(time=%s)"%round(time.time()-starttime,2) + self.master._finished_fn(self,(self.__uri_tail(self.uri), l, total)) + print "finished walking",self.uri,"(time=%s)"%round(time.time()-starttime,2) -- cgit