Commit f551841a authored by Stavros Sachtouris's avatar Stavros Sachtouris

Improve print_list/dict

parent 71882bca
......@@ -176,7 +176,7 @@ class Shell(Cmd):
cmd_args = {}
for arg in instance.arguments.values():
cmd_args[','.join(arg.parsed_name)] = arg.help
print_dict(cmd_args, ident=14)
print_dict(cmd_args, ident=2)
stdout.write('%s %s' % (self.prompt, line))
return subcmd.get_subnames()
self._register_method(complete_method, 'complete_%s' % cmd.name)
......
......@@ -72,21 +72,35 @@ class server_list(_init_cyclades):
super(server_list, self).__init__(arguments)
self.arguments['detail'] = FlagArgument('show detailed output', '-l')
def _info_print(self, server):
addr_dict = {}
if 'attachments' in server:
for addr in server['attachments']['values']:
ips = addr.pop('values', [])
for ip in ips:
addr['IPv%s' % ip['version']] = ip['addr']
if 'firewallProfile' in addr:
addr['firewall'] = addr.pop('firewallProfile')
addr_dict[addr.pop('id')] = addr
server['attachments'] = addr_dict if addr_dict is not {} else None
if 'metadata' in server:
server['metadata'] = server['metadata']['values']
print_dict(server, ident=2)
def _print(self, servers):
for server in servers:
sname = server.pop('name')
sid = server.pop('id')
print('%s (%s)' % (bold(sname), bold(unicode(sid))))
print('%s (%s)' % (sid, bold(sname)))
if self.get_argument('detail'):
server_info._print(server)
print('- - -')
self._info_print(server)
print(' ')
def main(self):
super(self.__class__, self).main()
try:
servers = self.client.list_servers(self.get_argument('detail'))
self._print(servers)
#print_items(servers)
except ClientError as err:
raiseCLIError(err)
......@@ -109,7 +123,7 @@ class server_info(_init_cyclades):
server['attachments'] = addr_dict if addr_dict is not {} else None
if 'metadata' in server:
server['metadata'] = server['metadata']['values']
print_dict(server, ident=14)
print_dict(server, ident=2)
def main(self, server_id):
super(self.__class__, self).main()
......@@ -435,13 +449,21 @@ class flavor_list(_init_cyclades):
super(flavor_list, self).__init__(arguments)
self.arguments['detail'] = FlagArgument('show detailed output', '-l')
@classmethod
def _print(self, flist):
for i, flavor in enumerate(flist):
print(bold('%s. %s' % (i, flavor['name'])))
print_dict(flavor, exclude=('name'), ident=2)
print(' ')
def main(self):
super(self.__class__, self).main()
try:
flavors = self.client.list_flavors(self.get_argument('detail'))
except ClientError as err:
raiseCLIError(err)
print_items(flavors)
#print_list(flavors)
self._print(flavors)
@command(flavor_cmds)
......@@ -522,7 +544,7 @@ class network_info(_init_cyclades):
if 'attachments' in net:
att = net['attachments']['values']
net['attachments'] = att if len(att) > 0 else None
print_dict(net, ident=14)
print_dict(net, ident=2)
def main(self, network_id):
super(self.__class__, self).main()
......
......@@ -263,9 +263,12 @@ class image_list(_init_cyclades):
for img in images:
iname = img.pop('name')
iid = img.pop('id')
print('%s (%s)' % (bold(iname), bold(unicode(iid))))
print('%s (%s)' % (unicode(iid), bold(iname)))
if self.get_argument('detail'):
image_info._print(img)
if 'metadata' in img:
img['metadata'] = img['metadata']['values']
print_dict(img, ident=2)
print(' ')
def main(self):
super(self.__class__, self).main()
......@@ -284,7 +287,7 @@ class image_info(_init_cyclades):
def _print(self, image):
if 'metadata' in image:
image['metadata'] = image['metadata']['values']
print_dict(image, ident=14)
print_dict(image)
def main(self, image_id):
super(self.__class__, self).main()
......
......@@ -1168,7 +1168,7 @@ class store_sharers(_store_account_command):
for acc in accounts:
stdout.write(bold(acc['name']) + ' ')
if self.get_argument('detail'):
print_dict(acc, exclude='name', ident=18)
print_dict(acc, exclude='name', ident=4)
if not self.get_argument('detail'):
print
......
......@@ -69,68 +69,46 @@ def pretty_keys(d, delim='_', recurcive=False):
return new_d
def print_dict(d, exclude=(), ident=0, rjust=True):
def print_dict(d, exclude=(), ident=0):
if not isinstance(d, dict):
raise CLIError(message='Cannot dict_print a non-dict object')
if rjust:
try:
margin = max(
1 + max(len(unicode(key).strip()) for key in d.keys() \
if not (isinstance(key, dict) or isinstance(key, list))),
ident)
except ValueError:
margin = ident
else:
margin = ident
margin = max(len(unicode(key).strip())\
for key in d.keys() if key not in exclude)
for key, val in sorted(d.items()):
if key in exclude:
continue
print_str = '%s:\t' % unicode(key).strip()
print_str = print_str.rjust(margin) if rjust\
else '%s%s' % (' ' * margin, print_str)
print_str = ' ' * ident
print_str += ('%s' % key).strip()
print_str += ' ' * (margin - len(unicode(key).strip()))
print_str += ': '
if isinstance(val, dict):
print(print_str + ' {')
print_dict(val, exclude=exclude, ident=margin + 6, rjust=rjust)
if rjust:
print '}'.rjust(margin)
else:
print '}'
print(print_str)
print_dict(val, exclude=exclude, ident=margin + ident)
elif isinstance(val, list):
print(print_str + ' [')
print_list(val, exclude=exclude, ident=margin + 6, rjust=rjust)
if rjust:
print ']'.rjust(margin)
else:
print']'
print(print_str)
print_list(val, exclude=exclude, ident=margin + ident)
else:
print print_str + ' ' + unicode(val).strip()
def print_list(l, exclude=(), ident=0, rjust=True):
def print_list(l, exclude=(), ident=0):
if not isinstance(l, list):
raise CLIError(message='Cannot list_print a non-list object')
try:
margin = max(
1 + max(len(unicode(item).strip()) for item in l \
if not isinstance(item, dict) and not isinstance(item, list)),
ident)
except ValueError:
margin = ident
margin = max(len(unicode(item).strip())\
for item in l if item not in exclude)
for item in sorted(l):
if item in exclude:
continue
if isinstance(item, dict):
print '{'.rjust(margin) if rjust else '{'
print_dict(item, exclude=exclude, ident=margin + 6)
print '}'.rjust(margin) if rjust else '}'
print_dict(item, exclude=exclude, ident=margin + ident)
elif isinstance(item, list):
print '['.rjust(margin) if rjust else ']'
print_list(item, exclude=exclude, ident=margin + 6)
print ']'.rjust(margin) if rjust else ']'
print_list(item, exclude=exclude, ident=margin + ident)
else:
print unicode(item).rjust(margin) if rjust else unicode(item)
print ' ' * ident + unicode(item)
def print_items(items, title=('id', 'name')):
......
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