diff options
author | Zuul <zuul@review.opendev.org> | 2019-07-10 18:54:30 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2019-07-10 18:54:30 +0000 |
commit | 2fcd4d872713dc30e7352845c37515280f1d21ab (patch) | |
tree | 76024c37110dfa755e8d0e752b1537c152aba3cf /swiftclient/shell.py | |
parent | 7cf8541b38a8782decdd27cb37909e737ed67b8a (diff) | |
parent | 936631eac617c83bec1f1c44b1adcaa51d36329c (diff) | |
download | python-swiftclient-2fcd4d872713dc30e7352845c37515280f1d21ab.tar.gz |
Merge "Optionally display listings in raw json"
Diffstat (limited to 'swiftclient/shell.py')
-rwxr-xr-x | swiftclient/shell.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py index a322a1c..cc4f325 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -33,7 +33,8 @@ from sys import argv as sys_argv, exit, stderr, stdin from time import gmtime, strftime from swiftclient import RequestException -from swiftclient.utils import config_true_value, generate_temp_url, prt_bytes +from swiftclient.utils import config_true_value, generate_temp_url, \ + prt_bytes, JSONableIterable from swiftclient.multithreading import OutputManager from swiftclient.exceptions import ClientException from swiftclient import __version__ as client_version @@ -578,6 +579,8 @@ def st_list(parser, args, output_manager, return_parser=False): help='Roll up items with the given delimiter. For containers ' 'only. See OpenStack Swift API documentation for ' 'what this means.') + parser.add_argument('-j', '--json', action='store_true', + help='print listing information in json') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], @@ -616,6 +619,20 @@ def st_list(parser, args, output_manager, return_parser=False): else: stats_parts_gen = swift.list(container=container) + if options.get('json', False): + def listing(stats_parts_gen=stats_parts_gen): + for stats in stats_parts_gen: + if stats["success"]: + for item in stats['listing']: + yield item + else: + raise stats["error"] + + json.dump( + JSONableIterable(listing()), output_manager.print_stream, + sort_keys=True, indent=2) + output_manager.print_msg('') + return for stats in stats_parts_gen: if stats["success"]: _print_stats(options, stats, human) |