diff options
Diffstat (limited to 'tools/interface_generator/bin/interface_generator.py')
-rwxr-xr-x | tools/interface_generator/bin/interface_generator.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tools/interface_generator/bin/interface_generator.py b/tools/interface_generator/bin/interface_generator.py new file mode 100755 index 0000000..eab93d7 --- /dev/null +++ b/tools/interface_generator/bin/interface_generator.py @@ -0,0 +1,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_declaration import InterfaceDeclaration +from templates import get_templates, get_templates_dir + +PROG = { + "name": "interface_generator", + "version": "0.1", +} + + +def generate_file( + template: Path, templates: Path, output, interface: InterfaceDeclaration +): + """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 declaration", + ) + 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 = InterfaceDeclaration(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() |