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(filename=os.path.join(script_dir, "writesensor.mako.cpp"))
20
21        output_cpp = os.path.join(output_dir, "sensor-gen.cpp")
22        with open(output_cpp, "w") as fd:
23            fd.write(t.render(sensorDict=ifile))
24
25
26def main():
27    valid_commands = {"generate-cpp": generate_cpp}
28    parser = argparse.ArgumentParser(
29        description="IPMI Sensor parser and code generator"
30    )
31
32    parser.add_argument(
33        "-i",
34        "--sensor_yaml",
35        dest="sensor_yaml",
36        default="example.yaml",
37        help="input sensor yaml file to parse",
38    )
39
40    parser.add_argument(
41        "-o",
42        "--output-dir",
43        dest="outputdir",
44        default=".",
45        help="output directory",
46    )
47
48    parser.add_argument(
49        "command",
50        metavar="COMMAND",
51        type=str,
52        choices=valid_commands.keys(),
53        help="Command to run.",
54    )
55
56    args = parser.parse_args()
57
58    if not (os.path.isfile(args.sensor_yaml)):
59        sys.exit("Can not find input yaml file " + args.sensor_yaml)
60
61    function = valid_commands[args.command]
62    function(args.sensor_yaml, args.outputdir)
63
64
65if __name__ == "__main__":
66    script_dir = os.path.dirname(os.path.realpath(__file__))
67    main()
68