Skip to content
Open
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
1 change: 1 addition & 0 deletions news/+one-propfind-listing.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
On CalDAV, ``collections()`` no longer sends one PROPFIND per calendar for its component set, where caldav keeps the listing's properties (after 3.7): 469 calendars on a Synology took 268 s, the listing alone 6 s. With caldav 3.7, or a server whose listing leaves the component set out, each calendar is still asked; so is each on a server whose listing names calendars that are not there (Bedework 5, by caldav's ``propfind.listed-members-exist`` hint), to leave those out. That takes caldav's Bedework 5 profile (``features: bedework_5_0_0``): without hints, Bedework 5's ghost ``tasks/`` is listed.
8 changes: 6 additions & 2 deletions src/calendaring/_async/caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .._caldav_features import (
backend_capabilities,
collection_capabilities,
lists_missing_collections,
multiget_serves_deleted,
stored_components,
)
Expand Down Expand Up @@ -272,11 +273,14 @@ async def _open(cls, url: str, **options: Any) -> AsyncCalDAVBackend:
return cls(AsyncDAV(url, **options), id=backend_id, loss=loss, verify=verify)

async def _wrap(self, calendar: Calendar) -> AsyncCalDAVCollection:
features = self._dav.client.features
# Where the listing names calendars that are not there, a fresh read of each is how they are found.
name = await self._dav.display_name(calendar, cached=not lists_missing_collections(features))
listed = frozenset(
Component(c) for c in await self._dav.supported_components(calendar) if c in {k.value for k in Component}
)
components = stored_components(self._dav.client.features, listed)
return self.collection_class(self, calendar, name=await self._dav.display_name(calendar), components=components)
components = stored_components(features, listed)
return self.collection_class(self, calendar, name=name, components=components)

async def collections(self) -> list[AsyncCollection]:
found: list[AsyncCollection] = []
Expand Down
11 changes: 9 additions & 2 deletions src/calendaring/_async/dav.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
SyncResult,
_awaited,
_etags,
_listed_components,
_raw,
_resource,
_strong,
Expand Down Expand Up @@ -57,12 +58,18 @@ async def delete_calendar(self, calendar: Calendar) -> None:
await _awaited(calendar.delete()) # type: ignore[no-untyped-call] # caldav leaves delete() unannotated

async def supported_components(self, calendar: Calendar) -> list[str]:
# From the listing if it said, or one PROPFIND per calendar: 268 s for 469 on a Synology.
if (listed := _listed_components(calendar)) is not None:
return listed
with translated():
return [str(c) for c in await _awaited(calendar.get_supported_components())]

async def display_name(self, calendar: Calendar) -> str | None:
async def display_name(self, calendar: Calendar, *, cached: bool = True) -> str | None:
"""The listed name, or with ``cached=False`` a fresh one: ``NotFoundError`` if the calendar is gone."""
with translated():
return await _awaited(calendar.get_display_name())
if cached:
return await _awaited(calendar.get_display_name())
return await _awaited(calendar.get_property(DisplayName(), use_cached=False))

async def set_display_name(self, calendar: Calendar, name: str) -> None:
with translated():
Expand Down
5 changes: 5 additions & 0 deletions src/calendaring/_caldav_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ def multiget_serves_deleted(features: Any) -> bool:
return _level(features, "multiget.deleted") is U


def lists_missing_collections(features: Any) -> bool:
"""Bedework 5 lists a calendar, display name and component set and all, that answers 404."""
return _level(features, "propfind.listed-members-exist") is U


def backend_capabilities(features: Any) -> Capabilities:
return Capabilities({f: v for f, v in _table(features).items() if f in BACKEND_FEATURES})

Expand Down
10 changes: 10 additions & 0 deletions src/calendaring/_caldav_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from caldav.calendarobjectresource import CalendarObjectResource, Event, Journal, Todo
from caldav.collection import Calendar
from caldav.davclient import DAVClient
from caldav.elements.cdav import SupportedCalendarComponentSet
from caldav.elements.dav import DisplayName, GetEtag
from caldav.lib import error as dav_error
from caldav.lib.url import URL
Expand Down Expand Up @@ -69,6 +70,15 @@

VALID_SYNC_TOKEN = "{DAV:}valid-sync-token" # RFC 6578 §3.2


