mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-02 02:56:52 +00:00
Merge pull request #109 from mattclegg/no-sort-keys
Adding no-sort-keys to pretty_format_json
This commit is contained in:
commit
ea867c2e68
6 changed files with 41 additions and 9 deletions
|
|
@ -2,14 +2,16 @@ from __future__ import print_function
|
|||
|
||||
import argparse
|
||||
import sys
|
||||
from collections import OrderedDict
|
||||
|
||||
import simplejson
|
||||
|
||||
|
||||
def _get_pretty_format(contents, indent):
|
||||
def _get_pretty_format(contents, indent, sort_keys=True):
|
||||
return simplejson.dumps(
|
||||
simplejson.loads(contents),
|
||||
sort_keys=True,
|
||||
simplejson.loads(contents,
|
||||
object_pairs_hook=None if sort_keys else OrderedDict),
|
||||
sort_keys=sort_keys,
|
||||
indent=indent
|
||||
) + "\n" # dumps don't end with a newline
|
||||
|
||||
|
|
@ -34,6 +36,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='Keep JSON nodes in the same order'
|
||||
)
|
||||
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||
args = parser.parse_args(argv)
|
||||
|
|
@ -46,7 +55,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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue