Skip to content
Snippets Groups Projects
Commit 6c09c465 authored by jan.bednarik's avatar jan.bednarik
Browse files

calendar utils: Fix parsing alarms

parent c7466501
No related branches found
No related tags found
2 merge requests!408Uniweb kalendáře,!407uniweb calendar
import re
from operator import attrgetter from operator import attrgetter
import arrow import arrow
...@@ -7,8 +8,15 @@ from ics import Calendar ...@@ -7,8 +8,15 @@ from ics import Calendar
EVENT_KEYS = ("begin", "end", "all_day", "name", "description", "location") 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): def parse_ical(source):
"""Parses iCalendar source and returns events as list of dicts.""" """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) cal = Calendar(source)
events = [] events = []
for event in sorted(cal.events, key=attrgetter("begin"), reverse=True): for event in sorted(cal.events, key=attrgetter("begin"), reverse=True):
......
...@@ -70,6 +70,21 @@ SEQUENCE:0 ...@@ -70,6 +70,21 @@ SEQUENCE:0
STATUS:CONFIRMED STATUS:CONFIRMED
SUMMARY:Setkání s místopředsedou Evropského parlamentu Marcelem Kolalokou SUMMARY:Setkání s místopředsedou Evropského parlamentu Marcelem Kolalokou
TRANSP:OPAQUE 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 END:VEVENT
BEGIN:VEVENT BEGIN:VEVENT
DTSTART:20200301T140000Z DTSTART:20200301T140000Z
......
...@@ -4,6 +4,7 @@ import pytest ...@@ -4,6 +4,7 @@ import pytest
from calendar_utils.parser import ( from calendar_utils.parser import (
parse_ical, parse_ical,
process_ical, process_ical,
remove_alarms,
set_event_duration, set_event_duration,
split_events, split_events,
) )
...@@ -56,3 +57,14 @@ def test_process_ical(sample, snapshot): ...@@ -56,3 +57,14 @@ def test_process_ical(sample, snapshot):
past_events, future_events = process_ical(sample) past_events, future_events = process_ical(sample)
snapshot.assert_match(past_events) snapshot.assert_match(past_events)
snapshot.assert_match(future_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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment