Introduce the json_dump() class method refactoring
authorAlexander Popov <alex.popov@linux.com>
Mon, 14 Feb 2022 21:57:42 +0000 (00:57 +0300)
committerAlexander Popov <alex.popov@linux.com>
Mon, 14 Feb 2022 22:03:18 +0000 (01:03 +0300)
kconfig_hardened_check/__init__.py

index 7db4f5db98815a14e475a61fd7a40831ad897542..d65e2df1301821a14c31d2943ba20af1cf575cb8 100644 (file)
@@ -122,6 +122,12 @@ class KconfigCheck(OptCheck):
     def type(self):
         return 'kconfig'
 
+    def json_dump(self, with_results):
+        dump = [self.name, self.type, self.expected, self.decision, self.reason]
+        if with_results:
+            dump.append(self.result)
+        return dump
+
 
 class VersionCheck:
     def __init__(self, ver_expected):
@@ -221,6 +227,12 @@ class ComplexOptCheck:
             if with_results:
                 print('| {}'.format(self.result), end='')
 
+    def json_dump(self, with_results):
+        dump = self.opts[0].json_dump(False)
+        if with_results:
+            dump.append(self.result)
+        return dump
+
 
 class OR(ComplexOptCheck):
     # self.opts[0] is the option that this OR-check is about.
@@ -645,13 +657,10 @@ def print_unknown_options(checklist, parsed_options):
 
 def print_checklist(mode, checklist, with_results):
     if mode == 'json':
-        opts = []
+        output = []
         for o in checklist:
-            opt = [o.name, o.type, o.expected, o.decision, o.reason]
-            if with_results:
-                opt.append(o.result)
-            opts.append(opt)
-        print(json.dumps(opts))
+            output.append(o.json_dump(with_results))
+        print(json.dumps(output))
         return
 
     # table header