Dhruvaraj Subhashchandran | 3c5def3 | 2023-07-09 04:31:28 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import argparse |
| 4 | import os |
| 5 | |
| 6 | import yaml |
| 7 | from mako.template import Template |
| 8 | |
| 9 | |
| 10 | def main(): |
| 11 | parser = argparse.ArgumentParser( |
| 12 | description="OpenPOWER map code generator" |
| 13 | ) |
| 14 | |
| 15 | parser.add_argument( |
| 16 | "-i", |
| 17 | "--input_yaml", |
| 18 | dest="input_yaml", |
| 19 | default="example.yaml", |
| 20 | help="input yaml file to parse", |
| 21 | ) |
| 22 | |
| 23 | parser.add_argument( |
| 24 | "-t", |
| 25 | "--template", |
| 26 | dest="template", |
| 27 | default="template.mako.cpp", |
| 28 | help="mako template file to use", |
| 29 | ) |
| 30 | |
| 31 | parser.add_argument( |
| 32 | "-o", |
| 33 | "--output_file", |
| 34 | dest="output_file", |
| 35 | default="output.cpp", |
| 36 | help="output cpp file", |
| 37 | ) |
| 38 | |
| 39 | parser.add_argument( |
| 40 | "-v", |
| 41 | "--var_name", |
| 42 | dest="var_name", |
| 43 | default="mapping", |
| 44 | help="variable name to use in the template", |
| 45 | ) |
| 46 | |
| 47 | args = parser.parse_args() |
| 48 | |
| 49 | with open(os.path.join(script_dir, args.input_yaml), "r") as fd: |
| 50 | yaml_dict = yaml.safe_load(fd) |
| 51 | |
| 52 | template = os.path.join(script_dir, args.template) |
| 53 | t = Template(filename=template) |
| 54 | with open(args.output_file, "w") as fd: |
| 55 | if args.var_name == "errDict": |
| 56 | fd.write(t.render(errDict=yaml_dict)) |
| 57 | else: |
| 58 | fd.write(t.render(DUMP_TYPE_TABLE=yaml_dict)) |
| 59 | |
| 60 | |
| 61 | if __name__ == "__main__": |
| 62 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 63 | main() |