collective / icalendar

icalendar parser library for Python

Home Page:https://icalendar.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

from_ical() Invalid iCalendar duration: -P9W2D

NikEasY opened this issue · comments

hi, can you please help me to figure out what's wrong here and why it's failing on this?
-P9W2D it's 65 days before event trigger
thanks in advance

ics file:

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
SEQUENCE:5
TRANS:OPAQUE
UID:cb84ea88-75ac-4d79-b0f9-e1206df1d41d
DTSTART:20230510T090000
DTEND:20230510T100000
SUMMARY:event with rrule by and without tz
DESCRIPTION:this event created with rrule but without zone also with remind
 er after 65 days
LOCATION:location field: test
DTSTAMP:20230518T200827Z
LAST-MODIFIED:20230518T200827Z
CATEGORY:General
RRULE:FREQ=DAILY;INTERVAL=1;UNTIL=20230512T000000
BEGIN:VALARM
TRIGGER:-P9W2D
DESCRIPTION:Alarm for: "event with rrule by and without tz"
ACTION:DISPLAY
END:VALARM
END:VEVENT
END:VCALENDAR

code:

import icalendar

with open('cal.ics', 'rb') as f:
    cal = icalendar.Calendar.from_ical(f.read())

error:

Traceback (most recent call last):
  File "/Users/we/venv/lib/python3.9/site-packages/icalendar/prop.py", line 480, in from_ical
    sign, weeks, days, hours, minutes, seconds = match.groups()
AttributeError: 'NoneType' object has no attribute 'groups'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/we/test_2.py", line 4, in <module>
    cal = icalendar.Calendar.from_ical(f.read())
  File "/Users/we/venv/lib/python3.9/site-packages/icalendar/cal.py", line 380, in from_ical
    vals = factory(factory.from_ical(vals))
  File "/Users/we/venv/lib/python3.9/site-packages/icalendar/prop.py", line 335, in from_ical
    return vDuration.from_ical(ical)
  File "/Users/we/venv/lib/python3.9/site-packages/icalendar/prop.py", line 492, in from_ical
    raise ValueError(f'Invalid iCalendar duration: {ical}')
ValueError: Invalid iCalendar duration: -P9W2D

version icalendar==5.0.5

yes it's a variant of ISO 8601 duration format
found next: DURATION_REGEX cannot parse it in venv/lib/python3.9/site-packages/icalendar/prop.py (line 70)
in code:

re.compile('([-+]?)P(?:(\\d+)W|(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?)?)$')

my variant looks like is working fine:

re.compile('([-+]?)P(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$')

but function from_ical (line 480) should be adjusted too

P9W2D it's 9 weeks and 2 days can be written in format P65D I guess

modified func:

    def from_ical(ical):
        try:
            match = DURATION_REGEX.match(ical)
            sign, weeks, days, hours, minutes, seconds = match.groups()
            if match:
                value = timedelta(weeks=int(weeks or 0),
                                  days=int(days or 0),
                                  hours=int(hours or 0),
                                  minutes=int(minutes or 0),
                                  seconds=int(seconds or 0))
            if sign == '-':
                value = -value
            return value
        except Exception:
            raise ValueError(f'Invalid iCalendar duration: {ical}')

but I'm new in python and it works but I'm not sure if it's right)

done #520