From 00974efa3189b7799d907cad9bebbc9bc3189731 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sun, 10 Dec 2017 08:57:34 +0000 Subject: [PATCH 1/3] 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. --- pre_commit_hooks/pretty_format_json.py | 25 ++++++++++++------------- setup.py | 2 -- tests/pretty_format_json_test.py | 5 ++++- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index bb7a3d0..9115077 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -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 diff --git a/setup.py b/setup.py index ce8de14..ad14930 100644 --- a/setup.py +++ b/setup.py @@ -28,8 +28,6 @@ setup( 'flake8!=2.5.3', 'autopep8>=1.3', 'pyyaml', - 'simplejson', - 'six', ], entry_points={ 'console_scripts': [ diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index eeef65b..4054b4c 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -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 From 5b6ddaf9f7ee96487709cb760a212ef1564b1c83 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sun, 10 Dec 2017 09:34:36 +0000 Subject: [PATCH 2/3] Fix pretty_format_json to use int indent The indent parameter for json should be integer and under Python2 is will raise an error if not. So switch from str to int and mention default value in help text. --- pre_commit_hooks/pretty_format_json.py | 29 +++++++++----------------- tests/pretty_format_json_test.py | 16 ++++++-------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 9115077..fb1c487 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -33,24 +33,12 @@ def _autofix(filename, new_contents): f.write(new_contents) -def parse_indent(s): - # type: (str) -> str +def parse_num_to_int(s): + """Convert string numbers to int, leaving strings as is.""" try: - int_indentation_spec = int(s) + return int(s) except ValueError: - if not s.strip(): - 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. ', - ) + return s def parse_topkeys(s): @@ -68,9 +56,12 @@ def pretty_format_json(argv=None): ) parser.add_argument( '--indent', - type=parse_indent, - default=' ', - help='String used as delimiter for one indentation level', + type=parse_num_to_int, + default='2', + 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( '--no-ensure-ascii', diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 4054b4c..7ce7e16 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -3,20 +3,16 @@ 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 parse_num_to_int from pre_commit_hooks.pretty_format_json import pretty_format_json from testing.util import get_resource_path -def test_parse_indent(): - assert parse_indent('0') == '' - assert parse_indent('2') == ' ' - assert parse_indent('\t') == '\t' - with pytest.raises(ValueError): - parse_indent('a') - with pytest.raises(ValueError): - parse_indent('-2') - +def test_parse_num_to_int(): + assert parse_num_to_int('0') == 0 + assert parse_num_to_int('2') == 2 + assert parse_num_to_int('\t') == '\t' + assert parse_num_to_int(' ') == ' ' @pytest.mark.parametrize( From 3e1b954a6e910b5f87bab8b4416084b3fad5fd88 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 11 Dec 2017 07:30:08 -0800 Subject: [PATCH 3/3] Add six back to setup.py --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index ad14930..8f9550d 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ setup( 'flake8!=2.5.3', 'autopep8>=1.3', 'pyyaml', + 'six', ], entry_points={ 'console_scripts': [