2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Definition of an asyncio compatible CalDAV calendar.
|
|
|
|
|
|
|
|
Caches events using `timed_alru_cache`.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 18:48:46 +00:00
|
|
|
import logging
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from datetime import datetime, timedelta
|
2022-09-04 20:59:45 +00:00
|
|
|
from functools import total_ordering
|
2022-09-04 18:48:46 +00:00
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
from caldav import Calendar
|
2022-09-04 21:41:40 +00:00
|
|
|
from caldav.lib.error import ReportError
|
|
|
|
from pydantic import BaseModel, validator
|
2022-09-04 20:50:11 +00:00
|
|
|
from vobject.icalendar import VEvent
|
2022-09-04 18:48:46 +00:00
|
|
|
|
2022-09-06 00:16:24 +00:00
|
|
|
from ovdashboard_api.config import Config
|
|
|
|
|
2022-09-04 22:30:40 +00:00
|
|
|
from .async_helpers import get_ttl_hash, run_in_executor, timed_alru_cache
|
|
|
|
from .dav_common import caldav_principal
|
2022-09-04 23:41:51 +00:00
|
|
|
from .settings import SETTINGS
|
2022-09-04 18:48:46 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-09-04 21:41:40 +00:00
|
|
|
def _string_strip(in_str: str) -> str:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Wrapper for str.strip().
|
|
|
|
|
|
|
|
Used to define `pydantic` validators.
|
|
|
|
"""
|
2022-09-04 21:41:40 +00:00
|
|
|
return in_str.strip()
|
|
|
|
|
|
|
|
|
2022-09-04 20:59:45 +00:00
|
|
|
@total_ordering
|
2022-09-04 18:48:46 +00:00
|
|
|
class CalEvent(BaseModel):
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
A CalDAV calendar event.
|
|
|
|
|
|
|
|
Properties are to be named as in the EVENT component of
|
|
|
|
RFC5545 (iCalendar).
|
|
|
|
|
|
|
|
https://icalendar.org/iCalendar-RFC-5545/3-6-1-event-component.html
|
|
|
|
"""
|
|
|
|
|
2022-09-04 21:41:40 +00:00
|
|
|
summary: str = ""
|
|
|
|
description: str = ""
|
|
|
|
dtstart: datetime = datetime.utcnow()
|
|
|
|
dtend: datetime = datetime.utcnow()
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
frozen = True
|
2022-09-04 18:48:46 +00:00
|
|
|
|
2022-09-04 20:59:45 +00:00
|
|
|
def __lt__(self, other: "CalEvent") -> bool:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Order Events by start time.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 20:59:45 +00:00
|
|
|
return self.dtstart < other.dtstart
|
|
|
|
|
|
|
|
def __eq__(self, other: "CalEvent") -> bool:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Compare all properties.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 20:59:45 +00:00
|
|
|
return self.dict() == other.dict()
|
|
|
|
|
2022-09-04 21:41:40 +00:00
|
|
|
_validate_summary = validator(
|
|
|
|
"summary",
|
|
|
|
allow_reuse=True,
|
|
|
|
)(_string_strip)
|
|
|
|
|
|
|
|
_validate_description = validator(
|
|
|
|
"description",
|
|
|
|
allow_reuse=True,
|
|
|
|
)(_string_strip)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_vevent(cls, event: VEvent) -> "CalEvent":
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Create a CalEvent instance from a `VObject.VEvent` object.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 21:41:40 +00:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
for key in cls().dict().keys():
|
|
|
|
try:
|
|
|
|
data[key] = event.contents[key][0].value
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return cls.parse_obj(data)
|
|
|
|
|
2022-09-04 18:48:46 +00:00
|
|
|
|
|
|
|
@timed_alru_cache(maxsize=20)
|
|
|
|
async def _get_calendar(
|
|
|
|
calendar_name: str,
|
|
|
|
) -> Calendar:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Get a calendar by name using the CalDAV principal object.
|
|
|
|
"""
|
2022-09-04 18:48:46 +00:00
|
|
|
|
|
|
|
@run_in_executor
|
2022-09-04 20:50:24 +00:00
|
|
|
def _inner() -> Calendar:
|
2022-09-04 20:50:11 +00:00
|
|
|
return caldav_principal().calendar(calendar_name)
|
|
|
|
|
2022-09-04 20:50:24 +00:00
|
|
|
return await _inner()
|
2022-09-04 20:50:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@timed_alru_cache(maxsize=20)
|
|
|
|
async def _get_calendar_events(
|
|
|
|
calendar_name: str,
|
|
|
|
) -> list[CalEvent]:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Get a sorted list of events by CalDAV calendar name.
|
|
|
|
|
|
|
|
Do not return an iterator here - this result is cached and
|
|
|
|
an iterator would get consumed.
|
|
|
|
"""
|
2022-09-04 20:50:11 +00:00
|
|
|
|
2022-09-06 00:16:24 +00:00
|
|
|
cfg = await Config.get()
|
|
|
|
search_span = timedelta(days=cfg.calendar.future_days)
|
|
|
|
|
2022-09-04 20:50:11 +00:00
|
|
|
@run_in_executor
|
|
|
|
def _inner() -> Iterator[VEvent]:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Get events by CalDAV calendar name.
|
|
|
|
|
|
|
|
This can return an iterator - only the outer function is
|
|
|
|
cached.
|
|
|
|
"""
|
2022-09-04 23:25:40 +00:00
|
|
|
_logger.info(f"updating {calendar_name!r} ...")
|
2022-09-04 18:48:46 +00:00
|
|
|
|
2022-09-04 20:50:11 +00:00
|
|
|
calendar = caldav_principal().calendar(calendar_name)
|
2022-09-04 21:41:40 +00:00
|
|
|
|
|
|
|
date_start = datetime.utcnow().date()
|
|
|
|
time_min = datetime.min.time()
|
|
|
|
dt_start = datetime.combine(date_start, time_min)
|
2022-09-06 00:16:24 +00:00
|
|
|
dt_end = dt_start + search_span
|
2022-09-04 21:41:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
search_result = calendar.date_search(
|
|
|
|
start=dt_start,
|
|
|
|
end=dt_end,
|
2022-09-04 20:50:11 +00:00
|
|
|
expand=True,
|
2022-09-04 21:41:40 +00:00
|
|
|
verify_expand=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
except ReportError:
|
|
|
|
_logger.warn("CalDAV server does not support expanded search")
|
|
|
|
|
|
|
|
search_result = calendar.date_search(
|
|
|
|
start=dt_start,
|
|
|
|
end=dt_end,
|
|
|
|
expand=False,
|
2022-09-04 20:50:11 +00:00
|
|
|
)
|
2022-09-04 21:41:40 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
vevent
|
|
|
|
for event in search_result
|
2022-09-04 20:50:11 +00:00
|
|
|
for vevent in event.vobject_instance.contents["vevent"]
|
|
|
|
)
|
2022-09-04 18:48:46 +00:00
|
|
|
|
2022-09-04 20:59:45 +00:00
|
|
|
return sorted([
|
2022-09-04 21:41:40 +00:00
|
|
|
CalEvent.from_vevent(vevent)
|
2022-09-04 20:50:11 +00:00
|
|
|
for vevent in await _inner()
|
2022-09-04 20:59:45 +00:00
|
|
|
])
|
2022-09-04 18:48:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class DavCalendar:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Object representation of a CalDAV calendar.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 18:48:46 +00:00
|
|
|
calendar_name: str
|
|
|
|
|
|
|
|
@property
|
|
|
|
async def calendar(self) -> Calendar:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Calendar as `caldav` library representation.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 18:48:46 +00:00
|
|
|
return await _get_calendar(
|
2022-09-04 23:41:51 +00:00
|
|
|
ttl_hash=get_ttl_hash(SETTINGS.cache_seconds),
|
2022-09-04 18:48:46 +00:00
|
|
|
calendar_name=self.calendar_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2022-09-04 20:50:11 +00:00
|
|
|
async def events(self) -> list[CalEvent]:
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Calendar events in object representation.
|
|
|
|
"""
|
|
|
|
|
2022-09-04 20:50:11 +00:00
|
|
|
return await _get_calendar_events(
|
2022-09-04 23:41:51 +00:00
|
|
|
ttl_hash=get_ttl_hash(SETTINGS.cache_seconds),
|
2022-09-04 20:50:11 +00:00
|
|
|
calendar_name=self.calendar_name,
|
2022-09-04 18:48:46 +00:00
|
|
|
)
|