summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringwalker.py
blob: 1bf5c688fb95edbf63125fd5054f967ec2d13cd9 (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
import os, sys
import threading
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)),
    }

    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
            self.thread = None

    def _progress_fn(self, walkthread, c, l, r):
        # only emit if called from the current request
        if walkthread == self.thread:
            gtk.gdk.threads_enter()
            self.emit("progress", c, l)
            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

    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)

        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)
        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
    
        # emit an intermediate version to fill up the screen while waiting
        self.result.sort()
        self.master.collection.set_sumlist(unicode(self.uri),self.result)
        self.master._progress_fn(self, 0, len(subdirectories), self.result)

        # walk the subdirectories
        c = 0
        for directory in subdirectories:
            c += 1

            self.result.append_child( self.build_tree_gnomevfs(self.uri.append_path(directory)) )
            if self.stopsignal: return

            self.result.sort()
            self.master.collection.set_sumlist(unicode(self.uri),self.result)

            self.master._progress_fn(self, c, len(subdirectories), self.result)

        # emit final signal
        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"