diff --git a/calendar_utils/parser.py b/calendar_utils/parser.py index d34623e9081105707f556de69711cb497849fbdc..7782d47e8a9ec23945fe16bdbd33afa9f55eece3 100644 --- a/calendar_utils/parser.py +++ b/calendar_utils/parser.py @@ -1,3 +1,4 @@ +import re from operator import attrgetter import arrow @@ -7,8 +8,15 @@ from ics import Calendar EVENT_KEYS = ("begin", "end", "all_day", "name", "description", "location") +def remove_alarms(source): + """Removes VALARM blocks from iCal source.""" + return re.sub(r"(BEGIN:VALARM.*?END:VALARM\r?\n)", "", source, flags=re.S) + + def parse_ical(source): """Parses iCalendar source and returns events as list of dicts.""" + # there is a bug in parsing alarms, but we don't need them, so we remove them + source = remove_alarms(source) cal = Calendar(source) events = [] for event in sorted(cal.events, key=attrgetter("begin"), reverse=True): diff --git a/tests/calendar_utils/sample.ics b/tests/calendar_utils/sample.ics index 895c4856917c5b6b69bc73c14da82de3aab37999..bca5df66a3e13e2aaa5ccfcf4ad8ce12903f00df 100644 --- a/tests/calendar_utils/sample.ics +++ b/tests/calendar_utils/sample.ics @@ -70,6 +70,21 @@ SEQUENCE:0 STATUS:CONFIRMED SUMMARY:Setkání s místopředsedou Evropského parlamentu Marcelem Kolalokou TRANSP:OPAQUE +BEGIN:VALARM +ACTION:EMAIL +TRIGGER;VALUE=DURATION:-PT1H +SUMMARY:Default Mozilla Summary +DESCRIPTION:Default Mozilla Description +X-LIC-ERROR;X-LIC-ERRORTYPE=PARAMETER-VALUE-PARSE-ERROR:Got a VALUE paramet + er with an illegal type for property: VALUE=DURATION +END:VALARM +BEGIN:VALARM +ACTION:DISPLAY +TRIGGER;VALUE=DURATION:-PT30M +DESCRIPTION:Default Mozilla Description +X-LIC-ERROR;X-LIC-ERRORTYPE=PARAMETER-VALUE-PARSE-ERROR:Got a VALUE paramet + er with an illegal type for property: VALUE=DURATION +END:VALARM END:VEVENT BEGIN:VEVENT DTSTART:20200301T140000Z diff --git a/tests/calendar_utils/test_parser.py b/tests/calendar_utils/test_parser.py index 3121910746fce10cca92cdf2a26683181062dde9..e7283717393cc7d1190204d895be39e0de23288d 100644 --- a/tests/calendar_utils/test_parser.py +++ b/tests/calendar_utils/test_parser.py @@ -4,6 +4,7 @@ import pytest from calendar_utils.parser import ( parse_ical, process_ical, + remove_alarms, set_event_duration, split_events, ) @@ -56,3 +57,14 @@ def test_process_ical(sample, snapshot): past_events, future_events = process_ical(sample) snapshot.assert_match(past_events) snapshot.assert_match(future_events) + + +def test_remove_alarms(snapshot): + source = ( + "BEGIN:VEVENT\ncontent\nBEGIN:VALARM\nalarm\nEND:VALARM\nEND:VEVENT\n" + "BEGIN:VEVENT\nother event\nBEGIN:VALARM\nnoooo\nEND:VALARM\nEND:VEVENT" + ) + expected = ( + "BEGIN:VEVENT\ncontent\nEND:VEVENT\nBEGIN:VEVENT\nother event\nEND:VEVENT" + ) + assert remove_alarms(source) == expected