Skip to contentSkip to conversation

utils/checkpackagelib: CommentsMenusPackagesOrder: append elements to arrays if needed

PATCH New Jerzy Grzegorek 1 participant 1 message Jul 19, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 utils/checkpackagelib: CommentsMenusPackagesOrder: append elements to arrays if needed

In the future max value of level variable (index of arrays) may increase, so we have to append elements to arrays if needed.

Also change order of variables.

---
Changes v1 -> v2:
- use try/except statement instead of if one (Yann)

At the beginning self.state is empty and variable level (index of arrays) is equal 0. We append elements to arrays for the first time when a line begining with "comment ", "if " or "menu ". At that moment level is equal 1 and each array should have two elements (index 0 and 1). To achieve this we can use empty arrays or arrays with an initial value (index 0).

1. Arrays with an initial value and if statement (previous version)
...
if level > len(array) - 1
append elements
...

2. Empty arrays and while statement
...
while level > len(array) - 1
append elements
...

3. Arrays with an initial value and try/except statement (this version)
...
try:
assigment staff
except IndexError:
append elements
...

---
 utils/checkpackagelib/lib_config.py | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
utils/checkpackagelib/lib_config.py +12 -6
index f0edb9993d..eabb882483 100644
@@ -61,9 +61,9 @@ class AttributesOrder(_CheckFunction):
 
 
 class CommentsMenusPackagesOrder(_CheckFunction):
-    print_package_warning = [True, True, True, True, True, True]
-    menu_of_packages = ["", "", "", "", "", ""]
-    package = ["", "", "", "", "", ""]
+    menu_of_packages = [""]
+    package = [""]
+    print_package_warning = [True]
 
     def before(self):
         self.state = ""
@@ -87,9 +87,15 @@ class CommentsMenusPackagesOrder(_CheckFunction):
                     self.state += "-menu"
 
             level = self.get_level()
-            self.package[level] = ""
-            self.print_package_warning[level] = True
-            self.menu_of_packages[level] = text[:-1]
+
+            try:
+                self.menu_of_packages[level] = text[:-1]
+                self.package[level] = ""
+                self.print_package_warning[level] = True
+            except IndexError:
+                self.menu_of_packages.append(text[:-1])
+                self.package.append("")
+                self.print_package_warning.append(True)
 
         elif text.startswith("endif") or text.startswith("endmenu"):
             if self.state.endswith("comment"):
Signature · 1 line
2.17.1