From a1c71aa1b50d092751626327421c2d32f188beff Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 26 Aug 2024 00:57:51 +0200 Subject: [PATCH] Ruff --- git_time_tracker/__init__.py | 23 ++++++++++++++++++----- git_time_tracker/data.py | 1 + git_time_tracker/format/icalendar.py | 17 +++++------------ git_time_tracker/source/git_repo.py | 20 ++++++++++++-------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/git_time_tracker/__init__.py b/git_time_tracker/__init__.py index 9e2c5ae..ece2e8a 100644 --- a/git_time_tracker/__init__.py +++ b/git_time_tracker/__init__.py @@ -32,7 +32,12 @@ import sys from collections.abc import Iterator from pathlib import Path -from .data import HIDDEN_LABEL_PREFIX, HIDDEN_LABEL_TOTAL, WorkSample, RealizedWorkSample +from .data import ( + HIDDEN_LABEL_PREFIX, + HIDDEN_LABEL_TOTAL, + RealizedWorkSample, + WorkSample, +) from .format import cli, icalendar from .source import git_repo @@ -45,11 +50,16 @@ HOUR = datetime.timedelta(hours=1) MINUTE = datetime.timedelta(minutes=1) -def filter_samples(samples: list[WorkSample], sample_filter: set[str]) -> list[WorkSample]: +def filter_samples( + samples: list[WorkSample], sample_filter: set[str], +) -> list[WorkSample]: assert len(sample_filter) > 0 return [s for s in samples if set(s.labels).intersection(sample_filter)] -def heuristically_realize_samples(samples: list[WorkSample]) -> Iterator[RealizedWorkSample]: + +def heuristically_realize_samples( + samples: list[WorkSample], +) -> Iterator[RealizedWorkSample]: """Secret sauce. Guarentees that: @@ -75,6 +85,7 @@ def heuristically_realize_samples(samples: list[WorkSample]) -> Iterator[Realize previous_sample_end = sample.end_at del sample + def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument( @@ -115,7 +126,7 @@ def main(): git_repo.iterate_samples_from_git_repository(repo_path), ) - shared_time_stamps = sorted(shared_time_stamps_set, key = lambda s: s.end_at) + shared_time_stamps = sorted(shared_time_stamps_set, key=lambda s: s.end_at) del shared_time_stamps_set sample_filter = args.sample_filter @@ -131,4 +142,6 @@ def main(): for t in cli.generate_report(shared_time_stamps): sys.stdout.write(t) elif args.format_mode == 'icalendar': - icalendar.generate_icalendar_file(shared_time_stamps, file='./output/samples.ics') + icalendar.generate_icalendar_file( + shared_time_stamps, file='./output/samples.ics', + ) diff --git a/git_time_tracker/data.py b/git_time_tracker/data.py index 1c99782..1b0860a 100644 --- a/git_time_tracker/data.py +++ b/git_time_tracker/data.py @@ -12,6 +12,7 @@ class WorkSample: start_at: datetime.datetime | None end_at: datetime.datetime | None + @dataclasses.dataclass(frozen=True, order=True) class RealizedWorkSample(WorkSample): start_at: datetime.datetime diff --git a/git_time_tracker/format/icalendar.py b/git_time_tracker/format/icalendar.py index 1731bdd..8d1e265 100644 --- a/git_time_tracker/format/icalendar.py +++ b/git_time_tracker/format/icalendar.py @@ -1,15 +1,8 @@ import datetime -from collections.abc import Iterator -import argparse -import datetime -import urllib.parse import icalendar -from personal_data.util import load_csv_file - - -from ..data import HIDDEN_LABEL_PREFIX, HIDDEN_LABEL_TOTAL, RealizedWorkSample +from ..data import HIDDEN_LABEL_PREFIX, RealizedWorkSample ZERO_DURATION = datetime.timedelta(seconds=0) HOUR = datetime.timedelta(hours=1) @@ -29,7 +22,7 @@ def create_title(sample: RealizedWorkSample) -> str: def generate_calendar( samples: list[RealizedWorkSample], - ) -> icalendar.Calendar: +) -> icalendar.Calendar: max_title_parts = 2 cal = icalendar.Calendar() @@ -37,7 +30,6 @@ def generate_calendar( cal.add('version', '2.0') for sample in samples: - title = create_title(sample) description = '' @@ -52,7 +44,9 @@ def generate_calendar( for label_and_type in sample.labels: if label_and_type.startswith('author:'): - event.add('organizer', 'mailto:'+label_and_type.removeprefix('author:')) + event.add( + 'organizer', 'mailto:' + label_and_type.removeprefix('author:'), + ) cal.add_component(event) del event @@ -64,7 +58,6 @@ def generate_icalendar_file( samples: list[RealizedWorkSample], file: str, ) -> None: - calendar = generate_calendar(samples) with open(file, 'wb') as f: diff --git a/git_time_tracker/source/git_repo.py b/git_time_tracker/source/git_repo.py index 1cb9ab6..b7e7800 100644 --- a/git_time_tracker/source/git_repo.py +++ b/git_time_tracker/source/git_repo.py @@ -38,19 +38,23 @@ def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: labels.append('project:' + project_name) labels.append('author:' + commit.author.email) - authored_date = datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC) - committed_date = datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC) + authored_date = datetime.datetime.fromtimestamp( + commit.authored_date, tz=datetime.UTC, + ) + committed_date = datetime.datetime.fromtimestamp( + commit.committed_date, tz=datetime.UTC, + ) yield WorkSample( - labels = tuple(labels), - start_at = None, - end_at = authored_date, + labels=tuple(labels), + start_at=None, + end_at=authored_date, ) if authored_date != committed_date: yield WorkSample( - labels = tuple(labels), - start_at = None, - end_at = committed_date, + labels=tuple(labels), + start_at=None, + end_at=committed_date, ) del labels