Skip to content

Commit d990323

Browse files
Refactor/update icalendar (#3352)
* Remove ical weekday parsing code I noticed that the icalendar package has already released a new version containing the `vWeekday` fixes, so the hacky parsing code can now be removed 🎉 - Upgrade the icalendar package - replace the manual weekday parsing function with the icalendar functionality
1 parent 86da15d commit d990323

File tree

2 files changed

+6
-39
lines changed

2 files changed

+6
-39
lines changed

integreat_cms/cms/utils/external_calendar_utils.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import dataclasses
88
import datetime
99
import logging
10-
import re
1110
from typing import Any, Self
1211

1312
import icalendar.cal
@@ -28,11 +27,6 @@
2827
from integreat_cms.cms.models import EventTranslation, ExternalCalendar, RecurrenceRule
2928
from integreat_cms.cms.utils.content_utils import clean_content
3029

31-
# Copied from https://github.com/collective/icalendar/blob/4725c1bd57ca3afe61e7d45805a2b337842fbd29/src/icalendar/prop.py#L68-L69
32-
WEEKDAY_RULE = re.compile(
33-
r"(?P<signal>[+-]?)(?P<relative>[\d]{0,2})(?P<weekday>[\w]{2})$"
34-
)
35-
3630

3731
@dataclasses.dataclass(frozen=True, kw_only=True)
3832
class ImportResult:
@@ -196,7 +190,6 @@ class RecurrenceRuleData:
196190
def from_ical_rrule(
197191
cls, recurrence_rule: vRecur, start: datetime.date, logger: logging.Logger
198192
) -> Self:
199-
# pylint: disable=too-many-locals
200193
"""
201194
Constructs this class from an ical recurrence rule.
202195
:return: An instance of this class
@@ -226,19 +219,17 @@ def pop_single_value(name: str, *, required: bool = False) -> Any:
226219

227220
# by_set_pos can also be specified in `by_day`. We don't support multiple days with `by_set_pos` right now, though.
228221
if by_day and len(by_day) == 1:
229-
set_pos, weekday = _parse_weekday(by_day[0])
230-
if by_set_pos is not None and set_pos is not None:
222+
if by_set_pos is not None and by_day[0].relative is not None:
231223
raise ValueError(
232224
f"Conflicting `BYSETPOS` and `BYDAY`: {by_set_pos} and {by_day}"
233225
)
234-
by_day[0] = weekday
235-
by_set_pos = set_pos or by_set_pos
226+
by_set_pos = by_day[0].relative or by_set_pos
227+
by_day[0] = by_day[0].weekday
236228
elif by_day:
237229
updated_days = []
238230
for day in by_day:
239-
set_pos, weekday = _parse_weekday(day)
240-
updated_days.append(weekday)
241-
if set_pos is not None:
231+
updated_days.append(day.weekday)
232+
if day.relative is not None:
242233
raise ValueError(
243234
f"Cannot support multiple days with frequency right now: {by_day}"
244235
)
@@ -338,30 +329,6 @@ def decode_by_day(self) -> list[int]:
338329
return weekdays
339330

340331

341-
def _parse_weekday(weekdaynum: str) -> tuple[int | None, str]:
342-
"""
343-
Parses a weekday according to https://www.rfc-editor.org/rfc/rfc5545#section-3.3.10 (see `weekdaynum`)
344-
TODO: This should be handled by our icalendar package. Remove this code when a new version of icalendar is released.
345-
:param weekdaynum: The weekday + frequency number
346-
:return: A tuple of frequency and weekday, where the frequency is None if the weekdaynum only contained a weekday.
347-
"""
348-
match = WEEKDAY_RULE.match(weekdaynum)
349-
assert match is not None
350-
sign = match["signal"]
351-
ordinal_week_number = match["relative"]
352-
weekday = match["weekday"]
353-
if not ordinal_week_number:
354-
return None, weekday
355-
ordinal_week_number = int(ordinal_week_number)
356-
match sign:
357-
case "-":
358-
return -ordinal_week_number, weekday
359-
case "+" | "":
360-
return ordinal_week_number, weekday
361-
case _:
362-
return None, weekday
363-
364-
365332
def import_events(calendar: ExternalCalendar, logger: logging.Logger) -> ImportResult:
366333
"""
367334
Imports events from this calendar and sets or clears the errors field of the calendar

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pinned = [
169169
"grpcio==1.66.2",
170170
"grpcio-status==1.66.2",
171171
"html5lib==1.1",
172-
"icalendar==6.0.0",
172+
"icalendar==6.1.1",
173173
"idna==3.10",
174174
"ipython==8.27.0",
175175
"jedi==0.19.1",

0 commit comments

Comments
 (0)