2024-08-25 22:26:04 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
import icalendar
|
|
|
|
|
2024-08-25 22:57:51 +00:00
|
|
|
from ..data import HIDDEN_LABEL_PREFIX, RealizedWorkSample
|
2024-08-25 22:26:04 +00:00
|
|
|
|
|
|
|
ZERO_DURATION = datetime.timedelta(seconds=0)
|
|
|
|
HOUR = datetime.timedelta(hours=1)
|
|
|
|
MINUTE = datetime.timedelta(minutes=1)
|
|
|
|
|
2024-08-25 22:37:51 +00:00
|
|
|
|
2024-08-27 19:04:37 +00:00
|
|
|
def create_title(sample: RealizedWorkSample) -> tuple[str,str]:
|
2024-08-25 22:37:51 +00:00
|
|
|
ls = []
|
2024-08-27 19:04:37 +00:00
|
|
|
desc = []
|
2024-08-25 22:37:51 +00:00
|
|
|
for label_and_type in sample.labels:
|
|
|
|
if label_and_type.startswith(HIDDEN_LABEL_PREFIX):
|
|
|
|
continue
|
|
|
|
if label_and_type.startswith('author:'):
|
|
|
|
continue
|
2024-08-27 19:04:37 +00:00
|
|
|
if len(ls) == 0:
|
|
|
|
ls.append(label_and_type.split(':')[1])
|
|
|
|
else:
|
|
|
|
desc.append(label_and_type)
|
|
|
|
return ' '.join(ls), '\n'.join(desc)
|
2024-08-25 22:37:51 +00:00
|
|
|
|
|
|
|
|
2024-08-25 22:26:04 +00:00
|
|
|
def generate_calendar(
|
|
|
|
samples: list[RealizedWorkSample],
|
2024-08-25 22:57:51 +00:00
|
|
|
) -> icalendar.Calendar:
|
2024-08-25 22:26:04 +00:00
|
|
|
max_title_parts = 2
|
|
|
|
|
|
|
|
cal = icalendar.Calendar()
|
|
|
|
cal.add('prodid', '-//personal_data_calendar//example.org//')
|
|
|
|
cal.add('version', '2.0')
|
|
|
|
|
|
|
|
for sample in samples:
|
2024-08-27 19:04:37 +00:00
|
|
|
title, description = create_title(sample)
|
2024-08-25 22:26:04 +00:00
|
|
|
|
|
|
|
# Create event
|
|
|
|
event = icalendar.Event()
|
|
|
|
|
|
|
|
event.add('summary', title)
|
|
|
|
event.add('description', description)
|
|
|
|
event.add('dtstart', sample.start_at)
|
|
|
|
event.add('dtend', sample.end_at)
|
|
|
|
|
2024-08-25 22:37:51 +00:00
|
|
|
for label_and_type in sample.labels:
|
|
|
|
if label_and_type.startswith('author:'):
|
2024-08-25 22:57:51 +00:00
|
|
|
event.add(
|
|
|
|
'organizer', 'mailto:' + label_and_type.removeprefix('author:'),
|
|
|
|
)
|
2024-08-25 22:37:51 +00:00
|
|
|
|
2024-08-25 22:26:04 +00:00
|
|
|
cal.add_component(event)
|
|
|
|
del event
|
|
|
|
|
|
|
|
return cal
|
|
|
|
|
|
|
|
|
|
|
|
def generate_icalendar_file(
|
|
|
|
samples: list[RealizedWorkSample],
|
|
|
|
file: str,
|
|
|
|
) -> None:
|
|
|
|
calendar = generate_calendar(samples)
|
|
|
|
|
|
|
|
with open(file, 'wb') as f:
|
|
|
|
f.write(calendar.to_ical())
|