summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringwalker.py
blob: 51373065e719ce31cde3d86572d8db60ae0f3a48 (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
import os, os.path, stat, sys
import gobject, gtk
from threading import Thread
import time
import fringtools


def print_tree( t, tab=0 ):
    """ An utility function to print out the tree returned by FringWalker::_parse """

    fn, data, size = t
    print " "*tab,"%s (%i)"%(fn,size)
    if not data: return
    for e in data:
        print_tree(e,tab+1)

class FringWalker( gobject.GObject ):
   
    __gsignals__ = {
        'list-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        'finished': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        'progress': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT,gobject.TYPE_INT)),
    }

    def __init__(self):
        gobject.GObject.__init__(self)
        self.thread = None
        self.stopsignal = False
        self.showhidden = False


    def walk(self,path):
        self.thread = Thread(None,self._parse,None,(path,))
        self.stopsignal = False
        print "start thread (%s)"%path
        self.thread.start()
        

    def stop(self):
        if self.thread is None:
            return

        if not self.thread.isAlive():
            self.thread = None
            return

        self.stopsignal = True
        print "stopping thread:"
        #self.thread.join()
        print "ok"


    def _parse(self,path):
        """ Parse the root directory """

        l = []
        i = 0
        subdirectories = {}

        total = 0

        try:
            for fn in os.listdir(path):

                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): 
                    subdirectories[i] = (fn,p);
                    l.append((fn, None, s.st_size))
                elif stat.S_ISREG(s.st_mode):
                    l.append((fn, None, s.st_size))

                total += s.st_size
                i += 1

        except:
            pass

        gtk.gdk.threads_enter()
        self.emit("list-changed",(os.path.split(path)[-1], l, total))
        gtk.gdk.threads_leave()

        c = 0
        for n in subdirectories:
            c += 1

            fn,p = subdirectories[n]
            sub = fringtools.build_tree(p,True) # call faster c++ extension
            total += sub[2]
            
            l[n] = (fn,sub[1],sub[2]); 

            if self.stopsignal: return

            # emit signals
            gtk.gdk.threads_enter()
            self.emit("list-changed",(os.path.split(path)[-1], l, total))
            print "%s parsed"%fn
            gtk.gdk.threads_leave()
            

        gtk.gdk.threads_enter()
        self.emit("finished",(os.path.split(path)[-1], l, total))
        gtk.gdk.threads_leave()

        print "finished walking",path