Skip to content

Commit fc617e6

Browse files
committed
Use library for calendar merging
It's functionally the same code, but could add some enhancements in future
1 parent 0864926 commit fc617e6

File tree

5 files changed

+157
-27
lines changed

5 files changed

+157
-27
lines changed

calmerge/calendars.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import icalendar
77
from aiocache import Cache
88
from aiohttp import ClientSession
9+
from mergecal import merge_calendars
910

1011
from .config import CalendarConfig
1112

1213
fetch_cache = Cache(Cache.MEMORY, ttl=3600)
1314

15+
PRODID = "-//Torchbox//Calmerge//EN"
16+
1417

1518
async def fetch_calendar(session: ClientSession, url: str) -> icalendar.Calendar:
1619
cache_key = "calendar_" + url
@@ -25,17 +28,15 @@ async def fetch_calendar(session: ClientSession, url: str) -> icalendar.Calendar
2528

2629

2730
async def fetch_merged_calendar(calendar_config: CalendarConfig) -> icalendar.Calendar:
28-
merged_calendar = icalendar.Calendar()
31+
calendars = []
2932

3033
async with ClientSession() as session:
31-
calendars = [fetch_calendar(session, str(url)) for url in calendar_config.urls]
32-
33-
for coro in asyncio.as_completed(calendars):
34-
calendar = await coro
35-
for component in calendar.walk("VEVENT"):
36-
merged_calendar.add_component(component)
34+
for coro in asyncio.as_completed(
35+
[fetch_calendar(session, str(url)) for url in calendar_config.urls]
36+
):
37+
calendars.append(await coro)
3738

38-
return merged_calendar
39+
return merge_calendars(calendars, prodid=PRODID)
3940

4041

4142
def shift_event_by_offset(event: icalendar.cal.Component, offset: timedelta) -> None:

poetry.lock

Lines changed: 141 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ readme = "README.md"
1010
python = "^3.11"
1111
aiohttp = "^3.9.3"
1212
pydantic = "^2.6.4"
13-
icalendar = "^5.0.11"
13+
icalendar = "^6"
1414
aiocache = "^0.12.2"
1515
aiohttp-jinja2 = "^1.6"
1616

1717
aiohttp-remotes = "^1.2.0"
18+
mergecal = "^0.3.6"
1819
[tool.poetry.group.dev.dependencies]
1920
ruff = "^0.3.2"
2021
pytest = "^8.1.1"

tests/test_calendar_view.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async def test_retrieves_calendars(client: TestClient) -> None:
1010
assert response.status == 200
1111

1212
calendar = icalendar.Calendar.from_ical(await response.text())
13-
assert not calendar.is_broken
13+
assert calendar.errors == []
1414

1515
assert calendar["X-WR-CALNAME"] == "Python"
1616
assert calendar["X-WR-CALDESC"] == "Python EOL"
@@ -37,7 +37,7 @@ async def test_requires_auth(client: TestClient) -> None:
3737
assert response.status == 200
3838

3939
calendar = icalendar.Calendar.from_ical(await response.text())
40-
assert not calendar.is_broken
40+
assert calendar.errors == []
4141

4242

4343
async def test_offset(client: TestClient) -> None:
@@ -47,8 +47,8 @@ async def test_offset(client: TestClient) -> None:
4747
original_response = await client.get("/python.ics")
4848
original_calendar = icalendar.Calendar.from_ical(await original_response.text())
4949

50-
assert not offset_calendar.is_broken
51-
assert not original_calendar.is_broken
50+
assert offset_calendar.errors == []
51+
assert original_calendar.errors == []
5252

5353
assert (
5454
len(offset_calendar.walk("VEVENT")) == len(original_calendar.walk("VEVENT")) * 2

tests/test_write.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ def test_write_config(tmp_path: Path, config: Config, config_path: Path) -> None
2626
assert calendar_path.is_file()
2727

2828
calendar = icalendar.Calendar.from_ical(calendar_path.read_text())
29-
assert not calendar.is_broken
29+
assert calendar.errors == []

0 commit comments

Comments
 (0)