{"id":8571,"url":"https://patchwork.libcamera.org/api/patches/8571/?format=json","web_url":"https://patchwork.libcamera.org/patch/8571/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200703001422.24324-3-laurent.pinchart@ideasonboard.com>","date":"2020-07-03T00:14:14","name":"[libcamera-devel,02/10] utils: raspberrypi: ctt: json_pretty_print: Turn printer into a class","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"2037a36e0a87cb55a75f4ebbd25ee9fc5b41305c","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/8571/mbox/","series":[{"id":1076,"url":"https://patchwork.libcamera.org/api/series/1076/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1076","date":"2020-07-03T00:14:12","name":"utils: raspberrypi: ctt: Improve JSON pretty printer","version":1,"mbox":"https://patchwork.libcamera.org/series/1076/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8571/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8571/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 62B17BE905\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  3 Jul 2020 00:14:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7B6BE60C57;\n\tFri,  3 Jul 2020 02:14:36 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A49C8609C7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  3 Jul 2020 02:14:34 +0200 (CEST)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 416041450;\n\tFri,  3 Jul 2020 02:14:34 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"LAfuRE/6\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1593735274;\n\tbh=CO5S+VTGnkOoEXjTW13pkq4J1r6yPzWmL2gw5xuMEag=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=LAfuRE/6xg2C7uND7esb1VD6q6UBpmuU92LLEmW5YgUZ661OpBFXa89JdC+vxW1tH\n\tF3M9uP62w4f4Sa2a/0SnvS+Zb4XbvSXpMPcVxa7gEldSwbI/ImZhTTtyUM0SEioAqu\n\tKMkEZSg566wVXoF4P0MvyTEPYxbQwA7KthShBB/o=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri,  3 Jul 2020 03:14:14 +0300","Message-Id":"<20200703001422.24324-3-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20200703001422.24324-1-laurent.pinchart@ideasonboard.com>","References":"<20200703001422.24324-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH 02/10] utils: raspberrypi: ctt:\n\tjson_pretty_print: Turn printer into a class","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Instead of passing a state dictionary to every method, turn the printer\ninto a class and store the state internally.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n .../raspberrypi/ctt/ctt_pretty_print_json.py  | 117 ++++++++++--------\n 1 file changed, 62 insertions(+), 55 deletions(-)","diff":"diff --git a/utils/raspberrypi/ctt/ctt_pretty_print_json.py b/utils/raspberrypi/ctt/ctt_pretty_print_json.py\nindex 18938c828b4e..0e9c56234f5c 100644\n--- a/utils/raspberrypi/ctt/ctt_pretty_print_json.py\n+++ b/utils/raspberrypi/ctt/ctt_pretty_print_json.py\n@@ -7,70 +7,77 @@\n import sys\n \n \n-\"\"\"\n-takes a collapsed json file and makes it more readable\n-\"\"\"\n-def process_file(string, fout, state):\n-    for c in string:\n-        process_char(c, fout, state)\n+class JSONPrettyPrinter(object):\n+    \"\"\"\n+    Take a collapsed JSON file and make it more readable\n+    \"\"\"\n+    def __init__(self):\n+        self.state = {\n+            \"indent\": 0,\n+            \"inarray\": [False],\n+            \"arraycount\": [],\n+            \"skipnewline\": True\n+        }\n \n+    def newline(self, fout):\n+        fout.write('\\n')\n+        fout.write(' ' * self.state[\"indent\"] * 4)\n \n-def print_newline(fout, state):\n-    fout.write('\\n')\n-    fout.write(' '*state[\"indent\"]*4)\n-\n-\n-def process_char(c, fout, state):\n-    if c == '{':\n-        if not state[\"skipnewline\"]:\n-            print_newline(fout, state)\n-        fout.write(c)\n-        state[\"indent\"] += 1\n-        print_newline(fout, state)\n-    elif c == '}':\n-        state[\"indent\"] -= 1\n-        print_newline(fout, state)\n-        fout.write(c)\n-    elif c == '[':\n-        print_newline(fout, state)\n-        fout.write(c)\n-        state[\"indent\"] += 1\n-        print_newline(fout, state)\n-        state[\"inarray\"] = [True] + state[\"inarray\"]\n-        state[\"arraycount\"] = [0] + state[\"arraycount\"]\n-    elif c == ']':\n-        state[\"indent\"] -= 1\n-        print_newline(fout, state)\n-        state[\"inarray\"].pop(0)\n-        state[\"arraycount\"].pop(0)\n-        fout.write(c)\n-    elif c == ':':\n-        fout.write(c)\n-        fout.write(' ')\n-    elif c == ' ':\n-        pass\n-    elif c == ',':\n-        if not state[\"inarray\"][0]:\n+    def process_char(self, c, fout):\n+        if c == '{':\n+            if not self.state[\"skipnewline\"]:\n+                self.newline(fout)\n+            fout.write(c)\n+            self.state[\"indent\"] += 1\n+            self.newline(fout)\n+        elif c == '}':\n+            self.state[\"indent\"] -= 1\n+            self.newline(fout)\n+            fout.write(c)\n+        elif c == '[':\n+            self.newline(fout)\n+            fout.write(c)\n+            self.state[\"indent\"] += 1\n+            self.newline(fout)\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(fout)\n+            self.state[\"inarray\"].pop(0)\n+            self.state[\"arraycount\"].pop(0)\n+            fout.write(c)\n+        elif c == ':':\n             fout.write(c)\n             fout.write(' ')\n-            print_newline(fout, state)\n-        else:\n-            fout.write(c)\n-            state[\"arraycount\"][0] += 1\n-            if state[\"arraycount\"][0] == 16:\n-                state[\"arraycount\"][0] = 0\n-                print_newline(fout, state)\n-            else:\n+        elif c == ' ':\n+            pass\n+        elif c == ',':\n+            if not self.state[\"inarray\"][0]:\n+                fout.write(c)\n                 fout.write(' ')\n-    else:\n-        fout.write(c)\n-    state[\"skipnewline\"] = (c == '[')\n+                self.newline(fout)\n+            else:\n+                fout.write(c)\n+                self.state[\"arraycount\"][0] += 1\n+                if self.state[\"arraycount\"][0] == 16:\n+                    self.state[\"arraycount\"][0] = 0\n+                    self.newline(fout)\n+                else:\n+                    fout.write(' ')\n+        else:\n+            fout.write(c)\n+        self.state[\"skipnewline\"] = (c == '[')\n+\n+    def print(self, string, fout):\n+        for c in string:\n+            self.process_char(c, fout)\n \n \n def pretty_print_json(str_in, output_filename):\n-    state = {\"indent\": 0, \"inarray\": [False], \"arraycount\": [], \"skipnewline\": True}\n     with open(output_filename, \"w\") as fout:\n-        process_file(str_in, fout, state)\n+        printer = JSONPrettyPrinter()\n+        printer.print(str_in, fout)\n \n \n if __name__ == '__main__':\n","prefixes":["libcamera-devel","02/10"]}