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