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