diff --git a/src/strava_gear/cli.py b/src/strava_gear/cli.py index 4b5dd55..f4cf90d 100644 --- a/src/strava_gear/cli.py +++ b/src/strava_gear/cli.py @@ -31,14 +31,20 @@ '--report', type=click.Choice(reports.keys()), default='bikes', show_default=True, help="Type of report") -def main(rules, csv, strava_database, report): +@click.option( + '--show-name/--hide-name', default=True, show_default=True, + help="Show long component names") +@click.option( + '--show-first-last/--hide-first-last', default=True, show_default=True, + help="Show first/last usage of components") +def main(rules, csv, strava_database, report, show_name, show_first_last): if csv: aliases, activities = {}, read_input_csv(csv) else: aliases, activities = read_strava_offline() rules = read_rules(rules, aliases=aliases) res = apply_rules(rules, activities) - print(reports[report](res)) + print(reports[report](res, show_name=show_name, show_first_last=show_first_last)) if __name__ == "__main__": diff --git a/src/strava_gear/report.py b/src/strava_gear/report.py index 96cdce7..1024934 100644 --- a/src/strava_gear/report.py +++ b/src/strava_gear/report.py @@ -1,7 +1,7 @@ from collections import defaultdict from functools import partial from typing import Dict -from typing import Iterable +from typing import Iterator from tabulate import tabulate @@ -11,16 +11,28 @@ from .data import Result -def report_components(res: Result) -> str: - components: Iterable[Component] = sorted(res.components, key=lambda c: c.firstlast) - return tabulate( - [[c.ident, c.name, c.distance / 1000, c.time / 3600, c.firstlast] for c in components], - headers=["id", "name", "km", "hour", "first … last"], - floatfmt=".1f", - ) +def report(f, res: Result, show_name: bool = True, show_first_last: bool = True) -> str: + def cols(d: Dict) -> Dict: + if not show_name: + del d["name"] + if not show_first_last: + del d["first … last"] + return d + return tabulate([cols(d) for d in f(res)], headers="keys", floatfmt=".1f") -def report_bikes(res: Result) -> str: +def report_components(res: Result) -> Iterator[Dict]: + for c in sorted(res.components, key=lambda c: c.firstlast): + yield { + "id": c.ident, + "name": c.name, + "km": c.distance / 1000, + "hour": c.time / 3600, + "first … last": c.firstlast, + } + + +def report_bikes(res: Result, show_names: bool = True, show_firstlasts: bool = True) -> Iterator[Dict]: bikes_firstlasts = bikes_firstlast(res) def sort_key(c: Component): @@ -29,24 +41,18 @@ def sort_key(c: Component): return bikes_firstlasts[b], b, t - def cols(c: Component): + for c in sorted((c for c in res.components if c.assignment), key=sort_key): assert c.assignment b, t = c.assignment - return [ - res.bike_names.get(b, b), - t, - c.ident, - c.name, - c.distance / 1000, - c.time / 3600, - c.firstlast - ] - - return tabulate( - [cols(c) for c in sorted((c for c in res.components if c.assignment), key=sort_key)], - headers=["bike", "role", "id", "name", "km", "hour", "first … last"], - floatfmt=".1f", - ) + yield { + "bike": res.bike_names.get(b, b), + "role": t, + "id": c.ident, + "name": c.name, + "km": c.distance / 1000, + "hour": c.time / 3600, + "first … last": c.firstlast, + } def bikes_firstlast(res: Result) -> Dict[BikeId, FirstLast]: @@ -59,6 +65,6 @@ def bikes_firstlast(res: Result) -> Dict[BikeId, FirstLast]: reports = { - 'components': partial(report_components), - 'bikes': report_bikes, + 'components': partial(report, report_components), + 'bikes': partial(report, report_bikes), }