Refactor legacy `indent: int` -> `indent: str`
The type of `indent` parameter is preferably `str`. See
http://simplejson.readthedocs.io/en/latest/index.html?highlight=dump#simplejson.dump
. This change allows to specify TABs as indentation delimiter to
`pretty_format_json`. Add input validator/converter for backward compat.
diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py
index 916735c..605cbec 100644
--- a/pre_commit_hooks/pretty_format_json.py
+++ b/pre_commit_hooks/pretty_format_json.py
@@ -24,6 +24,25 @@
f.write(new_contents)
+def parse_indent(s):
+ # type: (str) -> str
+ try:
+ int_indentation_spec = int(s)
+ if int_indentation_spec >= 0:
+ return int_indentation_spec * ' '
+ else:
+ raise ValueError(
+ 'Negative integer supplied to construct JSON indentation delimiter. ',
+ )
+ except ValueError:
+ if s.strip() == '':
+ return s
+ else:
+ raise ValueError(
+ 'Non-whitespace JSON indentation delimiter supplied. ',
+ )
+
+
def pretty_format_json(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument(
@@ -34,9 +53,9 @@
)
parser.add_argument(
'--indent',
- type=int,
- default=2,
- help='Number of indent spaces used to pretty-format files',
+ type=parse_indent,
+ default=' ',
+ help='String used as delimiter for one indentation level',
)
parser.add_argument(
'--no-sort-keys',