MCPcopy Index your code
hub / github.com/pre-commit/pre-commit-hooks / main

Function main

pre_commit_hooks/pretty_format_json.py:58–133  ·  view source on GitHub ↗
(argv: Sequence[str] | None = None)

Source from the content-addressed store, hash-verified

56
57
58def main(argv: Sequence[str] | None = None) -> int:
59 parser = argparse.ArgumentParser()
60 parser.add_argument(
61 '--autofix',
62 action='store_true',
63 dest='autofix',
64 help='Automatically fixes encountered not-pretty-formatted files',
65 )
66 parser.add_argument(
67 '--indent',
68 type=parse_num_to_int,
69 default='2',
70 help=(
71 'The number of indent spaces or a string to be used as delimiter'
72 ' for indentation level e.g. 4 or "\t" (Default: 2)'
73 ),
74 )
75 parser.add_argument(
76 '--no-ensure-ascii',
77 action='store_true',
78 dest='no_ensure_ascii',
79 default=False,
80 help=(
81 'Do NOT convert non-ASCII characters to Unicode escape sequences '
82 '(\\uXXXX)'
83 ),
84 )
85 parser.add_argument(
86 '--no-sort-keys',
87 action='store_true',
88 dest='no_sort_keys',
89 default=False,
90 help='Keep JSON nodes in the same order',
91 )
92 parser.add_argument(
93 '--top-keys',
94 type=parse_topkeys,
95 dest='top_keys',
96 default=[],
97 help='Ordered list of keys to keep at the top of JSON hashes',
98 )
99 parser.add_argument('filenames', nargs='*', help='Filenames to fix')
100 args = parser.parse_args(argv)
101
102 status = 0
103
104 for json_file in args.filenames:
105 with open(json_file, encoding='UTF-8') as f:
106 contents = f.read()
107
108 try:
109 pretty_contents = _get_pretty_format(
110 contents, args.indent, ensure_ascii=not args.no_ensure_ascii,
111 sort_keys=not args.no_sort_keys, top_keys=args.top_keys,
112 )
113 except ValueError:
114 print(
115 f'Input File {json_file} is not a valid JSON, consider using '

Callers 12

test_mainFunction · 0.90
test_unsorted_mainFunction · 0.90
test_tab_mainFunction · 0.90
test_non_ascii_mainFunction · 0.90
test_autofix_mainFunction · 0.90
test_invalid_mainFunction · 0.90
test_badfile_mainFunction · 0.90
test_diffing_outputFunction · 0.90

Calls 3

_get_pretty_formatFunction · 0.85
_autofixFunction · 0.85
get_diffFunction · 0.85

Tested by 11

test_mainFunction · 0.72
test_unsorted_mainFunction · 0.72
test_tab_mainFunction · 0.72
test_non_ascii_mainFunction · 0.72
test_autofix_mainFunction · 0.72
test_invalid_mainFunction · 0.72
test_badfile_mainFunction · 0.72
test_diffing_outputFunction · 0.72