Skip to content

Commit

Permalink
Use library for calendar merging
Browse files Browse the repository at this point in the history
It's functionally the same code, but could add some enhancements in future
  • Loading branch information
RealOrangeOne committed Oct 28, 2024
1 parent 0864926 commit fc617e6
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 27 deletions.
17 changes: 9 additions & 8 deletions calmerge/calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import icalendar
from aiocache import Cache
from aiohttp import ClientSession
from mergecal import merge_calendars

from .config import CalendarConfig

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

PRODID = "-//Torchbox//Calmerge//EN"


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


async def fetch_merged_calendar(calendar_config: CalendarConfig) -> icalendar.Calendar:
merged_calendar = icalendar.Calendar()
calendars = []

async with ClientSession() as session:
calendars = [fetch_calendar(session, str(url)) for url in calendar_config.urls]

for coro in asyncio.as_completed(calendars):
calendar = await coro
for component in calendar.walk("VEVENT"):
merged_calendar.add_component(component)
for coro in asyncio.as_completed(
[fetch_calendar(session, str(url)) for url in calendar_config.urls]
):
calendars.append(await coro)

return merged_calendar
return merge_calendars(calendars, prodid=PRODID)


def shift_event_by_offset(event: icalendar.cal.Component, offset: timedelta) -> None:
Expand Down
154 changes: 141 additions & 13 deletions poetry.lock

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

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ readme = "README.md"
python = "^3.11"
aiohttp = "^3.9.3"
pydantic = "^2.6.4"
icalendar = "^5.0.11"
icalendar = "^6"
aiocache = "^0.12.2"
aiohttp-jinja2 = "^1.6"

aiohttp-remotes = "^1.2.0"
mergecal = "^0.3.6"
[tool.poetry.group.dev.dependencies]
ruff = "^0.3.2"
pytest = "^8.1.1"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_calendar_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async def test_retrieves_calendars(client: TestClient) -> None:
assert response.status == 200

calendar = icalendar.Calendar.from_ical(await response.text())
assert not calendar.is_broken
assert calendar.errors == []

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

calendar = icalendar.Calendar.from_ical(await response.text())
assert not calendar.is_broken
assert calendar.errors == []


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

assert not offset_calendar.is_broken
assert not original_calendar.is_broken
assert offset_calendar.errors == []
assert original_calendar.errors == []

assert (
len(offset_calendar.walk("VEVENT")) == len(original_calendar.walk("VEVENT")) * 2
Expand Down
2 changes: 1 addition & 1 deletion tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def test_write_config(tmp_path: Path, config: Config, config_path: Path) -> None
assert calendar_path.is_file()

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

0 comments on commit fc617e6

Please sign in to comment.