summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringwalker.py
blob: f04858aeeb24641deb839f04340d0010b2459e61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os, sys
import threading
from time import time
import gobject, gtk
import posixpath
from gnomevfs import *
from fringdata import *

class FringWalker( gobject.GObject ):
    """ Manages requests for walking folder hierarchies.
        Folder data is stored in SumList objects.
    """
   
    __gsignals__ = {
        'list-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        'finished': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        'manually-stopped': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, () ),
        'progress': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
            (gobject.TYPE_INT,gobject.TYPE_INT,gobject.TYPE_STRING)),
    }

    def __init__(self, sumlistcollection):
        gobject.GObject.__init__(self)
        self.thread = None
        self.showhidden = True
        self.collection = sumlistcollection

    def walk(self,uri):
        self.stop()
        self.thread = WalkThread(self,uri,self.showhidden)
        self.thread.start()

    def stop(self):
        if self.thread:
            self.emit("manually-stopped")
            self.thread.stopsignal = True
            # remove unfinished sumlist from collection
            self.collection.rem_sumlist(unicode(self.thread.uri)) 
            self.thread = None

    def _progress_fn(self, walkthread, c, l, r, currentlyScanning):
        # only emit if called from the current request
        if walkthread == self.thread:
            gtk.gdk.threads_enter()
            self.emit("progress", c, l, currentlyScanning)
            self.emit("list-changed", r)
            gtk.gdk.threads_leave()

    def _finished_fn(self, walkthread, r):
        if walkthread == self.thread:
            gtk.gdk.threads_enter()
            self.emit("finished", r)
            gtk.gdk.threads_leave()
            self.thread = None


class WalkThread( threading.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 """
        threading.Thread.__init__(self)
        self.stopsignal = False
        self.master = master
        self.uri = URI(uri)
        self.showhidden = showhidden
        self.max_recursion_sort = 3  
        self.progresstimer = 0

    def build_tree_gnomevfs(self, uri, recursionlvl=0):
        result = SumList(self.__uri_tail(uri), None, 0)

        h = self.__open_directory(uri)
        if h is None: return result

        try:
            while True:
                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:
                    result.append_child( self.build_tree_gnomevfs(uri.append_path(d.name),recursionlvl+1) )
                else:
                    result.append_child( SumList(d.name, None, d.size) )
        except StopIteration: pass

        if recursionlvl <= self.max_recursion_sort:
            result.sort_by_size()
            self.master.collection.set_sumlist(unicode(uri),result)

        if recursionlvl <= 1:
            self.__check_progress()

        return result

    def run(self):
        """ Parse the root directory """

        self.progresstimer = time()

        self.result = SumList(self.__uri_tail(self.uri), None, 0)
        self.master._progress_fn(self, 0, 1, self.result, self.uri)

        # write some debug information
        starttime = time()
        print "start walking",self.uri

        # scan root directory first (using gnomevfs)
        h = self.__open_directory(self.uri)
        if h is None: return
        subdirectories = []
        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: subdirectories.append( d.name );
                else: self.result.append_child( SumList(d.name, None, d.size) )
        except StopIteration: pass
    
        # walk the subdirectories
        self.progress_total = len(subdirectories)
        self.progress_count = 0
        for self.directory in subdirectories:
            sub = self.build_tree_gnomevfs(self.uri.append_path(self.directory))
            self.result.append_child( sub )
            if self.stopsignal: return
            self.progress_count += 1

        # emit final signal
        self.master._finished_fn(self,self.result)
        self.master.collection.set_sumlist(unicode(self.uri),self.result)
        print "finished walking",self.uri,"(time=%s)"%round(time()-starttime,2)
       
    def __check_progress(self):
        if time()-self.progresstimer > .25:
            self.progresstimer = time()
            #self.result.sort_by_size()
            self.master._progress_fn(self,
                self.progress_count, self.progress_total,
                self.result,
                self.directory)
 
    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 "Could not open",uri,"(not a valid uri)"
        except NotFoundError: print "Could not open",uri,"(not found)"
        except AccessDeniedError: print "Could not open",uri,"(access denied)"