-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpretty_format_json5.py
More file actions
141 lines (117 loc) · 3.95 KB
/
pretty_format_json5.py
File metadata and controls
141 lines (117 loc) · 3.95 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import argparse
import sys
from difflib import unified_diff
from typing import List
from typing import Mapping
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Union
import json5
# Forked from https://github.com/pre-commit/pre-commit-hooks/blob/f48244a8055c1d51955ee6312d8942db325672cf/pre_commit_hooks/check_json.py
def _get_pretty_format(
contents: str,
indent: str,
ensure_ascii: bool = True,
sort_keys: bool = True,
top_keys: Sequence[str] = (),
) -> str:
def pairs_first(pairs: Sequence[Tuple[str, str]]) -> Mapping[str, str]:
before = [pair for pair in pairs if pair[0] in top_keys]
before = sorted(before, key=lambda x: top_keys.index(x[0]))
after = [pair for pair in pairs if pair[0] not in top_keys]
if sort_keys:
after.sort()
return dict(before + after)
json_pretty = json5.dumps(
json5.loads(contents, object_pairs_hook=pairs_first),
indent=indent,
ensure_ascii=ensure_ascii,
)
return f"{json_pretty}\n"
def _autofix(filename: str, new_contents: str) -> None:
print(f"Fixing file {filename}")
with open(filename, "w", encoding="UTF-8") as f:
f.write(new_contents)
def parse_num_to_int(s: str) -> Union[int, str]:
"""Convert string numbers to int, leaving strings as is."""
try:
return int(s)
except ValueError:
return s
def parse_topkeys(s: str) -> List[str]:
return s.split(",")
def get_diff(source: str, target: str, file: str) -> str:
source_lines = source.splitlines(True)
target_lines = target.splitlines(True)
diff = unified_diff(source_lines, target_lines, fromfile=file, tofile=file)
return "".join(diff)
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
"--no-autofix",
action="store_true",
dest="no_autofix",
default=False,
help="Don't automatically fixes encountered not-pretty-formatted files",
)
parser.add_argument(
"--indent",
type=parse_num_to_int,
default="2",
help=(
"The number of indent spaces or a string to be used as delimiter"
' for indentation level e.g. 4 or "\t" (Default: 2)'
),
)
parser.add_argument(
"--ensure-ascii",
action="store_true",
dest="ensure_ascii",
default=False,
help=("Convert non-ASCII characters to Unicode escape sequences " "(\\uXXXX)"),
)
parser.add_argument(
"--no-sort-keys",
action="store_true",
dest="no_sort_keys",
default=False,
help="Keep JSON nodes in the same order",
)
parser.add_argument(
"--top-keys",
type=parse_topkeys,
dest="top_keys",
default=[],
help="Ordered list of keys to keep at the top of JSON hashes",
)
parser.add_argument("filenames", nargs="*", help="Filenames to fix")
args = parser.parse_args(argv)
status = 0
for json_file in args.filenames:
with open(json_file, encoding="UTF-8") as f:
contents = f.read()
try:
pretty_contents = _get_pretty_format(
contents,
args.indent,
ensure_ascii=args.ensure_ascii,
sort_keys=not args.no_sort_keys,
top_keys=args.top_keys,
)
except ValueError:
print(
f"Input File {json_file} is not a valid JSON, consider using "
f"check-json",
)
return 1
if contents != pretty_contents:
if not args.no_autofix:
_autofix(json_file, pretty_contents)
else:
diff_output = get_diff(contents, pretty_contents, json_file)
sys.stdout.buffer.write(diff_output.encode())
status = 1
return status
if __name__ == "__main__":
sys.exit(main())