summaryrefslogtreecommitdiffstats
path: root/src/fringlib/fringdata.py
blob: 1da49c06b3a66f1554fdd8f447922dfa0876655d (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
import gc

#==================================================================================
class SumList:
    
    def __init__(self, name, children, size):
        self.name = name
        self.children = children
        if self.children is None: self.children = []
        self.size = size

    def append_child(self, sumlist):
        self.children.append(sumlist)
        self.size += sumlist.size

    def has_children(self):
        if len(self.children) > 0: return True
        return False

    def printout(self, tab=0 ):
        print "  "*tab,"%s (%i)"%(sumlist.name,sumlist.size)
        if not sumlist.children: return
        if tab > 1: return
        for e in sumlist.children:
            e.printout(tab+1)

    def sort(self):
        self.children.sort(self.__cmp_fn)
    
    def sort_by_size(self):
        self.children.sort(self.__cmp_by_size)

    def __cmp_by_size(self, b, a ):
        return cmp(a[2], b[2]) # compare sizes

    def __cmp_fn(self, b, a ):
        """ a and b are tuples describing a directory tree. Compare first by directory
            status, then by size, and finally by name. """    

        a_dir = a[1] != None
        b_dir = b[1] != None

        if a_dir and not b_dir:
            return 1
        elif b_dir and not a_dir:
            return -1
        elif a_dir and b_dir:
             return cmp(a[2], b[2]) # compare sizes
        else:
            return cmp(a[0], b[0]) # compare names

    def __getitem__(self, key):
        if key == 0: return self.name
        if key == 1: return self.children
        if key == 2: return self.size

    def __len__(self):
        return 3


#==================================================================================
class SumListCollection:
    """ A repository for folder data.

    Is filled by the walker, and retrieved by the UI class.
    An int is used to track changes in a path.

    Since sumlists are objects, python will store references to them instead
    of copying the data (as it would with tuples). 
    """

    def __init__(self):
        self.clear()

    def set_sumlist(self, uri, sumlist):
        if not isinstance(sumlist,SumList): raise ValueError
        if not isinstance(uri,unicode): raise ValueError
        if not sumlist.has_children(): return # skip files
        self.collection[uri] = sumlist
        if uri in self.versionControl:
            self.versionControl[uri] += 1
        else:
            self.versionControl[uri] = 0
        
    def get_sumlist(self, uri):
        if uri in self.collection:
            return self.collection[uri]

    def has_changed(self, uri, version):
        if uri in self.versionControl:
            if version != self.versionControl[uri]:
                return True
        return False

    def clear(self):
        self.collection = {}
        self.versionControl = {}
        gc.collect()