1#!/usr/bin/env python3
2import argparse
3import os
4
5import yaml
6from inflection import underscore
7
8if __name__ == "__main__":
9    script_dir = os.path.dirname(os.path.realpath(__file__))
10    parser = argparse.ArgumentParser()
11    parser.add_argument(
12        "-f", "--filename", default="led.yaml", help="Input File Name"
13    )
14    parser.add_argument(
15        "-i",
16        "--input-dir",
17        dest="inputdir",
18        default=script_dir,
19        help="Input directory",
20    )
21    parser.add_argument(
22        "-o",
23        "--output-dir",
24        dest="outputdir",
25        default=".",
26        help="Output directory.",
27    )
28
29    args = parser.parse_args()
30
31    # Default to the one that is in the current.
32    yaml_dir = script_dir
33    yaml_file = os.path.join(yaml_dir, "led.yaml")
34
35    if args.inputdir:
36        yaml_dir = args.inputdir
37
38    if args.filename:
39        yaml_file = os.path.join(yaml_dir, args.filename)
40
41    with open(yaml_file, "r") as f:
42        ifile = yaml.safe_load(f)
43
44    # Dictionary having [Name:Priority]
45    priority_dict = {}
46
47    with open(os.path.join(args.outputdir, "led-gen.hpp"), "w") as ofile:
48        ofile.write("/* !!! WARNING: This is a GENERATED Code..")
49        ofile.write("Please do NOT Edit !!! */\n\n")
50
51        ofile.write("static const phosphor::led::GroupMap")
52        ofile.write(" systemLedMap = {\n\n")
53        for group in list(ifile.keys()):
54            # This section generates an std::unordered_map of LedGroupNames to
55            # std::set of LEDs containing the name and properties
56            led_dict = ifile[group]
57            ofile.write(
58                '   {"'
59                + "/xyz/openbmc_project/led/groups/"
60                + underscore(group)
61                + '",{\n'
62            )
63
64            # Some LED groups could be empty
65            if not led_dict:
66                ofile.write("   }},\n")
67                continue
68
69            for led_name, list_dict in list(led_dict.items()):
70                value = list_dict.get("Priority")
71                if led_name in priority_dict:
72                    if value != priority_dict[led_name]:
73                        # Priority for a particular LED needs to stay SAME
74                        # across all groups
75                        ofile.close()
76                        os.remove(ofile.name)
77                        raise ValueError(
78                            "Priority for ["
79                            + led_name
80                            + "] is NOT same across all groups"
81                        )
82                else:
83                    priority_dict[led_name] = value
84
85                ofile.write('        {"' + underscore(led_name) + '",')
86                ofile.write(
87                    "phosphor::led::Layout::Action::"
88                    + str(list_dict.get("Action", "Off"))
89                    + ","
90                )
91                ofile.write(str(list_dict.get("DutyOn", 50)) + ",")
92                ofile.write(str(list_dict.get("Period", 0)) + ",")
93                priority = str(list_dict.get("Priority", "Blink"))
94                ofile.write("phosphor::led::Layout::Action::" + priority + ",")
95                ofile.write("},\n")
96            ofile.write("   }},\n")
97        ofile.write("};\n")
98