Table and Json formatters can write output to a file now.
Signed-off-by: Oleg Plakhotniuk <olegus8@gmail.com> closes #1412
This commit is contained in:
parent
36a4b78e67
commit
8875fad439
4 changed files with 39 additions and 8 deletions
|
|
@ -3,4 +3,8 @@ verbosity = 2
|
||||||
detailed-errors = 1
|
detailed-errors = 1
|
||||||
with-doctest = 1
|
with-doctest = 1
|
||||||
where = weboob
|
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):
|
def get_description(self, obj):
|
||||||
return None
|
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/>.
|
# 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.capabilities.base import NotAvailable, NotLoaded
|
||||||
from weboob.tools.json import json
|
from weboob.tools.json import json
|
||||||
|
|
||||||
|
|
@ -54,7 +52,7 @@ class JsonFormatter(IFormatter):
|
||||||
self.queue = []
|
self.queue = []
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
print(json.dumps(self.queue, cls=Encoder))
|
self.output(json.dumps(self.queue, cls=Encoder))
|
||||||
|
|
||||||
def format_dict(self, item):
|
def format_dict(self, item):
|
||||||
self.queue.append(item)
|
self.queue.append(item)
|
||||||
|
|
@ -66,4 +64,9 @@ class JsonLineFormatter(IFormatter):
|
||||||
The advantage is that it can be streamed.
|
The advantage is that it can be streamed.
|
||||||
"""
|
"""
|
||||||
def format_dict(self, item):
|
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/>.
|
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
from prettytable import PrettyTable
|
from prettytable import PrettyTable
|
||||||
|
|
||||||
from weboob.capabilities.base import empty
|
from weboob.capabilities.base import empty
|
||||||
|
|
@ -42,7 +40,7 @@ class TableFormatter(IFormatter):
|
||||||
def flush(self):
|
def flush(self):
|
||||||
s = self.get_formatted_table()
|
s = self.get_formatted_table()
|
||||||
if s is not None:
|
if s is not None:
|
||||||
print(s.encode('utf-8'))
|
self.output(s.encode('utf-8'))
|
||||||
|
|
||||||
def get_formatted_table(self):
|
def get_formatted_table(self):
|
||||||
if len(self.queue) == 0:
|
if len(self.queue) == 0:
|
||||||
|
|
@ -101,3 +99,12 @@ class TableFormatter(IFormatter):
|
||||||
|
|
||||||
class HTMLTableFormatter(TableFormatter):
|
class HTMLTableFormatter(TableFormatter):
|
||||||
HTML = True
|
HTML = True
|
||||||
|
|
||||||
|
def test():
|
||||||
|
from .iformatter import formatter_test_output as fmt
|
||||||
|
assert fmt(TableFormatter, {'foo': 'bar'}) == \
|
||||||
|
'+-----+\n' \
|
||||||
|
'| Foo |\n' \
|
||||||
|
'+-----+\n' \
|
||||||
|
'| bar |\n' \
|
||||||
|
'+-----+\n'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue