From 6c9f6b157d8472f6c92ff34df64d68c640b4d975 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sat, 8 Jun 2024 15:17:04 +0200 Subject: [PATCH] Sample filter cmdline argument implemented --- git_time_tracker/__init__.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/git_time_tracker/__init__.py b/git_time_tracker/__init__.py index d95fd7f..cc06f5f 100644 --- a/git_time_tracker/__init__.py +++ b/git_time_tracker/__init__.py @@ -12,12 +12,6 @@ import git logger = logging.getLogger(__name__) -def parse_arguments(): - parser = argparse.ArgumentParser() - parser.add_argument('repositories', action='extend', nargs='+', type=Path) - return parser.parse_args() - - @dataclasses.dataclass(frozen=True, order=True) class WorkSample: registered_at: datetime.datetime @@ -95,10 +89,9 @@ def fmt_year_ranges(years: list[int]) -> str: def fmt_line(label_type: str, label: str, total_time: datetime.timedelta) -> str: hours = int(total_time / HOUR) minutes = int((total_time - hours*HOUR)/MINUTE) - return f' {label_type:8} {label:40} {hours:-4d}h {minutes:-2d}m' + return f' {label_type:10} {label:40} {hours:-4d}h {minutes:-2d}m' -def generate_report(samples: list[WorkSample]) -> Iterator[str]: - SAMPLE_FILTER = {} +def generate_report(samples: list[WorkSample], sample_filter = frozenset()) -> Iterator[str]: LABEL_FILTER = {} # Time spent per label @@ -109,9 +102,9 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: est_time: datetime.timedelta = DEFAULT_EST_TIME est_time = min(sample.registered_at - prev_time, est_time) - if len(SAMPLE_FILTER) == 0: + if len(sample_filter) == 0: pass - elif not set(sample.labels).intersection(SAMPLE_FILTER): + elif not set(sample.labels).intersection(sample_filter): continue for label in sample.labels: @@ -137,8 +130,6 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: if len(LABEL_FILTER) > 0 and label_type not in LABEL_FILTER: continue - label_type = '' # TODO - yield fmt_line(label_type, label, total_time) yield ' (' yield fmt_year_ranges(years_per_label.get(label_and_type,[])) @@ -152,6 +143,14 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: yield fmt_line('', 'TOTAL', time_per_label.get(HIDDEN_LABEL_TOTAL, ZERO_DURATION)) yield '\n' + +def parse_arguments(): + parser = argparse.ArgumentParser() + parser.add_argument('repositories', action='extend', nargs='+', type=Path) + parser.add_argument('--filter', action='extend', nargs='+', type=str, dest='sample_filter', default=[]) + return parser.parse_args() + + def main(): logging.basicConfig() @@ -169,5 +168,5 @@ def main(): shared_time_stamps = sorted(shared_time_stamps) - for t in generate_report(shared_time_stamps): + for t in generate_report(shared_time_stamps, sample_filter=args.sample_filter): sys.stdout.write(t)