Vishwanatha Subbanna | 4f43f5e | 2020-03-09 03:56:57 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 2 | import argparse |
Patrick Williams | 6d254ee | 2022-12-06 10:52:53 -0600 | [diff] [blame] | 3 | import os |
| 4 | |
| 5 | import yaml |
Vishwanatha Subbanna | 32bb6f8 | 2017-03-27 18:15:52 +0530 | [diff] [blame] | 6 | from inflection import underscore |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 7 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 8 | if __name__ == "__main__": |
Vishwanatha Subbanna | bb8fe0b | 2016-11-12 18:29:38 +0530 | [diff] [blame] | 9 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 10 | parser = argparse.ArgumentParser() |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 11 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 12 | "-f", "--filename", default="led.yaml", help="Input File Name" |
| 13 | ) |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 14 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 15 | "-i", |
| 16 | "--input-dir", |
| 17 | dest="inputdir", |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 18 | default=script_dir, |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 19 | help="Input directory", |
| 20 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 21 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 22 | "-o", |
| 23 | "--output-dir", |
| 24 | dest="outputdir", |
| 25 | default=".", |
| 26 | help="Output directory.", |
| 27 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 28 | |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 29 | args = parser.parse_args() |
| 30 | |
| 31 | # Default to the one that is in the current. |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 32 | yaml_dir = script_dir |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 33 | yaml_file = os.path.join(yaml_dir, "led.yaml") |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 34 | |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 35 | if args.inputdir: |
| 36 | yaml_dir = args.inputdir |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 37 | |
| 38 | if args.filename: |
| 39 | yaml_file = os.path.join(yaml_dir, args.filename) |
| 40 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 41 | with open(yaml_file, "r") as f: |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 42 | ifile = yaml.safe_load(f) |
| 43 | |
Vishwanatha Subbanna | 1da8eb7 | 2017-06-15 13:46:05 +0530 | [diff] [blame] | 44 | # Dictionary having [Name:Priority] |
| 45 | priority_dict = {} |
| 46 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 47 | with open(os.path.join(args.outputdir, "led-gen.hpp"), "w") as ofile: |
| 48 | ofile.write("/* !!! WARNING: This is a GENERATED Code..") |
| 49 | ofile.write("Please do NOT Edit !!! */\n\n") |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 50 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 51 | ofile.write("static const phosphor::led::GroupMap") |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 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") |