From: Matthew Vernon Date: Fri, 5 Oct 2018 13:51:51 +0000 (+0100) Subject: Cache the auto-discovery values from Exchange X-Git-Tag: v0.1~28 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~matthewv/git?a=commitdiff_plain;h=70455f803bd1c8fa1829a49a6de936d42c90ad5a;p=gooswapper Cache the auto-discovery values from Exchange This saves us a bit of time. --- diff --git a/gooswapper.py b/gooswapper.py index 274cd13..d01d2b3 100644 --- a/gooswapper.py +++ b/gooswapper.py @@ -67,13 +67,38 @@ def get_ex_cred(username="SANGER\mv3",password=None): password = getpass.getpass(prompt="Password for user %s: " % username) return exchangelib.ServiceAccount(username,password) -def ex_login(emailaddr,autodiscover=True): +def ex_login(emailaddr,ad_cache_path=None): global exchange_credential + autodiscover = True if exchange_credential is None: exchange_credential = get_ex_cred() - return exchangelib.Account(emailaddr, - credentials = exchange_credential, - autodiscover = autodiscover) + if ad_cache_path is not None: + try: + with open(ad_cache_path,"rb") as f: + url,auth_type = pickle.load(f) + autodiscover = False + except FileNotFoundError: + pass + + if autodiscover: + ex_ac = exchangelib.Account(emailaddr, + credentials = exchange_credential, + autodiscover = autodiscover) + if ad_cache_path is not None: + cache=(ex_ac.protocol.service_endpoint, + ex_ac.protocol.auth_type) + with open(ad_cache_path,"wb") as f: + pickle.dump(cache,f) + else: + ex_conf = exchangelib.Configuration(service_endpoint=url, + credentials=exchange_credential, + auth_type=auth_type) + ex_ac = exchangelib.Account(emailaddr, + config=ex_conf, + autodiscover=False, + access_type=exchangelib.DELEGATE) + + return ex_ac def get_ex_events(calendar): ans={} @@ -166,7 +191,7 @@ def main(): except FileNotFoundError: cache = None - ex_account = ex_login("mv3@sanger.ac.uk") + ex_account = ex_login("mv3@sanger.ac.uk",".gooswapper_exch_conf.dat") current = get_ex_events(ex_account.calendar) gcal_account = gcal_login()