mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-31 18:46:53 +00:00
Attempt to fix the json hook under test
This commit is contained in:
parent
a8592669d9
commit
dc50b7f09c
2 changed files with 8 additions and 11 deletions
|
|
@ -1,10 +1,10 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import io
|
||||
import json
|
||||
import sys
|
||||
|
||||
import simplejson
|
||||
|
||||
|
||||
def check_json(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
|
|
@ -14,8 +14,8 @@ def check_json(argv=None):
|
|||
retval = 0
|
||||
for filename in args.filenames:
|
||||
try:
|
||||
simplejson.load(open(filename))
|
||||
except (simplejson.JSONDecodeError, UnicodeDecodeError) as exc:
|
||||
json.load(io.open(filename, encoding='UTF-8'))
|
||||
except (ValueError, UnicodeDecodeError) as exc:
|
||||
print('{}: Failed to json decode ({})'.format(filename, exc))
|
||||
retval = 1
|
||||
return retval
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ def _get_pretty_format(contents, indent, ensure_ascii=True, sort_keys=True, top_
|
|||
)) + "\n" # dumps does not end with a newline
|
||||
|
||||
|
||||
def _autofix(filename, new_contents, encoding=None):
|
||||
def _autofix(filename, new_contents):
|
||||
print("Fixing file {}".format(filename))
|
||||
with io.open(filename, 'w', encoding=encoding) as f:
|
||||
with io.open(filename, 'w', encoding='UTF-8') as f:
|
||||
f.write(new_contents)
|
||||
|
||||
|
||||
|
|
@ -100,7 +100,7 @@ def pretty_format_json(argv=None):
|
|||
status = 0
|
||||
|
||||
for json_file in args.filenames:
|
||||
with io.open(json_file, encoding='utf-8') as f:
|
||||
with io.open(json_file, encoding='UTF-8') as f:
|
||||
contents = f.read()
|
||||
|
||||
try:
|
||||
|
|
@ -113,10 +113,7 @@ def pretty_format_json(argv=None):
|
|||
print("File {} is not pretty-formatted".format(json_file))
|
||||
|
||||
if args.autofix:
|
||||
_autofix(
|
||||
json_file, pretty_contents,
|
||||
encoding='utf-8' if args.no_ensure_ascii else None,
|
||||
)
|
||||
_autofix(json_file, pretty_contents)
|
||||
|
||||
status = 1
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue