mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 18:16:52 +00:00
Remove pretty_format_json simplejson dependency
* The simplejson module is only needed for <=py25 so replace with builtin json. * Replace six dependecy for simple Py2 check for convertion to unicode. * Cleanup quotes.
This commit is contained in:
parent
1f262dab15
commit
00974efa31
3 changed files with 16 additions and 16 deletions
|
|
@ -2,11 +2,11 @@ from __future__ import print_function
|
|||
|
||||
import argparse
|
||||
import io
|
||||
import json
|
||||
import sys
|
||||
from collections import OrderedDict
|
||||
|
||||
import simplejson
|
||||
import six
|
||||
from six import text_type
|
||||
|
||||
|
||||
def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_keys=[]):
|
||||
|
|
@ -17,18 +17,18 @@ def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_
|
|||
if sort_keys:
|
||||
after = sorted(after, key=lambda x: x[0])
|
||||
return OrderedDict(before + after)
|
||||
return six.text_type(simplejson.dumps(
|
||||
simplejson.loads(
|
||||
contents,
|
||||
object_pairs_hook=pairs_first,
|
||||
),
|
||||
json_pretty = json.dumps(
|
||||
json.loads(contents, object_pairs_hook=pairs_first),
|
||||
indent=indent,
|
||||
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):
|
||||
print("Fixing file {}".format(filename))
|
||||
print('Fixing file {}'.format(filename))
|
||||
with io.open(filename, 'w', encoding='UTF-8') as f:
|
||||
f.write(new_contents)
|
||||
|
||||
|
|
@ -110,16 +110,15 @@ def pretty_format_json(argv=None):
|
|||
)
|
||||
|
||||
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:
|
||||
_autofix(json_file, pretty_contents)
|
||||
|
||||
status = 1
|
||||
|
||||
except simplejson.JSONDecodeError:
|
||||
except ValueError:
|
||||
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),
|
||||
)
|
||||
return 1
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -28,8 +28,6 @@ setup(
|
|||
'flake8!=2.5.3',
|
||||
'autopep8>=1.3',
|
||||
'pyyaml',
|
||||
'simplejson',
|
||||
'six',
|
||||
],
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import shutil
|
||||
|
||||
import pytest
|
||||
from six import PY2
|
||||
|
||||
from pre_commit_hooks.pretty_format_json import parse_indent
|
||||
from pre_commit_hooks.pretty_format_json import pretty_format_json
|
||||
|
|
@ -17,6 +18,7 @@ def test_parse_indent():
|
|||
parse_indent('-2')
|
||||
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('filename', 'expected_retval'), (
|
||||
('not_pretty_formatted_json.json', 1),
|
||||
|
|
@ -43,6 +45,7 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
|
|||
assert ret == expected_retval
|
||||
|
||||
|
||||
@pytest.mark.skipif(PY2, reason="Requires Python3")
|
||||
@pytest.mark.parametrize(
|
||||
('filename', 'expected_retval'), (
|
||||
('not_pretty_formatted_json.json', 1),
|
||||
|
|
@ -52,7 +55,7 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
|
|||
('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)])
|
||||
assert ret == expected_retval
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue