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