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

View File

@ -17,6 +17,7 @@ import abc
from cliff.command import Command as CliffCommand from cliff.command import Command as CliffCommand
from cliff.lister import Lister from cliff.lister import Lister
from cliff.show import ShowOne from cliff.show import ShowOne
from monikerclient import utils
from monikerclient.v1 import Client from monikerclient.v1 import Client
@ -67,10 +68,9 @@ class Command(CliffCommand):
class ListCommand(Command, Lister): class ListCommand(Command, Lister):
def post_execute(self, results): def post_execute(self, results):
if len(results) > 0: if len(results) > 0:
column_names = results[0].keys() columns = utils.get_columns(results)
data = [r.values() for r in results] data = [utils.get_item_properties(i, columns) for i in results]
return columns, data
return column_names, data
else: else:
return [], () return [], ()

View File

@ -36,3 +36,49 @@ def load_schema(version, name):
schema_string = resource_string('schemas', version, '%s.json' % name) schema_string = resource_string('schemas', version, '%s.json' % name)
return json.loads(schema_string) 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)