blob: da23328bc9cb9b2c5c747991fa39305a7fddd0b5 [file] [log] [blame]
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +05301#!/usr/bin/env python
2import yaml
Vishwanatha Subbannabb8fe0b2016-11-12 18:29:38 +05303import os
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +05304import argparse
Vishwanatha Subbanna32bb6f82017-03-27 18:15:52 +05305from inflection import underscore
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +05306
7if __name__ == '__main__':
Vishwanatha Subbannabb8fe0b2016-11-12 18:29:38 +05308 script_dir = os.path.dirname(os.path.realpath(__file__))
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +05309 parser = argparse.ArgumentParser()
Brad Bishop9a6220c2017-02-08 13:21:31 -050010 parser.add_argument(
11 "-f", "--filename",
12 default='led.yaml',
13 help="Input File Name")
14 parser.add_argument(
Brad Bishop513f58e2017-02-08 13:25:17 -050015 "-i", "--input-dir",
16 dest='inputdir',
Brad Bishop9a6220c2017-02-08 13:21:31 -050017 default=script_dir,
18 help="Input directory")
Brad Bishop513f58e2017-02-08 13:25:17 -050019 parser.add_argument(
20 '-o', '--output-dir',
21 dest='outputdir',
22 default='.',
23 help='Output directory.')
24
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053025 args = parser.parse_args()
26
27 # Default to the one that is in the current.
Brad Bishop9a6220c2017-02-08 13:21:31 -050028 yaml_dir = script_dir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053029 yaml_file = os.path.join(yaml_dir, 'led.yaml')
30
Brad Bishop513f58e2017-02-08 13:25:17 -050031 if args.inputdir:
32 yaml_dir = args.inputdir
Vishwanatha Subbannaccbdf672017-01-23 14:39:06 +053033
34 if args.filename:
35 yaml_file = os.path.join(yaml_dir, args.filename)
36
37 with open(yaml_file, 'r') as f:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053038 ifile = yaml.safe_load(f)
39
Brad Bishop513f58e2017-02-08 13:25:17 -050040 with open(os.path.join(args.outputdir, 'led-gen.hpp'), 'w') as ofile:
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053041 ofile.write('/* !!! WARNING: This is a GENERATED Code..')
42 ofile.write('Please do NOT Edit !!! */\n\n')
43
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053044 ofile.write('static const std::map<std::string,')
45 ofile.write(' std::set<phosphor::led::Layout::LedAction>>')
46 ofile.write(' systemLedMap = {\n\n')
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053047 for group in ifile.iterkeys():
Vishwanatha Subbanna8a50a502017-01-20 18:42:21 +053048 # This section generates an std::map of LedGroupNames to std::set
49 # of LEDs containing the name and properties
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053050 ledset = ifile[group]
Brad Bishop9a6220c2017-02-08 13:21:31 -050051 ofile.write(
52 ' {\"' +
Vishwanatha Subbannaa1c7f6a2017-02-02 14:51:42 +053053 "/xyz/openbmc_project/led/groups/" +
Vishwanatha Subbanna32bb6f82017-03-27 18:15:52 +053054 underscore(group) +
Brad Bishop9a6220c2017-02-08 13:21:31 -050055 '\",{\n')
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053056
Vishwanatha Subbannad53de6f2017-03-30 11:21:37 +053057 # Some LED groups could be empty
58 if not ledset:
59 ofile.write(' }},\n')
60 continue
61
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053062 for led_dict, list_dict in ledset.iteritems():
Vishwanatha Subbanna8a50a502017-01-20 18:42:21 +053063 # Need this to make sure the LED name is printed once
64 name_printed = False
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053065 for name, value in list_dict.iteritems():
Vishwanatha Subbanna8a50a502017-01-20 18:42:21 +053066 if group and led_dict and name:
67 if name_printed is False:
Vishwanatha Subbanna32bb6f82017-03-27 18:15:52 +053068 ofile.write(' {\"' + underscore(led_dict) +
69 '\",')
Vishwanatha Subbanna8a50a502017-01-20 18:42:21 +053070 name_printed = True
71 if name == 'Action':
72 ofile.write('phosphor::led::Layout::')
73 ofile.write(str(value) + ',')
74 ofile.write('},\n')
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053075 ofile.write(' }},\n')
76 ofile.write('};\n')