Fix actual != expected row count for items

Fixes bug #1083591

Change-Id: I162705a622c24090ce54eb7f12a65d96874611a7
This commit is contained in:
Endre Karlson 2012-11-27 14:49:47 +01:00
parent 6f280a8693
commit 168b7d736a
2 changed files with 50 additions and 4 deletions
monikerclient

@ -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 [], ()

@ -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)