Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions caldav/datastate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions changes/788.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Events and tasks with reminders now report their own ID even when a reminder has a separate ID.
29 changes: 29 additions & 0 deletions tests/test_caldav_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading