Lei YU | 43082e7 | 2020-03-09 10:32:48 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 2 | |
| 3 | import os |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 4 | import sys |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 5 | from argparse import ArgumentParser |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 6 | |
| 7 | import yaml |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 8 | from mako.lookup import TemplateLookup |
| 9 | |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 10 | if __name__ == "__main__": |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 11 | parser = ArgumentParser( |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 12 | description="Power sequencer UCD90160 definition parser" |
| 13 | ) |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 14 | |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 15 | parser.add_argument( |
| 16 | "-i", |
| 17 | "--input_yaml", |
| 18 | dest="input_yaml", |
| 19 | default="example/ucd90160.yaml", |
| 20 | help="UCD90160 definitions YAML", |
| 21 | ) |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 22 | |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 23 | parser.add_argument( |
| 24 | "-o", |
| 25 | "--output_dir", |
| 26 | dest="output_dir", |
| 27 | default=".", |
| 28 | help="output directory", |
| 29 | ) |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 30 | |
| 31 | args = parser.parse_args() |
| 32 | |
| 33 | if not args.input_yaml or not args.output_dir: |
| 34 | parser.print_usage() |
| 35 | sys.exit(1) |
| 36 | |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 37 | with open(args.input_yaml, "r") as ucd90160_input: |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 38 | ucd90160_data = yaml.safe_load(ucd90160_input) or {} |
| 39 | |
| 40 | templates_dir = os.path.join( |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 41 | os.path.dirname(os.path.realpath(__file__)), "templates" |
| 42 | ) |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 43 | |
| 44 | output_file = os.path.join(args.output_dir, "ucd90160_defs.cpp") |
| 45 | |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 46 | mylookup = TemplateLookup(directories=templates_dir.split()) |
| 47 | mytemplate = mylookup.get_template("ucd90160_defs.mako.cpp") |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 48 | |
Patrick Williams | a93243d | 2022-12-05 09:20:34 -0600 | [diff] [blame] | 49 | with open(output_file, "w") as output: |
Brandon Wyman | 34e257b | 2019-03-28 17:39:18 -0500 | [diff] [blame] | 50 | output.write(mytemplate.render(ucd90160s=ucd90160_data)) |