summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringwalker.py
diff options
context:
space:
mode:
authorFrederic Back <fredericback@gmail.com>2006-09-27 17:19:35 +0000
committerFrederic Back <fredericback@gmail.com>2006-09-27 17:19:35 +0000
commite39436576c68e70ac70d4ee108860d411b52d0bb (patch)
treee9b63c2e8374b99639694d67afd3349041cb893a /src/fringlib/fringwalker.py
parent5bcf37b8944759e09fe45c341a555757b53a6e20 (diff)
* added gnomevfs support
* added menu entry to choose walking method * merged back from trunk * UI now uses gnomevfs everywhere git-svn-id: file:///home/lennart/svn/public/fring/branches/c_walker@33 d0d2c35f-0a1e-0410-abeb-dabff30a67ee
Diffstat (limited to 'src/fringlib/fringwalker.py')
-rw-r--r--src/fringlib/fringwalker.py109
1 files changed, 98 insertions, 11 deletions
diff --git a/src/fringlib/fringwalker.py b/src/fringlib/fringwalker.py
index 99eaeff..364b997 100644
--- a/src/fringlib/fringwalker.py
+++ b/src/fringlib/fringwalker.py
@@ -1,12 +1,21 @@
import os, os.path, stat, sys
-import gobject, gtk
-from threading import Thread
+import threading
+import posixpath # instead of os.path for gnomevfs operations
import time
+
+import gobject, gtk
+from gnomevfs import *
+
import fringtools
+WALKER_CLASSIC = 0
+WALKER_GNOMEVFS = 1
+WALKER_CPP = 2
+
def print_tree( treearray, 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
@@ -55,13 +64,17 @@ class FringWalker( gobject.GObject ):
self.thread = None
self.showhidden = False
- def walk(self,path):
+ def walk(self,uri,method=WALKER_GNOMEVFS):
self.stop()
- self.thread = WalkThread(self,path,self.showhidden)
+
+ print "walkwalk",uri
+
+ self.thread = WalkThread(self,uri,self.showhidden,method)
self.thread.start()
def stop(self):
if self.thread:
+ self.emit("manually-stopped")
self.thread.stopsignal = True
self.thread = None
@@ -78,23 +91,90 @@ class FringWalker( gobject.GObject ):
gtk.gdk.threads_enter()
self.emit("finished", r)
gtk.gdk.threads_leave()
+ self.thread = None
-class WalkThread( Thread ):
+class WalkThread( threading.Thread ):
""" A separate class for the thread. """
- def __init__(self, master, path, showhidden):
+
+ def __init__(self, master, uri, showhidden, method=WALKER_CLASSIC):
""" Parameters: A FringWalker instance, a string with the path and a bool """
- Thread.__init__(self)
+ threading.Thread.__init__(self)
self.stopsignal = False
self.master = master
- self.path = path
+ self.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 build_tree_python(self, path, showhidden):
+ l = []
+ total = 0
+ for fn in os.listdir(path):
+ if self.stopsignal: return (None,None,0)
+ if not 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)
+ 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 __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):
+
+ 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)
+
+ l = []
+ total = 0
+ d = h.next()
+ try:
+ while True:
+ if self.stopsignal: return (None,None,0)
+ d = h.next()
+ if not showhidden and d.name[0] == ".": continue
+
+ if d.type == 2: # directory
+ sub = self.build_tree_gnomevfs(uri.append_path(d.name),showhidden)
+ l.append( sub )
+ total += sub[2]
+ else:
+ l.append( (d.name, None, d.size) )
+ total += d.size
+
+ except StopIteration: pass
+ return (self.__uri_tail(uri), l, total)
+
def run(self):
""" Parse the root directory """
- print "start walking",self.path
+ starttime = time.time()
+ print "start walking",self.path,
+ 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
@@ -136,7 +216,13 @@ class WalkThread( Thread ):
for fn, p in subdirectories:
c += 1
- sub = fringtools.build_tree(p, self.showhidden)
+ if self.method == WALKER_CPP:
+ sub = fringtools.build_tree(p, self.showhidden)
+ elif self.method == WALKER_GNOMEVFS:
+ sub = self.build_tree_gnomevfs(URI("file://"+p), self.showhidden)
+ else:
+ sub = self.build_tree_python(p, self.showhidden)
+
if self.stopsignal: return None
total += sub[2]
@@ -154,4 +240,5 @@ class WalkThread( Thread ):
# emit final signal
self.master._finished_fn(self,(os.path.split(self.path)[-1], l, total))
- print "finished walking",self.path
+ print "finished walking",self.path,"(time=%s)"%round(time.time()-starttime,2)
+