Skip to content

Commit 8032c32

Browse files
committed
support for ical files not coming from caldav servers (caldav wraps every unique uid into a separate VCALENDAR-object, other tools typically returns multiple objects wrapped into one VCALENDAR. Both should be supported. TODO: tests still missing!
1 parent 2c0b1c8 commit 8032c32

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

plann/lib.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import datetime
1212
import caldav
1313
import logging
14+
import re
1415
from collections import defaultdict
1516
from plann.template import Template
1617
from plann.timespec import tz, _now, _ensure_ts, parse_dt, parse_add_dur, parse_timespec
@@ -23,12 +24,34 @@
2324
attr_int = ['priority']
2425

2526
def _split_vcal(ical):
26-
ical = ical.strip()
27+
ical = ical.strip().replace('\r\n','\n')
2728
icals = []
2829
while ical.startswith("BEGIN:VCALENDAR\n"):
2930
pos = ical.find("\nEND:VCALENDAR") + 14
3031
icals.append(ical[:pos])
3132
ical = ical[pos:].lstrip()
33+
assert not ical
34+
if len(icals) == 1:
35+
ical_start = icals[0].find('BEGIN', 15)
36+
header = icals[0][:ical_start]
37+
ical = icals[0][ical_start:]
38+
icals = []
39+
prev_uid = None
40+
while ical.startswith('BEGIN:'):
41+
what = ical[6:ical.find('\n')]
42+
uidpos = ical.find('UID:')
43+
uidend = ical.find('\n', uidpos)
44+
uidline = ical[uidpos:uidend]
45+
pos = ical.find('END:' + what) + 4 + len(what)
46+
if uidline != prev_uid:
47+
icals.append(ical[:pos])
48+
prev_uid = uidline
49+
else:
50+
icals[-1].append(ical[:pos])
51+
ical = ical[pos:].strip()
52+
import pdb; pdb.set_trace()
53+
assert ical == 'END:VCALENDAR'
54+
icals = [f"{header}{x}\nEND:VCALENDAR" for x in icals]
3255
return icals
3356

3457
def find_calendars(args, raise_errors):

0 commit comments

Comments
 (0)