Mercurial > hg-stable
changeset 24543:747401086a38
get-with-headers: support parsing and pretty printing JSON
Upcoming patches will add support for JSON output from hgweb.
Because JSON output from the templater is hard to read and because it
is easy to introduce malformed JSON, we introduce a JSON processing
mode to get-with-headers.py that will parse and pretty print JSON
from HTTP responses. This will make tests easier to read and write
and it will ensure hgweb is emitting well-formed JSON.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 30 Mar 2015 20:56:54 -0700 |
parents | 9fbda55c68d7 |
children | 71e96b9fd3fd |
files | tests/get-with-headers.py |
diffstat | 1 files changed, 29 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/tests/get-with-headers.py Tue Mar 31 16:14:14 2015 -0500 +++ b/tests/get-with-headers.py Mon Mar 30 20:56:54 2015 -0700 @@ -6,6 +6,14 @@ import httplib, sys try: + import json +except ImportError: + try: + import simplejson as json + except ImportError: + json = None + +try: import msvcrt, os msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) @@ -20,6 +28,10 @@ if '--headeronly' in sys.argv: sys.argv.remove('--headeronly') headeronly = True +formatjson = False +if '--json' in sys.argv: + sys.argv.remove('--json') + formatjson = True reasons = {'Not modified': 'Not Modified'} # python 2.4 @@ -44,7 +56,23 @@ if not headeronly: print data = response.read() - sys.stdout.write(data) + + # Pretty print JSON. This also has the beneficial side-effect + # of verifying emitted JSON is well-formed. + if formatjson: + if not json: + print 'no json module not available' + print 'did you forget a #require json?' + sys.exit(1) + + # json.dumps() will print trailing newlines. Eliminate them + # to make tests easier to write. + data = json.loads(data) + lines = json.dumps(data, sort_keys=True, indent=2).splitlines() + for line in lines: + print line.rstrip() + else: + sys.stdout.write(data) if twice and response.getheader('ETag', None): tag = response.getheader('ETag')