1
0
personal-data/git_time_tracker/format/icalendar.py

66 lines
1.6 KiB
Python
Raw Permalink Normal View History

import datetime
import icalendar
2024-10-13 13:20:30 +00:00
from personal_data.activity import HIDDEN_LABEL_CATEGORY, RealizedActivitySample
ZERO_DURATION = datetime.timedelta(seconds=0)
HOUR = datetime.timedelta(hours=1)
MINUTE = datetime.timedelta(minutes=1)
2024-10-13 13:05:55 +00:00
def create_title(sample: RealizedActivitySample) -> tuple[str, str]:
ls = []
desc = []
for label in sample.labels:
if label.category in {HIDDEN_LABEL_CATEGORY, 'author'}:
continue
if len(ls) == 0:
2024-10-14 18:17:41 +00:00
ls.append(str(label.label))
else:
2024-10-14 18:17:41 +00:00
desc.append(str(label.label))
return ' '.join(ls), '\n'.join(desc)
def generate_calendar(
2024-10-13 13:05:55 +00:00
samples: list[RealizedActivitySample],
) -> icalendar.Calendar:
max_title_parts = 2
cal = icalendar.Calendar()
cal.add('prodid', '-//personal_data_calendar//example.org//')
cal.add('version', '2.0')
for sample in samples:
title, description = create_title(sample)
# 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)
for label in sample.labels:
if label.category == 'author':
event.add(
'organizer',
'mailto:' + label.label,
)
cal.add_component(event)
del event
return cal
def generate_icalendar_file(
2024-10-13 13:05:55 +00:00
samples: list[RealizedActivitySample],
file: str,
) -> None:
calendar = generate_calendar(samples)
with open(file, 'wb') as f:
f.write(calendar.to_ical())