1
0
This repository has been archived on 2024-10-13. You can view files and clone it, but cannot push or open issues or pull requests.
git-time-tracker/git_time_tracker/source/csv_file.py

71 lines
2.2 KiB
Python
Raw Normal View History

2024-08-27 19:05:08 +00:00
import argparse
from collections.abc import Iterator
2024-09-08 18:29:45 +00:00
from decimal import Decimal
2024-08-27 19:05:08 +00:00
import datetime
import urllib.parse
from pathlib import Path
from personal_data.util import load_csv_file
from ..data import WorkSample
def iterate_samples_from_dicts(rows: list[dict]) -> Iterator[WorkSample]:
max_title_parts = 2
for event_data in rows:
# Select data
possible_time_keys = [
k for k, v in event_data.items() if isinstance(v, datetime.date)
]
2024-09-08 18:29:45 +00:00
possible_duration_keys = [
k for k, v in event_data.items() if isinstance(v, Decimal) and 'duration_seconds' in k
]
2024-08-27 19:05:08 +00:00
possible_name_keys = [k for k, v in event_data.items() if isinstance(v, str)]
possible_image_keys = [
k for k, v in event_data.items() if isinstance(v, urllib.parse.ParseResult)
]
possible_misc_keys = list(event_data.keys())
for k in possible_image_keys:
if k in possible_misc_keys:
possible_misc_keys.remove(k)
del k
for k in possible_time_keys :
if k in possible_misc_keys:
possible_misc_keys.remove(k)
del k
date = event_data[possible_time_keys[0]] if possible_time_keys else None
image = event_data[possible_image_keys[0]] if possible_image_keys else None
if date is None:
continue
2024-09-08 18:29:45 +00:00
if len(possible_duration_keys) > 0:
start_at = date
seconds = event_data[possible_duration_keys[0]]
end_at = date + datetime.timedelta(seconds = float(seconds))
del seconds
else:
start_at = None
end_at = date
2024-08-27 19:05:08 +00:00
title = ': '.join(event_data[k] for k in possible_name_keys[:max_title_parts])
description = '\n\n'.join(event_data[k] for k in possible_name_keys[max_title_parts:])
labels = [f'{k}:{event_data[k]}' for k in possible_misc_keys]
# Create event
yield WorkSample(
labels=tuple(labels),
2024-09-08 18:29:45 +00:00
start_at=start_at,
end_at=end_at,
2024-08-27 19:05:08 +00:00
)
del event_data
def iterate_samples_from_csv_file(file_path: Path) -> Iterator[WorkSample]:
dicts = load_csv_file(file_path)
yield from iterate_samples_from_dicts(dicts)