From 168b7d736a9974251edaea3e67b25e98d7f2f217 Mon Sep 17 00:00:00 2001 From: Endre Karlson Date: Tue, 27 Nov 2012 14:49:47 +0100 Subject: [PATCH] Fix actual != expected row count for items Fixes bug #1083591 Change-Id: I162705a622c24090ce54eb7f12a65d96874611a7 --- monikerclient/cli/base.py | 8 +++---- monikerclient/utils.py | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/monikerclient/cli/base.py b/monikerclient/cli/base.py index a5d7993..8ab1a45 100644 --- a/monikerclient/cli/base.py +++ b/monikerclient/cli/base.py @@ -17,6 +17,7 @@ import abc from cliff.command import Command as CliffCommand from cliff.lister import Lister from cliff.show import ShowOne +from monikerclient import utils from monikerclient.v1 import Client @@ -67,10 +68,9 @@ class Command(CliffCommand): class ListCommand(Command, Lister): def post_execute(self, results): if len(results) > 0: - column_names = results[0].keys() - data = [r.values() for r in results] - - return column_names, data + columns = utils.get_columns(results) + data = [utils.get_item_properties(i, columns) for i in results] + return columns, data else: return [], () diff --git a/monikerclient/utils.py b/monikerclient/utils.py index dba207c..5501dc9 100644 --- a/monikerclient/utils.py +++ b/monikerclient/utils.py @@ -36,3 +36,49 @@ def load_schema(version, name): schema_string = resource_string('schemas', version, '%s.json' % name) return json.loads(schema_string) + + +def get_item_properties(item, fields, mixed_case_fields=[], formatters={}): + """Return a tuple containing the item properties. + + :param item: a single item resource (e.g. Server, Tenant, etc) + :param fields: tuple of strings with the desired field names + :param mixed_case_fields: tuple of field names to preserve case + :param formatters: dictionary mapping field names to callables + to format the values + """ + row = [] + + for field in fields: + if field in formatters: + row.append(formatters[field](item)) + else: + if field in mixed_case_fields: + field_name = field.replace(' ', '_') + else: + field_name = field.lower().replace(' ', '_') + if not hasattr(item, field_name) and \ + (isinstance(item, dict) and field_name in item): + data = item[field_name] + else: + data = getattr(item, field_name, '') + if data is None: + data = '' + row.append(data) + return tuple(row) + + +def get_columns(data): + """ + Some row's might have variable count of columns, ensure that we have the + same. + + :param data: Results in [{}, {]}] + """ + columns = set() + + def _seen(col): + columns.add(str(col)) + + map(lambda item: map(_seen, item.keys()), data) + return list(columns)