From 0ec80c72c50036fe36fec6c53637f41698f287ed Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Sun, 5 May 2019 19:33:49 +0200 Subject: [PATCH] Dynamically find formatter and add pprint stats formatter. --- librestash/cmd.py | 24 ++++++++++++------------ librestash/lib/exporters/formatters.py | 6 +++--- librestash/lib/stats/formatters.py | 4 ++++ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/librestash/cmd.py b/librestash/cmd.py index 80e8a15..8da4645 100644 --- a/librestash/cmd.py +++ b/librestash/cmd.py @@ -5,9 +5,9 @@ import logging from .lib.adaptors.gpx import GpxImporter from .lib.exporters.caches import CacheExporter -from .lib.exporters.formatters import caches_to_csv, caches_to_html, caches_to_json +from .lib.exporters import formatters as exporters_formatters from .lib.stats import all_stats -from .lib.stats.formatters import stats_to_html, stats_to_json +from .lib.stats import formatters as stats_formatters from .lib.config import get_default_config_path, get_config from .lib.model import get_db @@ -49,12 +49,11 @@ def export_caches(): exporter = CacheExporter(config, database) data = exporter.export(found_by=args.found_by) - if args.format == 'json': - print(caches_to_json(data)) - elif args.format == 'csv': - print(caches_to_csv(data)) - elif args.format == 'html': - print(caches_to_html(data, title=args.title)) + formatter = getattr(exporters_formatters, 'caches_to_%s' % args.format, None) + if formatter: + print(formatter(data, title=args.title)) + else: + raise RuntimeError('Formatter %s not found.' % args.format) def stats(): """ Command for all kinds of statistics """ @@ -71,7 +70,8 @@ def stats(): data = all_stats(database, geocacher_name=args.geocacher) - if args.format == 'json': - print(stats_to_json(data)) - elif args.format == 'html': - print(stats_to_html(data)) + formatter = getattr(stats_formatters, 'stats_to_%s' % args.format, None) + if formatter: + print(formatter(data)) + else: + raise RuntimeError('Formatter %s not found.' % args.format) diff --git a/librestash/lib/exporters/formatters.py b/librestash/lib/exporters/formatters.py index 88a74db..9aa991d 100644 --- a/librestash/lib/exporters/formatters.py +++ b/librestash/lib/exporters/formatters.py @@ -10,10 +10,10 @@ except ImportError: from ..util import flatten_dict, json_serialise -def caches_to_json(data): +def caches_to_json(data, **kwargs): return json.dumps(data, default=json_serialise) -def caches_to_csv(data): +def caches_to_csv(data, **kwargs): with io.StringIO() as csv_file: # Get top-level field names fieldnames = flatten_dict(data[0]).keys() @@ -26,7 +26,7 @@ def caches_to_csv(data): return csv_file.getvalue() -def caches_to_html(data, main_template='simple', title=None): +def caches_to_html(data, main_template='simple', title=None, **kwargs): if not _HAVE_JINJA: return '<html><body>Jinja2 could not be imported.</body></html>' diff --git a/librestash/lib/stats/formatters.py b/librestash/lib/stats/formatters.py index b716ecc..b4c0d75 100644 --- a/librestash/lib/stats/formatters.py +++ b/librestash/lib/stats/formatters.py @@ -1,4 +1,5 @@ import json +from pprint import pformat try: import jinja2 @@ -8,6 +9,9 @@ except ImportError: from ..util import json_serialise +def stats_to_pprint(data): + return pformat(data) + def stats_to_json(data): return json.dumps(data, default=json_serialise) -- GitLab