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 | |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame^] | 8 | |
| 9 | def check_led_priority(led_name, value, priority_dict): |
| 10 | |
| 11 | if led_name in priority_dict: |
| 12 | if value != priority_dict[led_name]: |
| 13 | # Priority for a particular LED needs to stay SAME |
| 14 | # across all groups |
| 15 | ofile.close() |
| 16 | os.remove(ofile.name) |
| 17 | raise ValueError( |
| 18 | "Priority for [" + led_name + "] is NOT same across all groups" |
| 19 | ) |
| 20 | else: |
| 21 | priority_dict[led_name] = value |
| 22 | |
| 23 | return 0 |
| 24 | |
| 25 | |
| 26 | def generate_file_single_led(ifile, led_name, list_dict, priority_dict, ofile): |
| 27 | |
| 28 | value = list_dict.get("Priority") |
| 29 | |
| 30 | check_led_priority(led_name, value, priority_dict) |
| 31 | |
| 32 | action = "phosphor::led::Layout::Action::" + str( |
| 33 | list_dict.get("Action", "Off") |
| 34 | ) |
| 35 | dutyOn = str(list_dict.get("DutyOn", 50)) |
| 36 | period = str(list_dict.get("Period", 0)) |
| 37 | priority = "phosphor::led::Layout::Action::" + str( |
| 38 | list_dict.get("Priority", "Blink") |
| 39 | ) |
| 40 | |
| 41 | ofile.write(' {"' + underscore(led_name) + '",') |
| 42 | ofile.write(action + ",") |
| 43 | ofile.write(dutyOn + ",") |
| 44 | ofile.write(period + ",") |
| 45 | ofile.write(priority + ",") |
| 46 | |
| 47 | ofile.write("},\n") |
| 48 | |
| 49 | return 0 |
| 50 | |
| 51 | |
| 52 | def generate_file_single_group(ifile, group, priority_dict, ofile): |
| 53 | # This section generates an std::unordered_map of LedGroupNames to |
| 54 | # std::set of LEDs containing the name and properties |
| 55 | led_dict = ifile[group] |
| 56 | |
| 57 | group_priority = 0 |
| 58 | has_group_priority = "Priority" in led_dict |
| 59 | |
| 60 | if has_group_priority: |
| 61 | group_priority = led_dict["Priority"] |
| 62 | # we do not want to enumerate this as a led group |
| 63 | del led_dict["Priority"] |
| 64 | |
| 65 | ofile.write( |
| 66 | ' {"' |
| 67 | + "/xyz/openbmc_project/led/groups/" |
| 68 | + underscore(group) |
| 69 | + '"' |
| 70 | + ",{ " |
| 71 | + str(group_priority) |
| 72 | + ",\n" |
| 73 | + "{\n" |
| 74 | ) |
| 75 | |
| 76 | for led_name, list_dict in list(led_dict.items()): |
| 77 | generate_file_single_led( |
| 78 | ifile, led_name, list_dict, priority_dict, ofile |
| 79 | ) |
| 80 | |
| 81 | ofile.write(" }}},\n") |
| 82 | |
| 83 | return 0 |
| 84 | |
| 85 | |
| 86 | def generate_file(ifile, ofile): |
| 87 | # Dictionary having [Name:Priority] |
| 88 | priority_dict = {} |
| 89 | |
| 90 | ofile.write("/* !!! WARNING: This is a GENERATED Code..") |
| 91 | ofile.write("Please do NOT Edit !!! */\n\n") |
| 92 | |
| 93 | ofile.write("static const phosphor::led::GroupMap") |
| 94 | ofile.write(" systemLedMap = {\n\n") |
| 95 | |
| 96 | for group in list(ifile.keys()): |
| 97 | generate_file_single_group(ifile, group, priority_dict, ofile) |
| 98 | ofile.write("};\n") |
| 99 | |
| 100 | return 0 |
| 101 | |
| 102 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 103 | if __name__ == "__main__": |
Vishwanatha Subbanna | bb8fe0b | 2016-11-12 18:29:38 +0530 | [diff] [blame] | 104 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 105 | parser = argparse.ArgumentParser() |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 106 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 107 | "-f", "--filename", default="led.yaml", help="Input File Name" |
| 108 | ) |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 109 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 110 | "-i", |
| 111 | "--input-dir", |
| 112 | dest="inputdir", |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 113 | default=script_dir, |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 114 | help="Input directory", |
| 115 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 116 | parser.add_argument( |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 117 | "-o", |
| 118 | "--output-dir", |
| 119 | dest="outputdir", |
| 120 | default=".", |
| 121 | help="Output directory.", |
| 122 | ) |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 123 | |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 124 | args = parser.parse_args() |
| 125 | |
| 126 | # Default to the one that is in the current. |
Brad Bishop | 9a6220c | 2017-02-08 13:21:31 -0500 | [diff] [blame] | 127 | yaml_dir = script_dir |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 128 | yaml_file = os.path.join(yaml_dir, "led.yaml") |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 129 | |
Brad Bishop | 513f58e | 2017-02-08 13:25:17 -0500 | [diff] [blame] | 130 | if args.inputdir: |
| 131 | yaml_dir = args.inputdir |
Vishwanatha Subbanna | ccbdf67 | 2017-01-23 14:39:06 +0530 | [diff] [blame] | 132 | |
| 133 | if args.filename: |
| 134 | yaml_file = os.path.join(yaml_dir, args.filename) |
| 135 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 136 | with open(yaml_file, "r") as f: |
Vishwanatha Subbanna | b21fda7 | 2016-10-17 17:46:37 +0530 | [diff] [blame] | 137 | ifile = yaml.safe_load(f) |
| 138 | |
Patrick Williams | fccfac9 | 2022-03-17 05:14:46 -0500 | [diff] [blame] | 139 | with open(os.path.join(args.outputdir, "led-gen.hpp"), "w") as ofile: |
Alexander Hansen | 5a9f651 | 2024-07-30 15:20:49 +0200 | [diff] [blame^] | 140 | generate_file(ifile, ofile) |