import getpass
import os
import pickle
+import collections
import logging
logger = logging.getLogger('gooswapper')
logger.setLevel(logging.INFO)
exchange_credential = None
+CachedExEvent=collections.namedtuple('CachedExEvent',
+ ['changekey','gcal_link'])
+
class ex_gcal_link(exchangelib.ExtendedProperty):
distinguished_property_set_id = 'PublicStrings'
property_name = "google calendar event id"
def get_ex_events(calendar):
ans={}
for event in calendar.all().only('changekey','item_id','gcal_link'):
- if event.gcal_link is not None:
+# if event.gcal_link is not None:
# event.delete()
- continue
+# continue
if event.item_id in ans:
logger.warning("Event item_id %s was duplicated!" % event.item_id)
- ans[event.item_id] = event.changekey
+ ans[event.item_id] = CachedExEvent(event.changekey,event.gcal_link)
logger.info("%d events found" % len(ans))
return ans
changed = []
#intersection - i.e. common to both sets
for event in olds & news:
- if old[event] != new[event]:
+ if old[event].changekey != new[event].changekey:
changed.append(event)
logger.info("%d events updated, %d added, %d deleted" % (len(changed),
len(added),
gevent=gcal_acct.events().insert(calendarId=gcal_id, body=gevent).execute()
event.gcal_link = gevent.get("id")
event.save()
- events[event.item_id] = event.changekey
+ events[event.item_id] = events[event.item_id]._replace(changekey=event.changekey)
else:
logger.warning("only all-day events supported")
-
+
+def del_ex_to_gcal(ex_acct, gcal_acct, events, deleted, gcal_id="primary"):
+ for ev_id in deleted:
+ if events[ev_id].gcal_link is not None:
+ gcal_acct.events().delete(calendarId=gcal_id,
+ eventId=events[ev_id].gcal_link,
+ sendUpdates="none").execute()
+
def get_gcal_cred():
#each such file can only store a single credential
storage = oauth2client.file.Storage(gcal_authpath)
if cache is not None:
added,deleted,changed = ex_event_changes(cache,current)
add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,added)
+ #delete op needs the "cache" set, as that has the link ids in
+ #for events that are now deleted
+ del_ex_to_gcal(ex_account,gcal_account,cache,deleted)
with open(cachepath,"wb") as f:
pickle.dump(current,f)