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>
This commit is contained in:
parent
926fe94e4c
commit
4321f6e96e
1 changed files with 30 additions and 28 deletions
|
@ -11,7 +11,7 @@ class JSONPrettyPrinter(object):
|
||||||
"""
|
"""
|
||||||
Take a collapsed JSON file and make it more readable
|
Take a collapsed JSON file and make it more readable
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self, fout):
|
||||||
self.state = {
|
self.state = {
|
||||||
"indent": 0,
|
"indent": 0,
|
||||||
"inarray": [False],
|
"inarray": [False],
|
||||||
|
@ -19,65 +19,67 @@ class JSONPrettyPrinter(object):
|
||||||
"skipnewline": True
|
"skipnewline": True
|
||||||
}
|
}
|
||||||
|
|
||||||
def newline(self, fout):
|
self.fout = fout
|
||||||
fout.write('\n')
|
|
||||||
fout.write(' ' * self.state["indent"] * 4)
|
|
||||||
|
|
||||||
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 c == '{':
|
||||||
if not self.state["skipnewline"]:
|
if not self.state["skipnewline"]:
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
self.state["indent"] += 1
|
self.state["indent"] += 1
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
elif c == '}':
|
elif c == '}':
|
||||||
self.state["indent"] -= 1
|
self.state["indent"] -= 1
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
elif c == '[':
|
elif c == '[':
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
self.state["indent"] += 1
|
self.state["indent"] += 1
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
self.state["inarray"] = [True] + self.state["inarray"]
|
self.state["inarray"] = [True] + self.state["inarray"]
|
||||||
self.state["arraycount"] = [0] + self.state["arraycount"]
|
self.state["arraycount"] = [0] + self.state["arraycount"]
|
||||||
elif c == ']':
|
elif c == ']':
|
||||||
self.state["indent"] -= 1
|
self.state["indent"] -= 1
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
self.state["inarray"].pop(0)
|
self.state["inarray"].pop(0)
|
||||||
self.state["arraycount"].pop(0)
|
self.state["arraycount"].pop(0)
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
elif c == ':':
|
elif c == ':':
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
fout.write(' ')
|
self.fout.write(' ')
|
||||||
elif c == ' ':
|
elif c == ' ':
|
||||||
pass
|
pass
|
||||||
elif c == ',':
|
elif c == ',':
|
||||||
if not self.state["inarray"][0]:
|
if not self.state["inarray"][0]:
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
fout.write(' ')
|
self.fout.write(' ')
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
else:
|
else:
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
self.state["arraycount"][0] += 1
|
self.state["arraycount"][0] += 1
|
||||||
if self.state["arraycount"][0] == 16:
|
if self.state["arraycount"][0] == 16:
|
||||||
self.state["arraycount"][0] = 0
|
self.state["arraycount"][0] = 0
|
||||||
self.newline(fout)
|
self.newline()
|
||||||
else:
|
else:
|
||||||
fout.write(' ')
|
self.fout.write(' ')
|
||||||
else:
|
else:
|
||||||
fout.write(c)
|
self.fout.write(c)
|
||||||
self.state["skipnewline"] = (c == '[')
|
self.state["skipnewline"] = (c == '[')
|
||||||
|
|
||||||
def print(self, string, fout):
|
def print(self, string):
|
||||||
for c in string:
|
for c in string:
|
||||||
self.process_char(c, fout)
|
self.process_char(c)
|
||||||
|
|
||||||
|
|
||||||
def pretty_print_json(str_in, output_filename):
|
def pretty_print_json(str_in, output_filename):
|
||||||
with open(output_filename, "w") as fout:
|
with open(output_filename, "w") as fout:
|
||||||
printer = JSONPrettyPrinter()
|
printer = JSONPrettyPrinter(fout)
|
||||||
printer.print(str_in, fout)
|
printer.print(str_in)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue