pre-commit-hooks/pre_commit_hooks/pretty_format_json.py

138 lines
3.9 KiB
Python
Raw Normal View History

from __future__ import annotations
import argparse
import json
import sys
from collections.abc import Mapping
from collections.abc import Sequence
2019-09-25 03:42:24 +08:00
from difflib import unified_diff
2016-04-14 16:30:42 +08:00
2019-02-12 11:56:15 +08:00
def _get_pretty_format(
2020-02-06 03:10:42 +08:00
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:
2020-02-06 03:10:42 +08:00
after.sort()
return dict(before + after)
json_pretty = json.dumps(
json.loads(contents, object_pairs_hook=pairs_first),
2017-03-16 11:27:34 +08:00
indent=indent,
2017-03-20 23:24:58 +08:00
ensure_ascii=ensure_ascii,
)
2020-02-06 03:10:42 +08:00
return f'{json_pretty}\n'
2016-11-04 06:49:04 +08:00
2020-02-06 03:10:42 +08:00
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) -> int | str:
"""Convert string numbers to int, leaving strings as is."""
try:
return int(s)
except ValueError:
return s
2016-11-04 06:54:48 +08:00
def parse_topkeys(s: str) -> list[str]:
return s.split(',')
2016-11-04 06:54:48 +08:00
2020-02-06 03:10:42 +08:00
def get_diff(source: str, target: str, file: str) -> str:
source_lines = source.splitlines(True)
target_lines = target.splitlines(True)
2019-09-25 03:42:24 +08:00
diff = unified_diff(source_lines, target_lines, fromfile=file, tofile=file)
return ''.join(diff)
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
'--autofix',
action='store_true',
dest='autofix',
2016-04-14 23:25:52 +08:00
help='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)'
),
)
2017-03-16 11:27:34 +08:00
parser.add_argument(
'--no-ensure-ascii',
action='store_true',
dest='no_ensure_ascii',
default=False,
2019-02-12 11:56:15 +08:00
help=(
'Do NOT convert non-ASCII characters to Unicode escape sequences '
'(\\uXXXX)'
),
2017-03-16 11:27:34 +08:00
)
parser.add_argument(
'--no-sort-keys',
action='store_true',
dest='no_sort_keys',
default=False,
2016-04-14 23:25:52 +08:00
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:
2020-02-06 03:10:42 +08:00
with open(json_file, encoding='UTF-8') as f:
contents = f.read()
2016-04-14 23:25:52 +08:00
try:
pretty_contents = _get_pretty_format(
2017-03-16 11:27:34 +08:00
contents, args.indent, ensure_ascii=not args.no_ensure_ascii,
2017-03-20 23:24:58 +08:00
sort_keys=not args.no_sort_keys, top_keys=args.top_keys,
2016-04-14 23:25:52 +08:00
)
except ValueError:
print(
2020-02-06 03:10:42 +08:00
f'Input File {json_file} is not a valid JSON, consider using '
f'check-json',
)
status = 1
else:
if contents != pretty_contents:
if args.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__':
raise SystemExit(main())