2024-10-13 12:31:23 +00:00
|
|
|
from collections.abc import Iterator
|
2024-10-14 18:17:41 +00:00
|
|
|
from logging import getLogger
|
2024-10-13 12:31:23 +00:00
|
|
|
from pathlib import Path
|
2024-10-13 13:20:30 +00:00
|
|
|
from typing import Any
|
2024-10-13 12:31:23 +00:00
|
|
|
|
2024-10-13 13:20:18 +00:00
|
|
|
from personal_data.activity import ActivitySample, Label
|
2024-10-13 13:20:30 +00:00
|
|
|
from personal_data.csv_import import determine_possible_keys, load_csv_file, start_end
|
2024-10-13 12:31:23 +00:00
|
|
|
|
2024-10-14 18:17:41 +00:00
|
|
|
print(__name__)
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2024-10-13 13:20:30 +00:00
|
|
|
|
|
|
|
def iterate_samples_from_dicts(rows: list[dict[str, Any]]) -> Iterator[ActivitySample]:
|
2024-10-13 12:31:23 +00:00
|
|
|
assert len(rows) > 0
|
|
|
|
max_title_parts = 2
|
|
|
|
|
|
|
|
if True:
|
2024-10-13 13:20:30 +00:00
|
|
|
event_data = rows[len(rows) // 2] # Hopefully select a useful representative.
|
2024-10-13 12:31:23 +00:00
|
|
|
possible_keys = determine_possible_keys(event_data)
|
2024-10-14 18:17:41 +00:00
|
|
|
logger.info('Found possible keys: %s', possible_keys)
|
2024-10-13 12:31:23 +00:00
|
|
|
del event_data
|
|
|
|
|
|
|
|
assert len(possible_keys.time_start) + len(possible_keys.time_end) >= 1
|
|
|
|
assert len(possible_keys.image) >= 0
|
|
|
|
|
|
|
|
for event_data in rows:
|
2024-10-13 13:20:30 +00:00
|
|
|
"""
|
2024-10-13 12:31:23 +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:]
|
|
|
|
)
|
|
|
|
image = event_data[possible_keys.image[0]] if possible_keys.image else None
|
2024-10-13 13:20:30 +00:00
|
|
|
"""
|
2024-10-13 12:31:23 +00:00
|
|
|
|
|
|
|
(start_at, end_at) = start_end(event_data, possible_keys)
|
2024-10-13 13:20:18 +00:00
|
|
|
labels = [Label(k, event_data[k]) for k in possible_keys.misc]
|
2024-10-13 12:31:23 +00:00
|
|
|
|
|
|
|
# Create event
|
2024-10-13 13:05:55 +00:00
|
|
|
yield ActivitySample(
|
2024-10-13 12:31:23 +00:00
|
|
|
labels=tuple(labels),
|
|
|
|
start_at=start_at,
|
|
|
|
end_at=end_at,
|
|
|
|
)
|
|
|
|
|
|
|
|
del event_data
|
|
|
|
|
|
|
|
|
2024-10-13 13:05:55 +00:00
|
|
|
def iterate_samples_from_csv_file(file_path: Path) -> Iterator[ActivitySample]:
|
2024-10-13 12:31:23 +00:00
|
|
|
dicts = load_csv_file(file_path)
|
|
|
|
samples = list(iterate_samples_from_dicts(dicts))
|
|
|
|
assert len(samples) > 0, 'Did not found any samples'
|
|
|
|
yield from samples
|