[libcamera-devel,3/7] utils: libtuning: parsers: Add raspberrypi parser
diff mbox series

Message ID 20221006120105.3861831-4-paul.elder@ideasonboard.com
State Superseded
Headers show
Series
  • utils: tuning: Add a new tuning infrastructure
Related show

Commit Message

Paul Elder Oct. 6, 2022, 12:01 p.m. UTC
Add a parser to libtuning for parsing configuration files that are the
same format as raspberrypi's ctt's configuration files.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 utils/tuning/libtuning/parsers/__init__.py    |  1 +
 .../libtuning/parsers/raspberrypi_parser.py   | 82 +++++++++++++++++++
 2 files changed, 83 insertions(+)
 create mode 100644 utils/tuning/libtuning/parsers/raspberrypi_parser.py

Patch
diff mbox series

diff --git a/utils/tuning/libtuning/parsers/__init__.py b/utils/tuning/libtuning/parsers/__init__.py
index e69de29b..ba829161 100644
--- a/utils/tuning/libtuning/parsers/__init__.py
+++ b/utils/tuning/libtuning/parsers/__init__.py
@@ -0,0 +1 @@ 
+from libtuning.parsers.raspberrypi_parser import RaspberryPiParser
diff --git a/utils/tuning/libtuning/parsers/raspberrypi_parser.py b/utils/tuning/libtuning/parsers/raspberrypi_parser.py
new file mode 100644
index 00000000..1a7d4c0a
--- /dev/null
+++ b/utils/tuning/libtuning/parsers/raspberrypi_parser.py
@@ -0,0 +1,82 @@ 
+from .parser import Parser
+
+import json
+import numbers
+
+import libtuning.utils as utils
+
+
+class RaspberryPiParser(Parser):
+    def __init__(self):
+        super().__init__()
+
+    # The string in the 'disable' and 'plot' lists are formatted as
+    # 'rpi.{module_name}'.
+    # @brief Enumerate, as a module, @a listt if its value exists in @a dictt
+    #        and it is the name of a valid module in @a modules
+    def enumerate_rpi_modules(listt, dictt, modules):
+        for x in listt:
+            name = x.replace('rpi.', '')
+            if name not in dictt:
+                continue
+            module = utils.getModuleByName(modules, name)
+            if module is not None:
+                yield module
+
+    def _validMacbethOption(value):
+        if not isinstance(value, dict):
+            return False
+
+        if list(value.keys()) != ['small', 'show']:
+            return False
+
+        for val in value.values():
+            if not isinstance(val, numbers.Number):
+                return False
+
+        return True
+
+    def __parse__(self, config_file, modules):
+        with open(config_file, 'r') as config_json:
+            config = json.load(config_json)
+
+        disable = []
+        for module in enumerate_rpi_modules(config['disable'], config, modules):
+            disable.append(module)
+            # Remove the disabled module's config too
+            config.pop(module.name)
+        config.pop('disable')
+
+        # The raspberrypi config format has 'plot' map to a list of module
+        # names which should be plotted.  libtuning has each module contain the
+        # plot information in itself so do this conversion.
+
+        for module in enumerate_rpi_modules(config['plot'], config, modules):
+            # It's fine to set the value of a potentially disabled module, as
+            # the object still exists at this point
+            module.appendValue('debug', 'plot')
+        config.pop('plot')
+
+        # Convert the keys from module name to module instance
+
+        for module_name in config:
+            module = utils.getModuleByName(modules, module_name)
+            if module is not None and \
+               module.validateConfig(config[module_name]):
+                new_config[module] = config.pop(module_name)
+
+        new_config['general'] = {}
+
+        if 'blacklevel' in config:
+            if not isinstance(config['blacklevel'], numbers.Number):
+                raise TypeError('Config "blacklevel" must be a number')
+            new_config['general']['blacklevel'] = config['blacklevel']
+
+        if 'macbeth' in config:
+            if not _validMacbethOption(config['macbeth']):
+                raise TypeError('Config "macbeth" must be a dict: {"small": number, "show": number}')
+            new_config['general']['macbeth'] = config['macbeth']
+        else:
+            new_config['general']['macbeth'] = {'small': 0, 'show': 0}
+
+        return new_config, disable