+#exchangelib gives us days in recurrence patterns as integers,
+#RFC5545 wants SU,MO,TU,WE,TH,FR,SA
+#it has a utility function to convert to Monday, Tuesday, ...
+def rr_daystr_from_int(i):
+ return exchangelib.recurrence._weekday_to_str(i).upper()[:2]
+
+def rrule_from_ex(event,gcal_tz):
+ if event.type != "RecurringMaster":
+ logger.error("Cannot make recurrence from not-recurring event")
+ return None
+ if event.recurrence is None:
+ logger.error("Empty recurrence structure")
+ return None
+ if isinstance(event.recurrence.pattern,
+ exchangelib.recurrence.DailyPattern):
+ rr = "RRULE:FREQ=DAILY;INTERVAL=%d" % event.recurrence.pattern.interval
+ elif isinstance(event.recurrence.pattern,
+ exchangelib.recurrence.WeeklyPattern):
+ rr = "RRULE:FREQ=WEEKLY;INTERVAL=%d;BYDAY=%s;WKST=%s" % \
+ (event.recurrence.pattern.interval,
+ ",".join([rr_daystr_from_int(x) for x in event.recurrence.pattern.weekdays]),
+ rr_daystr_from_int(event.recurrence.pattern.first_day_of_week) )
+ else:
+ logger.error("Recurrence %s not supported" % event.recurrence)
+ return None
+ if isinstance(event.recurrence.boundary,
+ exchangelib.recurrence.EndDatePattern):
+ rr += ";UNTIL={0:%Y}{0:%m}{0:%d}".format(event.recurrence.boundary.end)
+ else:
+ logger.error("Recurrence %s not supported" % event.recurrence)
+ return None
+ if event.modified_occurrences is not None or \
+ event.deleted_occurrences is not None:
+ logger.warning("Modified/Deleted recurrences not supported")
+ return [rr]
+
+def build_gcal_event_from_ex(event,gcal_tz):
+ gevent={}
+ gevent["summary"]=event.subject
+ if event.is_all_day:
+ gevent["end"]={"date": str(event.end.astimezone(gcal_tz).date())}
+ gevent["start"]={"date": str(event.start.astimezone(gcal_tz).date())}
+ else:
+ gevent["end"]={"dateTime": event.end.astimezone(gcal_tz).isoformat(),
+ "timeZone": str(gcal_tz)}
+ gevent["start"]={"dateTime": event.start.astimezone(gcal_tz).isoformat(),
+ "timeZone": str(gcal_tz)}
+ if event.text_body is not None and event.text_body.strip() != '':
+ gevent["description"] = event.text_body
+ if event.location is not None:
+ gevent["location"] = event.location
+ gevent["extendedProperties"]={"shared": {"ex_id": event.item_id}}
+ return gevent