Skip to content

Commit

Permalink
Check and warn about unknown bikes
Browse files Browse the repository at this point in the history
  • Loading branch information
liskin committed Sep 14, 2021
1 parent c205531 commit 87c818c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/strava_gear/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click

from .core import apply_rules
from .core import warn_unknown_bikes
from .input.activities import read_input_csv
from .input.activities import read_strava_offline
from .input.rules import read_rules
Expand Down Expand Up @@ -54,6 +55,7 @@ def main(rules, csv, strava_database, output, report, tablefmt, show_name, show_
rules = read_rules(rules, aliases=aliases)
res = apply_rules(rules, activities)
reports[report](res, output=output, tablefmt=tablefmt, show_name=show_name, show_first_last=show_first_last)
warn_unknown_bikes(rules, activities)


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions src/strava_gear/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict
from typing import Iterable
from typing import List
from warnings import warn

from .data import Result
from .data import Rule
Expand Down Expand Up @@ -59,3 +60,12 @@ def merge_asof(activities: Iterable[Dict], rules: Iterable[Rule]):
rule_cur, rule_next = rule_next, next(rules, None)

yield activity, rule_cur


def warn_unknown_bikes(rules: Rules, activities: List[Dict]) -> None:
known_bikes = set(rules.bike_names.keys())
used_gear = set(activity['gear_id'] for activity in activities if activity['gear_id'])

unknown_bikes = rules.all_rule_bike_ids() - known_bikes - used_gear
if unknown_bikes:
warn(f"Unknown bikes in rules, possibly a typo: {unknown_bikes}")
3 changes: 3 additions & 0 deletions src/strava_gear/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ class Rules:
components: List[Component]
rules: List[Rule]

def all_rule_bike_ids(self) -> Set[BikeId]:
return set(b for r in self.rules for b in r.bikes.keys())


@dataclass
class Usage:
Expand Down

0 comments on commit 87c818c

Please sign in to comment.