enhance formatters

This commit is contained in:
Christophe Benz 2010-05-21 19:19:28 +02:00
commit 2ddc249b2b
6 changed files with 140 additions and 51 deletions

View file

@ -15,12 +15,14 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from .multiline import MultilineFormatter
from .simple import SimpleFormatter
__all__ = ['formatters_classes']
__all__ = ['formatters']
formatters_classes = dict(
simple=SimpleFormatter,
formatters = dict(
multiline=MultilineFormatter(),
simple=SimpleFormatter(),
)

View file

@ -0,0 +1,70 @@
# -*- 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.
import types
__all__ = ['IFormatter']
class IFormatter(object):
def format(self, obj, selected_fields=None, condition=None):
"""
Format an object to be human-readable.
An object has fields which can be selected, and the objects
can be filtered using a condition (like SELECT and WHERE in SQL).
If the object provides an iter_fields() method, the formatter will
call it. It can be used to specify the fields order.
@param obj [object] object to format
@param selected_fields [list] fields to display
@param condition [Condition] condition to objects to display
@return a string of the formatted object
"""
item = self.to_dict(obj, condition)
if selected_fields is None:
selected_fields = sorted(item)
return self.format_dict(item=item, selected_fields=selected_fields)
def format_dict(self, item, selected_fields):
"""
Format an dict to be human-readable.
Called by format().
This method has to be overridden in child classes.
@param item [dict] item to format
@param selected_fields [list] fields to display
@return a string of the formatted dict
"""
raise NotImplementedError()
def iter_fields(self, obj):
for attribute_name in dir(obj):
if attribute_name.startswith('_'):
continue
attribute = getattr(obj, attribute_name)
if not isinstance(attribute, types.MethodType):
yield attribute_name, attribute
def to_dict(self, obj, condition=None):
fields_iterator = obj.iter_fields() if hasattr(obj, 'iter_fields') else self.iter_fields(obj)
d = dict((k, v) for k, v in fields_iterator)
if condition is not None:
if not condition.is_valid(d):
d = None
return d

View file

@ -0,0 +1,34 @@
# -*- 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 .iformatter import IFormatter
__all__ = ['MultilineFormatter']
class MultilineFormatter(IFormatter):
def __init__(self, key_value_separator=u': ', after_item=u'\n', display_keys=True):
self.key_value_separator = key_value_separator
self.after_item = after_item
self.display_keys = display_keys
def format_dict(self, item, selected_fields):
result_str = u'\n'.join(u'%s%s' % ((u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''),
unicode(item[k])) for k in selected_fields) + self.after_item
return result_str

View file

@ -16,38 +16,19 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from .iformatter import IFormatter
__all__ = ['SimpleFormatter']
class SimpleFormatter(object):
@classmethod
def format(cls, obj, selected_fields=None, where_condition=None):
def iter_fields(obj):
import types
for attribute_name in dir(obj):
if attribute_name.startswith('_'):
continue
attribute = getattr(obj, attribute_name)
if not isinstance(attribute, types.MethodType):
yield attribute_name, attribute
class SimpleFormatter(IFormatter):
def __init__(self, field_separator=u'\t', key_value_separator=u'=', display_keys=True):
self.field_separator = field_separator
self.key_value_separator = key_value_separator
self.display_keys = display_keys
if hasattr(obj, 'iter_fields'):
fields_iterator = obj.iter_fields()
else:
fields_iterator = iter_fields(obj)
d = dict((k, v) for k, v in fields_iterator)
if where_condition is not None:
if not where_condition.is_valid(d):
d = None
if not d:
return None
if selected_fields is None:
result = u'\n'.join(u'%s: %s' % (k, unicode(d[k])) for k in sorted(d)) + u'\n'
else:
result = u'\t'.join(unicode(d[k]) for k in selected_fields if d[k] is not None)
return result
def format_dict(self, item, selected_fields):
result_str = self.field_separator.join(u'%s%s' % ((u'%s%s' % (
k, self.key_value_separator) if self.display_keys else ''), unicode(item[k])) for k in selected_fields)
return result_str