py-vobject / vobject

A full-featured Python package for parsing and creating iCalendar and vCard files
http://py-vobject.github.io/
29 stars 7 forks source link

Be tolerant of spaces in parsing times #50

Closed akkana closed 3 months ago

akkana commented 3 months ago

(I hope this is the right version: I used pip install vobject to make sure I had the latest for the vobject that PyPI has.)

I made an appointment at Wells Fargo https://appointments.wellsfargo.com/maa/appointment/reasons and it emailed me a .ics file. I use a tiny script built around vobject to parse .ics attachments, but it failed (see error message below). The problem turned out to be the space in this line:

DTEND: 20240812T223000Z

I don't know if spaces are technically allowed in ics files. They're apparently generating this with "Force.com Labs//iCalendar Export" which, when I web search for it, seems to be a Salesforce thing, so it may be in fairly common use and other people might also be hitting this, so it might be good to be tolerant of spaces even if it's technically an error.

I'll attach the ics file. Github won't let me attach a .ics file, so I'll try renaming it .ics.txt. (Whoops, I attached a fixed version the first time. I'll attach the original version with the space separately since I can't do it in an edit.)

Here's the full error message. I tried to make it a code block, but there are quotes in it that cancel the markdown code block syntax. I hope it's readable anyway; it looks okay in the Preview tab.

`>>> fp = open('/tmp/appt.ics')

parsedCal = vobject.readOne(fp) Traceback (most recent call last): File "/tmp/testenv/lib/python3.12/site-packages/vobject/icalendar.py", line 1734, in stringToDateTime hour = int(s[9:11]) ^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: 'T2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/tmp/testenv/lib/python3.12/site-packages/vobject/icalendar.py", line 1956, in parseDtstart return stringToDateTime(contentline.value, tzinfo) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/testenv/lib/python3.12/site-packages/vobject/icalendar.py", line 1741, in stringToDateTime raise ParseError("'{0!s}' is not a valid DATE-TIME".format(s)) vobject.base.ParseError: "' 20240812T223000Z' is not a valid DATE-TIME"

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/tmp/testenv/lib/python3.12/site-packages/vobject/base.py", line 185, in transformToNative return self.behavior.transformToNative(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/testenv/lib/python3.12/site-packages/vobject/icalendar.py", line 808, in transformToNative obj.value = parseDtstart(obj, allowSignatureMismatch=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/testenv/lib/python3.12/site-packages/vobject/icalendar.py", line 1959, in parseDtstart return stringToDate(contentline.value) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/testenv/lib/python3.12/site-packages/vobject/icalendar.py", line 1723, in stringToDate return datetime.date(year, month, day) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: month must be in 1..12

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/tmp/testenv/lib/python3.12/site-packages/vobject/base.py", line 1155, in readOne return next(readComponents(stream, validate, transform, ignoreUnreadable, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/testenv/lib/python3.12/site-packages/vobject/base.py", line 1128, in readComponents component.transformChildrenToNative() File "/tmp/testenv/lib/python3.12/site-packages/vobject/base.py", line 674, in transformChildrenToNative child.transformChildrenToNative() File "/tmp/testenv/lib/python3.12/site-packages/vobject/base.py", line 673, in transformChildrenToNative child = child.transformToNative() ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/testenv/lib/python3.12/site-packages/vobject/base.py", line 198, in transformToNative raise ParseError(msg, lineNumber) vobject.base.ParseError: At line 27: In transformToNative, unhandled exception on line 27: <class 'ValueError'>: month must be in 1..12 (<DTEND{} 20240812T223000Z>) `

akkana commented 3 months ago

Here's the original version, the one they emailed me, renamed to have a .txt. Appointment.ics.txt

da4089 commented 3 months ago

Thanks for the report, @akkana!

The iCalendar validator at https://icalendar.org/validator.html reports the following errors with the supplied .ics file:

BUT, as you point out, this is likely to be a common source of iCalendar objects, and it'd be helpful to tolerate its output.

da4089 commented 3 months ago

I have reproduced the reported errors using the latest Git source and Python 3.8.

da4089 commented 3 months ago

This fix will be included in release 0.9.8.