55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import datetime
|
|
import urllib.parse
|
|
from typing import Any
|
|
from collections.abc import Iterator
|
|
from decimal import Decimal
|
|
from pathlib import Path
|
|
import dataclasses
|
|
|
|
from personal_data.csv_import import load_csv_file, start_end, determine_possible_keys
|
|
|
|
from ..data import ActivitySample
|
|
|
|
def iterate_samples_from_dicts(rows: list[dict[str,Any]]) -> Iterator[ActivitySample]:
|
|
assert len(rows) > 0
|
|
max_title_parts = 2
|
|
|
|
|
|
if True:
|
|
event_data = rows[len(rows)//2] # Hopefully select a useful representative.
|
|
print(event_data)
|
|
possible_keys = determine_possible_keys(event_data)
|
|
del event_data
|
|
|
|
print(possible_keys)
|
|
assert len(possible_keys.time_start) + len(possible_keys.time_end) >= 1
|
|
assert len(possible_keys.image) >= 0
|
|
|
|
for event_data in rows:
|
|
'''
|
|
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
|
|
'''
|
|
|
|
(start_at, end_at) = start_end(event_data, possible_keys)
|
|
labels = [f'{k}:{event_data[k]}' for k in possible_keys.misc]
|
|
|
|
# Create event
|
|
yield ActivitySample(
|
|
labels=tuple(labels),
|
|
start_at=start_at,
|
|
end_at=end_at,
|
|
)
|
|
|
|
del event_data
|
|
|
|
|
|
def iterate_samples_from_csv_file(file_path: Path) -> Iterator[ActivitySample]:
|
|
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
|