From 5c1102a744e27538c4f95448d35a788e89741cfe Mon Sep 17 00:00:00 2001 From: Jakob Date: Fri, 6 Dec 2019 14:22:30 -0600 Subject: [PATCH] Set auth type in config file --- main.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 171e38f..3765163 100644 --- a/main.py +++ b/main.py @@ -34,6 +34,12 @@ Example configuration file (Windows): [config] base_path: C:\Users\Jakob\blackboard\math_course + auth_type: cookie + +Schema: + + base_path: string + auth_type: string, "cookie" or "oauth" """ @@ -48,6 +54,10 @@ class Cli: from getpass import getpass return getpass("Blackboard password: ") + @staticmethod + def await_ack(): + input("Press Enter to continue: ") + @contextmanager def deleting(path): @@ -61,13 +71,24 @@ with open('config.ini') as f: cfg_section = cfg_parser['config'] with StorageManager(Path('auth_cache')) as storage_mgr: - auth_handler = auth.CookieAuthHandler( - storage_mgr, - BB_ROOT, - Cli, - auth.StorageMgrCookieJar(storage_mgr), - ) - api_iface = api.ApiInterface(BB_ROOT, auth_handler) + ui = Cli + + auth_type = cfg_section['auth_type'] + if auth_type == 'cookie': + auth_handler = auth.CookieAuthHandler( + storage_mgr, + BB_ROOT, + ui, + auth.StorageMgrCookieJar(storage_mgr), + ) + api_iface = api.ApiInterface(BB_ROOT, auth_handler) + elif auth_type == 'oauth': + # TODO dependency loop + auth_handler = auth.OauthHandler(storage_mgr, None) + api_iface = api.ApiInterface(BB_ROOT, auth_handler) + auth_handler.api_iface = api_iface + else: + raise ValueError("Invalid auth_type in config") url = input("Blackboard URL: ") params = urllib.parse.parse_qs(urllib.parse.urlparse(url).query) @@ -121,7 +142,7 @@ with StorageManager(Path('auth_cache')) as storage_mgr: log('info', "Checking attachment {id}: \"{fileName}\"".format(**attachment_doc), indent = 1) # On Windows, can't reopen the temp file while it's open. - # This is needed by `filecmp', so we can't use `tempfile''s auto deletion. + # Reopen is needed by `filecmp', so we can't use `tempfile''s auto deletion. with tempfile.NamedTemporaryFile(delete = False) as tmp: tmp_path = Path(tmp.name) with api_iface.download_attachment(course_id, child_doc['id'], att_id) as resp: @@ -177,5 +198,6 @@ with StorageManager(Path('auth_cache')) as storage_mgr: ) for path_str in sorted(map(str, result.paths)): log('error', path_str, indent = 3) + ui.await_ack() else: raise AssertionError() -- 2.30.2