55import uuid
66
77import icalendar
8+ from caldav .lib .python_utilities import to_local
89from caldav .lib .python_utilities import to_normal_str
10+ from caldav .lib .python_utilities import to_wire
911
1012## Fixups to the icalendar data to work around compatbility issues.
1113
@@ -81,8 +83,12 @@ def fix(event):
8183
8284## sorry for being english-language-euro-centric ... fits rather perfectly as default language for me :-)
8385def create_ical (ical_fragment = None , objtype = None , language = "en_DK" , ** props ):
84- """
85- I somehow feel this fits more into the icalendar library than here
86+ """Creates some icalendar based on properties given as parameters.
87+ It basically creates an icalendar object with all the boilerplate,
88+ some sensible defaults, the properties given and returns it as a
89+ string.
90+
91+ TODO: timezones not supported so far
8692 """
8793 ical_fragment = to_normal_str (ical_fragment )
8894 if not ical_fragment or not re .search ("^BEGIN:V" , ical_fragment , re .MULTILINE ):
@@ -105,18 +111,31 @@ def create_ical(ical_fragment=None, objtype=None, language="en_DK", **props):
105111 my_instance = icalendar .Calendar .from_ical (ical_fragment )
106112 component = my_instance .subcomponents [0 ]
107113 ical_fragment = None
114+ alarm = {}
108115 for prop in props :
109116 if props [prop ] is not None :
110117 if prop in ("child" , "parent" ):
111118 for value in props [prop ]:
112119 component .add (
113120 "related-to" , props [prop ], parameters = {"rel-type" : prop .upper ()}
114121 )
122+ elif prop .startswith ("alarm_" ):
123+ alarm [prop [6 :]] = props [prop ]
115124 else :
116125 component .add (prop , props [prop ])
126+ if alarm :
127+ add_alarm (my_instance , alarm )
117128 ret = to_normal_str (my_instance .to_ical ())
118129 if ical_fragment and ical_fragment .strip ():
119130 ret = re .sub (
120131 "^END:V" , ical_fragment .strip () + "\n END:V" , ret , flags = re .MULTILINE
121132 )
122133 return ret
134+
135+
136+ def add_alarm (ical , alarm ):
137+ ia = icalendar .Alarm ()
138+ for prop in alarm :
139+ ia .add (prop , alarm [prop ])
140+ ical .subcomponents [0 ].add_component (ia )
141+ return ical
0 commit comments