Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from_ical() Invalid iCalendar duration: -P9W2D #517

Closed
NikEasY opened this issue May 25, 2023 · 8 comments · Fixed by #520
Closed

from_ical() Invalid iCalendar duration: -P9W2D #517

NikEasY opened this issue May 25, 2023 · 8 comments · Fixed by #520

Comments

@NikEasY
Copy link
Contributor

NikEasY commented May 25, 2023

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
@NikEasY
Copy link
Contributor Author

NikEasY commented May 25, 2023

version icalendar==5.0.5

@niccokunzmann
Copy link
Member

niccokunzmann commented May 25, 2023 via email

@NikEasY
Copy link
Contributor Author

NikEasY commented May 25, 2023

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

@NikEasY
Copy link
Contributor Author

NikEasY commented May 25, 2023

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

@NikEasY
Copy link
Contributor Author

NikEasY commented May 25, 2023

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)

@NikEasY
Copy link
Contributor Author

NikEasY commented May 25, 2023

@niccokunzmann
Copy link
Member

niccokunzmann commented May 25, 2023 via email

@NikEasY
Copy link
Contributor Author

NikEasY commented May 26, 2023

done #520

@jacadzaca jacadzaca linked a pull request May 26, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants