aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-06-23 18:38:41 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-06-23 18:38:41 +0200
commitf8d7a03a140a6fed09f94843cde5af04c5e2b5f7 (patch)
treed3f0021a7838a0143387ddc922cf43ab73c09713
parentccd7f09ad8c783e6c42262a03b71df03034dc35e (diff)
First version of a python script to fill in the templates
-rwxr-xr-xbin/render80
1 files changed, 80 insertions, 0 deletions
diff --git a/bin/render b/bin/render
new file mode 100755
index 0000000..a8aa6ba
--- /dev/null
+++ b/bin/render
@@ -0,0 +1,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()