From f769c208bc737e6037f464c5da069cb001b24f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Larivi=C3=A8re?= Date: Sat, 12 Mar 2016 17:04:33 -0500 Subject: [PATCH] Addding no-sort-keys to pretty_format_json, this allows to disable the sort on the keys --- README.md | 2 ++ pre_commit_hooks/pretty_format_json.py | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b13b697..40f1f21 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ Add this to your `.pre-commit-config.yaml` - Use `args: ['--django']` to match `test*.py` instead. - `pyflakes` - Run pyflakes on your python files. - `pretty-format-json` - Checks that all your JSON files are pretty + - Use `args: ['--autofix']` to automatically fixing the encountered not-pretty-formatted files and + `args: ['--no-sort-keys']` to disable the sort on the keys. - `requirements-txt-fixer` - Sorts entries in requirements.txt - `trailing-whitespace` - Trims trailing whitespace. - Markdown linebreak trailing spaces preserved for `.md` and`.markdown`; diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index d22dada..1d601fa 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -6,10 +6,10 @@ import sys import simplejson -def _get_pretty_format(contents, indent): +def _get_pretty_format(contents, indent, no_sort_keys): return simplejson.dumps( simplejson.loads(contents), - sort_keys=True, + sort_keys=no_sort_keys, indent=indent ) + "\n" # dumps don't end with a newline @@ -34,6 +34,13 @@ def pretty_format_json(argv=None): default=2, help='Number of indent spaces used to pretty-format files' ) + parser.add_argument( + '--no-sort-keys', + action='store_true', + dest='no_sort_keys', + default=False, + help='Do not sort the keys' + ) parser.add_argument('filenames', nargs='*', help='Filenames to fix') args = parser.parse_args(argv) @@ -46,7 +53,7 @@ def pretty_format_json(argv=None): contents = f.read() f.close() - pretty_contents = _get_pretty_format(contents, args.indent) + pretty_contents = _get_pretty_format(contents, args.indent, (not args.no_sort_keys)) if contents != pretty_contents: print("File {0} is not pretty-formatted".format(json_file))