Show a patch.

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

{
    "id": 16822,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/16822/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/16822/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20220727023816.30008-11-laurent.pinchart@ideasonboard.com>",
    "date": "2022-07-27T02:38:12",
    "name": "[libcamera-devel,v7,10/14] utils: raspberrypi: ctt: Output version 2.0 format tuning files",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "6e3a11c5c0b4ef615a443ee467e5201357515292",
    "submitter": {
        "id": 2,
        "url": "https://patchwork.libcamera.org/api/1.1/people/2/?format=api",
        "name": "Laurent Pinchart",
        "email": "laurent.pinchart@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/16822/mbox/",
    "series": [
        {
            "id": 3331,
            "url": "https://patchwork.libcamera.org/api/1.1/series/3331/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3331",
            "date": "2022-07-27T02:38:02",
            "name": "Replace boost JSON parser with libyaml in Raspberry Pi IPA",
            "version": 7,
            "mbox": "https://patchwork.libcamera.org/series/3331/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/16822/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/16822/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 DBC1CBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 27 Jul 2022 02:38:34 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3FBE56332A;\n\tWed, 27 Jul 2022 04:38:34 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D91F263315\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Jul 2022 04:38:29 +0200 (CEST)",
            "from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 74F88835;\n\tWed, 27 Jul 2022 04:38:29 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1658889514;\n\tbh=J17VEa9ido8On/0VbfT25SuSYSoEfdZ+cSN4prEujs4=;\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=N4YQLPOXLY4BGH3qibt6EnXIFjXoIJ4RHxa1adovnymT+Gwj7KoWwPcZdivJHVbQI\n\t/EH1kQXivKTq5K+DGTUSvJz2U6VvzX2Ylj+eyng8ztQjXOBcVxJ9BphpkWYGsCRBew\n\tEJALW3OSrkvzyDTWTYJnvDwvX9PQIjPQl+y7m4kgnvqyKa6kAVHylyFCHBIr5lKftS\n\ta388o1oPpcKRuJFRyvwec6CtJDar3xF5waNLYcNlswR0QmI5HgohEI66Ebg8IM5BQS\n\tKEsooFv2N8IcK0Cx3/HGdgnT51nRih2hBGYuyvCd4nXHfYx+w1LCivcMou6JHzDpfG\n\tRk5xTZ7PBn9gQ==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1658889509;\n\tbh=J17VEa9ido8On/0VbfT25SuSYSoEfdZ+cSN4prEujs4=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=UcWBPjFTJ0YW0zZDi4gelsIlVOBuXM+55JMvq2Ltvvw4p4tAIDlAfVcvf9aonthW/\n\tiPw4caM+3+fQmYjrppyQTh2Pnh5IWOjIR7Bba/V7xhEqRRAdVWOjkGXiqZQoNXRc7F\n\toiZcmBgoWvOtq1PaXlBGcg9HVRPVik9/WJ0SwQFU="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"UcWBPjFT\"; dkim-atps=neutral",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Wed, 27 Jul 2022 05:38:12 +0300",
        "Message-Id": "<20220727023816.30008-11-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.35.1",
        "In-Reply-To": "<20220727023816.30008-1-laurent.pinchart@ideasonboard.com>",
        "References": "<20220727023816.30008-1-laurent.pinchart@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v7 10/14] utils: raspberrypi: ctt: Output\n\tversion 2.0 format tuning files",
        "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": "Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "From: Naushir Patuck <naush@raspberrypi.com>\n\nUpdate the ctt_pretty_print_json.py script to generate the new version 2.0\nformat camera tuning file. This script can be called through the command line\nto prettify an existing JSON file, or programatically by the CTT to format a\nnew JSON config dictionary.\n\nUpdate the CTT to produce a version 2.0 format json structure and use\nctt_pretty_print_json.pretty_print to prettify the output.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n utils/raspberrypi/ctt/ctt.py                  |  18 +-\n .../raspberrypi/ctt/ctt_pretty_print_json.py  | 188 +++++++++---------\n 2 files changed, 110 insertions(+), 96 deletions(-)\n mode change 100644 => 100755 utils/raspberrypi/ctt/ctt_pretty_print_json.py",
    "diff": "diff --git a/utils/raspberrypi/ctt/ctt.py b/utils/raspberrypi/ctt/ctt.py\nindex 13765b5d52f6..df98db80ad4f 100755\n--- a/utils/raspberrypi/ctt/ctt.py\n+++ b/utils/raspberrypi/ctt/ctt.py\n@@ -15,7 +15,7 @@ from ctt_alsc import *\n from ctt_lux import *\n from ctt_noise import *\n from ctt_geq import *\n-from ctt_pretty_print_json import *\n+from ctt_pretty_print_json import pretty_print\n import random\n import json\n import re\n@@ -511,13 +511,17 @@ class Camera:\n     \"\"\"\n     def write_json(self):\n         \"\"\"\n-        Write json dictionary to file\n+        Write json dictionary to file using our version 2 format\n         \"\"\"\n-        jstring = json.dumps(self.json, sort_keys=False)\n-        \"\"\"\n-        make it pretty :)\n-        \"\"\"\n-        pretty_print_json(jstring, self.jf)\n+\n+        out_json = {\n+            \"version\": 2.0,\n+            'target': 'bcm2835',\n+            \"algorithms\": [{name: data} for name, data in self.json.items()],\n+        }\n+\n+        with open(self.jf, 'w') as f:\n+            f.write(pretty_print(out_json))\n \n     \"\"\"\n     add a new section to the log file\ndiff --git a/utils/raspberrypi/ctt/ctt_pretty_print_json.py b/utils/raspberrypi/ctt/ctt_pretty_print_json.py\nold mode 100644\nnew mode 100755\nindex 0176aec63028..3e3b84752907\n--- a/utils/raspberrypi/ctt/ctt_pretty_print_json.py\n+++ b/utils/raspberrypi/ctt/ctt_pretty_print_json.py\n@@ -1,106 +1,116 @@\n+#!/usr/bin/env python3\n+#\n # SPDX-License-Identifier: BSD-2-Clause\n #\n-# Copyright (C) 2019, Raspberry Pi Ltd\n+# Copyright 2022 Raspberry Pi Ltd\n #\n-# ctt_pretty_print_json.py - camera tuning tool JSON formatter\n+# Script to pretty print a Raspberry Pi tuning config JSON structure in\n+# version 2.0 and later formats.\n \n-import sys\n+import argparse\n+import json\n+import textwrap\n \n \n-class JSONPrettyPrinter(object):\n-    \"\"\"\n-    Take a collapsed JSON file and make it more readable\n-    \"\"\"\n-    def __init__(self, fout):\n-        self.state = {\n-            \"indent\": 0,\n-            \"inarray\": [False],\n-            \"arraycount\": [],\n-            \"skipnewline\": True,\n-            \"need_indent\": False,\n-            \"need_space\": False,\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-        self.fout = fout\n-\n-    def newline(self):\n-        if not self.state[\"skipnewline\"]:\n-            self.fout.write('\\n')\n-            self.state[\"need_indent\"] = True\n-            self.state[\"need_space\"] = False\n-        self.state[\"skipnewline\"] = True\n-\n-    def write(self, c):\n-        if self.state[\"need_indent\"]:\n-            self.fout.write(' ' * self.state[\"indent\"] * 4)\n-            self.state[\"need_indent\"] = False\n-        if self.state[\"need_space\"]:\n-            self.fout.write(' ')\n-            self.state[\"need_space\"] = False\n-        self.fout.write(c)\n-        self.state[\"skipnewline\"] = False\n-\n-    def process_char(self, c):\n-        if c == '{':\n-            self.newline()\n-            self.write(c)\n-            self.state[\"indent\"] += 1\n-            self.newline()\n-        elif c == '}':\n-            self.state[\"indent\"] -= 1\n-            self.newline()\n-            self.write(c)\n-        elif c == '[':\n-            self.newline()\n-            self.write(c)\n-            self.state[\"indent\"] += 1\n-            self.newline()\n-            self.state[\"inarray\"] = [True] + self.state[\"inarray\"]\n-            self.state[\"arraycount\"] = [0] + self.state[\"arraycount\"]\n-        elif c == ']':\n-            self.state[\"indent\"] -= 1\n-            self.newline()\n-            self.state[\"inarray\"].pop(0)\n-            self.state[\"arraycount\"].pop(0)\n-            self.write(c)\n-        elif c == ':':\n-            self.write(c)\n-            self.state[\"need_space\"] = True\n-        elif c == ',':\n-            if not self.state[\"inarray\"][0]:\n-                self.write(c)\n-                self.newline()\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-                self.write(c)\n-                self.state[\"arraycount\"][0] += 1\n-                if self.state[\"arraycount\"][0] == 16:\n-                    self.state[\"arraycount\"][0] = 0\n-                    self.newline()\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-                    self.state[\"need_space\"] = True\n-        elif c.isspace():\n-            pass\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-            self.write(c)\n+            return ' ' + json.dumps(o)\n \n-    def print(self, string):\n-        for c in string:\n-            self.process_char(c)\n-        self.newline()\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-def pretty_print_json(str_in, output_filename):\n-    with open(output_filename, \"w\") as fout:\n-        printer = JSONPrettyPrinter(fout)\n-        printer.print(str_in)\n \n+def pretty_print(in_json: dict) -> str:\n \n-if __name__ == '__main__':\n-    if len(sys.argv) != 2:\n-        print(\"Usage: %s filename\" % sys.argv[0])\n-        sys.exit(1)\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-    input_filename = sys.argv[1]\n-    with open(input_filename, \"r\") as fin:\n-        printer = JSONPrettyPrinter(sys.stdout)\n-        printer.print(fin.read())\n+    return json.dumps(in_json, cls=Encoder, indent=4, sort_keys=False)\n+\n+\n+if __name__ == \"__main__\":\n+    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=\n+                    'Prettify a version 2.0 camera tuning config JSON file.')\n+    parser.add_argument('input', type=str, help='Input tuning file.')\n+    parser.add_argument('output', type=str, nargs='?',\n+                        help='Output converted tuning file. If not provided, the input file will be updated in-place.',\n+                        default=None)\n+    args = parser.parse_args()\n+\n+    with open(args.input, 'r') as f:\n+        in_json = json.load(f)\n+\n+    out_json = pretty_print(in_json)\n+\n+    with open(args.output if args.output is not None else args.input, 'w') as f:\n+        f.write(out_json)\n",
    "prefixes": [
        "libcamera-devel",
        "v7",
        "10/14"
    ]
}