Commit 8875fad4 authored by Oleg Plakhotniuk's avatar Oleg Plakhotniuk Committed by Laurent Bachelier

Table and Json formatters can write output to a file now.

Signed-off-by: 's avatarOleg Plakhotniuk <olegus8@gmail.com>

closes #1412
parent 36a4b78e
......@@ -3,4 +3,8 @@ verbosity = 2
detailed-errors = 1
with-doctest = 1
where = weboob
tests = weboob.tools.capabilities.paste,weboob.tools.path,weboob.capabilities.bank
tests = weboob.capabilities.bank,
weboob.tools.capabilities.paste,
weboob.tools.application.formatters.json,
weboob.tools.application.formatters.table,
weboob.tools.path
......@@ -229,3 +229,20 @@ class PrettyFormatter(IFormatter):
def get_description(self, obj):
return None
def formatter_test_output(Formatter, obj):
"""
Formats an object and returns output as a string.
For test purposes only.
"""
from tempfile import mkstemp
from os import remove
_, name = mkstemp()
fmt = Formatter()
fmt.outfile = name
fmt.format(obj)
fmt.flush()
with open(name) as f:
res = f.read()
remove(name)
return res
......@@ -18,8 +18,6 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
from weboob.capabilities.base import NotAvailable, NotLoaded
from weboob.tools.json import json
......@@ -54,7 +52,7 @@ class JsonFormatter(IFormatter):
self.queue = []
def flush(self):
print(json.dumps(self.queue, cls=Encoder))
self.output(json.dumps(self.queue, cls=Encoder))
def format_dict(self, item):
self.queue.append(item)
......@@ -66,4 +64,9 @@ class JsonLineFormatter(IFormatter):
The advantage is that it can be streamed.
"""
def format_dict(self, item):
print(json.dumps(item, cls=Encoder))
self.output(json.dumps(item, cls=Encoder))
def test():
from .iformatter import formatter_test_output as fmt
assert fmt(JsonFormatter, {'foo': 'bar'}) == '[{"foo": "bar"}]\n'
assert fmt(JsonLineFormatter, {'foo': 'bar'}) == '{"foo": "bar"}\n'
......@@ -18,8 +18,6 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
from prettytable import PrettyTable
from weboob.capabilities.base import empty
......@@ -42,7 +40,7 @@ class TableFormatter(IFormatter):
def flush(self):
s = self.get_formatted_table()
if s is not None:
print(s.encode('utf-8'))
self.output(s.encode('utf-8'))
def get_formatted_table(self):
if len(self.queue) == 0:
......@@ -101,3 +99,12 @@ class TableFormatter(IFormatter):
class HTMLTableFormatter(TableFormatter):
HTML = True
def test():
from .iformatter import formatter_test_output as fmt
assert fmt(TableFormatter, {'foo': 'bar'}) == \
'+-----+\n' \
'| Foo |\n' \
'+-----+\n' \
'| bar |\n' \
'+-----+\n'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment