aboutsummaryrefslogtreecommitdiff
path: root/bin/render
blob: a8aa6bad0482363b0b8ba02269b7c637d39348b7 (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
#!/usr/bin/env python3

import argparse
import os.path

import ruamel.yaml
import jinja2

class Renderer:
    def __init__(self):
        self.parse_args()
        self.env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(
                searchpath='src',
                followlinks=True,
                )
            )

    def parse_args(self):
        self.parser = argparse.ArgumentParser(
            description=''
            )
        self.parser.add_argument(
            'design'
            )
        self.parser.add_argument(
            '-o', '--output',
            )
        self.args = self.parser.parse_args()

    def load_design(self, design):
        with open(design, 'r') as fp:
            design = ruamel.yaml.safe_load(fp)
        template = design['template']
        settings = {}
        with open(os.path.join(
                'conf',
                'paper',
                design['paper']+'.yaml'
                ), 'r') as fp:
            settings.update(ruamel.yaml.safe_load(fp))
        with open(os.path.join(
                'conf',
                'margins',
                design['margins']+'.yaml'
                ), 'r') as fp:
            settings.update(ruamel.yaml.safe_load(fp))
        with open(os.path.join(
                'conf',
                'theme',
                design['theme']+'.yaml'
                ), 'r') as fp:
            settings.update(ruamel.yaml.safe_load(fp))
        with open(os.path.join(
                'conf',
                'settings',
                template,
                design['settings']+'.yaml'
                ), 'r') as fp:
            settings.update(ruamel.yaml.safe_load(fp))
        settings.update(design.get('override', {}))

        return template, settings

    def render_ps(self, template, settings):
        j_templ = self.env.get_template(template + '.ps.j2')
        return j_templ.render(**settings)

    def main(self):
        template, settings = self.load_design(self.args.design)
        ps = self.render_ps(template, settings)
        if self.args.output:
            with open(self.args.output, 'w') as fp:
                fp.write(ps)
        else:
            print(ps)


if __name__ == '__main__':
    Renderer().main()