diff options
Diffstat (limited to 'lib/python/qmk/info_json_encoder.py')
-rwxr-xr-x | lib/python/qmk/info_json_encoder.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/python/qmk/info_json_encoder.py b/lib/python/qmk/info_json_encoder.py new file mode 100755 index 000000000..60dae7247 --- /dev/null +++ b/lib/python/qmk/info_json_encoder.py | |||
@@ -0,0 +1,96 @@ | |||
1 | """Class that pretty-prints QMK info.json files. | ||
2 | """ | ||
3 | import json | ||
4 | from decimal import Decimal | ||
5 | |||
6 | |||
7 | class InfoJSONEncoder(json.JSONEncoder): | ||
8 | """Custom encoder to make info.json's a little nicer to work with. | ||
9 | """ | ||
10 | container_types = (list, tuple, dict) | ||
11 | indentation_char = " " | ||
12 | |||
13 | def __init__(self, *args, **kwargs): | ||
14 | super().__init__(*args, **kwargs) | ||
15 | self.indentation_level = 0 | ||
16 | |||
17 | if not self.indent: | ||
18 | self.indent = 4 | ||
19 | |||
20 | def encode(self, obj): | ||
21 | """Encode JSON objects for QMK. | ||
22 | """ | ||
23 | if isinstance(obj, Decimal): | ||
24 | if obj == int(obj): # I can't believe Decimal objects don't have .is_integer() | ||
25 | return int(obj) | ||
26 | return float(obj) | ||
27 | |||
28 | elif isinstance(obj, (list, tuple)): | ||
29 | if self._primitives_only(obj): | ||
30 | return "[" + ", ".join(self.encode(element) for element in obj) + "]" | ||
31 | |||
32 | else: | ||
33 | self.indentation_level += 1 | ||
34 | output = [self.indent_str + self.encode(element) for element in obj] | ||
35 | self.indentation_level -= 1 | ||
36 | return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" | ||
37 | |||
38 | elif isinstance(obj, dict): | ||
39 | if obj: | ||
40 | if self.indentation_level == 4: | ||
41 | # These are part of a layout, put them on a single line. | ||
42 | return "{ " + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items())) + " }" | ||
43 | |||
44 | else: | ||
45 | self.indentation_level += 1 | ||
46 | output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_root_dict)] | ||
47 | self.indentation_level -= 1 | ||
48 | return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}" | ||
49 | else: | ||
50 | return "{}" | ||
51 | else: | ||
52 | return super().encode(obj) | ||
53 | |||
54 | def _primitives_only(self, obj): | ||
55 | """Returns true if the object doesn't have any container type objects (list, tuple, dict). | ||
56 | """ | ||
57 | if isinstance(obj, dict): | ||
58 | obj = obj.values() | ||
59 | |||
60 | return not any(isinstance(element, self.container_types) for element in obj) | ||
61 | |||
62 | def sort_root_dict(self, key): | ||
63 | """Forces layout to the back of the sort order. | ||
64 | """ | ||
65 | key = key[0] | ||
66 | |||
67 | if self.indentation_level == 1: | ||
68 | if key == 'manufacturer': | ||
69 | return '10keyboard_name' | ||
70 | |||
71 | elif key == 'keyboard_name': | ||
72 | return '11keyboard_name' | ||
73 | |||
74 | elif key == 'maintainer': | ||
75 | return '12maintainer' | ||
76 | |||
77 | elif key in ('height', 'width'): | ||
78 | return '40' + str(key) | ||
79 | |||
80 | elif key == 'community_layouts': | ||
81 | return '97community_layouts' | ||
82 | |||
83 | elif key == 'layout_aliases': | ||
84 | return '98layout_aliases' | ||
85 | |||
86 | elif key == 'layouts': | ||
87 | return '99layouts' | ||
88 | |||
89 | else: | ||
90 | return '50' + str(key) | ||
91 | |||
92 | return key | ||
93 | |||
94 | @property | ||
95 | def indent_str(self): | ||
96 | return self.indentation_char * (self.indentation_level * self.indent) | ||