def _listed_components(calendar: Calendar) -> list[str] | None:
"""The component set the calendar listing answered, if caldav kept it (after 3.7): None to ask.

Read from ``props`` rather than ``get_supported_components(use_cached=True)``, which 3.7 does not take."""
listed = calendar.props.get(SupportedCalendarComponentSet.tag)
return [str(c) for c in listed] if listed else None


# A sync-collection report: hrefs changed, hrefs removed, the new token (None if the server gave
# none), and whether it is incomplete (truncated beyond what caldav itself follows, RFC 6578 §3.6).
SyncResult = tuple[list[str], list[str], str | None, bool]
Expand Down
8 changes: 6 additions & 2 deletions src/calendaring/_sync/caldav.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/calendaring/_sync/dav.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 58 additions & 1 deletion tests/_async/test_caldav_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@

from caldav.collection import Calendar # noqa: E402
from caldav.compatibility_hints import FeatureSet # noqa: E402
from caldav.elements.cdav import SupportedCalendarComponentSet # noqa: E402
from caldav.elements.dav import DisplayName # noqa: E402
from caldav.lib.url import URL # noqa: E402

from calendaring._async.caldav import _name_of # noqa: E402
from calendaring._caldav_io import _resource # noqa: E402
from calendaring._caldav_features import lists_missing_collections # noqa: E402
from calendaring._caldav_io import _listed_components, _resource # noqa: E402


@pytest.fixture
Expand Down Expand Up @@ -90,10 +93,64 @@ async def test_a_listed_calendar_that_is_not_there_is_left_out(
assert len(await backend.collections()) == len(real)


async def test_a_listed_calendar_that_is_not_there_is_left_out_where_the_listing_says_otherwise(
collection: AsyncCollection, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Bedework 5 lists its ghost ``tasks/`` with a display name and a component set, all a listing could say."""
caldav_collection: Any = collection
backend = caldav_collection._backend
features = backend._dav.client.features
if "propfind.listed-members-exist" not in features.FEATURES:
pytest.skip("caldav up to 3.7 has no propfind.listed-members-exist hint")
features.set_feature("propfind.listed-members-exist", {"support": "broken"})
real = await backend._dav.calendars()
ghost = Calendar(
client=backend._dav.client,
url=URL.objectify(caldav_collection.url).join("../ghost/"),
props={DisplayName.tag: "tasks", SupportedCalendarComponentSet.tag: ["VTODO"]},
)
monkeypatch.setattr(backend._dav, "calendars", lambda: _returning([*real, ghost]))
assert [c.id for c in await backend.collections()] == [_name_of(str(c.url)) for c in real]


async def _returning(value: Any) -> Any:
return value


async def test_listing_collections_costs_the_same_however_many_there_are(
collection: AsyncCollection, monkeypatch: pytest.MonkeyPatch
) -> None:
"""One PROPFIND per calendar took 268 s for 469 calendars on a Synology; the listing alone takes 6 s."""
backend: Any = collection._backend
if not all(_listed_components(c) for c in await backend._dav.calendars()):
pytest.skip("the listing kept no component set: caldav up to 3.7 keeps only the display name")
if lists_missing_collections(backend._dav.client.features):
pytest.skip("where the listing names calendars that are not there, each is asked")
client = backend._dav.client
request = client.request
methods: list[str] = []

async def counted(url: Any, method: str = "GET", *args: Any, **kwargs: Any) -> Any:
methods.append(method)
return await request(url, method, *args, **kwargs)

monkeypatch.setattr(client, "request", counted)
one = await backend.collections()
one_propfinds, methods[:] = methods.count("PROPFIND"), []
assert one_propfinds >= 1 # the counting sees the requests at all
monkeypatch.setattr(client, "request", request)
other = await backend.create_collection("backend-tests-2", components=(Component.TASK,))
try:
monkeypatch.setattr(client, "request", counted)
two = await backend.collections()
assert len(two) == len(one) + 1
assert methods.count("PROPFIND") == one_propfinds
assert {c.id: c.components for c in two}[collection.id] == {Component.TASK}
finally:
monkeypatch.setattr(client, "request", request)
await other.delete_collection()


async def test_a_token_is_bearer_auth() -> None:
backend = await AsyncBackend.open("https://dav.example.com/", kind="caldav", token="s3cret")
async with backend:
Expand Down
59 changes: 58 additions & 1 deletion tests/_sync/test_caldav_backend.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading