diff --git a/weboob/tools/application/formatters/json.py b/weboob/tools/application/formatters/json.py index cfb0f4bf..cb78fc04 100644 --- a/weboob/tools/application/formatters/json.py +++ b/weboob/tools/application/formatters/json.py @@ -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 . -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) diff --git a/weboob/tools/application/formatters/load.py b/weboob/tools/application/formatters/load.py index 4b0b66f6..8cb45604 100644 --- a/weboob/tools/application/formatters/load.py +++ b/weboob/tools/application/formatters/load.py @@ -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