#!/usr/bin/env python3
+# Copyright (C) 2018 Genome Research Limited
+#
+# Author: Matthew Vernon <mv3@sanger.ac.uk>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
import sys
import getpass
import os
+import os.path
import pickle
import collections
import argparse
+import time
import logging
logger = logging.getLogger('gooswapper')
logger.setLevel(logging.INFO)
gcal_client_secret,
scope)
+gsdir = os.path.expanduser("~/.gooswapper")
+gcal_authpath = gsdir + "/.gooswap_gcal_creds.dat"
-gcal_authpath=".gooswap_gcal_creds.dat"
-
-cachepath=".gooswapcache"
+cachepath=None
exchange_credential = None
else:
gcal_id = args.gcalid
+ #Make our config dir if it doesn't exist
+ if not os.path.exists(gsdir):
+ os.mkdir(gsdir,0o700)
+ #Cache file is specific to the Exchange calendar
+ global cachepath
+ cachepath = gsdir + "/.cache-%s" % \
+ (args.exchemail.replace('@','_').replace('/','_'))
+
#log in to the accounts
ex_account = ex_login(args.exchuser,args.exchemail,
- ".gooswapper_exch_conf.dat")
+ gsdir+"/.gooswapper_exch_conf.dat")
gcal_account = gcal_login(args)
gcal_tz = get_gcal_timezone(gcal_account,gcal_id)
-
- try:
- with open(cachepath,"rb") as f:
- cache = pickle.load(f)
- except FileNotFoundError:
- cache = None
-
- current = get_ex_events(ex_account.calendar)
-
- 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,gcal_id)
- #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,gcal_id)
- update_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,
- changed,gcal_id)
- else:
- toadd = match_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,
- gcal_id)
- add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,toadd,gcal_id)
+ #Main loop (broken at the end if login is false)
+ while True:
+ try:
+ with open(cachepath,"rb") as f:
+ cache = pickle.load(f)
+ except FileNotFoundError:
+ cache = None
+
+ current = get_ex_events(ex_account.calendar)
+
+ 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,gcal_id)
+ #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,gcal_id)
+ update_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,
+ changed,gcal_id)
+ else:
+ toadd = match_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,
+ gcal_id)
+ add_ex_to_gcal(ex_account,gcal_account,gcal_tz,current,
+ toadd,gcal_id)
- with open(cachepath,"wb") as f:
- pickle.dump(current,f)
+ with open(cachepath,"wb") as f:
+ pickle.dump(current,f)
+
+ #If not looping, break here (after 1 run)
+ if args.loop==False:
+ break
+ #otherwise, wait 10 minutes, then go round again
+ time.sleep(600)
if __name__ == "__main__":
main()