| 1 | """Selection of all components with the correct adapters.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from typing import TYPE_CHECKING, Sequence |
| 6 | |
| 7 | from recurring_ical_events.occurrence import Occurrence |
| 8 | from recurring_ical_events.selection.base import SelectComponents |
| 9 | from recurring_ical_events.selection.name import ComponentsWithName |
| 10 | from recurring_ical_events.series import Series |
| 11 | |
| 12 | if TYPE_CHECKING: |
| 13 | from icalendar.cal import Component |
| 14 | |
| 15 | from recurring_ical_events.adapters.component import ComponentAdapter |
| 16 | |
| 17 | |
| 18 | class AllKnownComponents(SelectComponents): |
| 19 | """Group all known components into series.""" |
| 20 | |
| 21 | @property |
| 22 | def _component_adapters(self) -> Sequence[ComponentAdapter]: |
| 23 | """Return all known component adapters.""" |
| 24 | return ComponentsWithName.component_adapters |
| 25 | |
| 26 | @property |
| 27 | def names(self) -> list[str]: |
| 28 | """Return the names of the components to collect.""" |
| 29 | result = [adapter.component_name() for adapter in self._component_adapters] |
| 30 | result.sort() |
| 31 | return result |
| 32 | |
| 33 | def __init__( |
| 34 | self, |
| 35 | series: type[Series] = Series, |
| 36 | occurrence: type[Occurrence] = Occurrence, |
| 37 | collector: type[ComponentsWithName] = ComponentsWithName, |
| 38 | ) -> None: |
| 39 | """Collect all known components and overide the series and occurrence. |
| 40 | |
| 41 | series - the Series class to override that is queried for Occurrences |
| 42 | occurrence - the occurrence class that creates the resulting components |
| 43 | collector - if you want to override the SelectComponentsByName class |
| 44 | """ |
| 45 | self._series = series |
| 46 | self._occurrence = occurrence |
| 47 | self._collector = collector |
| 48 | |
| 49 | def collect_series_from( |
| 50 | self, source: Component, suppress_errors: tuple[Exception] |
| 51 | ) -> Sequence[Series]: |
| 52 | """Collect the components from the source groups into a series.""" |
| 53 | result = [] |
| 54 | for name in self.names: |
| 55 | collector = self._collector( |
| 56 | name, series=self._series, occurrence=self._occurrence |
| 57 | ) |
| 58 | result.extend(collector.collect_series_from(source, suppress_errors)) |
| 59 | return result |
| 60 | |
| 61 | |
| 62 | __all__ = ["AllKnownComponents"] |