Add a json formatter that works on a line level

This commit is contained in:
Laurent Bachelier 2014-05-07 00:01:48 +02:00
commit 132d131f87
2 changed files with 21 additions and 6 deletions

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2013 Julien Hebert
# Copyright(C) 2013-2014 Julien Hebert, Laurent Bachelier
#
# This file is part of weboob.
#
@ -18,12 +18,12 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.json import json
from weboob.capabilities.base import NotAvailable, NotLoaded
from weboob.tools.json import json
from .iformatter import IFormatter
__all__ = ['JsonFormatter']
__all__ = ['JsonFormatter', 'JsonLineFormatter']
class Encoder(json.JSONEncoder):
@ -44,6 +44,9 @@ class Encoder(json.JSONEncoder):
class JsonFormatter(IFormatter):
"""
Formats the whole list as a single JSON list object.
"""
def __init__(self):
IFormatter.__init__(self)
self.queue = []
@ -53,3 +56,12 @@ class JsonFormatter(IFormatter):
def format_dict(self, item):
self.queue.append(item)
class JsonLineFormatter(IFormatter):
"""
Formats the list as received, with a JSON object per line.
The advantage is that it can be streamed.
"""
def format_dict(self, item):
print json.dumps(item, cls=Encoder)

View file

@ -26,7 +26,7 @@ class FormatterLoadError(Exception):
class FormattersLoader(object):
BUILTINS = ['htmltable', 'multiline', 'simple', 'table', 'csv', 'webkit', 'json']
BUILTINS = ['htmltable', 'multiline', 'simple', 'table', 'csv', 'webkit', 'json', 'json_line']
def __init__(self):
self.formatters = {}
@ -41,7 +41,7 @@ class FormattersLoader(object):
return l
def build_formatter(self, name):
if not name in self.formatters:
if name not in self.formatters:
try:
self.formatters[name] = self.load_builtin_formatter(name)
except ImportError as e:
@ -50,7 +50,7 @@ class FormattersLoader(object):
return self.formatters[name]()
def load_builtin_formatter(self, name):
if not name in self.BUILTINS:
if name not in self.BUILTINS:
raise FormatterLoadError('Formatter "%s" does not exist' % name)
if name == 'htmltable':
@ -74,3 +74,6 @@ class FormattersLoader(object):
elif name == 'json':
from .json import JsonFormatter
return JsonFormatter
elif name == 'json_line':
from .json import JsonLineFormatter
return JsonLineFormatter