Skip to content
Snippets Groups Projects
Unverified Commit 0ec80c72 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Dynamically find formatter and add pprint stats formatter.

parent 0525ade8
No related branches found
No related tags found
No related merge requests found
...@@ -5,9 +5,9 @@ import logging ...@@ -5,9 +5,9 @@ import logging
from .lib.adaptors.gpx import GpxImporter from .lib.adaptors.gpx import GpxImporter
from .lib.exporters.caches import CacheExporter 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 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.config import get_default_config_path, get_config
from .lib.model import get_db from .lib.model import get_db
...@@ -49,12 +49,11 @@ def export_caches(): ...@@ -49,12 +49,11 @@ def export_caches():
exporter = CacheExporter(config, database) exporter = CacheExporter(config, database)
data = exporter.export(found_by=args.found_by) data = exporter.export(found_by=args.found_by)
if args.format == 'json': formatter = getattr(exporters_formatters, 'caches_to_%s' % args.format, None)
print(caches_to_json(data)) if formatter:
elif args.format == 'csv': print(formatter(data, title=args.title))
print(caches_to_csv(data)) else:
elif args.format == 'html': raise RuntimeError('Formatter %s not found.' % args.format)
print(caches_to_html(data, title=args.title))
def stats(): def stats():
""" Command for all kinds of statistics """ """ Command for all kinds of statistics """
...@@ -71,7 +70,8 @@ def stats(): ...@@ -71,7 +70,8 @@ def stats():
data = all_stats(database, geocacher_name=args.geocacher) data = all_stats(database, geocacher_name=args.geocacher)
if args.format == 'json': formatter = getattr(stats_formatters, 'stats_to_%s' % args.format, None)
print(stats_to_json(data)) if formatter:
elif args.format == 'html': print(formatter(data))
print(stats_to_html(data)) else:
raise RuntimeError('Formatter %s not found.' % args.format)
...@@ -10,10 +10,10 @@ except ImportError: ...@@ -10,10 +10,10 @@ except ImportError:
from ..util import flatten_dict, json_serialise 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) return json.dumps(data, default=json_serialise)
def caches_to_csv(data): def caches_to_csv(data, **kwargs):
with io.StringIO() as csv_file: with io.StringIO() as csv_file:
# Get top-level field names # Get top-level field names
fieldnames = flatten_dict(data[0]).keys() fieldnames = flatten_dict(data[0]).keys()
...@@ -26,7 +26,7 @@ def caches_to_csv(data): ...@@ -26,7 +26,7 @@ def caches_to_csv(data):
return csv_file.getvalue() 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: if not _HAVE_JINJA:
return '<html><body>Jinja2 could not be imported.</body></html>' return '<html><body>Jinja2 could not be imported.</body></html>'
......
import json import json
from pprint import pformat
try: try:
import jinja2 import jinja2
...@@ -8,6 +9,9 @@ except ImportError: ...@@ -8,6 +9,9 @@ except ImportError:
from ..util import json_serialise from ..util import json_serialise
def stats_to_pprint(data):
return pformat(data)
def stats_to_json(data): def stats_to_json(data):
return json.dumps(data, default=json_serialise) return json.dumps(data, default=json_serialise)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment