Commit c19f938c authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Merge branch 'feature-parametrize-output-format' into develop

parents 3eb7eaef 5a9c2b17
......@@ -32,4 +32,5 @@ Features:
- Add -R/--recursive in file listing, to list containers with their contents
[#4301]
- Add -w/--wait to server firewall set [#4298]
- Add --output-format where applicable, keep -j/--json too, for bw
compatibility [#4342]
......@@ -36,6 +36,7 @@ from kamaki.cli.utils import (
print_list, print_dict, print_json, print_items, ask_user,
filter_dicts_by_dict)
from kamaki.cli.argument import FlagArgument, ValueArgument
from kamaki.cli.errors import CLIInvalidArgument
from sys import stdin, stdout, stderr
log = get_logger(__name__)
......@@ -242,11 +243,38 @@ class _command_init(object):
# feature classes - inherit them to get special features for your commands
class OutputFormatArgument(ValueArgument):
"""Accepted output formats: json (default)"""
formats = ('json', )
def ___init__(self, *args, **kwargs):
super(OutputFormatArgument, self).___init__(*args, **kwargs)
@property
def value(self):
return self._value
@value.setter
def value(self, newvalue):
if not newvalue:
self._value = self.default
elif newvalue.lower() in self.formats:
self._value = newvalue.lower
else:
raise CLIInvalidArgument(
'Invalid value %s for argument %s' % (
newvalue, '/'.join(self.parsed_name)),
details=['Valid output formats: %s' % ', '.join(self.formats)])
class _optional_output_cmd(object):
oo_arguments = dict(
with_output=FlagArgument('show response headers', ('--with-output')),
json_output=FlagArgument('show headers in json', ('-j', '--json'))
json_output=FlagArgument(
'show headers in json (DEPRECATED from v0.12,'
' please use --output-format=json instead)', ('-j', '--json'))
)
def _optional_output(self, r):
......@@ -259,11 +287,17 @@ class _optional_output_cmd(object):
class _optional_json(object):
oj_arguments = dict(
json_output=FlagArgument('show headers in json', ('-j', '--json'))
output_format=OutputFormatArgument(
'Show output in chosen output format (%s)' % ', '.join(
OutputFormatArgument.formats),
'--output-format'),
json_output=FlagArgument(
'show output in json (DEPRECATED from v0.12,'
' please use --output-format instead)', ('-j', '--json'))
)
def _print(self, output, print_method=print_items, **print_method_kwargs):
if self['json_output']:
if self['json_output'] or self['output_format']:
print_json(output, out=self._out)
else:
print_method_kwargs.setdefault('out', self._out)
......
......@@ -251,9 +251,11 @@ class server_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
if withmeta:
servers = self._filter_by_metadata(servers)
if self['detail'] and not self['json_output']:
if self['detail'] and not (
self['json_output'] or self['output_format']):
servers = self._add_user_name(servers)
elif not (self['detail'] or self['json_output']):
elif not (self['detail'] or (
self['json_output'] or self['output_format'])):
remove_from_items(servers, 'links')
if detail and not self['detail']:
for srv in servers:
......@@ -797,7 +799,8 @@ class flavor_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
flavors = self._filter_by_id(flavors)
if withcommons:
flavors = self._apply_common_filters(flavors)
if not (self['detail'] or self['json_output']):
if not (self['detail'] or (
self['json_output'] or self['output_format'])):
remove_from_items(flavors, 'links')
if detail and not self['detail']:
for flv in flavors:
......@@ -941,13 +944,15 @@ class network_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
networks = self._filter_by_id(networks)
if withcommons:
networks = self._apply_common_filters(networks)
if not (self['detail'] or self['json_output']):
if not (self['detail'] or (
self['json_output'] or self['output_format'])):
remove_from_items(networks, 'links')
if detail and not self['detail']:
for net in networks:
for key in set(net).difference(self.PERMANENTS):
net.pop(key)
if self['detail'] and not self['json_output']:
if self['detail'] and not (
self['json_output'] or self['output_format']):
self._add_name(networks)
self._add_name(networks, 'tenant_id')
kwargs = dict(with_enumeration=self['enum'])
......@@ -1130,7 +1135,8 @@ class ip_pools(_init_cyclades, _optional_json):
@errors.cyclades.connection
def _run(self):
r = self.client.get_floating_ip_pools()
self._print(r if self['json_output'] else r['floating_ip_pools'])
self._print(r if self['json_output'] or self['output_format'] else r[
'floating_ip_pools'])
def main(self):
super(self.__class__, self)._run()
......@@ -1145,7 +1151,8 @@ class ip_list(_init_cyclades, _optional_json):
@errors.cyclades.connection
def _run(self):
r = self.client.get_floating_ips()
self._print(r if self['json_output'] else r['floating_ips'])
self._print(r if self['json_output'] or self['output_format'] else r[
'floating_ips'])
def main(self):
super(self.__class__, self)._run()
......
......@@ -261,7 +261,8 @@ class image_list(_init_image, _optional_json, _name_filter, _id_filter):
images = self._filter_by_id(images)
images = self._non_exact_name_filter(images)
if self['detail'] and not self['json_output']:
if self['detail'] and not (
self['json_output'] or self['output_format']):
images = self._add_owner_name(images)
elif detail and not self['detail']:
for img in images:
......@@ -301,7 +302,7 @@ class image_info(_init_image, _optional_json):
@errors.plankton.id
def _run(self, image_id):
meta = self.client.get_meta(image_id)
if not self['json_output']:
if not (self['json_output'] or self['output_format']):
meta['owner'] += ' (%s)' % self._uuid2username(meta['owner'])
self._print(meta, self.print_dict)
......@@ -639,7 +640,7 @@ class image_register(_init_image, _optional_json):
self.error(
'Failed to dump metafile %s:%s' % (dst_cont, meta_path))
return
if self['json_output']:
if self['json_output'] or self['output_format']:
self.print_json(dict(
metafile_location='%s:%s' % (dst_cont, meta_path),
headers=meta_headers))
......@@ -699,7 +700,7 @@ class image_members_list(_init_image, _optional_json):
@errors.plankton.id
def _run(self, image_id):
members = self.client.list_members(image_id)
if not self['json_output']:
if not (self['json_output'] or self['output_format']):
uuids = [member['member_id'] for member in members]
usernames = self._uuids2usernames(uuids)
for member in members:
......@@ -821,7 +822,8 @@ class image_compute_list(
images = self._filter_by_user(images)
if withmeta:
images = self._filter_by_metadata(images)
if self['detail'] and not self['json_output']:
if self['detail'] and not (
self['json_output'] or self['output_format']):
images = self._add_name(self._add_name(images, 'tenant_id'))
elif detail and not self['detail']:
for img in images:
......
......@@ -477,7 +477,7 @@ class file_list(_file_container_command, _optional_json, _name_filter):
if self['more']:
outbu, self._out = self._out, StringIO()
try:
if self['json_output']:
if self['json_output'] or self['output_format']:
self._print(files)
else:
prnt(files)
......@@ -1169,7 +1169,8 @@ class file_upload(_file_container_command, _optional_output_cmd):
rpath, f,
etag=self['etag'], withHashFile=self['use_hashes'],
**params)
if self['with_output'] or self['json_output']:
if self['with_output'] or (
self['json_output'] or self['output_format']):
r['name'] = '%s: %s' % (self.client.container, rpath)
uploaded.append(r)
else:
......@@ -1188,7 +1189,8 @@ class file_upload(_file_container_command, _optional_output_cmd):
upload_cb=upload_cb,
container_info_cache=container_info_cache,
**params)
if self['with_output'] or self['json_output']:
if self['with_output'] or (
self['json_output'] or self['output_format']):
r['name'] = '%s: %s' % (self.client.container, rpath)
uploaded.append(r)
except Exception:
......@@ -2068,7 +2070,7 @@ class file_sharers(_file_account_command, _optional_json):
@errors.pithos.connection
def _run(self):
accounts = self.client.get_sharing_accounts(marker=self['marker'])
if not self['json_output']:
if not (self['json_output'] or self['output_format']):
usernames = self._uuids2usernames(
[acc['name'] for acc in accounts])
for item in accounts:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment