mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
Merge pull request #254 from cas--/refactor/pretty-format-json
Refactor/pretty format json
This commit is contained in:
commit
cf04ab0186
3 changed files with 31 additions and 43 deletions
|
|
@ -2,11 +2,11 @@ from __future__ import print_function
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import io
|
import io
|
||||||
|
import json
|
||||||
import sys
|
import sys
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
import simplejson
|
from six import text_type
|
||||||
import six
|
|
||||||
|
|
||||||
|
|
||||||
def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_keys=[]):
|
def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_keys=[]):
|
||||||
|
|
@ -17,40 +17,28 @@ def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_
|
||||||
if sort_keys:
|
if sort_keys:
|
||||||
after = sorted(after, key=lambda x: x[0])
|
after = sorted(after, key=lambda x: x[0])
|
||||||
return OrderedDict(before + after)
|
return OrderedDict(before + after)
|
||||||
return six.text_type(simplejson.dumps(
|
json_pretty = json.dumps(
|
||||||
simplejson.loads(
|
json.loads(contents, object_pairs_hook=pairs_first),
|
||||||
contents,
|
|
||||||
object_pairs_hook=pairs_first,
|
|
||||||
),
|
|
||||||
indent=indent,
|
indent=indent,
|
||||||
ensure_ascii=ensure_ascii,
|
ensure_ascii=ensure_ascii,
|
||||||
)) + "\n" # dumps does not end with a newline
|
separators=(',', ': '), # Workaround for https://bugs.python.org/issue16333
|
||||||
|
)
|
||||||
|
# Ensure unicode (Py2) and add the newline that dumps does not end with.
|
||||||
|
return text_type(json_pretty) + '\n'
|
||||||
|
|
||||||
|
|
||||||
def _autofix(filename, new_contents):
|
def _autofix(filename, new_contents):
|
||||||
print("Fixing file {}".format(filename))
|
print('Fixing file {}'.format(filename))
|
||||||
with io.open(filename, 'w', encoding='UTF-8') as f:
|
with io.open(filename, 'w', encoding='UTF-8') as f:
|
||||||
f.write(new_contents)
|
f.write(new_contents)
|
||||||
|
|
||||||
|
|
||||||
def parse_indent(s):
|
def parse_num_to_int(s):
|
||||||
# type: (str) -> str
|
"""Convert string numbers to int, leaving strings as is."""
|
||||||
try:
|
try:
|
||||||
int_indentation_spec = int(s)
|
return int(s)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if not s.strip():
|
return s
|
||||||
return s
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
'Non-whitespace JSON indentation delimiter supplied. ',
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
if int_indentation_spec >= 0:
|
|
||||||
return int_indentation_spec * ' '
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
'Negative integer supplied to construct JSON indentation delimiter. ',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_topkeys(s):
|
def parse_topkeys(s):
|
||||||
|
|
@ -68,9 +56,12 @@ def pretty_format_json(argv=None):
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--indent',
|
'--indent',
|
||||||
type=parse_indent,
|
type=parse_num_to_int,
|
||||||
default=' ',
|
default='2',
|
||||||
help='String used as delimiter for one indentation level',
|
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(
|
parser.add_argument(
|
||||||
'--no-ensure-ascii',
|
'--no-ensure-ascii',
|
||||||
|
|
@ -110,16 +101,15 @@ def pretty_format_json(argv=None):
|
||||||
)
|
)
|
||||||
|
|
||||||
if contents != pretty_contents:
|
if contents != pretty_contents:
|
||||||
print("File {} is not pretty-formatted".format(json_file))
|
print('File {} is not pretty-formatted'.format(json_file))
|
||||||
|
|
||||||
if args.autofix:
|
if args.autofix:
|
||||||
_autofix(json_file, pretty_contents)
|
_autofix(json_file, pretty_contents)
|
||||||
|
|
||||||
status = 1
|
status = 1
|
||||||
|
except ValueError:
|
||||||
except simplejson.JSONDecodeError:
|
|
||||||
print(
|
print(
|
||||||
"Input File {} is not a valid JSON, consider using check-json"
|
'Input File {} is not a valid JSON, consider using check-json'
|
||||||
.format(json_file),
|
.format(json_file),
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
|
|
|
||||||
1
setup.py
1
setup.py
|
|
@ -28,7 +28,6 @@ setup(
|
||||||
'flake8!=2.5.3',
|
'flake8!=2.5.3',
|
||||||
'autopep8>=1.3',
|
'autopep8>=1.3',
|
||||||
'pyyaml',
|
'pyyaml',
|
||||||
'simplejson',
|
|
||||||
'six',
|
'six',
|
||||||
],
|
],
|
||||||
entry_points={
|
entry_points={
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,18 @@
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from six import PY2
|
||||||
|
|
||||||
from pre_commit_hooks.pretty_format_json import parse_indent
|
from pre_commit_hooks.pretty_format_json import parse_num_to_int
|
||||||
from pre_commit_hooks.pretty_format_json import pretty_format_json
|
from pre_commit_hooks.pretty_format_json import pretty_format_json
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
def test_parse_indent():
|
def test_parse_num_to_int():
|
||||||
assert parse_indent('0') == ''
|
assert parse_num_to_int('0') == 0
|
||||||
assert parse_indent('2') == ' '
|
assert parse_num_to_int('2') == 2
|
||||||
assert parse_indent('\t') == '\t'
|
assert parse_num_to_int('\t') == '\t'
|
||||||
with pytest.raises(ValueError):
|
assert parse_num_to_int(' ') == ' '
|
||||||
parse_indent('a')
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
parse_indent('-2')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
@ -43,6 +41,7 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(PY2, reason="Requires Python3")
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('filename', 'expected_retval'), (
|
('filename', 'expected_retval'), (
|
||||||
('not_pretty_formatted_json.json', 1),
|
('not_pretty_formatted_json.json', 1),
|
||||||
|
|
@ -52,7 +51,7 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
|
||||||
('tab_pretty_formatted_json.json', 0),
|
('tab_pretty_formatted_json.json', 0),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_tab_pretty_format_json(filename, expected_retval):
|
def test_tab_pretty_format_json(filename, expected_retval): # pragma: no cover
|
||||||
ret = pretty_format_json(['--indent', '\t', get_resource_path(filename)])
|
ret = pretty_format_json(['--indent', '\t', get_resource_path(filename)])
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue