[libcamera-devel,06/10] utils: raspberrypi: ctt: json_pretty_print: Add character write method

Message ID 20200703001422.24324-7-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • utils: raspberrypi: ctt: Improve JSON pretty printer
Related show

Commit Message

Laurent Pinchart July 3, 2020, 12:14 a.m. UTC
Add a write method to the JSONPrettyPrinter class to output a character.
This will be used to handle state updates when outputting individual
characters.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 .../raspberrypi/ctt/ctt_pretty_print_json.py  | 25 +++++++++++--------
 1 file changed, 14 insertions(+), 11 deletions(-)

Patch

diff --git a/utils/raspberrypi/ctt/ctt_pretty_print_json.py b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
index dfa99a6d4034..9d450f6d300c 100644
--- a/utils/raspberrypi/ctt/ctt_pretty_print_json.py
+++ b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
@@ -25,20 +25,23 @@  class JSONPrettyPrinter(object):
         self.fout.write('\n')
         self.fout.write(' ' * self.state["indent"] * 4)
 
+    def write(self, c):
+        self.fout.write(c)
+
     def process_char(self, c):
         if c == '{':
             if not self.state["skipnewline"]:
                 self.newline()
-            self.fout.write(c)
+            self.write(c)
             self.state["indent"] += 1
             self.newline()
         elif c == '}':
             self.state["indent"] -= 1
             self.newline()
-            self.fout.write(c)
+            self.write(c)
         elif c == '[':
             self.newline()
-            self.fout.write(c)
+            self.write(c)
             self.state["indent"] += 1
             self.newline()
             self.state["inarray"] = [True] + self.state["inarray"]
@@ -48,27 +51,27 @@  class JSONPrettyPrinter(object):
             self.newline()
             self.state["inarray"].pop(0)
             self.state["arraycount"].pop(0)
-            self.fout.write(c)
+            self.write(c)
         elif c == ':':
-            self.fout.write(c)
-            self.fout.write(' ')
+            self.write(c)
+            self.write(' ')
         elif c == ',':
             if not self.state["inarray"][0]:
-                self.fout.write(c)
-                self.fout.write(' ')
+                self.write(c)
+                self.write(' ')
                 self.newline()
             else:
-                self.fout.write(c)
+                self.write(c)
                 self.state["arraycount"][0] += 1
                 if self.state["arraycount"][0] == 16:
                     self.state["arraycount"][0] = 0
                     self.newline()
                 else:
-                    self.fout.write(' ')
+                    self.write(' ')
         elif c.isspace():
             pass
         else:
-            self.fout.write(c)
+            self.write(c)
         self.state["skipnewline"] = (c == '[')
 
     def print(self, string):