summaryrefslogtreecommitdiff
path: root/pycco/utils.py
blob: 4cfb9f351116cc158e7cac30e677fa64d22552a1 (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
import sys
import os


def get_all_files(path, extension):
    def relative(path):
        relpath = os.path.relpath(path)
        if relpath == '.':
            return ''
        else:
            return relpath + "/"

    for path, dirs, files in os.walk(path):
        for filename in files:
            if filename.endswith(extension):
                yield "%s%s" % (relative(path), filename)


class Source:
    def __init__(self, name, start):
        self.name       = name
        self.title      = os.path.basename(self.name)
        self.dirpath    = os.path.dirname(self.name) or '.'
        self.dirname    = os.path.relpath(self.dirpath, start)
        self.start      = start

    def save_path(self):
        return "docs/%s/%s" % (self.dirname, self.title)

    def relative_path(self, source):
        html = lambda x: "%s.html" % os.path.splitext(x)[0]
        rel  = os.path.relpath(source.dirpath, self.dirpath)
        return "%s/%s" % (rel, html(source.title))

    def relative_paths(self, sources):
        root_ = ''
        list_ = []
        dict_ = {}
        id_ = 1
        title    = lambda s: {'title': s.title, 'url': self.relative_path(s)}
        new_dict = lambda s: {'id': id_, 'dirname': s.dirname, 'display': 'none', 'titles': [title(s)]}

        for source in sources:
            if source.dirpath != root_:
                if dict_:
                    list_.append(dict_)
                root_  = source.dirpath
                dict_  = new_dict(source)
                id_   += 1
            else:
                dict_['titles'].append(title(source))

        list_.append(dict_)
        if len(list_) == 1:
            list_[0]['display'] = 'block'
        return list_


class Sources:
    def __init__(self, sources, start):
        self.list = [Source(name, start) for name in sources]
        self.get  = lambda x: self.list[index]

    def names(self):
        return [i.name for i in self.list]

    def __iter__(self):
        for i in self.list:
            yield i