Tom Joseph | 31eed5c | 2020-03-23 20:56:47 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import yaml |
| 6 | import argparse |
| 7 | from mako.template import Template |
| 8 | |
| 9 | |
| 10 | def generate_cpp(sensor_yaml, output_dir): |
Vernon Mauery | 5f7ac71 | 2019-04-30 11:19:08 -0700 | [diff] [blame] | 11 | with open(sensor_yaml, 'r') as f: |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 12 | ifile = yaml.safe_load(f) |
| 13 | if not isinstance(ifile, dict): |
| 14 | ifile = {} |
| 15 | |
| 16 | # Render the mako template |
| 17 | |
| 18 | t = Template(filename=os.path.join( |
| 19 | script_dir, |
| 20 | "inventorysensor.mako.cpp")) |
| 21 | |
| 22 | output_cpp = os.path.join(output_dir, "inventory-sensor-gen.cpp") |
| 23 | with open(output_cpp, 'w') as fd: |
| 24 | fd.write(t.render(sensorDict=ifile)) |
| 25 | |
| 26 | |
| 27 | def main(): |
| 28 | |
| 29 | valid_commands = { |
| 30 | 'generate-cpp': generate_cpp |
| 31 | } |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description="Inventory Object to IPMI SensorID code generator") |
| 34 | |
| 35 | parser.add_argument( |
| 36 | '-i', '--sensor_yaml', dest='sensor_yaml', |
| 37 | default='example.yaml', help='input sensor yaml file to parse') |
| 38 | |
| 39 | parser.add_argument( |
| 40 | "-o", "--output-dir", dest="outputdir", |
| 41 | default=".", |
| 42 | help="output directory") |
| 43 | |
| 44 | parser.add_argument( |
| 45 | 'command', metavar='COMMAND', type=str, |
| 46 | choices=valid_commands.keys(), |
| 47 | help='Command to run.') |
| 48 | |
| 49 | args = parser.parse_args() |
| 50 | |
Vernon Mauery | 5f7ac71 | 2019-04-30 11:19:08 -0700 | [diff] [blame] | 51 | if (not (os.path.isfile(args.sensor_yaml))): |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 52 | sys.exit("Can not find input yaml file " + args.sensor_yaml) |
| 53 | |
| 54 | function = valid_commands[args.command] |
| 55 | function(args.sensor_yaml, args.outputdir) |
| 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 60 | main() |