enhance formatters
This commit is contained in:
parent
742b0ad62b
commit
5547e14e11
8 changed files with 196 additions and 65 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -15,30 +15,28 @@
|
|||
# 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__ = ['load_formatter']
|
||||
|
||||
|
||||
__all__ = ['formatters']
|
||||
formatters = ('htmltable', 'multiline', 'simple', 'table', 'webkit')
|
||||
|
||||
|
||||
|
||||
formatters = dict(
|
||||
multiline=MultilineFormatter(),
|
||||
simple=SimpleFormatter(),
|
||||
)
|
||||
|
||||
try:
|
||||
from .table import TableFormatter
|
||||
formatters.update(dict(
|
||||
table=TableFormatter(),
|
||||
htmltable=TableFormatter(result_funcname='get_html_string'),
|
||||
))
|
||||
try:
|
||||
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
|
||||
formatters.update(dict(
|
||||
webkit=WebkitGtkFormatter(),
|
||||
))
|
||||
except ImportError:
|
||||
pass
|
||||
except ImportError:
|
||||
pass
|
||||
return WebkitGtkFormatter()
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue