| 1 |
"""Selection for alarms.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import contextlib |
| 6 |
import datetime |
| 7 |
from typing import TYPE_CHECKING, Sequence |
| 8 |
|
| 9 |
from recurring_ical_events.adapters.event import EventAdapter |
| 10 |
from recurring_ical_events.adapters.todo import TodoAdapter |
| 11 |
from recurring_ical_events.selection.base import SelectComponents |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from icalendar.cal import Component |
| 15 |
|
| 16 |
from recurring_ical_events.adapters.component import ComponentAdapter |
| 17 |
from recurring_ical_events.series import Series |
| 18 |
|
| 19 |
|
| 20 |
class Alarms(SelectComponents): |
| 21 |
"""Select alarms and find their times. |
| 22 |
|
| 23 |
By default, alarms from TODOs and events are collected. |
| 24 |
You can use this to change which alarms are collected: |
| 25 |
|
| 26 |
Alarms((EventAdapter,)) |
| 27 |
Alarms((TodoAdapter,)) |
| 28 |
""" |
| 29 |
|
| 30 |
def __init__( |
| 31 |
self, |
| 32 |
parents: tuple[type[ComponentAdapter] | SelectComponents] = ( |
| 33 |
EventAdapter, |
| 34 |
TodoAdapter, |
| 35 |
), |
| 36 |
): |
| 37 |
self.parents = parents |
| 38 |
|
| 39 |
@staticmethod |
| 40 |
def component_name(): |
| 41 |
"""The name of the component we calculate.""" |
| 42 |
return "VALARM" |
| 43 |
|
| 44 |
def collect_parent_series_from( |
| 45 |
self, source: Component, suppress_errors: tuple[Exception] |
| 46 |
) -> Sequence[Series]: |
| 47 |
"""Collect the parent components of alarms.""" |
| 48 |
return [ |
| 49 |
s |
| 50 |
for parent in self.parents |
| 51 |
for s in parent.collect_series_from(source, suppress_errors) |
| 52 |
] |
| 53 |
|
| 54 |
def collect_series_from( |
| 55 |
self, source: Component, suppress_errors: tuple[Exception] |
| 56 |
) -> Sequence[Series]: |
| 57 |
"""Collect all TODOs and Alarms from VEVENTs and VTODOs. |
| 58 |
|
| 59 |
suppress_errors - a list of errors that should be suppressed. |
| 60 |
A Series of events with such an error is removed from all results. |
| 61 |
""" |
| 62 |
from recurring_ical_events.series.alarm import ( |
| 63 |
AbsoluteAlarmSeries, |
| 64 |
AlarmSeriesRelativeToEnd, |
| 65 |
AlarmSeriesRelativeToStart, |
| 66 |
) |
| 67 |
|
| 68 |
absolute_alarms = AbsoluteAlarmSeries() |
| 69 |
result = [] |
| 70 |
|
| 71 |
for series in self.collect_parent_series_from(source, suppress_errors): |
| 72 |
used_alarms = [] |
| 73 |
for component in series.components: |
| 74 |
for alarm in component.alarms: |
| 75 |
with contextlib.suppress(suppress_errors): |
| 76 |
trigger = alarm.TRIGGER |
| 77 |
if trigger is None or alarm in used_alarms: |
| 78 |
continue |
| 79 |
if isinstance(trigger, datetime.datetime): |
| 80 |
absolute_alarms.add(alarm, component) |
| 81 |
used_alarms.append(alarm) |
| 82 |
elif alarm.TRIGGER_RELATED == "START": |
| 83 |
result.append(AlarmSeriesRelativeToStart(alarm, series)) |
| 84 |
used_alarms.append(alarm) |
| 85 |
elif alarm.TRIGGER_RELATED == "END": |
| 86 |
result.append(AlarmSeriesRelativeToEnd(alarm, series)) |
| 87 |
used_alarms.append(alarm) |
| 88 |
if not absolute_alarms.is_empty(): |
| 89 |
result.append(absolute_alarms) |
| 90 |
return result |
| 91 |
|
| 92 |
|
| 93 |
__all__ = ["Alarms"] |