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
81
82
83
84
85
86
|
#!/usr/bin/env python3
"""
interface_generator.py
"""
from argparse import ArgumentParser
from pathlib import Path
import sys
from mako.lookup import TemplateLookup
from interface_definition import InterfaceDefinition
from templates import get_templates, get_templates_dir
PROG = {
"name": "interface_generator",
"version": "0.1",
}
def generate_file(
template: Path, templates: Path, output, interface: InterfaceDefinition
):
"""generate file using a tempalte and write it to the output location"""
lookup = TemplateLookup(directories=[".", templates.absolute()])
mako_template = lookup.get_template(str(template.relative_to(templates)))
output_name = template.stem.replace("interface", interface.name)
print(f"{interface.name} via {template.name} => {output_name}")
result = mako_template.render(**interface.into_dict(), PROG=PROG)
if isinstance(output, Path):
# print(f"writing to {(output / output_name).absolute()}")
with open(output / output_name, "w", encoding="utf-8") as output_file:
print(result, file=output_file)
else:
print(result, file=output)
def main():
"""main function"""
parser = ArgumentParser(
prog="interface_generator",
description="Generate C header and mock files from an interface definition",
)
parser.add_argument(
"-i",
"--interface",
type=Path,
# required=True,
help="path to interface file",
)
parser.add_argument(
"-t",
"--templates",
type=Path,
default=get_templates_dir(),
help="templates location",
)
parser.add_argument(
"-l",
"--license",
type=Path,
required=True,
help="path to license file",
)
parser.add_argument(
"-o",
"--output",
type=Path,
default=sys.stdout,
help="path to output, stdout by default",
)
args = parser.parse_args()
# print(args)
interface = InterfaceDefinition(args.interface, args.license)
# print(interface)
for template in get_templates(args.templates):
# print(template)
generate_file(template, args.templates, args.output, interface)
if __name__ == "__main__":
main()
|