Skip to content

Commit 5addf2f

Browse files
authored
Merge pull request #1482 from demaledetti/issue/1436/exdate-with-date-values
Issue/1436/exdate with date values
2 parents a466b7c + 9d0d2e8 commit 5addf2f

4 files changed

Lines changed: 104 additions & 10 deletions

File tree

‎AUTHORS.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ John Wassilak - john [at] wassilak [dot] com
6363
Pierre Wilke - wilke.pierre [at] gmail [dot] com
6464
Muhammad Umar Nadeem
6565
Aleksei Stepanov - penguinolog [at] gmail [dot] com
66+
Ben Finney - ben+python [at] benfinney [dot] id [dot] au
67+
Stefano Debenedetti - 44903077+demaledetti [at] users [dot] noreply [dot] github [dot] com

‎CHANGELOG.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ unreleased
2525
* FIX ikhal compatibility with urwid 5.0, which removed ``Columns.focus_col``,
2626
``Columns.set_focus_column()``, ``Pile.set_focus()``, ``Frame.set_body()`` and
2727
``AttrWrap.w``
28+
* FIX crash when expanding recurring events with a time whose EXDATE is a
29+
date only; such an EXDATE now excludes all instances on that day
2830

2931
0.14.1
3032
======

‎khal/icalendar.py‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,11 @@ def expand(
260260
events_tz = getattr(dtstart_prop.dt, "tzinfo", None) if not allday else None
261261

262262
def sanitize_datetime(date: dt.date) -> dt.date:
263-
if allday and isinstance(date, dt.datetime):
264-
date = date.date()
265-
if events_tz is not None:
266-
date = events_tz.localize(date)
263+
if isinstance(date, dt.datetime):
264+
if allday:
265+
date = date.date()
266+
if events_tz is not None:
267+
date = events_tz.localize(date)
267268
return date
268269

269270
rrule_param = vevent.get("RRULE")
@@ -356,14 +357,31 @@ def get_dates(vevent, key):
356357

357358
# remove excluded dates
358359
if expand:
359-
for date in get_dates(vevent, "EXDATE") or ():
360+
for exdate in get_dates(vevent, "EXDATE") or ():
360361
try:
361-
dtstartl.remove(date)
362+
# try the happy path first:
363+
# * assume recurrences have the same type as exdates
364+
# * and trust that they are supposed to match
365+
dtstartl.remove(exdate)
362366
except KeyError:
363-
logger.warning(
364-
f"In event {href}, excluded instance starting at {date} "
365-
"not found, event might be invalid."
366-
)
367+
found = False
368+
# if there was no match, and if the exdate is a date
369+
# exclude any recurrence datetime that has that date
370+
if type(exdate) is dt.date:
371+
_dtstartl = {
372+
start_datetime
373+
for start_datetime in dtstartl
374+
if (type(start_datetime) is dt.date or start_datetime.date() != exdate)
375+
}
376+
if dtstartl != _dtstartl:
377+
found = True
378+
dtstartl = _dtstartl
379+
# both attempts at finding this exdate among the recurrences failed
380+
if not found:
381+
logger.warning(
382+
f"In event {href}, excluded instance starting at {exdate} "
383+
"not found, event might be invalid."
384+
)
367385

368386
dtstartend = [(start, start + duration) for start in dtstartl]
369387
# not necessary, but I prefer deterministic output

‎tests/khalendar_utils_test.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,49 @@ def test_expand_rrule_notz_until_z(self):
731731
END:VCALENDAR
732732
"""
733733

734+
event_exdate_d = """BEGIN:VCALENDAR
735+
VERSION:2.0
736+
BEGIN:VEVENT
737+
UID:event_exdate_d123
738+
DTSTAMP:20140627T162546Z
739+
DTSTART;TZID=Europe/Berlin:20140702T190000
740+
DTEND;TZID=Europe/Berlin:20140702T193000
741+
SUMMARY:Test event
742+
RRULE:FREQ=DAILY;COUNT=10
743+
EXDATE:20140703
744+
END:VEVENT
745+
END:VCALENDAR
746+
"""
747+
748+
event_exdate_ds = """BEGIN:VCALENDAR
749+
VERSION:2.0
750+
BEGIN:VEVENT
751+
UID:event_exdate_ds123
752+
DTSTAMP:20140627T162546Z
753+
DTSTART;VALUE=DATE:20140702
754+
DTEND;VALUE=Date:20140702
755+
SUMMARY:Test event
756+
RRULE:FREQ=DAILY;COUNT=10
757+
EXDATE:20140703
758+
END:VEVENT
759+
END:VCALENDAR
760+
"""
761+
762+
event_exdate_d_multi = """BEGIN:VCALENDAR
763+
VERSION:2.0
764+
BEGIN:VEVENT
765+
UID:event_exdate_d_multi123
766+
DTSTAMP:20140627T162546Z
767+
DTSTART;TZID=Europe/Berlin:20140702T080000
768+
DTEND;TZID=Europe/Berlin:20140702T083000
769+
SUMMARY:Test event
770+
RRULE:FREQ=HOURLY;INTERVAL=8;COUNT=9
771+
EXDATE;VALUE=DATE:20140703
772+
EXDATE;TZID=Europe/Berlin:20140704T080000
773+
END:VEVENT
774+
END:VCALENDAR
775+
"""
776+
734777
latest_bug = """BEGIN:VCALENDAR
735778
VERSION:2.0
736779
BEGIN:VEVENT
@@ -818,6 +861,35 @@ def test_event_exdatesl_dt(self):
818861
assert dtstart[0][0] == berlin.localize(dt.datetime(2014, 7, 2, 19, 0))
819862
assert dtstart[-1][0] == berlin.localize(dt.datetime(2014, 7, 11, 19, 0))
820863

864+
def test_event_exdate_d(self):
865+
"""recurring event, one date excluded via EXCLUDE with date"""
866+
vevent = _get_vevent(event_exdate_d)
867+
dtstart = icalendar_helpers.expand(vevent, berlin)
868+
assert len(dtstart) == 9
869+
assert dtstart[0][0] == berlin.localize(dt.datetime(2014, 7, 2, 19, 0))
870+
assert dtstart[-1][0] == berlin.localize(dt.datetime(2014, 7, 11, 19, 0))
871+
872+
def test_event_exdate_ds(self):
873+
"""recurring event with dates, one date excluded via EXCLUDE with date"""
874+
vevent = _get_vevent(event_exdate_ds)
875+
dtstart = icalendar_helpers.expand(vevent, berlin)
876+
assert len(dtstart) == 9
877+
assert dtstart[0][0] == dt.date(2014, 7, 2)
878+
assert dtstart[-1][0] == dt.date(2014, 7, 11)
879+
880+
def test_event_exdate_d_multiple_instances_per_day(self):
881+
"""a date EXDATE excludes all instances on that day, a datetime one
882+
only the matching instance"""
883+
vevent = _get_vevent(event_exdate_d_multi)
884+
dtstart = [start for start, _ in icalendar_helpers.expand(vevent, berlin)]
885+
assert dtstart == [
886+
berlin.localize(dt.datetime(2014, 7, 2, 8)),
887+
berlin.localize(dt.datetime(2014, 7, 2, 16)),
888+
berlin.localize(dt.datetime(2014, 7, 4, 0)),
889+
berlin.localize(dt.datetime(2014, 7, 4, 16)),
890+
berlin.localize(dt.datetime(2014, 7, 5, 0)),
891+
]
892+
821893
def test_event_exdates_remove(self):
822894
"""check if we can remove one more instance"""
823895
vevent = _get_vevent(event_exdatesl_dt)

0 commit comments

Comments
 (0)