diff --git a/caldav/datastate.py b/caldav/datastate.py index 625afb96..c5fd5d72 100644 --- a/caldav/datastate.py +++ b/caldav/datastate.py @@ -65,6 +65,8 @@ def get_uid(self) -> str | None: Default implementation parses the data, but subclasses can optimize. """ cal = self.get_icalendar_copy() + if cal.name in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY"): + return str(cal["UID"]) if "UID" in cal else None for comp in cal.subcomponents: if comp.name in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY") and "UID" in comp: return str(comp["UID"]) @@ -145,10 +147,31 @@ def get_uid(self) -> str | None: match = re.search(r"^UID:([^\r\n]*(?:\r?\n[ \t][^\r\n]*)*)", self._data, re.MULTILINE) if match: uid = re.sub(r"\r?\n[ \t]", "", match.group(1)).strip() - # leave backslash escapes to the full parse if "\\" not in uid: - return uid - # Fall back to parsing if regex fails (e.g., UID with parameters) + # A preceding VTIMEZONE is common. Only the components still + # open at the UID determine whether it belongs to the resource. + stack: list[str] = [] + for header in re.finditer( + r"^(BEGIN|END|UID)[:;]([^\r\n]*)", + self._data[: match.start()], + re.IGNORECASE | re.MULTILINE, + ): + token, name = header.group(1).upper(), header.group(2).upper() + if token == "BEGIN": + stack.append(name) + elif token == "END" and stack and stack[-1] == name: + stack.pop() + else: + # Earlier UID with parameters/case differences, or malformed nesting. + break + else: + if ( + len(stack) == 2 + and stack[0] == "VCALENDAR" + and stack[1] in ("VEVENT", "VTODO", "VJOURNAL", "VFREEBUSY") + ): + return uid + # Parse parameterized or escaped UIDs and resolve ambiguous component nesting. return super().get_uid() def get_component_type(self) -> str | None: diff --git a/changes/788.fixed.md b/changes/788.fixed.md new file mode 100644 index 00000000..70fb0893 --- /dev/null +++ b/changes/788.fixed.md @@ -0,0 +1 @@ +Events and tasks with reminders now report their own ID even when a reminder has a separate ID. diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 62f5956b..cd10fcfa 100755 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -7030,6 +7030,35 @@ def test_cheap_accessor_unfolds(self, fold) -> None: assert RawDataState(ical).get_uid() == self.UID assert Event(data=ical).id == self.UID + @pytest.mark.parametrize(("kind", "resource"), [("VEVENT", Event), ("VTODO", Todo)]) + @pytest.mark.parametrize("parent_uid", ["UID:parent-1", ""]) + def test_nested_alarm_uid_is_not_the_resource_id(self, kind, resource, parent_uid) -> None: + ical = ( + f"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:{kind}\r\n" + "DTSTART:20260101T000000Z\r\nBEGIN:VALARM\r\nUID:alarm-1\r\n" + "ACTION:DISPLAY\r\nTRIGGER:-PT5M\r\nDESCRIPTION:Reminder\r\nEND:VALARM\r\n" + + (parent_uid + "\r\n" if parent_uid else "") + + f"END:{kind}\r\nEND:VCALENDAR\r\n" + ) + assert resource(data=ical).id == ("parent-1" if parent_uid else None) + + @pytest.mark.parametrize("alarm", ["", "BEGIN:VALARM\r\nUID:alarm-1\r\nEND:VALARM\r\n"]) + def test_unwrapped_event_uid(self, alarm) -> None: + data = f"BEGIN:VEVENT\r\n{alarm}UID:event-1\r\nEND:VEVENT\r\n" + assert Event(data=data).id == "event-1" + + def test_timezone_before_resource_uid_needs_no_full_parse(self) -> None: + from caldav.datastate import RawDataState + + zone = ( + "BEGIN:VTIMEZONE\r\nTZID:Example\r\nBEGIN:STANDARD\r\n" + "DTSTART:19700101T000000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\n" + "END:STANDARD\r\nEND:VTIMEZONE\r\n" + ) + data = self._ical("\r\n ").replace("BEGIN:VEVENT", zone + "BEGIN:VEVENT") + with mock.patch("caldav.datastate.parse_ical", side_effect=AssertionError): + assert RawDataState(data).get_uid() == self.UID + def test_folded_uid_needs_no_full_parse(self) -> None: from caldav.datastate import RawDataState