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