From 959a5a80152ad8b29a5f1facc0139a46743dc767 Mon Sep 17 00:00:00 2001 From: Piotr Popieluch Date: Sun, 16 Apr 2017 00:01:25 +0200 Subject: [PATCH] whisper-info: add --json output option fixes #159 --- bin/whisper-info.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/bin/whisper-info.py b/bin/whisper-info.py index a20831b1..945e516b 100755 --- a/bin/whisper-info.py +++ b/bin/whisper-info.py @@ -4,6 +4,7 @@ import sys import signal import optparse +import json try: import whisper @@ -17,7 +18,9 @@ #OS=windows pass -option_parser = optparse.OptionParser(usage='''%prog path [field]''') +option_parser = optparse.OptionParser(usage='''%prog [options] path [field]''') +option_parser.add_option('--json', default=False, action='store_true', + help="Output results in JSON form") (options, args) = option_parser.parse_args() if len(args) < 1: @@ -45,14 +48,16 @@ print(info[field]) sys.exit(0) - -archives = info.pop('archives') -for key,value in info.items(): - print('%s: %s' % (key,value)) -print - -for i,archive in enumerate(archives): - print('Archive %d' % i) - for key,value in archive.items(): +if options.json: + print(json.dumps(info, indent=2, separators=(',', ': '))) +else: + archives = info.pop('archives') + for key,value in info.items(): print('%s: %s' % (key,value)) - print + print('') + + for i,archive in enumerate(archives): + print('Archive %d' % i) + for key,value in archive.items(): + print('%s: %s' % (key,value)) + print('')