#!/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 parse as parse_interface from templates import get_templates, get_templates_dir PROG = { "name": "interface_generator", "version": "0.1", } def generate_file(template: Path, templates: Path, output, kwargs): """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))) result = mako_template.render(**kwargs, PROG=PROG) output_name = template.stem.replace("interface", kwargs["name"]) print(f'{kwargs["name"]} via {template.name} => {output_name}') 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 = parse_interface(args) # print(interface) for template in get_templates(args.templates): # print(template) generate_file(template, args.templates, args.output, interface) if __name__ == "__main__": main()