gcal-sync 9.2.1__tar.gz → 9.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/PKG-INFO +1 -1
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/model.py +3 -4
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/sync.py +39 -2
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync.egg-info/PKG-INFO +1 -1
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/pyproject.toml +5 -1
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/tests/test_sync.py +221 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/LICENSE +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/README.md +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/__init__.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/api.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/auth.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/const.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/exceptions.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/py.typed +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/store.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync/timeline.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync.egg-info/SOURCES.txt +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync.egg-info/dependency_links.txt +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync.egg-info/requires.txt +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/gcal_sync.egg-info/top_level.txt +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/setup.cfg +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/tests/test_api.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/tests/test_auth.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/tests/test_model.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/tests/test_store.py +0 -0
- {gcal_sync-9.2.1 → gcal_sync-9.2.2}/tests/test_timeline.py +0 -0
|
@@ -896,10 +896,9 @@ class Event(CalendarBaseModel):
|
|
|
896
896
|
):
|
|
897
897
|
# Date should be floating
|
|
898
898
|
return date_value.replace(tzinfo=None)
|
|
899
|
-
elif dtstart.date:
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
return date_value.date()
|
|
899
|
+
elif dtstart.date and isinstance(date_value, datetime.datetime):
|
|
900
|
+
# UNTIL is a DATE-TIME but must be a DATE
|
|
901
|
+
return date_value.date()
|
|
903
902
|
return date_value
|
|
904
903
|
|
|
905
904
|
@model_validator(mode="before")
|
|
@@ -13,7 +13,6 @@ may then query the latest information from the local store service.
|
|
|
13
13
|
from __future__ import annotations
|
|
14
14
|
|
|
15
15
|
import datetime
|
|
16
|
-
import json
|
|
17
16
|
import logging
|
|
18
17
|
from collections.abc import Awaitable, Callable
|
|
19
18
|
from typing import Any
|
|
@@ -32,6 +31,7 @@ from .api import (
|
|
|
32
31
|
)
|
|
33
32
|
from .const import CALENDAR_LIST_SYNC, EVENT_SYNC, ITEMS, SYNC_TOKEN, SYNC_TOKEN_VERSION
|
|
34
33
|
from .exceptions import InvalidSyncTokenException
|
|
34
|
+
from .model import Event, EventStatusEnum
|
|
35
35
|
from .store import CalendarStore, InMemoryCalendarStore, ScopedCalendarStore
|
|
36
36
|
|
|
37
37
|
_LOGGER = logging.getLogger(__name__)
|
|
@@ -49,10 +49,46 @@ def _items_func(
|
|
|
49
49
|
for item in result.items:
|
|
50
50
|
if not item.id:
|
|
51
51
|
continue
|
|
52
|
-
|
|
52
|
+
if isinstance(item, Event) and item.status == EventStatusEnum.CANCELLED:
|
|
53
|
+
# Convert to a minimal tombstone, stripping event content (summary,
|
|
54
|
+
# description, attendees, etc.). Deleted non-recurring tombstones are
|
|
55
|
+
# pruned after all pages complete; recurring exception tombstones are
|
|
56
|
+
# kept only while their parent recurring event remains active.
|
|
57
|
+
items[item.id] = item.model_dump(
|
|
58
|
+
mode="json",
|
|
59
|
+
include={"id", "status", "recurring_event_id", "original_start_time"},
|
|
60
|
+
exclude_none=True,
|
|
61
|
+
)
|
|
62
|
+
else:
|
|
63
|
+
items[item.id] = item.model_dump(mode="json", exclude_none=True)
|
|
53
64
|
return items
|
|
54
65
|
|
|
55
66
|
|
|
67
|
+
def _prune_cancelled_events(stored_items: dict[str, Any]) -> None:
|
|
68
|
+
"""Remove cancelled events and orphaned recurring exceptions from the store.
|
|
69
|
+
|
|
70
|
+
Cancelled exceptions of an active recurring event (those with both
|
|
71
|
+
`recurring_event_id` and `original_start_time`) must be retained for the
|
|
72
|
+
lifetime of the parent recurring event so timeline expansion can exclude
|
|
73
|
+
those cancelled occurrences from the parent's recurrence rule. All other
|
|
74
|
+
cancelled events and any children of a cancelled recurring master are
|
|
75
|
+
removed.
|
|
76
|
+
"""
|
|
77
|
+
to_delete: list[str] = []
|
|
78
|
+
for item_id, data in stored_items.items():
|
|
79
|
+
parent_id = data.get("recurring_event_id")
|
|
80
|
+
parent = stored_items.get(parent_id) if parent_id else None
|
|
81
|
+
if (
|
|
82
|
+
parent is not None and parent.get("status") == EventStatusEnum.CANCELLED
|
|
83
|
+
) or (
|
|
84
|
+
data.get("status") == EventStatusEnum.CANCELLED
|
|
85
|
+
and not (parent is not None and data.get("original_start_time"))
|
|
86
|
+
):
|
|
87
|
+
to_delete.append(item_id)
|
|
88
|
+
for item_id in to_delete:
|
|
89
|
+
del stored_items[item_id]
|
|
90
|
+
|
|
91
|
+
|
|
56
92
|
async def _run_sync[T: SyncableRequest, S: SyncableResponse](
|
|
57
93
|
store_data: dict[str, Any],
|
|
58
94
|
new_request: Callable[[str | None], T],
|
|
@@ -208,4 +244,5 @@ class CalendarEventSyncManager:
|
|
|
208
244
|
store_data = await _run_sync(
|
|
209
245
|
store_data, new_request, self._api.async_list_events_page, _items_func
|
|
210
246
|
)
|
|
247
|
+
_prune_cancelled_events(store_data[ITEMS])
|
|
211
248
|
await self._store.async_save(store_data)
|
|
@@ -4,7 +4,7 @@ requires = ["setuptools>=77.0"]
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "gcal_sync"
|
|
7
|
-
version = "9.2.
|
|
7
|
+
version = "9.2.2"
|
|
8
8
|
license = "Apache-2.0"
|
|
9
9
|
license-files = ["LICENSE"]
|
|
10
10
|
description = "A python library for syncing Google Calendar to local storage"
|
|
@@ -69,3 +69,7 @@ warn_untyped_fields = true
|
|
|
69
69
|
|
|
70
70
|
[tool.ty.src]
|
|
71
71
|
exclude = ["tests", "examples"]
|
|
72
|
+
|
|
73
|
+
[tool.coverage.run]
|
|
74
|
+
core = "sysmon"
|
|
75
|
+
disable_warnings = ["no-sysmon"]
|
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import datetime
|
|
6
6
|
import zoneinfo
|
|
7
7
|
from collections.abc import Awaitable, Callable
|
|
8
|
+
from typing import Any
|
|
8
9
|
from unittest.mock import patch
|
|
9
10
|
|
|
10
11
|
import aiohttp
|
|
@@ -16,6 +17,7 @@ from gcal_sync.api import (
|
|
|
16
17
|
LocalListEventsRequest,
|
|
17
18
|
SyncEventsRequest,
|
|
18
19
|
)
|
|
20
|
+
from gcal_sync.const import EVENT_SYNC, ITEMS, SYNC_TOKEN, SYNC_TOKEN_VERSION
|
|
19
21
|
from gcal_sync.exceptions import ApiException, InvalidSyncTokenException
|
|
20
22
|
from gcal_sync.model import EVENT_FIELDS, AccessRole, Calendar, DateOrDatetime, Event
|
|
21
23
|
from gcal_sync.store import CalendarStore
|
|
@@ -619,6 +621,7 @@ async def test_event_token_version_invalidation(
|
|
|
619
621
|
@freeze_time("2022-04-05 07:31:02", tz_offset=-7)
|
|
620
622
|
async def test_canceled_events(
|
|
621
623
|
event_sync_manager_cb: Callable[[], Awaitable[CalendarEventSyncManager]],
|
|
624
|
+
store: CalendarStore,
|
|
622
625
|
json_response: ApiResult,
|
|
623
626
|
) -> None:
|
|
624
627
|
"""Test lookup events API."""
|
|
@@ -684,6 +687,10 @@ async def test_canceled_events(
|
|
|
684
687
|
"id": "some-event-id-1",
|
|
685
688
|
"status": "cancelled",
|
|
686
689
|
},
|
|
690
|
+
{
|
|
691
|
+
"id": "never-cached-event-id",
|
|
692
|
+
"status": "cancelled",
|
|
693
|
+
},
|
|
687
694
|
{
|
|
688
695
|
"id": "some-event-id-3",
|
|
689
696
|
"summary": "Event 3",
|
|
@@ -700,6 +707,8 @@ async def test_canceled_events(
|
|
|
700
707
|
}
|
|
701
708
|
)
|
|
702
709
|
await sync.run()
|
|
710
|
+
assert set(await _stored_events(store)) == {"some-event-id-2", "some-event-id-3"}
|
|
711
|
+
|
|
703
712
|
result = await sync.store_service.async_list_events(
|
|
704
713
|
LocalListEventsRequest(
|
|
705
714
|
start_time=datetime.datetime.fromisoformat("2001-01-01T00:00:00"),
|
|
@@ -743,6 +752,218 @@ async def test_canceled_events(
|
|
|
743
752
|
assert [event.summary for event in event_iter] == []
|
|
744
753
|
|
|
745
754
|
|
|
755
|
+
async def _stored_events(store: CalendarStore) -> dict[str, Any]:
|
|
756
|
+
"""Return raw stored event items for CALENDAR_ID."""
|
|
757
|
+
store_data = await store.async_load()
|
|
758
|
+
assert store_data is not None
|
|
759
|
+
return store_data[EVENT_SYNC][CALENDAR_ID][ITEMS] # type: ignore[no-any-return]
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
async def test_cancelled_recurring_events_in_store(
|
|
763
|
+
event_sync_manager_cb: Callable[[], Awaitable[CalendarEventSyncManager]],
|
|
764
|
+
store: CalendarStore,
|
|
765
|
+
json_response: ApiResult,
|
|
766
|
+
) -> None:
|
|
767
|
+
"""Test cancelled recurring exceptions, multi-page ordering, and standalone instances."""
|
|
768
|
+
# Page 1: cancelled exception arrives before its master on Page 2
|
|
769
|
+
json_response(
|
|
770
|
+
{
|
|
771
|
+
"items": [
|
|
772
|
+
{
|
|
773
|
+
"id": "recurring-1_20220420",
|
|
774
|
+
"status": "cancelled",
|
|
775
|
+
"recurringEventId": "recurring-1",
|
|
776
|
+
"originalStartTime": {"date": "2022-04-20"},
|
|
777
|
+
"summary": "Details that should be stripped",
|
|
778
|
+
"description": "Long description",
|
|
779
|
+
},
|
|
780
|
+
],
|
|
781
|
+
"nextPageToken": "page-1",
|
|
782
|
+
}
|
|
783
|
+
)
|
|
784
|
+
json_response(
|
|
785
|
+
{
|
|
786
|
+
"items": [
|
|
787
|
+
{
|
|
788
|
+
"id": "recurring-1",
|
|
789
|
+
"iCalUID": "recurring-1@google.com",
|
|
790
|
+
"summary": "Weekly Event",
|
|
791
|
+
"start": {"date": "2022-04-13"},
|
|
792
|
+
"end": {"date": "2022-04-14"},
|
|
793
|
+
"recurrence": ["RRULE:FREQ=WEEKLY;COUNT=3"],
|
|
794
|
+
"status": "confirmed",
|
|
795
|
+
},
|
|
796
|
+
{
|
|
797
|
+
"id": "recurring-1_20220427",
|
|
798
|
+
"iCalUID": "recurring-1@google.com",
|
|
799
|
+
"summary": "Modified Weekly Event",
|
|
800
|
+
"start": {"date": "2022-04-28"},
|
|
801
|
+
"end": {"date": "2022-04-29"},
|
|
802
|
+
"recurringEventId": "recurring-1",
|
|
803
|
+
"originalStartTime": {"date": "2022-04-27"},
|
|
804
|
+
"status": "confirmed",
|
|
805
|
+
},
|
|
806
|
+
{
|
|
807
|
+
"id": "other-master_20220420",
|
|
808
|
+
"iCalUID": "other-master@google.com",
|
|
809
|
+
"summary": "Invite-Only Standalone Instance",
|
|
810
|
+
"start": {"date": "2022-04-21"},
|
|
811
|
+
"end": {"date": "2022-04-22"},
|
|
812
|
+
"recurringEventId": "other-master",
|
|
813
|
+
"originalStartTime": {"date": "2022-04-20"},
|
|
814
|
+
"status": "confirmed",
|
|
815
|
+
},
|
|
816
|
+
],
|
|
817
|
+
"nextSyncToken": "sync-token-1",
|
|
818
|
+
}
|
|
819
|
+
)
|
|
820
|
+
|
|
821
|
+
sync = await event_sync_manager_cb()
|
|
822
|
+
await sync.run()
|
|
823
|
+
|
|
824
|
+
stored_items = await _stored_events(store)
|
|
825
|
+
assert set(stored_items) == {
|
|
826
|
+
"recurring-1",
|
|
827
|
+
"recurring-1_20220420",
|
|
828
|
+
"recurring-1_20220427",
|
|
829
|
+
"other-master_20220420",
|
|
830
|
+
}
|
|
831
|
+
assert stored_items["recurring-1_20220420"] == {
|
|
832
|
+
"id": "recurring-1_20220420",
|
|
833
|
+
"status": "cancelled",
|
|
834
|
+
"recurring_event_id": "recurring-1",
|
|
835
|
+
"original_start_time": {"date": "2022-04-20"},
|
|
836
|
+
}
|
|
837
|
+
assert None not in stored_items["recurring-1"].values()
|
|
838
|
+
|
|
839
|
+
result = await sync.store_service.async_list_events(
|
|
840
|
+
LocalListEventsRequest(
|
|
841
|
+
start_time=datetime.datetime.fromisoformat("2022-04-01T00:00:00"),
|
|
842
|
+
)
|
|
843
|
+
)
|
|
844
|
+
assert [event.start.value for event in result.events] == [
|
|
845
|
+
datetime.date(2022, 4, 13),
|
|
846
|
+
datetime.date(2022, 4, 21),
|
|
847
|
+
datetime.date(2022, 4, 28),
|
|
848
|
+
]
|
|
849
|
+
|
|
850
|
+
# Cancel master on Page 1; exception of that master and cancelled standalone instance on Page 2
|
|
851
|
+
json_response(
|
|
852
|
+
{
|
|
853
|
+
"items": [{"id": "recurring-1", "status": "cancelled"}],
|
|
854
|
+
"nextPageToken": "page-2",
|
|
855
|
+
}
|
|
856
|
+
)
|
|
857
|
+
json_response(
|
|
858
|
+
{
|
|
859
|
+
"items": [
|
|
860
|
+
{
|
|
861
|
+
"id": "recurring-1_20220427",
|
|
862
|
+
"status": "cancelled",
|
|
863
|
+
"recurringEventId": "recurring-1",
|
|
864
|
+
"originalStartTime": {"date": "2022-04-27"},
|
|
865
|
+
},
|
|
866
|
+
{
|
|
867
|
+
"id": "other-master_20220420",
|
|
868
|
+
"status": "cancelled",
|
|
869
|
+
"recurringEventId": "other-master",
|
|
870
|
+
"originalStartTime": {"date": "2022-04-20"},
|
|
871
|
+
},
|
|
872
|
+
],
|
|
873
|
+
"nextSyncToken": "sync-token-2",
|
|
874
|
+
}
|
|
875
|
+
)
|
|
876
|
+
await sync.run()
|
|
877
|
+
assert await _stored_events(store) == {}
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
async def test_legacy_store_cancelled_events_cleaned_without_full_resync(
|
|
881
|
+
event_sync_manager_cb: Callable[[], Awaitable[CalendarEventSyncManager]],
|
|
882
|
+
store: CalendarStore,
|
|
883
|
+
json_response: ApiResult,
|
|
884
|
+
url_request: Callable[[], list[str]],
|
|
885
|
+
) -> None:
|
|
886
|
+
"""Test pre-existing cancelled events in a legacy store are cleaned up in place."""
|
|
887
|
+
await store.async_save(
|
|
888
|
+
{
|
|
889
|
+
EVENT_SYNC: {
|
|
890
|
+
CALENDAR_ID: {
|
|
891
|
+
SYNC_TOKEN: "sync-token-1",
|
|
892
|
+
SYNC_TOKEN_VERSION: VERSION,
|
|
893
|
+
ITEMS: {
|
|
894
|
+
"active-1": {
|
|
895
|
+
"id": "active-1",
|
|
896
|
+
"summary": "Active Event",
|
|
897
|
+
"start": {"date": "2022-04-13"},
|
|
898
|
+
"end": {"date": "2022-04-14"},
|
|
899
|
+
"status": "confirmed",
|
|
900
|
+
},
|
|
901
|
+
"legacy-cancelled-single": {
|
|
902
|
+
"id": "legacy-cancelled-single",
|
|
903
|
+
"status": "cancelled",
|
|
904
|
+
},
|
|
905
|
+
"legacy-cancelled-master": {
|
|
906
|
+
"id": "legacy-cancelled-master",
|
|
907
|
+
"status": "cancelled",
|
|
908
|
+
},
|
|
909
|
+
"legacy-cancelled-master_20220420": {
|
|
910
|
+
"id": "legacy-cancelled-master_20220420",
|
|
911
|
+
"recurring_event_id": "legacy-cancelled-master",
|
|
912
|
+
"original_start_time": {"date": "2022-04-20"},
|
|
913
|
+
"status": "cancelled",
|
|
914
|
+
},
|
|
915
|
+
"legacy-cancelled-master_20220427": {
|
|
916
|
+
"id": "legacy-cancelled-master_20220427",
|
|
917
|
+
"recurring_event_id": "legacy-cancelled-master",
|
|
918
|
+
"original_start_time": {"date": "2022-04-27"},
|
|
919
|
+
"status": "confirmed",
|
|
920
|
+
},
|
|
921
|
+
"orphan-cancelled-exception": {
|
|
922
|
+
"id": "orphan-cancelled-exception",
|
|
923
|
+
"recurring_event_id": "missing-master",
|
|
924
|
+
"original_start_time": {"date": "2022-04-20"},
|
|
925
|
+
"status": "cancelled",
|
|
926
|
+
},
|
|
927
|
+
"cancelled-exception-no-ost": {
|
|
928
|
+
"id": "cancelled-exception-no-ost",
|
|
929
|
+
"recurring_event_id": "active-master",
|
|
930
|
+
"status": "cancelled",
|
|
931
|
+
},
|
|
932
|
+
"active-master": {
|
|
933
|
+
"id": "active-master",
|
|
934
|
+
"summary": "Active Recurring",
|
|
935
|
+
"start": {"date": "2022-04-13"},
|
|
936
|
+
"end": {"date": "2022-04-14"},
|
|
937
|
+
"recurrence": ["RRULE:FREQ=WEEKLY;COUNT=3"],
|
|
938
|
+
"status": "confirmed",
|
|
939
|
+
},
|
|
940
|
+
"active-master_20220420": {
|
|
941
|
+
"id": "active-master_20220420",
|
|
942
|
+
"recurring_event_id": "active-master",
|
|
943
|
+
"original_start_time": {"date": "2022-04-20"},
|
|
944
|
+
"status": "cancelled",
|
|
945
|
+
},
|
|
946
|
+
},
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
)
|
|
951
|
+
|
|
952
|
+
json_response({"items": [], "nextSyncToken": "sync-token-2"})
|
|
953
|
+
|
|
954
|
+
sync = await event_sync_manager_cb()
|
|
955
|
+
await sync.run()
|
|
956
|
+
|
|
957
|
+
assert url_request() == [
|
|
958
|
+
f"/calendars/some-calendar-id/events?{EVENT_PAGE_PARAMS}&syncToken=sync-token-1"
|
|
959
|
+
]
|
|
960
|
+
assert set(await _stored_events(store)) == {
|
|
961
|
+
"active-1",
|
|
962
|
+
"active-master",
|
|
963
|
+
"active-master_20220420",
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
|
|
746
967
|
async def test_event_sync_recover_failure(
|
|
747
968
|
calendar_list_sync_manager_cb: Callable[[], Awaitable[CalendarListSyncManager]],
|
|
748
969
|
json_response: ApiResult,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|