enhance formatters, add table formatter

This commit is contained in:
Christophe Benz 2010-06-02 02:01:33 +02:00
commit 676a95047d
6 changed files with 98 additions and 32 deletions

View file

@ -154,22 +154,20 @@ class ConsoleApplication(BaseApplication):
logging.error(errors) logging.error(errors)
return 1 return 1
# Process result self._formatter.flush()
if isinstance(command_result, Results):
self.format(command_result) # Process result if value is returned by command
return 0 if isinstance(command_result, (str, unicode)):
elif isinstance(command_result, (str, unicode)):
print command_result print command_result
return 0
elif isinstance(command_result, int): elif isinstance(command_result, int):
return command_result return command_result
elif command_result is None:
return 0
else: else:
try: try:
print unicode(command_result) print unicode(command_result)
except ValueError: except ValueError:
raise Exception('command_result type not expected: %s' % type(command_result)) raise Exception(u'Command result type not expected: %s' % type(command_result))
return 0
_commands = [] _commands = []
def register_command(f, doc_string, register_to=_commands): def register_command(f, doc_string, register_to=_commands):
@ -200,9 +198,7 @@ class ConsoleApplication(BaseApplication):
def format(self, result): def format(self, result):
try: try:
result = self._formatter.format(result, selected_fields=self.selected_fields, condition=self.condition) self._formatter.format(result, selected_fields=self.selected_fields, condition=self.condition)
if result is not None:
print result
except ResultsConditionException, e: except ResultsConditionException, e:
logging.error(e) logging.error(e)

View file

@ -17,6 +17,7 @@
from .multiline import MultilineFormatter from .multiline import MultilineFormatter
from .simple import SimpleFormatter from .simple import SimpleFormatter
from .table import TableFormatter
__all__ = ['formatters'] __all__ = ['formatters']
@ -25,4 +26,6 @@ __all__ = ['formatters']
formatters = dict( formatters = dict(
multiline=MultilineFormatter(), multiline=MultilineFormatter(),
simple=SimpleFormatter(), simple=SimpleFormatter(),
table=TableFormatter(),
htmltable=TableFormatter(result_funcname='get_html_string'),
) )

View file

@ -23,7 +23,16 @@ __all__ = ['IFormatter']
class IFormatter(object): class IFormatter(object):
def format(self, obj, selected_fields=None, condition=None): def __init__(self, display_keys=True):
self.display_keys = display_keys
def after_format(self, formatted):
raise NotImplementedError()
def flush(self):
raise NotImplementedError()
def format(self, obj, selected_fields=None, condition=None, return_only=False):
""" """
Format an object to be human-readable. Format an object to be human-readable.
An object has fields which can be selected, and the objects An object has fields which can be selected, and the objects
@ -32,23 +41,25 @@ class IFormatter(object):
call it. It can be used to specify the fields order. call it. It can be used to specify the fields order.
@param obj [object] object to format @param obj [object] object to format
@param selected_fields [list] fields to display @param selected_fields [list] fields to display. If None, all fields are selected.
@param condition [Condition] condition to objects to display @param condition [Condition] condition to objects to display
@return a string of the formatted object @return a string of the formatted object
""" """
item = self.to_dict(obj, condition) item = self.to_dict(obj, condition)
if selected_fields is None: if selected_fields is not None:
selected_fields = sorted(item) item = dict((k, v) for k, v in item.iteritems() if k in selected_fields)
return self.format_dict(item=item, selected_fields=selected_fields) formatted = self.format_dict(item=item)
if not return_only and formatted:
self.after_format(formatted)
return formatted
def format_dict(self, item, selected_fields): def format_dict(self, item):
""" """
Format an dict to be human-readable. Format an dict to be human-readable. The dict is already simplified if user provides selected fields.
Called by format(). Called by format().
This method has to be overridden in child classes. This method has to be overridden in child classes.
@param item [dict] item to format @param item [dict] item to format
@param selected_fields [list] fields to display
@return a string of the formatted dict @return a string of the formatted dict
""" """
raise NotImplementedError() raise NotImplementedError()

View file

@ -23,12 +23,15 @@ __all__ = ['MultilineFormatter']
class MultilineFormatter(IFormatter): class MultilineFormatter(IFormatter):
def __init__(self, key_value_separator=u': ', after_item=u'\n', display_keys=True): def __init__(self, key_value_separator=u': ', after_item=u'\n'):
self.key_value_separator = key_value_separator self.key_value_separator = key_value_separator
self.after_item = after_item self.after_item = after_item
self.display_keys = display_keys
def format_dict(self, item, selected_fields): def after_format(self, formatted):
result_str = u'\n'.join(u'%s%s' % ((u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), print formatted.encode('utf-8')
unicode(item[k])) for k in selected_fields) + self.after_item
return result_str def flush(self):
pass
def format_dict(self, item):
return 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()) + self.after_item

View file

@ -23,12 +23,16 @@ __all__ = ['SimpleFormatter']
class SimpleFormatter(IFormatter): class SimpleFormatter(IFormatter):
def __init__(self, field_separator=u'\t', key_value_separator=u'=', display_keys=True): def __init__(self, field_separator=u'\t', key_value_separator=u'='):
IFormatter.__init__(self)
self.field_separator = field_separator self.field_separator = field_separator
self.key_value_separator = key_value_separator self.key_value_separator = key_value_separator
self.display_keys = display_keys
def format_dict(self, item, selected_fields): def after_format(self, formatted):
result_str = self.field_separator.join(u'%s%s' % ((u'%s%s' % ( print formatted.encode('utf-8')
k, self.key_value_separator) if self.display_keys else ''), unicode(item[k])) for k in selected_fields)
return result_str def flush(self):
pass
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())

View file

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010 Christophe Benz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from prettytable import PrettyTable
from .iformatter import IFormatter
__all__ = ['TableFormatter']
class TableFormatter(IFormatter):
column_headers = None
queue = []
def __init__(self, result_funcname='get_string'):
self.result_funcname = result_funcname
def after_format(self, formatted):
if self.column_headers is None:
self.column_headers = formatted.keys()
self.queue.append(formatted.values())
def flush(self):
table = PrettyTable(self.column_headers)
for column_header in self.column_headers:
table.set_field_align(column_header, 'l')
for line in self.queue:
table.add_row(line)
print getattr(table, self.result_funcname)().encode('utf-8')
def format_dict(self, item):
# format is done in self.flush() by prettytable
return item