-
Notifications
You must be signed in to change notification settings - Fork 20
/
gen-defs.py
50 lines (44 loc) · 1.96 KB
/
gen-defs.py
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
#!/usr/bin/env python3
from jinja2 import Environment, FileSystemLoader, Template
import sys
import argparse
import yaml
import os
class DefGenerator:
def __init__(self, basepath, outputdir=None, defs_yml=None, templatedir=None):
if not templatedir: templatedir = "{0}/templates".format(basepath)
if not defs_yml: defs_yml = "{0}/defs.yml".format(args.basepath)
if not outputdir: outputdir = "{0}/include/defs".format(basepath)
self.outputdir = outputdir
env = Environment(loader=FileSystemLoader(templatedir),
lstrip_blocks=True, trim_blocks=True)
self.templates = {}
for name in ['property', 'element']:
self.templates[name] = env.get_template(name)
with open(defs_yml) as f:
self.specs = yaml.load(f)
os.makedirs(self.outputdir, exist_ok=True)
def generate(self):
for cat in self.templates:
sys.stderr.write("[{0}]\n\t".format(cat))
for name in self.specs[cat]:
definition = self.specs[cat][name]
definition['name'] = name
fname = "{0}/{1}".format(self.outputdir, name)
with open(fname, 'w') as f:
sys.stderr.write(name + " ")
f.write(self.templates[cat].render(definition))
sys.stderr.write("\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--basepath', default=os.getcwd(),
help='Path for defs.yml and templates/. Default: %(default)s')
parser.add_argument('--defs_yml',
help='Definitions YAML. Default: [basepath]/defs.yml')
parser.add_argument('--templatedir',
help='Templates directory. Default: [basepath]/templates')
parser.add_argument('--outputdir',
help='Output directory. Default: [basepath]/include/defs')
args = parser.parse_args()
generator = DefGenerator(**vars(args))
generator.generate()