libcamera/utils/raspberrypi/ctt/ctt_pretty_print_json.py
Laurent Pinchart d9617a499a utils: raspberrypi: ctt: json_pretty_print: Fix indentation handling
Indentation is handled by outputting spaces right after outputting a
newline character. That works in most cases, but would result in the
input '{}' being printed as

{
    }

instead of

{
}

Fix it by outputting the indentation before outputting the next
character after a newline. The indentation value will be updated by
then.

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>
2020-07-03 18:15:47 +03:00

100 lines
2.8 KiB
Python

# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (C) 2019, Raspberry Pi (Trading) Limited
#
# ctt_pretty_print_json.py - camera tuning tool JSON formatter
import sys
class JSONPrettyPrinter(object):
"""
Take a collapsed JSON file and make it more readable
"""
def __init__(self, fout):
self.state = {
"indent": 0,
"inarray": [False],
"arraycount": [],
"skipnewline": True,
"need_indent": False,
}
self.fout = fout
def newline(self):
self.fout.write('\n')
self.state["need_indent"] = True
def write(self, c):
if self.state["need_indent"]:
self.fout.write(' ' * self.state["indent"] * 4)
self.state["need_indent"] = False
self.fout.write(c)
def process_char(self, c):
if c == '{':
if not self.state["skipnewline"]:
self.newline()
self.write(c)
self.state["indent"] += 1
self.newline()
elif c == '}':
self.state["indent"] -= 1
self.newline()
self.write(c)
elif c == '[':
self.newline()
self.write(c)
self.state["indent"] += 1
self.newline()
self.state["inarray"] = [True] + self.state["inarray"]
self.state["arraycount"] = [0] + self.state["arraycount"]
elif c == ']':
self.state["indent"] -= 1
self.newline()
self.state["inarray"].pop(0)
self.state["arraycount"].pop(0)
self.write(c)
elif c == ':':
self.write(c)
self.write(' ')
elif c == ',':
if not self.state["inarray"][0]:
self.write(c)
self.write(' ')
self.newline()
else:
self.write(c)
self.state["arraycount"][0] += 1
if self.state["arraycount"][0] == 16:
self.state["arraycount"][0] = 0
self.newline()
else:
self.write(' ')
elif c.isspace():
pass
else:
self.write(c)
self.state["skipnewline"] = (c == '[')
def print(self, string):
for c in string:
self.process_char(c)
def pretty_print_json(str_in, output_filename):
with open(output_filename, "w") as fout:
printer = JSONPrettyPrinter(fout)
printer.print(str_in)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: %s filename" % sys.argv[0])
sys.exit(1)
input_filename = sys.argv[1]
with open(input_filename, "r") as fin:
printer = JSONPrettyPrinter(sys.stdout)
printer.print(fin.read())