enhance formatters

This commit is contained in:
Christophe Benz 2010-07-11 22:00:18 +02:00
commit 5547e14e11
4 changed files with 196 additions and 65 deletions

View file

@ -27,7 +27,7 @@ from weboob.core import CallErrors
from weboob.core.modules import BackendsConfig
from .base import BackendNotFound, BaseApplication
from .formatters.instances import formatters
from .formatters.load import formatters, load_formatter
from .results import ResultsCondition, ResultsConditionException
@ -60,8 +60,7 @@ class ConsoleApplication(BaseApplication):
results_options = OptionGroup(self._parser, 'Results Options')
results_options.add_option('-c', '--condition', help='filter result items to display given a boolean condition')
results_options.add_option('-f', '--formatter', default='multiline', choices=formatters.keys(),
help='select output formatter (%s)' % u','.join(formatters.keys()))
results_options.add_option('-f', '--formatter', choices=formatters, help='select output formatter (%s)' % u','.join(formatters))
results_options.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)')
self._parser.add_option_group(results_options)
@ -73,7 +72,11 @@ class ConsoleApplication(BaseApplication):
pass
def _handle_app_options(self):
self.formatter = formatters[self.options.formatter]
if self.options.formatter:
formatter_name = self.options.formatter
else:
formatter_name = 'multiline'
self.formatter = load_formatter(formatter_name)
if self.options.no_header:
self.formatter.display_header = False
@ -205,7 +208,11 @@ class ConsoleApplication(BaseApplication):
def command(doc_string, f=register_command):
return partial(f, doc_string=doc_string)
def set_header(self, string):
def set_default_formatter(self, name):
if not self.options.formatter:
self.formatter = load_formatter(name)
def set_formatter_header(self, string):
self.formatter.set_header(string)
def format(self, result, backend_name=None):

View file

@ -43,7 +43,8 @@ class IFormatter(object):
call it. It can be used to specify the fields order.
@param obj [object] object to format
@param selected_fields [list] fields to display. If None, all fields are selected.
@param backend_name [str] name of backend, used to create object ID
@param selected_fields [tuple] fields to display. If None, all fields are selected
@param condition [Condition] condition to objects to display
@return a string of the formatted object
"""

View file

@ -0,0 +1,42 @@
# -*- 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.
__all__ = ['load_formatter']
formatters = ('htmltable', 'multiline', 'simple', 'table', 'webkit')
def load_formatter(name):
if name not in formatters:
raise Exception(u'Formatter "%s" not found' % name)
if name in ('htmltable', 'table'):
from .table import TableFormatter
if name == 'htmltable':
return TableFormatter(result_funcname='get_html_string')
elif name == 'table':
return TableFormatter()
elif name == 'simple':
from .simple import SimpleFormatter
return SimpleFormatter()
elif name == 'multiline':
from .multiline import MultilineFormatter
return MultilineFormatter()
elif name == 'webkit':
from .webkit import WebkitGtkFormatter
return WebkitGtkFormatter()

View file

@ -48,7 +48,7 @@ class TableFormatter(IFormatter):
elif self.result_funcname == 'get_html_string':
s+= '<p>%s</p>' % self.header
s += "\n"
table = PrettyTable(self.column_headers)
table = PrettyTable(list(self.column_headers))
for column_header in self.column_headers:
table.set_field_align(column_header, 'l')
for line in self.queue: