Skip to content

Commit 00974ef

Browse files
author
Calum Lind
committed
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.
1 parent 1f262da commit 00974ef

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import argparse
44
import io
5+
import json
56
import sys
67
from collections import OrderedDict
78

8-
import simplejson
9-
import six
9+
from six import text_type
1010

1111

1212
def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_keys=[]):
@@ -17,18 +17,18 @@ def pairs_first(pairs):
1717
if sort_keys:
1818
after = sorted(after, key=lambda x: x[0])
1919
return OrderedDict(before + after)
20-
return six.text_type(simplejson.dumps(
21-
simplejson.loads(
22-
contents,
23-
object_pairs_hook=pairs_first,
24-
),
20+
json_pretty = json.dumps(
21+
json.loads(contents, object_pairs_hook=pairs_first),
2522
indent=indent,
2623
ensure_ascii=ensure_ascii,
27-
)) + "\n" # dumps does not end with a newline
24+
separators=(',', ': '), # Workaround for https://bugs.python.org/issue16333
25+
)
26+
# Ensure unicode (Py2) and add the newline that dumps does not end with.
27+
return text_type(json_pretty) + '\n'
2828

2929

3030
def _autofix(filename, new_contents):
31-
print("Fixing file {}".format(filename))
31+
print('Fixing file {}'.format(filename))
3232
with io.open(filename, 'w', encoding='UTF-8') as f:
3333
f.write(new_contents)
3434

@@ -110,16 +110,15 @@ def pretty_format_json(argv=None):
110110
)
111111

112112
if contents != pretty_contents:
113-
print("File {} is not pretty-formatted".format(json_file))
113+
print('File {} is not pretty-formatted'.format(json_file))
114114

115115
if args.autofix:
116116
_autofix(json_file, pretty_contents)
117117

118118
status = 1
119-
120-
except simplejson.JSONDecodeError:
119+
except ValueError:
121120
print(
122-
"Input File {} is not a valid JSON, consider using check-json"
121+
'Input File {} is not a valid JSON, consider using check-json'
123122
.format(json_file),
124123
)
125124
return 1

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
'flake8!=2.5.3',
2929
'autopep8>=1.3',
3030
'pyyaml',
31-
'simplejson',
32-
'six',
3331
],
3432
entry_points={
3533
'console_scripts': [

tests/pretty_format_json_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import shutil
22

33
import pytest
4+
from six import PY2
45

56
from pre_commit_hooks.pretty_format_json import parse_indent
67
from pre_commit_hooks.pretty_format_json import pretty_format_json
@@ -17,6 +18,7 @@ def test_parse_indent():
1718
parse_indent('-2')
1819

1920

21+
2022
@pytest.mark.parametrize(
2123
('filename', 'expected_retval'), (
2224
('not_pretty_formatted_json.json', 1),
@@ -43,6 +45,7 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
4345
assert ret == expected_retval
4446

4547

48+
@pytest.mark.skipif(PY2, reason="Requires Python3")
4649
@pytest.mark.parametrize(
4750
('filename', 'expected_retval'), (
4851
('not_pretty_formatted_json.json', 1),
@@ -52,7 +55,7 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
5255
('tab_pretty_formatted_json.json', 0),
5356
),
5457
)
55-
def test_tab_pretty_format_json(filename, expected_retval):
58+
def test_tab_pretty_format_json(filename, expected_retval): # pragma: no cover
5659
ret = pretty_format_json(['--indent', '\t', get_resource_path(filename)])
5760
assert ret == expected_retval
5861

0 commit comments

Comments
 (0)