#!/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()