ability to format dicts
This commit is contained in:
parent
4fe8f13984
commit
f7214e5e62
5 changed files with 51 additions and 30 deletions
|
|
@ -33,9 +33,7 @@ class CSVFormatter(IFormatter):
|
|||
def flush(self):
|
||||
self.started = False
|
||||
|
||||
def format_obj(self, obj, alias):
|
||||
item = self.to_dict(obj)
|
||||
|
||||
def format_dict(self, item):
|
||||
result = u''
|
||||
if not self.started:
|
||||
result += self.field_separator.join(item.iterkeys()) + '\n'
|
||||
|
|
|
|||
|
|
@ -124,46 +124,74 @@ class IFormatter(object):
|
|||
An object has fields which can be selected.
|
||||
|
||||
:param obj: object to format
|
||||
:type obj: CapBaseObject
|
||||
:type obj: CapBaseObject or dict
|
||||
:param selected_fields: fields to display. If None, all fields are selected
|
||||
:type selected_fields: tuple
|
||||
:param alias: an alias to use instead of the object's ID
|
||||
:type alias: unicode
|
||||
"""
|
||||
assert isinstance(obj, (dict,CapBaseObject)), 'Object is unexpected type "%r"' % obj
|
||||
if isinstance(obj, CapBaseObject):
|
||||
if selected_fields is not None and not '*' in selected_fields:
|
||||
obj = obj.copy()
|
||||
for name, value in obj.iter_fields():
|
||||
if not name in selected_fields:
|
||||
delattr(obj, name)
|
||||
|
||||
if selected_fields is not None and not '*' in selected_fields:
|
||||
obj = obj.copy()
|
||||
for name, value in obj.iter_fields():
|
||||
if not name in selected_fields:
|
||||
delattr(obj, name)
|
||||
if self.MANDATORY_FIELDS:
|
||||
missing_fields = set(self.MANDATORY_FIELDS) - set([name for name, value in obj.iter_fields()])
|
||||
if missing_fields:
|
||||
raise MandatoryFieldsNotFound(missing_fields)
|
||||
|
||||
if self.MANDATORY_FIELDS:
|
||||
missing_fields = set(self.MANDATORY_FIELDS) - set([name for name, value in obj.iter_fields()])
|
||||
if missing_fields:
|
||||
raise MandatoryFieldsNotFound(missing_fields)
|
||||
formatted = self.format_obj(obj, alias)
|
||||
else:
|
||||
obj = self.to_dict(obj)
|
||||
|
||||
if selected_fields is not None and not '*' in selected_fields:
|
||||
obj = obj.copy()
|
||||
for name, value in obj.iteritems():
|
||||
if not name in selected_fields:
|
||||
obj.pop(name)
|
||||
|
||||
if self.MANDATORY_FIELDS:
|
||||
missing_fields = set(self.MANDATORY_FIELDS) - set(obj.iterkeys())
|
||||
if missing_fields:
|
||||
raise MandatoryFieldsNotFound(missing_fields)
|
||||
|
||||
formatted = self.format_dict(obj)
|
||||
|
||||
formatted = self.format_obj(obj, alias)
|
||||
if formatted:
|
||||
self.output(formatted)
|
||||
return formatted
|
||||
|
||||
|
||||
def format_obj(self, obj, alias=None):
|
||||
"""
|
||||
Format an object to be human-readable.
|
||||
Format a dict to be human-readable. The dict is already simplified
|
||||
if user provides selected fields.
|
||||
Called by format().
|
||||
This method has to be overridden in child classes.
|
||||
|
||||
@param item [dict] item to format
|
||||
@return a string of the formatted dict
|
||||
:param obj: object to format
|
||||
:type obj: CapBaseObject
|
||||
:rtype: str
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
return self.format_dict(self.to_dict(obj))
|
||||
|
||||
def format_dict(self, obj):
|
||||
"""
|
||||
Format a dict to be human-readable.
|
||||
|
||||
:param obj: dict to format
|
||||
:type obj: dict
|
||||
:rtype: str
|
||||
"""
|
||||
return NotImplementedError()
|
||||
|
||||
def to_dict(self, obj):
|
||||
if isinstance(obj, dict):
|
||||
return obj
|
||||
if not isinstance(obj, CapBaseObject):
|
||||
try:
|
||||
return OrderedDict(obj)
|
||||
except ValueError:
|
||||
raise TypeError('Please give a CapBaseObject or a dict')
|
||||
|
||||
def iter_decorate(d):
|
||||
for key, value in d:
|
||||
|
|
|
|||
|
|
@ -34,9 +34,7 @@ class MultilineFormatter(IFormatter):
|
|||
def flush(self):
|
||||
pass
|
||||
|
||||
def format_obj(self, obj, alias):
|
||||
item = self.to_dict(obj)
|
||||
|
||||
def format_dict(self, item):
|
||||
result = u'\n'.join(u'%s%s' % (
|
||||
(u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), v)
|
||||
for k, v in item.iteritems() if (v is not NotLoaded and v is not NotAvailable))
|
||||
|
|
|
|||
|
|
@ -30,8 +30,7 @@ class SimpleFormatter(IFormatter):
|
|||
self.field_separator = field_separator
|
||||
self.key_value_separator = key_value_separator
|
||||
|
||||
def format_obj(self, obj, alias):
|
||||
item = self.to_dict(obj)
|
||||
def format_dict(self, item):
|
||||
return self.field_separator.join(u'%s%s' % (
|
||||
(u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), v)
|
||||
for k, v in item.iteritems())
|
||||
|
|
|
|||
|
|
@ -82,9 +82,7 @@ class TableFormatter(IFormatter):
|
|||
|
||||
return s
|
||||
|
||||
def format_obj(self, obj, alias):
|
||||
item = self.to_dict(obj)
|
||||
|
||||
def format_dict(self, item):
|
||||
if self.keys is None:
|
||||
self.keys = item.keys()
|
||||
self.queue.append(item.values())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue