yaml: refactor cpp gen, update for group priority

The complete literal was generated in one function.

The cpp gen can be extracted into multiple functions which each
generate the literal for one struct/object.

Which makes things more readable and easier to work on.

Also updated the script to generate the additional field for the group
priority.

Change-Id: I68b747afb26b76228184fe37f393db37d3f4a502
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/scripts/parse_led.py b/scripts/parse_led.py
index 2dacc0e..f6394dc 100755
--- a/scripts/parse_led.py
+++ b/scripts/parse_led.py
@@ -5,6 +5,101 @@
 import yaml
 from inflection import underscore
 
+
+def check_led_priority(led_name, value, priority_dict):
+
+    if led_name in priority_dict:
+        if value != priority_dict[led_name]:
+            # Priority for a particular LED needs to stay SAME
+            # across all groups
+            ofile.close()
+            os.remove(ofile.name)
+            raise ValueError(
+                "Priority for [" + led_name + "] is NOT same across all groups"
+            )
+    else:
+        priority_dict[led_name] = value
+
+    return 0
+
+
+def generate_file_single_led(ifile, led_name, list_dict, priority_dict, ofile):
+
+    value = list_dict.get("Priority")
+
+    check_led_priority(led_name, value, priority_dict)
+
+    action = "phosphor::led::Layout::Action::" + str(
+        list_dict.get("Action", "Off")
+    )
+    dutyOn = str(list_dict.get("DutyOn", 50))
+    period = str(list_dict.get("Period", 0))
+    priority = "phosphor::led::Layout::Action::" + str(
+        list_dict.get("Priority", "Blink")
+    )
+
+    ofile.write('        {"' + underscore(led_name) + '",')
+    ofile.write(action + ",")
+    ofile.write(dutyOn + ",")
+    ofile.write(period + ",")
+    ofile.write(priority + ",")
+
+    ofile.write("},\n")
+
+    return 0
+
+
+def generate_file_single_group(ifile, group, priority_dict, ofile):
+    # This section generates an std::unordered_map of LedGroupNames to
+    # std::set of LEDs containing the name and properties
+    led_dict = ifile[group]
+
+    group_priority = 0
+    has_group_priority = "Priority" in led_dict
+
+    if has_group_priority:
+        group_priority = led_dict["Priority"]
+        # we do not want to enumerate this as a led group
+        del led_dict["Priority"]
+
+    ofile.write(
+        '   {"'
+        + "/xyz/openbmc_project/led/groups/"
+        + underscore(group)
+        + '"'
+        + ",{ "
+        + str(group_priority)
+        + ",\n"
+        + "{\n"
+    )
+
+    for led_name, list_dict in list(led_dict.items()):
+        generate_file_single_led(
+            ifile, led_name, list_dict, priority_dict, ofile
+        )
+
+    ofile.write("   }}},\n")
+
+    return 0
+
+
+def generate_file(ifile, ofile):
+    # Dictionary having [Name:Priority]
+    priority_dict = {}
+
+    ofile.write("/* !!! WARNING: This is a GENERATED Code..")
+    ofile.write("Please do NOT Edit !!! */\n\n")
+
+    ofile.write("static const phosphor::led::GroupMap")
+    ofile.write(" systemLedMap = {\n\n")
+
+    for group in list(ifile.keys()):
+        generate_file_single_group(ifile, group, priority_dict, ofile)
+    ofile.write("};\n")
+
+    return 0
+
+
 if __name__ == "__main__":
     script_dir = os.path.dirname(os.path.realpath(__file__))
     parser = argparse.ArgumentParser()
@@ -41,57 +136,5 @@
     with open(yaml_file, "r") as f:
         ifile = yaml.safe_load(f)
 
-    # Dictionary having [Name:Priority]
-    priority_dict = {}
-
     with open(os.path.join(args.outputdir, "led-gen.hpp"), "w") as ofile:
-        ofile.write("/* !!! WARNING: This is a GENERATED Code..")
-        ofile.write("Please do NOT Edit !!! */\n\n")
-
-        ofile.write("static const phosphor::led::GroupMap")
-        ofile.write(" systemLedMap = {\n\n")
-        for group in list(ifile.keys()):
-            # This section generates an std::unordered_map of LedGroupNames to
-            # std::set of LEDs containing the name and properties
-            led_dict = ifile[group]
-            ofile.write(
-                '   {"'
-                + "/xyz/openbmc_project/led/groups/"
-                + underscore(group)
-                + '",{\n'
-            )
-
-            # Some LED groups could be empty
-            if not led_dict:
-                ofile.write("   }},\n")
-                continue
-
-            for led_name, list_dict in list(led_dict.items()):
-                value = list_dict.get("Priority")
-                if led_name in priority_dict:
-                    if value != priority_dict[led_name]:
-                        # Priority for a particular LED needs to stay SAME
-                        # across all groups
-                        ofile.close()
-                        os.remove(ofile.name)
-                        raise ValueError(
-                            "Priority for ["
-                            + led_name
-                            + "] is NOT same across all groups"
-                        )
-                else:
-                    priority_dict[led_name] = value
-
-                ofile.write('        {"' + underscore(led_name) + '",')
-                ofile.write(
-                    "phosphor::led::Layout::Action::"
-                    + str(list_dict.get("Action", "Off"))
-                    + ","
-                )
-                ofile.write(str(list_dict.get("DutyOn", 50)) + ",")
-                ofile.write(str(list_dict.get("Period", 0)) + ",")
-                priority = str(list_dict.get("Priority", "Blink"))
-                ofile.write("phosphor::led::Layout::Action::" + priority + ",")
-                ofile.write("},\n")
-            ofile.write("   }},\n")
-        ofile.write("};\n")
+        generate_file(ifile, ofile)