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