| 1 |
"""Selecting components by name.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import contextlib |
| 6 |
from collections import defaultdict |
| 7 |
from typing import TYPE_CHECKING, Sequence |
| 8 |
|
| 9 |
from recurring_ical_events.adapters.event import EventAdapter |
| 10 |
from recurring_ical_events.adapters.journal import JournalAdapter |
| 11 |
from recurring_ical_events.adapters.todo import TodoAdapter |
| 12 |
from recurring_ical_events.occurrence import Occurrence |
| 13 |
from recurring_ical_events.selection.alarm import Alarms |
| 14 |
from recurring_ical_events.selection.base import SelectComponents |
| 15 |
from recurring_ical_events.series import Series |
| 16 |
from recurring_ical_events.util import cached_property |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from icalendar.cal import Component |
| 20 |
|
| 21 |
from recurring_ical_events.adapters.component import ComponentAdapter |
| 22 |
|
| 23 |
|
| 24 |
class ComponentsWithName(SelectComponents): |
| 25 |
"""This is a component collecttion strategy. |
| 26 |
|
| 27 |
Components can be collected in different ways. |
| 28 |
This class allows extension of the functionality by |
| 29 |
- subclassing to filter the resulting components |
| 30 |
- composition to combine collection behavior (see AllKnownComponents) |
| 31 |
""" |
| 32 |
|
| 33 |
component_adapters: list[type[ComponentAdapter] | SelectComponents] = [ |
| 34 |
EventAdapter, |
| 35 |
TodoAdapter, |
| 36 |
JournalAdapter, |
| 37 |
Alarms(), |
| 38 |
] |
| 39 |
|
| 40 |
@cached_property |
| 41 |
def _component_adapters(self) -> dict[str : type[ComponentAdapter]]: |
| 42 |
"""A mapping of component adapters.""" |
| 43 |
return { |
| 44 |
adapter.component_name(): adapter for adapter in self.component_adapters |
| 45 |
} |
| 46 |
|
| 47 |
def __init__( |
| 48 |
self, |
| 49 |
name: str, |
| 50 |
adapter: type[ComponentAdapter] | None = None, |
| 51 |
series: type[Series] = Series, |
| 52 |
occurrence: type[Occurrence] = Occurrence, |
| 53 |
) -> None: |
| 54 |
"""Create a new way of collecting components. |
| 55 |
|
| 56 |
name - the name of the component to collect ("VEVENT", "VTODO", "VJOURNAL") |
| 57 |
adapter - the adapter to use for these components with that name |
| 58 |
series - the series class that hold a series of components |
| 59 |
occurrence - the occurrence class that creates the resulting components |
| 60 |
""" |
| 61 |
if adapter is None: |
| 62 |
if name not in self._component_adapters: |
| 63 |
raise ValueError( |
| 64 |
f'"{name}" is an unknown name for a ' |
| 65 |
"recurring component. " |
| 66 |
f"I only know these: {', '.join(self._component_adapters)}." |
| 67 |
) |
| 68 |
adapter = self._component_adapters[name] |
| 69 |
if occurrence is not Occurrence: |
| 70 |
_occurrence = occurrence |
| 71 |
|
| 72 |
class series(series): |
| 73 |
occurrence = _occurrence |
| 74 |
|
| 75 |
self._name = name |
| 76 |
self._series = series |
| 77 |
self._adapter = adapter |
| 78 |
|
| 79 |
def collect_series_from( |
| 80 |
self, source: Component, suppress_errors: tuple[Exception] |
| 81 |
) -> Sequence[Series]: |
| 82 |
"""Collect all components from the source component. |
| 83 |
|
| 84 |
suppress_errors - a list of errors that should be suppressed. |
| 85 |
A Series of events with such an error is removed from all results. |
| 86 |
""" |
| 87 |
if isinstance(self._adapter, SelectComponents): |
| 88 |
return self._adapter.collect_series_from(source, suppress_errors) |
| 89 |
components: dict[str, list[Component]] = defaultdict(list) |
| 90 |
for component in source.walk(self._name): |
| 91 |
adapter = self._adapter(component) |
| 92 |
components[adapter.uid].append(adapter) |
| 93 |
result = [] |
| 94 |
for components in components.values(): |
| 95 |
with contextlib.suppress(suppress_errors): |
| 96 |
result.append(self._series(components)) |
| 97 |
return result |
| 98 |
|
| 99 |
|
| 100 |
__all__ = ["ComponentsWithName"] |