Set auth type in config file
authorJakob <jakob@jcornell.net>
Fri, 6 Dec 2019 20:22:30 +0000 (14:22 -0600)
committerJakob <jakob@jcornell.net>
Fri, 6 Dec 2019 20:22:30 +0000 (14:22 -0600)
main.py

diff --git a/main.py b/main.py
index 171e38f87a27625e2caecb3051f028db2b5a6159..37651632841ba679c7be3412ec31fda4598e1eb2 100644 (file)
--- 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()