##// END OF EJS Templates
get-with-headers: support parsing and pretty printing JSON...
Gregory Szorc -
r24543:74740108 default
parent child Browse files
Show More
@@ -6,6 +6,14 b' a subset of the headers plus the body of'
6 import httplib, sys
6 import httplib, sys
7
7
8 try:
8 try:
9 import json
10 except ImportError:
11 try:
12 import simplejson as json
13 except ImportError:
14 json = None
15
16 try:
9 import msvcrt, os
17 import msvcrt, os
10 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
18 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
11 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
19 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
@@ -20,6 +28,10 b' headeronly = False'
20 if '--headeronly' in sys.argv:
28 if '--headeronly' in sys.argv:
21 sys.argv.remove('--headeronly')
29 sys.argv.remove('--headeronly')
22 headeronly = True
30 headeronly = True
31 formatjson = False
32 if '--json' in sys.argv:
33 sys.argv.remove('--json')
34 formatjson = True
23
35
24 reasons = {'Not modified': 'Not Modified'} # python 2.4
36 reasons = {'Not modified': 'Not Modified'} # python 2.4
25
37
@@ -44,7 +56,23 b' def request(host, path, show):'
44 if not headeronly:
56 if not headeronly:
45 print
57 print
46 data = response.read()
58 data = response.read()
47 sys.stdout.write(data)
59
60 # Pretty print JSON. This also has the beneficial side-effect
61 # of verifying emitted JSON is well-formed.
62 if formatjson:
63 if not json:
64 print 'no json module not available'
65 print 'did you forget a #require json?'
66 sys.exit(1)
67
68 # json.dumps() will print trailing newlines. Eliminate them
69 # to make tests easier to write.
70 data = json.loads(data)
71 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
72 for line in lines:
73 print line.rstrip()
74 else:
75 sys.stdout.write(data)
48
76
49 if twice and response.getheader('ETag', None):
77 if twice and response.getheader('ETag', None):
50 tag = response.getheader('ETag')
78 tag = response.getheader('ETag')
General Comments 0
You need to be logged in to leave comments. Login now