MCPcopy Index your code
hub / github.com/RustPython/RustPython / main

Function main

Lib/json/tool.py:44–114  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

42
43
44def main():
45 description = ('A simple command line interface for json module '
46 'to validate and pretty-print JSON objects.')
47 parser = argparse.ArgumentParser(description=description, color=True)
48 parser.add_argument('infile', nargs='?',
49 help='a JSON file to be validated or pretty-printed',
50 default='-')
51 parser.add_argument('outfile', nargs='?',
52 help='write the output of infile to outfile',
53 default=None)
54 parser.add_argument('--sort-keys', action='store_true', default=False,
55 help='sort the output of dictionaries alphabetically by key')
56 parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
57 help='disable escaping of non-ASCII characters')
58 parser.add_argument('--json-lines', action='store_true', default=False,
59 help='parse input using the JSON Lines format. '
60 'Use with --no-indent or --compact to produce valid JSON Lines output.')
61 group = parser.add_mutually_exclusive_group()
62 group.add_argument('--indent', default=4, type=int,
63 help='separate items with newlines and use this number '
64 'of spaces for indentation')
65 group.add_argument('--tab', action='store_const', dest='indent',
66 const='\t', help='separate items with newlines and use '
67 'tabs for indentation')
68 group.add_argument('--no-indent', action='store_const', dest='indent',
69 const=None,
70 help='separate items with spaces rather than newlines')
71 group.add_argument('--compact', action='store_true',
72 help='suppress all whitespace separation (most compact)')
73 options = parser.parse_args()
74
75 dump_args = {
76 'sort_keys': options.sort_keys,
77 'indent': options.indent,
78 'ensure_ascii': options.ensure_ascii,
79 }
80 if options.compact:
81 dump_args['indent'] = None
82 dump_args['separators'] = ',', ':'
83
84 try:
85 if options.infile == '-':
86 infile = sys.stdin
87 else:
88 infile = open(options.infile, encoding='utf-8')
89 try:
90 if options.json_lines:
91 objs = (json.loads(line) for line in infile)
92 else:
93 objs = (json.load(infile),)
94 finally:
95 if infile is not sys.stdin:
96 infile.close()
97
98 if options.outfile is None:
99 outfile = sys.stdout
100 else:
101 outfile = open(options.outfile, 'w', encoding='utf-8')

Callers 1

tool.pyFile · 0.70

Calls 13

parse_argsMethod · 0.95
can_colorizeFunction · 0.90
get_themeFunction · 0.90
_colorize_jsonFunction · 0.85
openFunction · 0.50
add_argumentMethod · 0.45
loadsMethod · 0.45
loadMethod · 0.45
closeMethod · 0.45
dumpsMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected