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__)
# 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):
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)
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:
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:
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'])
@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()
@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()
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:
@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)
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))
@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:
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:
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)
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:
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:
@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: