aboutsummaryrefslogtreecommitdiff
path: root/license_protected_downloads/render_text_files.py
blob: 125c4320849a22e05ce2484e2b16b331c57c1012 (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
from textile.textilefactory import TextileFactory


class RenderTextFiles:

    def __init__(self):
        pass

    @classmethod
    def find_and_render(cls, path):

        result = {}

        try:
            # This method should raise some custom error if there is more
            # then one file of the same type recursively found.
            filepaths = cls.find_relevant_files(path)
        except:
            # this is ok, no tabs when none is returned.
            return None

        # TODO: Mocking files. Remove this when gesha is finished implementing
        # find_relevant_files method.
        filepaths = ['README', 'HACKING', 'INSTALL']
        if filepaths:
            for filepath in filepaths:
                try:
                    file_obj = open(filepath, 'r')
                    title, formatted = cls.render_file(file_obj)
                    result[title] = formatted
                except:
                    # TODO: log error or something.
                    continue
        else:
            return None

        return result

    @classmethod
    def render_file(cls, file_obj):
        # TODO: introduce special options to textile factory if necessary.
        textile_factory = TextileFactory()
        title = file_obj.readline()
        file_obj.seek(0)
        return title, textile_factory.process(file_obj.read())

    @classmethod
    def find_relevant_files(cls, path):
        # Go recursively and find howto's, readme's, hackings, installs.
        # If there are more of the same type then one, throw custom error as
        # written above.
        return None