1#!/usr/bin/env python3 2import argparse 3import os 4 5import yaml 6from inflection import underscore 7 8 9def 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 26def 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 52def 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 86def 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 103if __name__ == "__main__": 104 script_dir = os.path.dirname(os.path.realpath(__file__)) 105 parser = argparse.ArgumentParser() 106 parser.add_argument( 107 "-f", "--filename", default="led.yaml", help="Input File Name" 108 ) 109 parser.add_argument( 110 "-l", 111 "--output-filename", 112 dest="outputfilename", 113 default="led-gen.hpp", 114 help="Output File Name", 115 ) 116 parser.add_argument( 117 "-i", 118 "--input-dir", 119 dest="inputdir", 120 default=script_dir, 121 help="Input directory", 122 ) 123 parser.add_argument( 124 "-o", 125 "--output-dir", 126 dest="outputdir", 127 default=".", 128 help="Output directory.", 129 ) 130 131 args = parser.parse_args() 132 133 # Default to the one that is in the current. 134 yaml_dir = script_dir 135 yaml_file = os.path.join(yaml_dir, "led.yaml") 136 137 if args.inputdir: 138 yaml_dir = args.inputdir 139 140 if args.filename: 141 yaml_file = os.path.join(yaml_dir, args.filename) 142 143 with open(yaml_file, "r") as f: 144 ifile = yaml.safe_load(f) 145 146 with open(os.path.join(args.outputdir, args.outputfilename), "w") as ofile: 147 generate_file(ifile, ofile) 148