Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5600afa

Browse files
committed
Move main function from json.__main__ to json.tool
1 parent ae4ca62 commit 5600afa

3 files changed

Lines changed: 73 additions & 78 deletions

File tree

Lib/json/__main__.py

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,20 @@
22
33
Usage::
44
5-
$ echo '{"json":"obj"}' | python -m json.tool
5+
$ echo '{"json":"obj"}' | python -m json
66
{
77
"json": "obj"
88
}
9-
$ echo '{ 1.2:3.4}' | python -m json.tool
9+
$ echo '{ 1.2:3.4}' | python -m json
1010
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
1111
1212
"""
13-
import argparse
14-
import json
13+
import json.tool
1514
import sys
1615

1716

18-
def main():
19-
prog = 'python -m json.tool'
20-
description = ('A simple command line interface for json module '
21-
'to validate and pretty-print JSON objects.')
22-
parser = argparse.ArgumentParser(prog=prog, description=description)
23-
parser.add_argument('infile', nargs='?',
24-
help='a JSON file to be validated or pretty-printed',
25-
default='-')
26-
parser.add_argument('outfile', nargs='?',
27-
help='write the output of infile to outfile',
28-
default=None)
29-
parser.add_argument('--sort-keys', action='store_true', default=False,
30-
help='sort the output of dictionaries alphabetically by key')
31-
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
32-
help='disable escaping of non-ASCII characters')
33-
parser.add_argument('--json-lines', action='store_true', default=False,
34-
help='parse input using the JSON Lines format. '
35-
'Use with --no-indent or --compact to produce valid JSON Lines output.')
36-
group = parser.add_mutually_exclusive_group()
37-
group.add_argument('--indent', default=4, type=int,
38-
help='separate items with newlines and use this number '
39-
'of spaces for indentation')
40-
group.add_argument('--tab', action='store_const', dest='indent',
41-
const='\t', help='separate items with newlines and use '
42-
'tabs for indentation')
43-
group.add_argument('--no-indent', action='store_const', dest='indent',
44-
const=None,
45-
help='separate items with spaces rather than newlines')
46-
group.add_argument('--compact', action='store_true',
47-
help='suppress all whitespace separation (most compact)')
48-
options = parser.parse_args()
49-
50-
dump_args = {
51-
'sort_keys': options.sort_keys,
52-
'indent': options.indent,
53-
'ensure_ascii': options.ensure_ascii,
54-
}
55-
if options.compact:
56-
dump_args['indent'] = None
57-
dump_args['separators'] = ',', ':'
58-
59-
try:
60-
if options.infile == '-':
61-
infile = sys.stdin
62-
else:
63-
infile = open(options.infile, encoding='utf-8')
64-
try:
65-
if options.json_lines:
66-
objs = (json.loads(line) for line in infile)
67-
else:
68-
objs = (json.load(infile),)
69-
finally:
70-
if infile is not sys.stdin:
71-
infile.close()
72-
73-
if options.outfile is None:
74-
outfile = sys.stdout
75-
else:
76-
outfile = open(options.outfile, 'w', encoding='utf-8')
77-
with outfile:
78-
for obj in objs:
79-
json.dump(obj, outfile, **dump_args)
80-
outfile.write('\n')
81-
except ValueError as e:
82-
raise SystemExit(e)
83-
84-
8517
if __name__ == '__main__':
8618
try:
87-
main()
19+
json.tool.main()
8820
except BrokenPipeError as exc:
8921
sys.exit(exc.errno)

Lib/json/tool.py

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,78 @@
1+
import argparse
2+
import json
13
import sys
24
import warnings
35

4-
from . import __main__
5-
66

77
def main():
8-
warnings.warn('The json.tool module is deprecated',
9-
DeprecationWarning, stacklevel=2)
10-
__main__.main()
8+
prog = 'python -m json.tool'
9+
description = ('A simple command line interface for json module '
10+
'to validate and pretty-print JSON objects.')
11+
parser = argparse.ArgumentParser(prog=prog, description=description)
12+
parser.add_argument('infile', nargs='?',
13+
help='a JSON file to be validated or pretty-printed',
14+
default='-')
15+
parser.add_argument('outfile', nargs='?',
16+
help='write the output of infile to outfile',
17+
default=None)
18+
parser.add_argument('--sort-keys', action='store_true', default=False,
19+
help='sort the output of dictionaries alphabetically by key')
20+
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
21+
help='disable escaping of non-ASCII characters')
22+
parser.add_argument('--json-lines', action='store_true', default=False,
23+
help='parse input using the JSON Lines format. '
24+
'Use with --no-indent or --compact to produce valid JSON Lines output.')
25+
group = parser.add_mutually_exclusive_group()
26+
group.add_argument('--indent', default=4, type=int,
27+
help='separate items with newlines and use this number '
28+
'of spaces for indentation')
29+
group.add_argument('--tab', action='store_const', dest='indent',
30+
const='\t', help='separate items with newlines and use '
31+
'tabs for indentation')
32+
group.add_argument('--no-indent', action='store_const', dest='indent',
33+
const=None,
34+
help='separate items with spaces rather than newlines')
35+
group.add_argument('--compact', action='store_true',
36+
help='suppress all whitespace separation (most compact)')
37+
options = parser.parse_args()
38+
39+
dump_args = {
40+
'sort_keys': options.sort_keys,
41+
'indent': options.indent,
42+
'ensure_ascii': options.ensure_ascii,
43+
}
44+
if options.compact:
45+
dump_args['indent'] = None
46+
dump_args['separators'] = ',', ':'
47+
48+
try:
49+
if options.infile == '-':
50+
infile = sys.stdin
51+
else:
52+
infile = open(options.infile, encoding='utf-8')
53+
try:
54+
if options.json_lines:
55+
objs = (json.loads(line) for line in infile)
56+
else:
57+
objs = (json.load(infile),)
58+
finally:
59+
if infile is not sys.stdin:
60+
infile.close()
61+
62+
if options.outfile is None:
63+
outfile = sys.stdout
64+
else:
65+
outfile = open(options.outfile, 'w', encoding='utf-8')
66+
with outfile:
67+
for obj in objs:
68+
json.dump(obj, outfile, **dump_args)
69+
outfile.write('\n')
70+
except ValueError as e:
71+
raise SystemExit(e)
1172

1273

1374
if __name__ == '__main__':
75+
warnings.warn('The json.tool module is deprecated', DeprecationWarning)
1476
try:
1577
main()
1678
except BrokenPipeError as exc:

Lib/test/test_json/test_tool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,5 @@ def test_with_warnings(self):
254254
rc, out, err = assert_python_ok('-m', self.module, infile)
255255
self.assertEqual(rc, 0)
256256
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
257-
self.assertEqual(err.decode(), f'{filename}:15: DeprecationWarning: The json.tool module is deprecated\n main()\n')
257+
self.assertRegex(err.decode(),
258+
r'^.+/json/tool\.py:\d+: DeprecationWarning: The json.tool module is deprecated\n.+\n$')

0 commit comments

Comments
 (0)