Vishwanatha Subbanna | 4f43f5e | 2020-03-09 03:56:57 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 2 | import yaml |
Vishwanatha Subbanna | bb8fe0b | 2016-11-12 18:29:38 +0530 | [diff] [blame] | 3 | import os |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 4 | import argparse |
Vishwanatha Subbanna | 32bb6f8 | 2017-03-27 18:15:52 +0530 | [diff] [blame] | 5 | from inflection import underscore |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 6 | |
| 7 | if __name__ == '__main__': |
Vishwanatha Subbanna | bb8fe0b | 2016-11-12 18:29:38 +0530 | [diff] [blame] | 8 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 9 | parser = argparse.ArgumentParser() |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 10 | parser.add_argument( |
| 11 | "-f", "--filename", |
| 12 | default='led.yaml', |
| 13 | help="Input File Name") |
| 14 | parser.add_argument( |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 15 | "-i", "--input-dir", |
| 16 | dest='inputdir', |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 17 | default=script_dir, |
| 18 | help="Input directory") |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 19 | parser.add_argument( |
| 20 | '-o', '--output-dir', |
| 21 | dest='outputdir', |
| 22 | default='.', |
| 23 | help='Output directory.') |
| 24 | |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 25 | args = parser.parse_args() |
| 26 | |
| 27 | # Default to the one that is in the current. |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 28 | yaml_dir = script_dir |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 29 | yaml_file = os.path.join(yaml_dir, 'led.yaml') |
| 30 | |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 31 | if args.inputdir: |
| 32 | yaml_dir = args.inputdir |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 33 | |
| 34 | if args.filename: |
| 35 | yaml_file = os.path.join(yaml_dir, args.filename) |
| 36 | |
| 37 | with open(yaml_file, 'r') as f: |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 38 | ifile = yaml.safe_load(f) |
| 39 | |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 40 | # Dictionary having [Name:Priority] |
| 41 | priority_dict = {} |
| 42 | |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 43 | with open(os.path.join(args.outputdir, 'led-gen.hpp'), 'w') as ofile: |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 44 | ofile.write('/* !!! WARNING: This is a GENERATED Code..') |
| 45 | ofile.write('Please do NOT Edit !!! */\n\n') |
| 46 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 47 | ofile.write('static const std::map<std::string,') |
| 48 | ofile.write(' std::set<phosphor::led::Layout::LedAction>>') |
| 49 | ofile.write(' systemLedMap = {\n\n') |
Vishwanatha Subbanna | e0cf8fe | 2020-03-03 02:38:21 -0600 | [diff] [blame] | 50 | for group in list(ifile.keys()): |
Vishwanatha Subbanna | 8a50a50 | 2017-01-20 18:42:21 +0530 | [diff] [blame] | 51 | # This section generates an std::map of LedGroupNames to std::set |
| 52 | # of LEDs containing the name and properties |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 53 | led_dict = ifile[group] |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 54 | ofile.write( |
| 55 | ' {\"' + |
Vishwanatha Subbanna | a1c7f6a | 2017-02-02 14:51:42 +0530 | [diff] [blame] | 56 | "/xyz/openbmc_project/led/groups/" + |
Vishwanatha Subbanna | 32bb6f8 | 2017-03-27 18:15:52 +0530 | [diff] [blame] | 57 | underscore(group) + |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 58 | '\",{\n') |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 59 | |
Vishwanatha Subbanna | d53de6f | 2017-03-30 11:21:37 +0530 | [diff] [blame] | 60 | # Some LED groups could be empty |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 61 | if not led_dict: |
Vishwanatha Subbanna | d53de6f | 2017-03-30 11:21:37 +0530 | [diff] [blame] | 62 | ofile.write(' }},\n') |
| 63 | continue |
| 64 | |
Vishwanatha Subbanna | e0cf8fe | 2020-03-03 02:38:21 -0600 | [diff] [blame] | 65 | for led_name, list_dict in list(led_dict.items()): |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 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) + '\",') |
Patrick Williams | d27c68f | 2017-05-31 17:12:37 -0500 | [diff] [blame] | 80 | ofile.write('phosphor::led::Layout::' + |
| 81 | str(list_dict.get('Action', 'Off')) + ',') |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 82 | ofile.write(str(list_dict.get('DutyOn', 50)) + ',') |
Patrick Williams | d27c68f | 2017-05-31 17:12:37 -0500 | [diff] [blame] | 83 | ofile.write(str(list_dict.get('Period', 0)) + ',') |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 84 | priority = str(list_dict.get('Priority', 'Blink')) |
| 85 | ofile.write('phosphor::led::Layout::' + priority + ',') |
Vishwanatha Subbanna | 8a50a50 | 2017-01-20 18:42:21 +0530 | [diff] [blame] | 86 | ofile.write('},\n') |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 87 | ofile.write(' }},\n') |
| 88 | ofile.write('};\n') |