summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-07-03 01:43:57 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-07-03 18:15:38 +0300
commit4321f6e96e89be0cdab5abff8aee6ab46ea44322 (patch)
treeef6c221416cd9e8c52940d80e34e4c6e7271c11d
parent926fe94e4ccfa34532155517351c068743a9e25c (diff)
utils: raspberrypi: ctt: json_pretty_print: Make output file a class member
Instead of passing the output file to every method of the printer class, make it a class member. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: David Plowman <david.plowman@raspberrypi.com> Tested-by: David Plowman <david.plowman@raspberrypi.com>
-rw-r--r--utils/raspberrypi/ctt/ctt_pretty_print_json.py58
1 files changed, 30 insertions, 28 deletions
diff --git a/utils/raspberrypi/ctt/ctt_pretty_print_json.py b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
index 0e9c5623..703a23fe 100644
--- a/utils/raspberrypi/ctt/ctt_pretty_print_json.py
+++ b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
@@ -11,7 +11,7 @@ class JSONPrettyPrinter(object):
"""
Take a collapsed JSON file and make it more readable
"""
- def __init__(self):
+ def __init__(self, fout):
self.state = {
"indent": 0,
"inarray": [False],
@@ -19,65 +19,67 @@ class JSONPrettyPrinter(object):
"skipnewline": True
}
- def newline(self, fout):
- fout.write('\n')
- fout.write(' ' * self.state["indent"] * 4)
+ self.fout = fout
- def process_char(self, c, fout):
+ def newline(self):
+ self.fout.write('\n')
+ self.fout.write(' ' * self.state["indent"] * 4)
+
+ def process_char(self, c):
if c == '{':
if not self.state["skipnewline"]:
- self.newline(fout)
- fout.write(c)
+ self.newline()
+ self.fout.write(c)
self.state["indent"] += 1
- self.newline(fout)
+ self.newline()
elif c == '}':
self.state["indent"] -= 1
- self.newline(fout)
- fout.write(c)
+ self.newline()
+ self.fout.write(c)
elif c == '[':
- self.newline(fout)
- fout.write(c)
+ self.newline()
+ self.fout.write(c)
self.state["indent"] += 1
- self.newline(fout)
+ self.newline()
self.state["inarray"] = [True] + self.state["inarray"]
self.state["arraycount"] = [0] + self.state["arraycount"]
elif c == ']':
self.state["indent"] -= 1
- self.newline(fout)
+ self.newline()
self.state["inarray"].pop(0)
self.state["arraycount"].pop(0)
- fout.write(c)
+ self.fout.write(c)
elif c == ':':
- fout.write(c)
- fout.write(' ')
+ self.fout.write(c)
+ self.fout.write(' ')
elif c == ' ':
pass
elif c == ',':
if not self.state["inarray"][0]:
- fout.write(c)
- fout.write(' ')
- self.newline(fout)
+ self.fout.write(c)
+ self.fout.write(' ')
+ self.newline()
else:
- fout.write(c)
+ self.fout.write(c)
self.state["arraycount"][0] += 1
if self.state["arraycount"][0] == 16:
self.state["arraycount"][0] = 0
- self.newline(fout)
+ self.newline()
else:
- fout.write(' ')
+ self.fout.write(' ')
else:
- fout.write(c)
+ self.fout.write(c)
self.state["skipnewline"] = (c == '[')
- def print(self, string, fout):
+ def print(self, string):
for c in string:
- self.process_char(c, fout)
+ self.process_char(c)
def pretty_print_json(str_in, output_filename):
with open(output_filename, "w") as fout:
- printer = JSONPrettyPrinter()
- printer.print(str_in, fout)
+ printer = JSONPrettyPrinter(fout)
+ printer.print(str_in)
if __name__ == '__main__':