mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-14 09:27:21 +08:00
Add new hook pretty-format-json
This new hook allows to standardize one's JSON files (sorted key/4 spaces indent). By default it just fails if any file is not complying with the standard, but you can also pass the arg `--autofix` and the hook will pretty-format the file itself. Good in use combined with the `check-json` hook.
This commit is contained in:
70
pre_commit_hooks/pretty_format_json.py
Normal file
70
pre_commit_hooks/pretty_format_json.py
Normal file
@ -0,0 +1,70 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import simplejson
|
||||
|
||||
|
||||
def _get_pretty_format(contents, indent):
|
||||
return simplejson.dumps(
|
||||
simplejson.loads(contents),
|
||||
sort_keys=True,
|
||||
indent=indent
|
||||
) + "\n" # dumps don't end with a newline
|
||||
|
||||
|
||||
def _autofix(filename, new_contents):
|
||||
print("Fixing file {0}".format(filename))
|
||||
with open(filename, 'w') as f:
|
||||
f.write(new_contents)
|
||||
|
||||
|
||||
def pretty_format_json(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--autofix',
|
||||
action='store_true',
|
||||
dest='autofix',
|
||||
help='Automatically fixes encountered not-pretty-formatted files'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--indent',
|
||||
type=int,
|
||||
default=2,
|
||||
help='Number of indent spaces used to pretty-format files'
|
||||
)
|
||||
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
status = 0
|
||||
|
||||
for json_file in args.filenames:
|
||||
try:
|
||||
f = open(json_file, 'r')
|
||||
contents = f.read()
|
||||
f.close()
|
||||
|
||||
pretty_contents = _get_pretty_format(contents, args.indent)
|
||||
|
||||
if contents != pretty_contents:
|
||||
print("File {0} is not pretty-formatted".format(json_file))
|
||||
|
||||
if args.autofix:
|
||||
_autofix(json_file, pretty_contents)
|
||||
|
||||
status = 1
|
||||
|
||||
except simplejson.JSONDecodeError:
|
||||
print(
|
||||
"Input File {0} is not a valid JSON, consider using check-json"
|
||||
.format(json_file)
|
||||
)
|
||||
return 1
|
||||
|
||||
return status
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(pretty_format_json())
|
Reference in New Issue
Block a user