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 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 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( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 11 | "-f", "--filename", default="led.yaml", help="Input File Name" |
| 12 | ) |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 13 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 14 | "-i", |
| 15 | "--input-dir", |
| 16 | dest="inputdir", |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 17 | default=script_dir, |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 18 | help="Input directory", |
| 19 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 20 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 21 | "-o", |
| 22 | "--output-dir", |
| 23 | dest="outputdir", |
| 24 | default=".", |
| 25 | help="Output directory.", |
| 26 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 27 | |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 28 | args = parser.parse_args() |
| 29 | |
| 30 | # Default to the one that is in the current. |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 31 | yaml_dir = script_dir |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 32 | yaml_file = os.path.join(yaml_dir, "led.yaml") |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 33 | |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 34 | if args.inputdir: |
| 35 | yaml_dir = args.inputdir |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 36 | |
| 37 | if args.filename: |
| 38 | yaml_file = os.path.join(yaml_dir, args.filename) |
| 39 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 40 | with open(yaml_file, "r") as f: |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 41 | ifile = yaml.safe_load(f) |
| 42 | |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 43 | # Dictionary having [Name:Priority] |
| 44 | priority_dict = {} |
| 45 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 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") |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 49 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 50 | ofile.write("static const std::unordered_map<std::string,") |
| 51 | ofile.write(" std::set<phosphor::led::Layout::LedAction>>") |
| 52 | ofile.write(" systemLedMap = {\n\n") |
Vishwanatha Subbanna | e0cf8fe | 2020-03-03 02:38:21 -0600 | [diff] [blame] | 53 | for group in list(ifile.keys()): |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 54 | # This section generates an std::unordered_map of LedGroupNames to |
| 55 | # std::set of LEDs containing the name and properties |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 56 | led_dict = ifile[group] |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 57 | ofile.write( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 58 | ' {"' |
| 59 | + "/xyz/openbmc_project/led/groups/" |
| 60 | + underscore(group) |
| 61 | + '",{\n' |
| 62 | ) |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 63 | |
Vishwanatha Subbanna | d53de6f | 2017-03-30 11:21:37 +0530 | [diff] [blame] | 64 | # Some LED groups could be empty |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 65 | if not led_dict: |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 66 | ofile.write(" }},\n") |
Vishwanatha Subbanna | d53de6f | 2017-03-30 11:21:37 +0530 | [diff] [blame] | 67 | continue |
| 68 | |
Vishwanatha Subbanna | e0cf8fe | 2020-03-03 02:38:21 -0600 | [diff] [blame] | 69 | for led_name, list_dict in list(led_dict.items()): |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 70 | value = list_dict.get("Priority") |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 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) |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 77 | raise ValueError( |
| 78 | "Priority for [" |
| 79 | + led_name |
| 80 | + "] is NOT same across all groups" |
| 81 | ) |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 82 | else: |
| 83 | priority_dict[led_name] = value |
| 84 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame^] | 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") |