From c2e90943e947a2b4ac8e5c0ce80b99717eec44d2 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sat, 8 Jun 2024 14:43:44 +0200 Subject: [PATCH] Ruff --- git_time_tracker/__init__.py | 48 +++++++++++++++++++++++++----------- git_time_tracker/__main__.py | 1 - git_time_tracker/_version.py | 2 +- test/test_main.py | 1 + 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/git_time_tracker/__init__.py b/git_time_tracker/__init__.py index ae83fc2..00c5c57 100644 --- a/git_time_tracker/__init__.py +++ b/git_time_tracker/__init__.py @@ -1,21 +1,23 @@ import argparse +import dataclasses +import datetime +import logging import sys import time -import dataclasses -import git -import datetime from collections.abc import Iterator, Sequence from pathlib import Path -import logging +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 @@ -29,15 +31,18 @@ def determine_default(repo: git.Repo): except: return 'master' + HIDDEN_LABEL_PREFIX = '__' HIDDEN_LABEL_TOTAL = HIDDEN_LABEL_PREFIX + 'TOTAL' + def determine_project_name(repo: git.Repo) -> str: remotes = repo.remotes if len(remotes) > 0: return remotes.origin.url.removeprefix('git@gitfub.space:') return Path(repo.working_tree_dir).name + def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: project_name = determine_project_name(repo) assert project_name is not None @@ -49,15 +54,23 @@ def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: for commit in repo.iter_commits(determine_default(repo)): labels = [HIDDEN_LABEL_TOTAL] labels.append('project:' + project_name) - labels.append('author:' + commit.author.email) - yield WorkSample(datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC), tuple(labels)) - yield WorkSample(datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC), tuple(labels)) + labels.append('author:' + commit.author.email) + yield WorkSample( + datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC), + tuple(labels), + ) + yield WorkSample( + datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC), + tuple(labels), + ) del labels -DEFAULT_EST_TIME=datetime.timedelta(hours=1) -ZERO_DURATION = datetime.timedelta(seconds = 0) -HOUR = datetime.timedelta(hours = 1) +DEFAULT_EST_TIME = datetime.timedelta(hours=1) + +ZERO_DURATION = datetime.timedelta(seconds=0) +HOUR = datetime.timedelta(hours=1) + def generate_report(samples: list[WorkSample]) -> Iterator[str]: SAMPLE_FILTER = {} @@ -76,19 +89,19 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: continue for label in sample.labels: - time_per_label.setdefault(label,ZERO_DURATION) + time_per_label.setdefault(label, ZERO_DURATION) time_per_label[label] += est_time prev_time = sample.registered_at del sample, est_time - time_and_label = [(duration, label) for label,duration in time_per_label.items()] + time_and_label = [(duration, label) for label, duration in time_per_label.items()] time_and_label.sort(reverse=True) # yield '-' * 66 yield '\n' - for (total_time, label_and_type) in time_and_label: + for total_time, label_and_type in time_and_label: if label_and_type.startswith(HIDDEN_LABEL_PREFIX): continue @@ -97,7 +110,7 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: if len(LABEL_FILTER) > 0 and label_type not in LABEL_FILTER: continue - label_type = '' # TODO + label_type = '' # TODO yield f' {label_type:8} {label:40} {total_time / HOUR:-4.2f} hours\n' del label, total_time @@ -105,7 +118,12 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]: yield '-' * 66 yield '\n' - yield ' {label_type:8} {label:40} {hours:-4.0f} hours\n'.format(label_type='', label='TOTAL', hours = time_per_label.get(HIDDEN_LABEL_TOTAL, ZERO_DURATION) / HOUR) + yield ' {label_type:8} {label:40} {hours:-4.0f} hours\n'.format( + label_type='', + label='TOTAL', + hours=time_per_label.get(HIDDEN_LABEL_TOTAL, ZERO_DURATION) / HOUR, + ) + def main(): logging.basicConfig() diff --git a/git_time_tracker/__main__.py b/git_time_tracker/__main__.py index c0668b9..7224f22 100644 --- a/git_time_tracker/__main__.py +++ b/git_time_tracker/__main__.py @@ -2,4 +2,3 @@ from git_time_tracker import main if __name__ == '__main__': main() - diff --git a/git_time_tracker/_version.py b/git_time_tracker/_version.py index 34da6b8..10939f0 100644 --- a/git_time_tracker/_version.py +++ b/git_time_tracker/_version.py @@ -1 +1 @@ -__version__ = '0.1.2' \ No newline at end of file +__version__ = '0.1.2' diff --git a/test/test_main.py b/test/test_main.py index 4f5020c..1fc1f88 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -1,4 +1,5 @@ import git_time_tracker + def test_report_empty(): assert list(git_time_tracker.generate_report([]))