diff --git a/docs/reference/api/icalendar.cal.location.rst b/docs/reference/api/icalendar.cal.location.rst new file mode 100644 index 000000000..25f58ae8f --- /dev/null +++ b/docs/reference/api/icalendar.cal.location.rst @@ -0,0 +1,8 @@ +icalendar.cal.location module +============================= + +.. automodule:: icalendar.cal.location + :ignore-module-all: + :members: + :show-inheritance: + :undoc-members: diff --git a/docs/reference/api/icalendar.cal.participant.rst b/docs/reference/api/icalendar.cal.participant.rst new file mode 100644 index 000000000..4fb7857ee --- /dev/null +++ b/docs/reference/api/icalendar.cal.participant.rst @@ -0,0 +1,8 @@ +icalendar.cal.participant module +================================ + +.. automodule:: icalendar.cal.participant + :ignore-module-all: + :members: + :show-inheritance: + :undoc-members: diff --git a/docs/reference/api/icalendar.cal.resource.rst b/docs/reference/api/icalendar.cal.resource.rst new file mode 100644 index 000000000..010699351 --- /dev/null +++ b/docs/reference/api/icalendar.cal.resource.rst @@ -0,0 +1,8 @@ +icalendar.cal.resource module +============================= + +.. automodule:: icalendar.cal.resource + :ignore-module-all: + :members: + :show-inheritance: + :undoc-members: diff --git a/docs/reference/api/icalendar.cal.rst b/docs/reference/api/icalendar.cal.rst index 66ebb2837..89d484e1e 100644 --- a/docs/reference/api/icalendar.cal.rst +++ b/docs/reference/api/icalendar.cal.rst @@ -18,6 +18,9 @@ Submodules icalendar.cal.free_busy icalendar.cal.journal icalendar.cal.lazy + icalendar.cal.location + icalendar.cal.participant + icalendar.cal.resource icalendar.cal.timezone icalendar.cal.todo diff --git a/news/656.feature.1 b/news/656.feature.1 new file mode 100644 index 000000000..c8bfbd628 --- /dev/null +++ b/news/656.feature.1 @@ -0,0 +1 @@ +Added :class:`~icalendar.cal.participant.Participant`, :class:`~icalendar.cal.location.Location`, and :class:`~icalendar.cal.resource.Resource` components from :rfc:`9073`. :user:`SashankBhamidi` diff --git a/news/656.feature.2 b/news/656.feature.2 new file mode 100644 index 000000000..e09dce0f5 --- /dev/null +++ b/news/656.feature.2 @@ -0,0 +1 @@ +Added ORDER, SCHEMA, and DERIVED property parameters from :rfc:`9073`. :user:`SashankBhamidi` diff --git a/news/656.feature.3 b/news/656.feature.3 new file mode 100644 index 000000000..f85cd8810 --- /dev/null +++ b/news/656.feature.3 @@ -0,0 +1 @@ +Added PARTICIPANT-TYPE, RESOURCE-TYPE, LOCATION-TYPE, CALENDAR-ADDRESS, STYLED-DESCRIPTION, and STRUCTURED-DATA properties from :rfc:`9073`. :user:`SashankBhamidi` diff --git a/news/657.feature.1 b/news/657.feature.1 new file mode 100644 index 000000000..b35029231 --- /dev/null +++ b/news/657.feature.1 @@ -0,0 +1 @@ +Completed :rfc:`9074` support: added PROXIMITY enum, typed Alarm.proximity accessor, RELTYPE.SNOOZE, and registered VLOCATION inside VALARM. :user:`SashankBhamidi` diff --git a/src/icalendar/__init__.py b/src/icalendar/__init__.py index 1684a0e07..e72cc82e5 100644 --- a/src/icalendar/__init__.py +++ b/src/icalendar/__init__.py @@ -13,6 +13,9 @@ FreeBusy, Journal, LazyCalendar, + Location, + Participant, + Resource, Timezone, TimezoneDaylight, TimezoneStandard, @@ -24,10 +27,13 @@ CLASS, CUTYPE, FBTYPE, + PARTICIPANT_TYPE, PARTSTAT, + PROXIMITY, RANGE, RELATED, RELTYPE, + RESOURCE_TYPE, ROLE, STATUS, TRANSP, @@ -109,10 +115,13 @@ "CLASS", "CUTYPE", "FBTYPE", + "PARTICIPANT_TYPE", "PARTSTAT", + "PROXIMITY", "RANGE", "RELATED", "RELTYPE", + "RESOURCE_TYPE", "ROLE", "STATUS", "TRANSP", @@ -145,8 +154,11 @@ "Journal", "LazyCalendar", "LocalTimezoneMissing", + "Location", "NFields", "Parameters", + "Participant", + "Resource", "Timezone", "TimezoneDaylight", "TimezoneStandard", diff --git a/src/icalendar/alarms.py b/src/icalendar/alarms.py index 36b9888fc..13fdf4b32 100644 --- a/src/icalendar/alarms.py +++ b/src/icalendar/alarms.py @@ -141,6 +141,15 @@ def attachments(self) -> list[vUri | vBinary]: """ return self.alarm.attachments + @property + def proximity(self): + """The type of location-based trigger for this alarm. + + This delegates to :attr:`Alarm.proximity + `. + """ + return self.alarm.proximity + @property def parent(self) -> Parent | None: """The component that contains the alarm. diff --git a/src/icalendar/attr/__init__.py b/src/icalendar/attr/__init__.py index 5e068e8bc..e3d1b6667 100644 --- a/src/icalendar/attr/__init__.py +++ b/src/icalendar/attr/__init__.py @@ -1385,17 +1385,18 @@ def _del_organizer(self: Component): def single_string_enum_property( name: str, enum: type[StrEnum], - default: StrEnum | str, + default: StrEnum | str | None, docs: str, preserve_unknown: bool = False, ) -> property: """Create a property to access a single string value and convert it to an enum.""" - prop = single_string_property(name, docs, default=default) + _str_default = "" if default is None else default + prop = single_string_property(name, docs, default=_str_default) - def fget(self: Component) -> StrEnum | str: + def fget(self: Component) -> StrEnum | str | None: """Get the value.""" value = prop.fget(self) - if value == default: + if value == _str_default: return default try: return enum(str(value)) diff --git a/src/icalendar/cal/__init__.py b/src/icalendar/cal/__init__.py index 0a8279344..a82501a1d 100644 --- a/src/icalendar/cal/__init__.py +++ b/src/icalendar/cal/__init__.py @@ -14,6 +14,9 @@ from .free_busy import FreeBusy from .journal import Journal from .lazy import LazyCalendar +from .location import Location +from .participant import Participant +from .resource import Resource from .timezone import Timezone, TimezoneDaylight, TimezoneStandard from .todo import Todo @@ -28,6 +31,9 @@ "FreeBusy", "Journal", "LazyCalendar", + "Location", + "Participant", + "Resource", "Timezone", "TimezoneDaylight", "TimezoneStandard", diff --git a/src/icalendar/cal/alarm.py b/src/icalendar/cal/alarm.py index a6a3c7a15..be2fc3881 100644 --- a/src/icalendar/cal/alarm.py +++ b/src/icalendar/cal/alarm.py @@ -30,6 +30,7 @@ from icalendar.cal.component import Component from icalendar.cal.examples import get_example from icalendar.enums import ACTION as AlarmAction # noqa: N811 +from icalendar.enums import PROXIMITY from icalendar.error import InvalidCalendar from icalendar.prop import vCalAddress @@ -204,6 +205,24 @@ class Alarm(Component): """, ) + proximity = single_string_enum_property( + "PROXIMITY", + PROXIMITY, + None, + """The type of location-based trigger for this alarm per :rfc:`9074#section-8.1`. + + Although the RFC says that when this sub-component is set, a VLOCATION + sub-component MUST also be present to identify the location that + triggers the alarm, icalendar does not enforce this requirement. + + Returns: + Either a :class:`~icalendar.enums.PROXIMITY` member for known values + (``ARRIVE``, ``DEPART``, ``CONNECT``, or ``DISCONNECT``), a plain + :class:`str` for unknown values, or ``None`` when absent. + """, + preserve_unknown=True, + ) + TRIGGER = create_single_property( "TRIGGER", "dt", diff --git a/src/icalendar/cal/component_factory.py b/src/icalendar/cal/component_factory.py index 0dc7d77a0..81c7c6a7a 100644 --- a/src/icalendar/cal/component_factory.py +++ b/src/icalendar/cal/component_factory.py @@ -52,6 +52,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: from icalendar.cal.event import Event from icalendar.cal.free_busy import FreeBusy from icalendar.cal.journal import Journal + from icalendar.cal.location import Location + from icalendar.cal.participant import Participant + from icalendar.cal.resource import Resource from icalendar.cal.timezone import ( Timezone, TimezoneDaylight, @@ -70,6 +73,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: self.add_component_class(Alarm) self.add_component_class(Available) self.add_component_class(Availability) + self.add_component_class(Participant) + self.add_component_class(Location) + self.add_component_class(Resource) def add_component_class(self, cls: type[Component]) -> None: """Add a component class to the factory. diff --git a/src/icalendar/cal/event.py b/src/icalendar/cal/event.py index 6fd534909..21a77a4c5 100644 --- a/src/icalendar/cal/event.py +++ b/src/icalendar/cal/event.py @@ -260,6 +260,27 @@ class Event(Component): ) ignore_exceptions = True + @property + def participants(self): + """All PARTICIPANT sub-components of this event per :rfc:`9073#section-7.1`.""" + from icalendar.cal.participant import Participant + + return self.walk(Participant.name) + + @property + def locations(self): + """All VLOCATION sub-components of this event per :rfc:`9073#section-7.2`.""" + from icalendar.cal.location import Location + + return self.walk(Location.name) + + @property + def resources(self): + """All VRESOURCE sub-components of this event per :rfc:`9073#section-7.3`.""" + from icalendar.cal.resource import Resource + + return self.walk(Resource.name) + @property def alarms(self) -> Alarms: """Compute the alarm times for this component. diff --git a/src/icalendar/cal/free_busy.py b/src/icalendar/cal/free_busy.py index a0f2ab560..9b8f7cfe9 100644 --- a/src/icalendar/cal/free_busy.py +++ b/src/icalendar/cal/free_busy.py @@ -182,6 +182,27 @@ def new( cls._validate_start_and_end(start, end) return free_busy + @property + def participants(self): + """All PARTICIPANT sub-components of this component per :rfc:`9073#section-7.1`.""" + from icalendar.cal.participant import Participant + + return self.walk(Participant.name) + + @property + def locations(self): + """All VLOCATION sub-components of this component per :rfc:`9073#section-7.2`.""" + from icalendar.cal.location import Location + + return self.walk(Location.name) + + @property + def resources(self): + """All VRESOURCE sub-components of this component per :rfc:`9073#section-7.3`.""" + from icalendar.cal.resource import Resource + + return self.walk(Resource.name) + @classmethod def example(cls, name: str = "example") -> FreeBusy: """Return the FreeBusy example with the given name.""" diff --git a/src/icalendar/cal/journal.py b/src/icalendar/cal/journal.py index 72ecec9d1..af6b495b8 100644 --- a/src/icalendar/cal/journal.py +++ b/src/icalendar/cal/journal.py @@ -303,6 +303,27 @@ def new( return journal + @property + def participants(self): + """All PARTICIPANT sub-components of this journal per :rfc:`9073#section-7.1`.""" + from icalendar.cal.participant import Participant + + return self.walk(Participant.name) + + @property + def locations(self): + """All VLOCATION sub-components of this journal per :rfc:`9073#section-7.2`.""" + from icalendar.cal.location import Location + + return self.walk(Location.name) + + @property + def resources(self): + """All VRESOURCE sub-components of this journal per :rfc:`9073#section-7.3`.""" + from icalendar.cal.resource import Resource + + return self.walk(Resource.name) + @classmethod def example(cls, name: str = "example") -> Journal: """Return the journal example with the given name.""" diff --git a/src/icalendar/cal/location.py b/src/icalendar/cal/location.py new file mode 100644 index 000000000..1c77928f7 --- /dev/null +++ b/src/icalendar/cal/location.py @@ -0,0 +1,87 @@ +""":rfc:`9073` Location component.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from icalendar.attr import ( + description_property, + single_string_property, + uid_property, +) +from icalendar.cal.component import Component + +if TYPE_CHECKING: + from icalendar.prop import vGeo + + +class Location(Component): + """A VLOCATION calendar component from :rfc:`9073#section-7.2`. + + This component provides structured location information for a calendar + component. + + It may appear inside :class:`~icalendar.cal.alarm.Alarm`, + :class:`~icalendar.cal.event.Event`, + :class:`~icalendar.cal.free_busy.FreeBusy`, + :class:`~icalendar.cal.journal.Journal`, + :class:`~icalendar.cal.participant.Participant`, or + :class:`~icalendar.cal.todo.Todo` per :rfc:`9074#section-8`. Multiple + instances are allowed. + + Example: + + .. code-block:: pycon + + >>> from icalendar import Location + >>> loc = Location() + >>> loc.location_name = "Conference Room A" + >>> loc.uid = "room-a-uid@example.com" + >>> str(loc.location_name) + 'Conference Room A' + """ + + name = "VLOCATION" + singletons = ("NAME", "DESCRIPTION", "GEO", "LOCATION-TYPE", "UID") + + location_name = single_string_property( + "NAME", + """The display name of this location per :rfc:`7986#section-5.1`. + + Returns: + The value of the NAME property when set, or the empty string when + no NAME property is set. + """, + ) + + description = description_property + + uid = uid_property + + location_type = single_string_property( + "LOCATION-TYPE", + """The raw LOCATION-TYPE value per :rfc:`9073#section-6.1`. + + The property value is a comma-separated list of location type tokens + from the IANA registry defined in :rfc:`4589#section-3`, + for example, ``"office,airport"``. This accessor returns the raw + string. Split on commas to work with individual tokens. + + Returns: + The value of the LOCATION-TYPE property if set, or the empty string + when not set. + """, + ) + + @property + def geo(self) -> vGeo | None: + """The geographic position of this location per :rfc:`5545#section-3.8.1.6`. + + Returns: + A :class:`~icalendar.prop.geo.vGeo` instance, or ``None`` when + no GEO property is set. + """ + return self.get("GEO") + + +__all__ = ["Location"] diff --git a/src/icalendar/cal/participant.py b/src/icalendar/cal/participant.py new file mode 100644 index 000000000..6b9827543 --- /dev/null +++ b/src/icalendar/cal/participant.py @@ -0,0 +1,98 @@ +""":rfc:`9073` PARTICIPANT component.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from icalendar.attr import ( + description_property, + single_string_enum_property, + summary_property, + uid_property, +) +from icalendar.cal.component import Component +from icalendar.enums import PARTICIPANT_TYPE + +if TYPE_CHECKING: + from icalendar.prop import vCalAddress + + +class Participant(Component): + """A PARTICIPANT calendar component from :rfc:`9073#section-7.1`. + + This component provides structured participant information for a calendar + component. It may appear inside :class:`~icalendar.cal.event.Event`, + :class:`~icalendar.cal.free_busy.FreeBusy`, + :class:`~icalendar.cal.journal.Journal`, or + :class:`~icalendar.cal.todo.Todo` per :rfc:`9074#section-8`. Multiple + instances are allowed. + + Example: + + .. code-block:: pycon + + >>> from icalendar import Participant, PARTICIPANT_TYPE + >>> p = Participant() + >>> p.participant_type = PARTICIPANT_TYPE.ACTIVE + >>> p.participant_type + + """ + + name = "PARTICIPANT" + singletons = ( + "PARTICIPANT-TYPE", + "CALENDAR-ADDRESS", + "DESCRIPTION", + "SUMMARY", + "UID", + ) + required = ("PARTICIPANT-TYPE",) + + participant_type = single_string_enum_property( + "PARTICIPANT-TYPE", + PARTICIPANT_TYPE, + None, + """The type of participant per :rfc:`9073#section-6.2`. + + Returns a :class:`~icalendar.enums.PARTICIPANT_TYPE` member for + IANA-registered values, a plain :class:`str` for unknown values, + or ``None`` when absent. icalendar does not enforce that this + property appears exactly once. + + IANA-registered values from :rfc:`9073#section-11.2.1`: + ``ACTIVE``, ``INACTIVE``, ``SPONSOR``, ``CONTACT``, + ``BOOKING-CONTACT``, ``EMERGENCY-CONTACT``, ``PUBLICITY-CONTACT``, + ``PLANNER-CONTACT``, ``PERFORMER``, ``SPEAKER``. + """, + preserve_unknown=True, + ) + + @property + def calendar_address(self) -> vCalAddress | None: + """The scheduling address for this participant per :rfc:`9073#section-6.4`. + + Returns a :class:`~icalendar.prop.cal_address.vCalAddress` instance + or ``None`` when no CALENDAR-ADDRESS property is present. + """ + return self.get("CALENDAR-ADDRESS") + + uid = uid_property + summary = summary_property + description = description_property + + @property + def locations(self) -> list: + """All VLOCATION sub-components of this participant per :rfc:`9073#section-7.2`.""" + from icalendar.cal.location import Location + + return self.walk(Location.name) + + @property + def resources(self) -> list: + """All VRESOURCE sub-components of this participant per :rfc:`9073#section-7.3`.""" + from icalendar.cal.resource import Resource + + return self.walk(Resource.name) + + +__all__ = ["Participant"] diff --git a/src/icalendar/cal/resource.py b/src/icalendar/cal/resource.py new file mode 100644 index 000000000..cd8992282 --- /dev/null +++ b/src/icalendar/cal/resource.py @@ -0,0 +1,90 @@ +""":rfc:`9073` Resource component.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from icalendar.attr import ( + description_property, + single_string_enum_property, + single_string_property, + uid_property, +) +from icalendar.cal.component import Component +from icalendar.enums import RESOURCE_TYPE + +if TYPE_CHECKING: + from icalendar.prop import vGeo + + +class Resource(Component): + """A VRESOURCE calendar component from :rfc:`9073#section-7.3`. + + This component provides structured resource information for a calendar + component. It may appear inside :class:`~icalendar.cal.event.Event`, + :class:`~icalendar.cal.free_busy.FreeBusy`, + :class:`~icalendar.cal.journal.Journal`, + :class:`~icalendar.cal.participant.Participant`, or + :class:`~icalendar.cal.todo.Todo` per :rfc:`9074#section-8`. Multiple + instances are allowed. + + Example: + + .. code-block:: pycon + + >>> from icalendar import Resource, RESOURCE_TYPE + >>> res = Resource() + >>> res.resource_name = "Projector" + >>> res.resource_type = RESOURCE_TYPE.PROJECTOR + >>> res.resource_type + + """ + + name = "VRESOURCE" + singletons = ("NAME", "DESCRIPTION", "GEO", "RESOURCE-TYPE", "UID") + + resource_name = single_string_property( + "NAME", + """The display name of this resource per :rfc:`7986#section-5.1`. + + Returns: + The value of the NAME property when set, or the empty string when + no NAME property is set. + """, + ) + + description = description_property + + uid = uid_property + + resource_type = single_string_enum_property( + "RESOURCE-TYPE", + RESOURCE_TYPE, + None, + """The type of resource per :rfc:`9073#section-6.3`. + + IANA-registered values are defined in :rfc:`9073#section-11.2.2`: + ``ROOM``, ``PROJECTOR``, ``REMOTE-CONFERENCE-AUDIO``, and + ``REMOTE-CONFERENCE-VIDEO``. + + Returns: + A :class:`~icalendar.enums.RESOURCE_TYPE` member for + IANA-registered values, a plain :class:`str` for unknown values, or + ``None`` when absent. + + """, + preserve_unknown=True, + ) + + @property + def geo(self) -> vGeo | None: + """The geographic position of this resource per :rfc:`5545#section-3.8.1.6`. + + Returns: + A :class:`~icalendar.prop.geo.vGeo` instance, or ``None`` when + no GEO property is set. + """ + return self.get("GEO") + + +__all__ = ["Resource"] diff --git a/src/icalendar/cal/todo.py b/src/icalendar/cal/todo.py index 350383287..920c4ac08 100644 --- a/src/icalendar/cal/todo.py +++ b/src/icalendar/cal/todo.py @@ -307,6 +307,27 @@ def set_end( X_MOZ_SNOOZE_TIME = X_MOZ_SNOOZE_TIME_property X_MOZ_LASTACK = X_MOZ_LASTACK_property + @property + def participants(self): + """All PARTICIPANT sub-components of this to-do per :rfc:`9073#section-7.1`.""" + from icalendar.cal.participant import Participant + + return self.walk(Participant.name) + + @property + def locations(self): + """All VLOCATION sub-components of this to-do per :rfc:`9073#section-7.2`.""" + from icalendar.cal.location import Location + + return self.walk(Location.name) + + @property + def resources(self): + """All VRESOURCE sub-components of this to-do per :rfc:`9073#section-7.3`.""" + from icalendar.cal.resource import Resource + + return self.walk(Resource.name) + @property def alarms(self) -> Alarms: """Compute the alarm times for this component. diff --git a/src/icalendar/enums.py b/src/icalendar/enums.py index 6ab2959e2..ba4f3d526 100644 --- a/src/icalendar/enums.py +++ b/src/icalendar/enums.py @@ -369,6 +369,7 @@ class RELTYPE(StrEnum): DEPENDS_ON = "DEPENDS-ON" REFID = "REFID" CONCEPT = "CONCEPT" + SNOOZE = "SNOOZE" class RANGE(StrEnum): @@ -583,16 +584,96 @@ class TRANSP(StrEnum): TRANSPARENT = "TRANSPARENT" +class PARTICIPANT_TYPE(StrEnum): # noqa: N801 + """Enum for PARTICIPANT-TYPE from :rfc:`9073#section-6.2`. + + This parameter identifies the type of participant represented by a + PARTICIPANT component. IANA-registered values are listed in + :rfc:`9073#section-11.2.1`. Unknown values are allowed, that is, they + are IANA-extensible. + + Possible values are the following. + + - :attr:`ACTIVE` + - :attr:`BOOKING_CONTACT` + - :attr:`CONTACT` + - :attr:`EMERGENCY_CONTACT` + - :attr:`INACTIVE` + - :attr:`PERFORMER` + - :attr:`PLANNER_CONTACT` + - :attr:`PUBLICITY_CONTACT` + - :attr:`SPEAKER` + - :attr:`SPONSOR` + """ + + ACTIVE = "ACTIVE" + BOOKING_CONTACT = "BOOKING-CONTACT" + CONTACT = "CONTACT" + EMERGENCY_CONTACT = "EMERGENCY-CONTACT" + INACTIVE = "INACTIVE" + PERFORMER = "PERFORMER" + PLANNER_CONTACT = "PLANNER-CONTACT" + PUBLICITY_CONTACT = "PUBLICITY-CONTACT" + SPEAKER = "SPEAKER" + SPONSOR = "SPONSOR" + + +class RESOURCE_TYPE(StrEnum): # noqa: N801 + """Enum for RESOURCE-TYPE from :rfc:`9073#section-6.3`. + + This parameter identifies the type of resource represented by a VRESOURCE + component. IANA-registered values are listed in :rfc:`9073#section-11.2.2`. + Unknown values are allowed, that is, they are IANA-extensible. + + Possible values are the following. + + - :attr:`PROJECTOR` + - :attr:`REMOTE_CONFERENCE_AUDIO` + - :attr:`REMOTE_CONFERENCE_VIDEO` + - :attr:`ROOM` + """ + + PROJECTOR = "PROJECTOR" + REMOTE_CONFERENCE_AUDIO = "REMOTE-CONFERENCE-AUDIO" + REMOTE_CONFERENCE_VIDEO = "REMOTE-CONFERENCE-VIDEO" + ROOM = "ROOM" + + +class PROXIMITY(StrEnum): + """Enum for PROXIMITY from :rfc:`9074#section-8.1`. + + This parameter specifies the type of location-based trigger for a + :class:`~icalendar.cal.alarm.Alarm` component. Although the RFC requires + that when PROXIMITY set, a VLOCATION sub-component must be present to + define the location, icalendar doesn't enforce that requirement. + + Possible values are the following. + + - :attr:`ARRIVE` + - :attr:`CONNECT` + - :attr:`DEPART` + - :attr:`DISCONNECT` + """ + + ARRIVE = "ARRIVE" + CONNECT = "CONNECT" + DEPART = "DEPART" + DISCONNECT = "DISCONNECT" + + __all__ = [ "ACTION", "BUSYTYPE", "CLASS", "CUTYPE", "FBTYPE", + "PARTICIPANT_TYPE", "PARTSTAT", + "PROXIMITY", "RANGE", "RELATED", "RELTYPE", + "RESOURCE_TYPE", "ROLE", "STATUS", "TRANSP", diff --git a/src/icalendar/error.py b/src/icalendar/error.py index d49cad92c..b52718476 100644 --- a/src/icalendar/error.py +++ b/src/icalendar/error.py @@ -157,7 +157,7 @@ def __init__( self, message: str, parser: str | type = "", - path: list[str | int] | None | str | int = None, + path: list[str | int] | str | int | None = None, value: object = _default_value, ) -> None: self.path = self._get_path(path) @@ -200,7 +200,7 @@ def reraise_with_path_added( ).with_traceback(e.__traceback__) from e @staticmethod - def _get_path(path: list[str | int] | None | str | int) -> list[str | int]: + def _get_path(path: list[str | int] | str | int | None) -> list[str | int]: """Return the path as a list. :meta private: @@ -216,7 +216,7 @@ def validate_property( cls, jcal_property: list[object], parser: str | type, - path: list[str | int] | None | str | int = None, + path: list[str | int] | str | int | None = None, ) -> None: """Validate a jCal property. @@ -276,7 +276,7 @@ def validate_value_type( expected_type: type[str | int | float | bool] | tuple[type[str | int | float | bool], ...], parser: str | type = "", - path: list[str | int] | None | str | int = None, + path: list[str | int] | str | int | None = None, ) -> None: """Validate the type of a jCal value.""" if not isinstance(jcal, expected_type): @@ -298,7 +298,7 @@ def validate_list_type( jcal: object, expected_type: type[str | int | float | bool], parser: str | type = "", - path: list[str | int] | None | str | int = None, + path: list[str | int] | str | int | None = None, ) -> None: """Validate the type of each item in a jCal list.""" path = cls._get_path(path) @@ -325,7 +325,7 @@ def validate_jcal_token( name: str, kind: str, parser: str | type = "", - path: list[str | int] | None | str | int = None, + path: list[str | int] | str | int | None = None, ) -> None: r"""Validate a jCal ``name`` as a lowercase iCalendar token. diff --git a/src/icalendar/param.py b/src/icalendar/param.py index bef05cb6f..28eb03177 100644 --- a/src/icalendar/param.py +++ b/src/icalendar/param.py @@ -853,12 +853,71 @@ def _del_GAP(prop): # noqa: N802 GAP = property(_get_GAP, _set_GAP, _del_GAP) + +def _convert_order_read(value: str) -> int: + """Convert ORDER string to int on read.""" + return int(value) + + +def _convert_order_write(value) -> str: + """Validate and convert ORDER to string for storage.""" + result = int(value) + if result < 1: + raise ValueError(f"ORDER must be a positive integer >= 1, got {result}") + return str(result) + + +ORDER = string_parameter( + "ORDER", + """Indicate the relative ordering of a property amongst multiple properties +of the same type. + +This property parameter is specified in :rfc:`9073#section-5.1`. + +This parameter may be specified on any property that permits multiple instances +in a single component. The value is a positive integer starting at ``1``. If +it's absent, that means the property is unordered among those without an ORDER +value. Although the RFC states that no duplicate ORDER values are allowed +within the same property name on a component, icalendar doesn't enforce this +requirement. +""", + convert=_convert_order_read, + convert_to=_convert_order_write, +) + +SCHEMA = string_parameter( + "SCHEMA", + """Identify the schema that a STRUCTURED-DATA value conforms to. + +This property parameter is specified in :rfc:`9073#section-5.2`. + +This parameter should be specified on the STRUCTURED-DATA property. Its value +is a URI identifying the schema the structured data follows. It supplements the +media type given by the FMTTYPE parameter, which alone may not be specific +enough to interpret the value. +""", +) + +DERIVED = boolean_parameter("DERIVED", False) +"""Indicate that a property's value is derived from other property values. + +This property parameter is specified in :rfc:`9073#section-5.3`. + +This parameter may be specified on any property whose value can be derived from +the value of one or more other properties. In accordance with the RFC, when +it's set to ``TRUE``, icalendar doesn't update the property's value directly, +since it's expected to be recomputed from the properties from which it derives. +The default, when absent, is ``FALSE``. +""" + + __all__ = [ "ALTREP", "CN", "CUTYPE", "DELEGATED_FROM", "DELEGATED_TO", + "DERIVED", "DIR", "EMAIL", "FBTYPE", @@ -868,11 +927,13 @@ def _del_GAP(prop): # noqa: N802 "LANGUAGE", "LINKREL", "MEMBER", + "ORDER", "PARTSTAT", "RANGE", "RELATED", "ROLE", "RSVP", + "SCHEMA", "SENT_BY", "TZID", "VALUE", diff --git a/src/icalendar/prop/cal_address.py b/src/icalendar/prop/cal_address.py index 53e4c7bcd..58d7d4df4 100644 --- a/src/icalendar/prop/cal_address.py +++ b/src/icalendar/prop/cal_address.py @@ -219,10 +219,12 @@ def has_email(self, email: str) -> bool: CUTYPE, DELEGATED_FROM, DELEGATED_TO, + DERIVED, DIR, EMAIL, LANGUAGE, MEMBER, + ORDER, PARTSTAT, ROLE, RSVP, diff --git a/src/icalendar/prop/dt/time.py b/src/icalendar/prop/dt/time.py index 79a38cb18..f163eb541 100644 --- a/src/icalendar/prop/dt/time.py +++ b/src/icalendar/prop/dt/time.py @@ -173,7 +173,7 @@ def ical_value(self) -> time: return self.dt @staticmethod - def from_ical(ical: str, timezone: str | None | tzinfo = None) -> time: + def from_ical(ical: str, timezone: str | tzinfo | None = None) -> time: """Convert an ical string into a time. This parses the time forms defined in :rfc:`5545#section-3.3.12`: diff --git a/src/icalendar/prop/factory.py b/src/icalendar/prop/factory.py index a12cfd31d..ab377c1e3 100644 --- a/src/icalendar/prop/factory.py +++ b/src/icalendar/prop/factory.py @@ -188,6 +188,15 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: "exrule": "recur", "rdate": "date-time-list", "rrule": "recur", + # RFC 7986 properties + "name": "text", + # RFC 9073 properties + "location-type": "text", + "participant-type": "text", + "resource-type": "text", + "calendar-address": "cal-address", + "styled-description": "text", # full VALUE dispatch in future + "structured-data": "unknown", # full VALUE dispatch in future # Alarm Component Properties "action": "text", "repeat": "integer", diff --git a/src/icalendar/prop/text.py b/src/icalendar/prop/text.py index 64b9e49a4..8fefc4e69 100644 --- a/src/icalendar/prop/text.py +++ b/src/icalendar/prop/text.py @@ -175,7 +175,7 @@ def ical_value(self) -> str: """The string value of the text.""" return str(self) - from icalendar.param import ALTREP, GAP, LANGUAGE, RELTYPE, VALUE + from icalendar.param import ALTREP, DERIVED, GAP, LANGUAGE, ORDER, RELTYPE, VALUE def to_jcal(self, name: str) -> list: """The jCal representation of this property according to :rfc:`7265`.""" diff --git a/src/icalendar/prop/uri.py b/src/icalendar/prop/uri.py index 85b2257b2..c03a47c69 100644 --- a/src/icalendar/prop/uri.py +++ b/src/icalendar/prop/uri.py @@ -130,7 +130,17 @@ def __repr__(self) -> str: """ return f"{self.__class__.__name__}({self.uri!r})" - from icalendar.param import FMTTYPE, GAP, LABEL, LANGUAGE, LINKREL, RELTYPE, VALUE + from icalendar.param import ( + DERIVED, + FMTTYPE, + GAP, + LABEL, + LANGUAGE, + LINKREL, + ORDER, + RELTYPE, + VALUE, + ) def to_xcal(self, element: Element) -> None: """Add the xCal representation of this property according to :rfc:`6321`.""" diff --git a/src/icalendar/tests/events/rfc_9073_order_parameter.ics b/src/icalendar/tests/events/rfc_9073_order_parameter.ics new file mode 100644 index 000000000..7e484fff5 --- /dev/null +++ b/src/icalendar/tests/events/rfc_9073_order_parameter.ics @@ -0,0 +1,8 @@ +BEGIN:VEVENT +SUMMARY:Things That Need Ordering (Unlike This Meeting) +DTSTART:20261121T090000Z +DTEND:20261121T100000Z +UID:rfc9073-order-test@example.com +CATEGORIES;ORDER=1:Coffee +CATEGORIES;ORDER=2:Agenda +END:VEVENT diff --git a/src/icalendar/tests/events/rfc_9073_participant.ics b/src/icalendar/tests/events/rfc_9073_participant.ics new file mode 100644 index 000000000..16025f795 --- /dev/null +++ b/src/icalendar/tests/events/rfc_9073_participant.ics @@ -0,0 +1,17 @@ +BEGIN:VEVENT +SUMMARY:How to Parse Your Own Calendar Invite (and Why It Hurts) +DTSTART:20261115T140000Z +DTEND:20261115T150000Z +UID:rfc9073-participant-test@example.com +BEGIN:PARTICIPANT +PARTICIPANT-TYPE:ACTIVE +CALENDAR-ADDRESS:mailto:guido@pycon.example.com +UID:rfc9073-participant-uid@example.com +SUMMARY:Guido van Rossum Memorial Keynote Stand-In +BEGIN:VLOCATION +UID:rfc9073-vlocation-in-participant@example.com +NAME:Main Stage (The Big One) +LOCATION-TYPE:arena +END:VLOCATION +END:PARTICIPANT +END:VEVENT diff --git a/src/icalendar/tests/events/rfc_9073_vlocation.ics b/src/icalendar/tests/events/rfc_9073_vlocation.ics new file mode 100644 index 000000000..dfde9e1f3 --- /dev/null +++ b/src/icalendar/tests/events/rfc_9073_vlocation.ics @@ -0,0 +1,12 @@ +BEGIN:VEVENT +SUMMARY:Astronomical Society Night: We Finally Looked Up +DTSTART:20261121T210000Z +DTEND:20261121T230000Z +UID:rfc9073-vlocation-test@example.com +BEGIN:VLOCATION +UID:rfc9073-vlocation-uid@example.com +NAME:Hydrogen Line Hackerspace Observatory +LOCATION-TYPE:office +GEO:40.4168;-3.7038 +END:VLOCATION +END:VEVENT diff --git a/src/icalendar/tests/events/rfc_9073_vresource.ics b/src/icalendar/tests/events/rfc_9073_vresource.ics new file mode 100644 index 000000000..f32a1d1f7 --- /dev/null +++ b/src/icalendar/tests/events/rfc_9073_vresource.ics @@ -0,0 +1,11 @@ +BEGIN:VEVENT +SUMMARY:RFC Reading Group: 9073 Is Actually Interesting +DTSTART:20261121T160000Z +DTEND:20261121T170000Z +UID:rfc9073-vresource-test@example.com +BEGIN:VRESOURCE +UID:rfc9073-vresource-uid@example.com +NAME:The Sentient Projector (It Knows When You Click Past Its Slide) +RESOURCE-TYPE:PROJECTOR +END:VRESOURCE +END:VEVENT diff --git a/src/icalendar/tests/rfc_6321_xcal/test_xml_property.py b/src/icalendar/tests/rfc_6321_xcal/test_xml_property.py index b675efa64..a4872d9cc 100644 --- a/src/icalendar/tests/rfc_6321_xcal/test_xml_property.py +++ b/src/icalendar/tests/rfc_6321_xcal/test_xml_property.py @@ -175,12 +175,16 @@ def test_xml_property_from_xcal_reads_what_to_xcal_writes(): "properties", [ # case C: an element in another namespace inside of a property - "" - 'Hello', + ( + "" + 'Hello' + ), # case D: an element in another namespace inside of an unknown value type - "" - 'Hello' - "", + ( + "" + 'Hello' + "" + ), ], ) def test_xml_property_from_xcal_only_directly_in_properties(properties): diff --git a/src/icalendar/tests/test_rfc_9073.py b/src/icalendar/tests/test_rfc_9073.py new file mode 100644 index 000000000..81a80feab --- /dev/null +++ b/src/icalendar/tests/test_rfc_9073.py @@ -0,0 +1,198 @@ +"""Test RFC 9073 Event Publishing Extensions to iCalendar. + +See https://www.rfc-editor.org/rfc/rfc9073 +and also https://github.com/PyCalendar/icalendar/issues/656 +""" + +import pytest + +from icalendar import ( + PARTICIPANT_TYPE, + PROXIMITY, + RELTYPE, + RESOURCE_TYPE, + Location, + Participant, + Resource, + vCalAddress, + vText, + vUri, +) + + +@pytest.mark.parametrize( + "name", + [ + "PARTICIPANT_TYPE", + "RESOURCE_TYPE", + "PROXIMITY", + "Participant", + "Location", + "Resource", + ], +) +def test_public_names_importable(name): + """All RFC 9073 public names are importable from the top-level package.""" + import icalendar + + assert hasattr(icalendar, name) + + +def test_reltype_snooze(): + """RELTYPE.SNOOZE is defined per :rfc:`9074#section-7.1`.""" + assert RELTYPE.SNOOZE == "SNOOZE" + + +@pytest.mark.parametrize( + ("prop_cls", "value"), + [ + (vCalAddress, "mailto:speaker@example.com"), + (vUri, "https://example.com/cal"), + ], +) +def test_order_parameter_on_prop_types(prop_cls, value): + """ORDER parameter is accessible on multi-instance property types per :rfc:`9073#section-5.1`.""" + prop = prop_cls(value) + prop.ORDER = 1 + assert prop.ORDER == 1 + + +@pytest.mark.parametrize( + ("prop_cls", "value"), + [ + (vCalAddress, "mailto:speaker@example.com"), + (vUri, "https://example.com/cal"), + ], +) +def test_derived_parameter_on_prop_types(prop_cls, value): + """DERIVED parameter is accessible on multi-instance property types per :rfc:`9073#section-5.3`.""" + prop = prop_cls(value) + prop.DERIVED = True + assert prop.DERIVED is True + + +def test_order_validation(): + """ORDER parameter rejects values less than 1 per :rfc:`9073#section-5.1`.""" + t = vText("test") + with pytest.raises((ValueError, TypeError)): + t.ORDER = 0 + + +def test_event_participants_accessor(events): + """Event.participants returns typed Participant instances per :rfc:`9073#section-7.1`.""" + event = events.rfc_9073_participant + assert len(event.participants) == 1 + assert isinstance(event.participants[0], Participant) + + +def test_event_locations_accessor(events): + """Event.locations returns typed Location instances per :rfc:`9073#section-7.2`.""" + event = events.rfc_9073_vlocation + assert len(event.locations) == 1 + assert isinstance(event.locations[0], Location) + + +def test_event_resources_accessor(events): + """Event.resources returns typed Resource instances per :rfc:`9073#section-7.3`.""" + event = events.rfc_9073_vresource + assert len(event.resources) == 1 + assert isinstance(event.resources[0], Resource) + + +def test_participant_type_accessor(events): + """Participant.participant_type returns PARTICIPANT_TYPE enum per :rfc:`9073#section-6.2`.""" + p = events.rfc_9073_participant.participants[0] + assert p.participant_type == PARTICIPANT_TYPE.ACTIVE + + +def test_participant_calendar_address(events): + """Participant.calendar_address returns vCalAddress per :rfc:`9073#section-6.4`.""" + p = events.rfc_9073_participant.participants[0] + assert isinstance(p.calendar_address, vCalAddress) + assert "guido@pycon.example.com" in str(p.calendar_address) + + +def test_location_in_participant(events): + """VLOCATION nested inside PARTICIPANT is parsed per :rfc:`9073#section-7.1`.""" + p = events.rfc_9073_participant.participants[0] + assert len(p.locations) == 1 + assert isinstance(p.locations[0], Location) + + +def test_location_name_accessor(events): + """Location.location_name returns the NAME property per :rfc:`9073#section-7.2`.""" + loc = events.rfc_9073_vlocation.locations[0] + assert loc.location_name == "Hydrogen Line Hackerspace Observatory" + + +def test_resource_type_accessor(events): + """Resource.resource_type returns RESOURCE_TYPE enum per :rfc:`9073#section-7.3`.""" + res = events.rfc_9073_vresource.resources[0] + assert res.resource_type == RESOURCE_TYPE.PROJECTOR + + +def test_alarm_proximity_typed(events): + """Alarm.proximity returns PROXIMITY enum per :rfc:`9074#section-8.1`.""" + alarm = events.rfc_9074_example_proximity.subcomponents[0] + assert alarm.proximity == PROXIMITY.DEPART + + +def test_location_in_alarm_registered(events): + """VLOCATION inside VALARM parses as a Location instance per :rfc:`9074#section-8`.""" + alarm = events.rfc_9074_example_proximity.subcomponents[0] + assert isinstance(alarm.subcomponents[0], Location) + + +def test_order_round_trip(events): + """ORDER parameter round-trips through parse and serialize per :rfc:`9073#section-5.1`.""" + from icalendar import Calendar + + event = events.rfc_9073_order_parameter + cal = Calendar() + cal.add_component(event) + reparsed = Calendar.from_ical(cal.to_ical()) + revent = reparsed.events[0] + cats = revent.get("CATEGORIES") + if isinstance(cats, list): + orders = [int(c.params.get("ORDER", 0)) for c in cats] + else: + orders = [int(cats.params.get("ORDER", 0))] if cats else [] + assert 1 in orders + assert 2 in orders + + +def test_derived_round_trip(): + """DERIVED parameter round-trips through parse and serialize per :rfc:`9073#section-5.3`.""" + from icalendar import Calendar, Event + + event = Event() + addr = vCalAddress("mailto:speaker@example.com") + addr.DERIVED = True + event.add("ATTENDEE", addr) + cal = Calendar() + cal.add_component(event) + reparsed = Calendar.from_ical(cal.to_ical()) + attendee = reparsed.events[0].get("ATTENDEE") + assert attendee.params.get("DERIVED") == "TRUE" + + +def test_participant_type_enum_values(): + """PARTICIPANT_TYPE enum has all IANA-registered values from :rfc:`9073#section-11.2.1`.""" + assert PARTICIPANT_TYPE.ACTIVE == "ACTIVE" + assert PARTICIPANT_TYPE.INACTIVE == "INACTIVE" + assert PARTICIPANT_TYPE.SPONSOR == "SPONSOR" + assert PARTICIPANT_TYPE.CONTACT == "CONTACT" + assert PARTICIPANT_TYPE.BOOKING_CONTACT == "BOOKING-CONTACT" + assert PARTICIPANT_TYPE.EMERGENCY_CONTACT == "EMERGENCY-CONTACT" + assert PARTICIPANT_TYPE.PUBLICITY_CONTACT == "PUBLICITY-CONTACT" + assert PARTICIPANT_TYPE.PLANNER_CONTACT == "PLANNER-CONTACT" + assert PARTICIPANT_TYPE.PERFORMER == "PERFORMER" + assert PARTICIPANT_TYPE.SPEAKER == "SPEAKER" + + +def test_resource_type_enum_values(): + """RESOURCE_TYPE enum has all IANA-registered values from :rfc:`9073#section-11.2.2`.""" + assert RESOURCE_TYPE.ROOM == "ROOM" + assert RESOURCE_TYPE.PROJECTOR == "PROJECTOR" + assert RESOURCE_TYPE.REMOTE_CONFERENCE_AUDIO == "REMOTE-CONFERENCE-AUDIO" + assert RESOURCE_TYPE.REMOTE_CONFERENCE_VIDEO == "REMOTE-CONFERENCE-VIDEO"