make print_dict split strings with newlines into multiple rows

When printing a dict e.g. "nova show" one of the properties
is "fault". When the user is admin, "details" of the fault
is shown, which includes a backtrace. This causes the
printing of the table to be unreadable as the backtrace
overflows the table row. This patch adds a separate row
for each substring after splitting on newlines.

Change-Id: I4c1bc8725a2bb6970be2c884c5e044d9eade8302
This commit is contained in:
Melanie Witt 2013-01-10 01:27:33 +00:00
parent 5b8099cd0e
commit f9aa5ec834

@ -167,8 +167,21 @@ def print_list(objs, fields, formatters={}, sortby_index=0):
def print_dict(d, dict_property="Property"):
pt = prettytable.PrettyTable([dict_property, 'Value'], caching=False)
pt.align = 'l'
[pt.add_row(list(r)) for r in d.iteritems()]
print pt.get_string(sortby=dict_property)
for k, v in d.iteritems():
# convert dict to str to check length
if isinstance(v, dict):
v = str(v)
# if value has a newline, add in multiple rows
# e.g. fault with stacktrace
if v and isinstance(v, basestring) and r'\n' in v:
lines = v.strip().split(r'\n')
col1 = k
for line in lines:
pt.add_row([col1, line])
col1 = ''
else:
pt.add_row([k, v])
print pt.get_string()
def find_resource(manager, name_or_id):