From 6c09c4654ced07b4b7c24c5b434a52efa8787000 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Wed, 6 Oct 2021 18:11:58 +0200
Subject: [PATCH] calendar utils: Fix parsing alarms

---
 calendar_utils/parser.py            |  8 ++++++++
 tests/calendar_utils/sample.ics     | 15 +++++++++++++++
 tests/calendar_utils/test_parser.py | 12 ++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/calendar_utils/parser.py b/calendar_utils/parser.py
index d34623e9..7782d47e 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 895c4856..bca5df66 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 31219107..e7283717 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
-- 
GitLab