summaryrefslogtreecommitdiff
path: root/troveclient/utils.py
diff options
context:
space:
mode:
authorPeter Stachowski <peter@tesora.com>2014-04-23 16:05:37 -0400
committerPeter Stachowski <peter@tesora.com>2014-04-24 14:58:08 -0400
commitc2b501d91b507445f5cc3b50c0bfdab8f9b609c7 (patch)
tree03ebe8c0d18304262329733639992e1108f6cc23 /troveclient/utils.py
parent4d2b11adc95edc9c405db0d938531295bf71a4a4 (diff)
downloadpython-troveclient-c2b501d91b507445f5cc3b50c0bfdab8f9b609c7.tar.gz
Changed Trove CLI list headers to match Nova CLI
Added ability to pass in header labels to print_list. If not passed in, header lables are automatically created from the fields by replacing '_' with spaces and capitalizing each word. IDs are also capitalized. All field data is now left-aligned, except numerics which are right-aligned. Also changed print_dict so that all values are left-aligned. Change-Id: I3b69dc7f92c496594cf37832f4ff98f1abdf52dd Closes-Bug: #1272700
Diffstat (limited to 'troveclient/utils.py')
-rw-r--r--troveclient/utils.py57
1 files changed, 38 insertions, 19 deletions
diff --git a/troveclient/utils.py b/troveclient/utils.py
index b5e3965..936f1e0 100644
--- a/troveclient/utils.py
+++ b/troveclient/utils.py
@@ -138,35 +138,54 @@ def _print(pt, order):
print(strutils.safe_encode(pt.get_string(sortby=order)))
-def print_list(objs, fields, formatters={}, order_by=None, obj_is_dict=False):
+def print_list(objs, fields, formatters={}, order_by=None, obj_is_dict=False,
+ labels={}):
try:
_output_override(objs, 'list')
return
except BaseException:
pass
- mixed_case_fields = []
- pt = prettytable.PrettyTable([f for f in fields], caching=False)
- pt.aligns = ['l' for f in fields]
-
- for o in objs:
+ # Make nice labels from the fields, if not provided in the labels arg
+ if not labels:
+ labels = {}
+ for field in fields:
+ if field not in labels:
+ # No underscores (use spaces instead) and uppercase any ID's
+ label = field.replace("_", " ").replace("id", "ID")
+ # Uppercase anything else that's less than 3 chars
+ if len(label) < 3:
+ label = label.upper()
+ # Capitalize each word otherwise
+ else:
+ label = ' '.join(word[0].upper() + word[1:]
+ for word in label.split())
+ labels[field] = label
+
+ pt = prettytable.PrettyTable(
+ [labels[field] for field in fields], caching=False)
+ # set the default alignment to left-aligned
+ align = dict((labels[field], 'l') for field in fields)
+ set_align = True
+ for obj in objs:
row = []
for field in fields:
- if field in formatters:
- row.append(formatters[field](o))
+ if formatters and field in formatters:
+ row.append(formatters[field](obj))
+ elif obj_is_dict:
+ data = obj.get(field, '')
else:
- if field in mixed_case_fields:
- field_name = field.replace(' ', '_')
- else:
- field_name = field.lower().replace(' ', '_')
- if not obj_is_dict:
- data = getattr(o, field_name, '')
- else:
- data = o.get(field_name, '')
- row.append(data)
+ data = getattr(obj, field, '')
+ row.append(data)
+ # set the alignment to right-aligned if it's a numeric
+ if set_align and hasattr(data, '__int__'):
+ align[labels[field]] = 'r'
+ set_align = False
pt.add_row(row)
+ pt._align = align
- if order_by is None:
+ if not order_by:
order_by = fields[0]
+ order_by = labels[order_by]
_print(pt, order_by)
@@ -177,7 +196,7 @@ def print_dict(d, property="Property"):
except BaseException:
pass
pt = prettytable.PrettyTable([property, 'Value'], caching=False)
- pt.aligns = ['l', 'l']
+ pt.align = 'l'
[pt.add_row(list(r)) for r in six.iteritems(d)]
_print(pt, property)