1
0
This commit is contained in:
Jon Michael Aanes 2024-06-08 14:43:44 +02:00
parent a201860553
commit c2e90943e9
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
4 changed files with 35 additions and 17 deletions

View File

@ -1,21 +1,23 @@
import argparse import argparse
import dataclasses
import datetime
import logging
import sys import sys
import time import time
import dataclasses
import git
import datetime
from collections.abc import Iterator, Sequence from collections.abc import Iterator, Sequence
from pathlib import Path from pathlib import Path
import logging import git
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('repositories', action='extend', nargs='+', type=Path) parser.add_argument('repositories', action='extend', nargs='+', type=Path)
return parser.parse_args() return parser.parse_args()
@dataclasses.dataclass(frozen=True, order=True) @dataclasses.dataclass(frozen=True, order=True)
class WorkSample: class WorkSample:
registered_at: datetime.datetime registered_at: datetime.datetime
@ -29,15 +31,18 @@ def determine_default(repo: git.Repo):
except: except:
return 'master' return 'master'
HIDDEN_LABEL_PREFIX = '__' HIDDEN_LABEL_PREFIX = '__'
HIDDEN_LABEL_TOTAL = HIDDEN_LABEL_PREFIX + 'TOTAL' HIDDEN_LABEL_TOTAL = HIDDEN_LABEL_PREFIX + 'TOTAL'
def determine_project_name(repo: git.Repo) -> str: def determine_project_name(repo: git.Repo) -> str:
remotes = repo.remotes remotes = repo.remotes
if len(remotes) > 0: if len(remotes) > 0:
return remotes.origin.url.removeprefix('git@gitfub.space:') return remotes.origin.url.removeprefix('git@gitfub.space:')
return Path(repo.working_tree_dir).name return Path(repo.working_tree_dir).name
def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]: def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
project_name = determine_project_name(repo) project_name = determine_project_name(repo)
assert project_name is not None assert project_name is not None
@ -50,15 +55,23 @@ def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
labels = [HIDDEN_LABEL_TOTAL] labels = [HIDDEN_LABEL_TOTAL]
labels.append('project:' + project_name) labels.append('project:' + project_name)
labels.append('author:' + commit.author.email) labels.append('author:' + commit.author.email)
yield WorkSample(datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC), tuple(labels)) yield WorkSample(
yield WorkSample(datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC), tuple(labels)) 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 del labels
DEFAULT_EST_TIME = datetime.timedelta(hours=1) DEFAULT_EST_TIME = datetime.timedelta(hours=1)
ZERO_DURATION = datetime.timedelta(seconds=0) ZERO_DURATION = datetime.timedelta(seconds=0)
HOUR = datetime.timedelta(hours=1) HOUR = datetime.timedelta(hours=1)
def generate_report(samples: list[WorkSample]) -> Iterator[str]: def generate_report(samples: list[WorkSample]) -> Iterator[str]:
SAMPLE_FILTER = {} SAMPLE_FILTER = {}
LABEL_FILTER = {} LABEL_FILTER = {}
@ -88,7 +101,7 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
# #
yield '-' * 66 yield '-' * 66
yield '\n' 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): if label_and_type.startswith(HIDDEN_LABEL_PREFIX):
continue continue
@ -105,7 +118,12 @@ def generate_report(samples: list[WorkSample]) -> Iterator[str]:
yield '-' * 66 yield '-' * 66
yield '\n' 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(): def main():
logging.basicConfig() logging.basicConfig()

View File

@ -2,4 +2,3 @@ from git_time_tracker import main
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,4 +1,5 @@
import git_time_tracker import git_time_tracker
def test_report_empty(): def test_report_empty():
assert list(git_time_tracker.generate_report([])) assert list(git_time_tracker.generate_report([]))