utils: raspberrypi: ctt: json_pretty_print: Collapse newlines
Simplify the newline skipping logic by simply collapsing newlines. If a newline has been output, all subsequent newlines will be skipped until the next non-newline character is output. 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
d9617a499a
commit
bfad33c3ef
1 changed files with 6 additions and 5 deletions
|
@ -23,18 +23,20 @@ class JSONPrettyPrinter(object):
|
||||||
self.fout = fout
|
self.fout = fout
|
||||||
|
|
||||||
def newline(self):
|
def newline(self):
|
||||||
|
if not self.state["skipnewline"]:
|
||||||
self.fout.write('\n')
|
self.fout.write('\n')
|
||||||
self.state["need_indent"] = True
|
self.state["need_indent"] = True
|
||||||
|
self.state["skipnewline"] = True
|
||||||
|
|
||||||
def write(self, c):
|
def write(self, c):
|
||||||
if self.state["need_indent"]:
|
if self.state["need_indent"]:
|
||||||
self.fout.write(' ' * self.state["indent"] * 4)
|
self.fout.write(' ' * self.state["indent"] * 4)
|
||||||
self.state["need_indent"] = False
|
self.state["need_indent"] = False
|
||||||
self.fout.write(c)
|
self.fout.write(c)
|
||||||
|
self.state["skipnewline"] = False
|
||||||
|
|
||||||
def process_char(self, c):
|
def process_char(self, c):
|
||||||
if c == '{':
|
if c == '{':
|
||||||
if not self.state["skipnewline"]:
|
|
||||||
self.newline()
|
self.newline()
|
||||||
self.write(c)
|
self.write(c)
|
||||||
self.state["indent"] += 1
|
self.state["indent"] += 1
|
||||||
|
@ -76,7 +78,6 @@ class JSONPrettyPrinter(object):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.write(c)
|
self.write(c)
|
||||||
self.state["skipnewline"] = (c == '[')
|
|
||||||
|
|
||||||
def print(self, string):
|
def print(self, string):
|
||||||
for c in string:
|
for c in string:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue