Show a patch.

GET /api/patches/17874/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 17874,
    "url": "https://patchwork.libcamera.org/api/patches/17874/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/17874/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20221124113550.2182342-9-paul.elder@ideasonboard.com>",
    "date": "2022-11-24T11:35:46",
    "name": "[libcamera-devel,v4,08/12] utils: libtuning: generators: Add raspberrypi output",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "9cb65a3076a880e2eb371aba90c42a4d7e4cc7a9",
    "submitter": {
        "id": 17,
        "url": "https://patchwork.libcamera.org/api/people/17/?format=api",
        "name": "Paul Elder",
        "email": "paul.elder@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/17874/mbox/",
    "series": [
        {
            "id": 3636,
            "url": "https://patchwork.libcamera.org/api/series/3636/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3636",
            "date": "2022-11-24T11:35:38",
            "name": "utils: tuning: Add a new tuning infrastructure",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/3636/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/17874/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/17874/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 0F4CFC3286\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 24 Nov 2022 11:36:18 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B888C63329;\n\tThu, 24 Nov 2022 12:36:17 +0100 (CET)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AE01863319\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 24 Nov 2022 12:36:16 +0100 (CET)",
            "from pyrite.tail37cf.ts.net (h175-177-042-159.catv02.itscom.jp\n\t[175.177.42.159])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 398A0496;\n\tThu, 24 Nov 2022 12:36:14 +0100 (CET)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1669289777;\n\tbh=Y7nEBUYH5b4W9avHl2TdJIL1TOaNPAQgLWaKihtoTmY=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=ab5gvRuu45GvV68r4vHXw/bYUJM184WeH6DYkbctkdqRFeNfmuqiUlSsNucl8peyc\n\tKqQTlZfB/DgqGOpMC6+W5G1VliZfVaEyJfMT9F1iigRDJdx9NstA376JJqGwBV3My7\n\tV8BaMCmNN2pI658krDBLzYJiy0mAMHvQoK9tzDHhUNs6h6qV6m8UkeScW+pFDwqAuh\n\tmNLwIRePegeyPnZuP7jANzMz3/tFoCrFBX03CHEERcBX/H3GyRiKrPQq3Z647wQI/G\n\t2GRWYi03qSUTJl323CmAIYo+SATwE8shb8h+0Xf7IG54Yw4ci/gpPaYQiG/Qw2AWPL\n\tZ8RDU/mrANbBg==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1669289776;\n\tbh=Y7nEBUYH5b4W9avHl2TdJIL1TOaNPAQgLWaKihtoTmY=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=iVeUAOGYqEzve9MYV9jz+FEfzNRTaphfKy7x476Ustsv9jgOvruQHry4Cnamo5FwS\n\tLC+Xu9mr8gnLpVDqjs8pYTyYeg1Rd+0qb5jrQxS5nEZml2a0q5akqvDEAJcJkv6I+y\n\tK1PjgUzNoIh9nG9IwDz3jZWnXguankTPSSC5s0tw="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"iVeUAOGY\"; dkim-atps=neutral",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 24 Nov 2022 20:35:46 +0900",
        "Message-Id": "<20221124113550.2182342-9-paul.elder@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.35.1",
        "In-Reply-To": "<20221124113550.2182342-1-paul.elder@ideasonboard.com>",
        "References": "<20221124113550.2182342-1-paul.elder@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v4 08/12] utils: libtuning: generators:\n\tAdd raspberrypi output",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "From": "Paul Elder via libcamera-devel <libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Paul Elder <paul.elder@ideasonboard.com>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "Add a generator to libtuning for writing tuning output to a json file\nformatted the same way that raspberrypi's ctt formats them.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n---\nChanges in v4:\n- move adding the license to generators __init__.py to the appropriate\n  patch earlier in the series\n\nChanges in v3:\n- move pretty_print into a private class member\n- move RaspberryPiOutput to end of file\n- add file descriptions\n- remove indirection from fake polymorphism\n\nChanges in v2:\n- add SPDX and copyright\n- fix style\n- move the 'rpi.' prefix of module names in the tuning output from here\n  to into the Modules' out_name field\n---\n utils/tuning/libtuning/generators/__init__.py |   2 +\n .../generators/raspberrypi_output.py          | 114 ++++++++++++++++++\n 2 files changed, 116 insertions(+)\n create mode 100644 utils/tuning/libtuning/generators/raspberrypi_output.py",
    "diff": "diff --git a/utils/tuning/libtuning/generators/__init__.py b/utils/tuning/libtuning/generators/__init__.py\nindex 9ccabb0e..937aff30 100644\n--- a/utils/tuning/libtuning/generators/__init__.py\n+++ b/utils/tuning/libtuning/generators/__init__.py\n@@ -1,3 +1,5 @@\n # SPDX-License-Identifier: GPL-2.0-or-later\n #\n # Copyright (C) 2022, Paul Elder <paul.elder@ideasonboard.com>\n+\n+from libtuning.generators.raspberrypi_output import RaspberryPiOutput\ndiff --git a/utils/tuning/libtuning/generators/raspberrypi_output.py b/utils/tuning/libtuning/generators/raspberrypi_output.py\nnew file mode 100644\nindex 00000000..813491cd\n--- /dev/null\n+++ b/utils/tuning/libtuning/generators/raspberrypi_output.py\n@@ -0,0 +1,114 @@\n+# SPDX-License-Identifier: BSD-2-Clause\n+#\n+# Copyright 2022 Raspberry Pi Ltd\n+#\n+# raspberrypi_output.py - Generate tuning file in Raspberry Pi's json format\n+#\n+# (Copied from ctt_pretty_print_json.py)\n+\n+from .generator import Generator\n+\n+import json\n+from pathlib import Path\n+import textwrap\n+\n+\n+class Encoder(json.JSONEncoder):\n+\n+    def __init__(self, *args, **kwargs):\n+        super().__init__(*args, **kwargs)\n+        self.indentation_level = 0\n+        self.hard_break = 120\n+        self.custom_elems = {\n+            'table': 16,\n+            'luminance_lut': 16,\n+            'ct_curve': 3,\n+            'ccm': 3,\n+            'gamma_curve': 2,\n+            'y_target': 2,\n+            'prior': 2\n+        }\n+\n+    def encode(self, o, node_key=None):\n+        if isinstance(o, (list, tuple)):\n+            # Check if we are a flat list of numbers.\n+            if not any(isinstance(el, (list, tuple, dict)) for el in o):\n+                s = ', '.join(json.dumps(el) for el in o)\n+                if node_key in self.custom_elems.keys():\n+                    # Special case handling to specify number of elements in a row for tables, ccm, etc.\n+                    self.indentation_level += 1\n+                    sl = s.split(', ')\n+                    num = self.custom_elems[node_key]\n+                    chunk = [self.indent_str + ', '.join(sl[x:x + num]) for x in range(0, len(sl), num)]\n+                    t = ',\\n'.join(chunk)\n+                    self.indentation_level -= 1\n+                    output = f'\\n{self.indent_str}[\\n{t}\\n{self.indent_str}]'\n+                elif len(s) > self.hard_break - len(self.indent_str):\n+                    # Break a long list with wraps.\n+                    self.indentation_level += 1\n+                    t = textwrap.fill(s, self.hard_break, break_long_words=False,\n+                                      initial_indent=self.indent_str, subsequent_indent=self.indent_str)\n+                    self.indentation_level -= 1\n+                    output = f'\\n{self.indent_str}[\\n{t}\\n{self.indent_str}]'\n+                else:\n+                    # Smaller lists can remain on a single line.\n+                    output = f' [ {s} ]'\n+                return output\n+            else:\n+                # Sub-structures in the list case.\n+                self.indentation_level += 1\n+                output = [self.indent_str + self.encode(el) for el in o]\n+                self.indentation_level -= 1\n+                output = ',\\n'.join(output)\n+                return f' [\\n{output}\\n{self.indent_str}]'\n+\n+        elif isinstance(o, dict):\n+            self.indentation_level += 1\n+            output = []\n+            for k, v in o.items():\n+                if isinstance(v, dict) and len(v) == 0:\n+                    # Empty config block special case.\n+                    output.append(self.indent_str + f'{json.dumps(k)}: {{ }}')\n+                else:\n+                    # Only linebreak if the next node is a config block.\n+                    sep = f'\\n{self.indent_str}' if isinstance(v, dict) else ''\n+                    output.append(self.indent_str + f'{json.dumps(k)}:{sep}{self.encode(v, k)}')\n+            output = ',\\n'.join(output)\n+            self.indentation_level -= 1\n+            return f'{{\\n{output}\\n{self.indent_str}}}'\n+\n+        else:\n+            return ' ' + json.dumps(o)\n+\n+    @property\n+    def indent_str(self) -> str:\n+        return ' ' * self.indentation_level * self.indent\n+\n+    def iterencode(self, o, **kwargs):\n+        return self.encode(o)\n+\n+\n+class RaspberryPiOutput(Generator):\n+    def __init__(self):\n+        super().__init__()\n+\n+    def _pretty_print(self, in_json: dict) -> str:\n+\n+        if 'version' not in in_json or \\\n+           'target' not in in_json or \\\n+           'algorithms' not in in_json or \\\n+           in_json['version'] < 2.0:\n+            raise RuntimeError('Incompatible JSON dictionary has been provided')\n+\n+        return json.dumps(in_json, cls=Encoder, indent=4, sort_keys=False)\n+\n+    def write(self, output_file: Path, output_dict: dict, output_order: list):\n+        # Write json dictionary to file using ctt's version 2 format\n+        out_json = {\n+            \"version\": 2.0,\n+            'target': 'bcm2835',\n+            \"algorithms\": [{f'{module.out_name}': output_dict[module]} for module in output_order]\n+        }\n+\n+        with open(output_file, 'w') as f:\n+            f.write(self._pretty_print(out_json))\n",
    "prefixes": [
        "libcamera-devel",
        "v4",
        "08/12"
    ]
}