Sync every calendar in an account#
One store holds as many scopes as you sync. Where each calendar is a scope of its own, as with CalDAVAdapter, make an adapter for each calendar, sync each one, and drop the scopes of calendars that are gone.
from caldav.davclient import DAVClient
from calendaring_sync import Replica, SQLiteStore
from calendaring_sync.adapters.caldav import CalDAVAdapter
URL = "https://caldav.example.com/"
USERNAME = "alice"
PASSWORD = "secret"
with (
SQLiteStore("alice.db") as store,
DAVClient(url=URL, username=USERNAME, password=PASSWORD) as client,
):
replica = Replica(store)
adapters = [CalDAVAdapter(replica, c) for c in client.get_calendars()]
for adapter in adapters:
adapter.sync()
still_there = {adapter.scope_id for adapter in adapters}
dropped = [replica.drop_scope(s.id) for s in replica.scopes() if s.id not in still_there]
Each calendar keeps its scope from run to run, through the adapter’s default scope_id. A calendar deleted on the server no longer appears in the account’s list, so the last line drops its scope: drop_scope() returns the edits it discards, which you can show or save. Keep one store for each account, so the scopes it drops are only that account’s.
What a scope covers is each adapter’s choice: Adapters gives it for each adapter that ships with calendaring-sync. Where one scope already covers the whole account, one adapter syncs it all.