summaryrefslogtreecommitdiff
path: root/utils/raspberrypi/ctt/ctt_pretty_print_json.py
blob: f1c61347d8fc842f74404832a15b5b7e1fe3befa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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())