summaryrefslogtreecommitdiff
path: root/caldav/lib/vcal.py
diff options
context:
space:
mode:
Diffstat (limited to 'caldav/lib/vcal.py')
-rw-r--r--caldav/lib/vcal.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/caldav/lib/vcal.py b/caldav/lib/vcal.py
index f0e2cf4..2b132ae 100644
--- a/caldav/lib/vcal.py
+++ b/caldav/lib/vcal.py
@@ -3,7 +3,9 @@
import re
-from caldav.lib.python_utilities import to_local
+from caldav.lib.python_utilities import to_local, to_wire
+import datetime
+import uuid
## Fixups to the icalendar data to work around compatbility issues.
@@ -75,3 +77,37 @@ def fix(event):
fixed2 += line + "\n"
return fixed2
+
+## sorry for being english-language-euro-centric ... fits rather perfectly as default language for me :-)
+def create_ical(ical_fragment=None, objtype=None, language='en_DK', **attributes):
+ """
+ I somehow feel this fits more into the icalendar library than here
+ """
+ ## late import, icalendar is not an explicit requirement for v0.x of the caldav library.
+ ## (perhaps I should change my position on that soon)
+ import icalendar
+ if ical_fragment:
+ ical_fragment = to_wire(ical_fragment)
+ if not ical_fragment or not re.search(b'^BEGIN:V', ical_fragment, re.MULTILINE):
+ my_instance = icalendar.Calendar()
+ my_instance.add('prodid', f'-//python-caldav//caldav//{language}')
+ my_instance.add('version', '2.0')
+ if objtype is None:
+ objtype='VEVENT'
+ component = icalendar.cal.component_factory[objtype]()
+ component.add('dtstamp', datetime.datetime.now())
+ component.add('uid', uuid.uuid1())
+ my_instance.add_component(component)
+ else:
+ if not ical_fragment.startswith(b'BEGIN:VCALENDAR'):
+ ical_fragment = b"BEGIN:VCALENDAR\n"+to_wire(ical_fragment.strip())+b"\nEND:VCALENDAR\n"
+ my_instance = icalendar.Calendar.from_ical(ical_fragment)
+ component = my_instance.subcomponents[0]
+ ical_fragment = None
+ for attribute in attributes:
+ if attributes[attribute] is not None:
+ component.add(attribute, attributes[attribute])
+ ret = my_instance.to_ical()
+ if ical_fragment is not None:
+ ret = re.sub(b"^END:V", ical_fragment.strip() + b"\nEND:V", ret, flags=re.MULTILINE)
+ return ret