Refactor: Add annotations to lftools.config 39/72239/1
authorAndrew Grimberg <agrimberg@linuxfoundation.org>
Tue, 10 Oct 2023 13:37:36 +0000 (06:37 -0700)
committerAndrew Grimberg <agrimberg@linuxfoundation.org>
Tue, 10 Oct 2023 13:37:36 +0000 (06:37 -0700)
* Update lftools.oauth2_helper for new type hints out of config

Issue: RELENG-4933
Signed-off-by: Andrew Grimberg <agrimberg@linuxfoundation.org>
Change-Id: I7659432c722b1074c41f801170bac1b5b2c6b5d6

lftools/config.py
lftools/oauth2_helper.py

index 9686049..370e872 100644 (file)
@@ -9,54 +9,55 @@
 # http://www.eclipse.org/legal/epl-v10.html
 ##############################################################################
 """Configuration subsystem for lftools."""
+from __future__ import annotations
 
-
-import configparser
 import logging
 import os.path
 import sys
+from configparser import ConfigParser, NoOptionError, NoSectionError
+from typing import List, Optional
 
 from xdg import XDG_CONFIG_HOME
 
-log = logging.getLogger(__name__)
+log: logging.Logger = logging.getLogger(__name__)
 
-LFTOOLS_CONFIG_FILE = os.path.join(XDG_CONFIG_HOME, "lftools", "lftools.ini")
+LFTOOLS_CONFIG_FILE: str = os.path.join(XDG_CONFIG_HOME, "lftools", "lftools.ini")
 
 
-def get_config():
+def get_config() -> ConfigParser:
     """Get the config object."""
-    config = configparser.ConfigParser()  # noqa
+    config: ConfigParser = ConfigParser()  # noqa
     config.read(LFTOOLS_CONFIG_FILE)
     return config
 
 
-def has_section(section):
+def has_section(section: str) -> bool:
     """Get a configuration from a section."""
     config = get_config()
     return config.has_section(section)
 
 
-def get_setting(section, option=None):
+def get_setting(section: str, option: Optional[str] = None) -> str | List[str]:
     """Get a configuration from a section."""
     sys.tracebacklimit = 0
-    config = get_config()
+    config: ConfigParser = get_config()
 
     if option:
         try:
             return config.get(section, option)
-        except (configparser.NoOptionError, configparser.NoSectionError) as e:
+        except (NoOptionError, NoSectionError) as e:
             raise e
 
     else:
         try:
             return config.options(section)
-        except configparser.NoSectionError as e:
+        except NoSectionError as e:
             raise e
 
 
-def set_setting(section, option, value):
+def set_setting(section: str, option: str, value: str) -> None:
     """Save a configuration setting to config file."""
-    config = get_config()
+    config: ConfigParser = get_config()
     config.set(section, option, value)
 
     with open(LFTOOLS_CONFIG_FILE, "w") as configfile:
index ef73ecd..858eca9 100644 (file)
@@ -22,11 +22,11 @@ from lftools import config
 def oauth_helper() -> Tuple[str, str]:
     """Helper script to get access_token for lfid api."""
     logging.getLogger("oauth2client").setLevel(logging.ERROR)
-    client_id: str = config.get_setting("lfid", "clientid")
-    client_secret: str = config.get_setting("lfid", "client_secret")
-    refresh_token: str = config.get_setting("lfid", "refresh_token")
-    token_uri: str = config.get_setting("lfid", "token_uri")
-    url: str = config.get_setting("lfid", "url")
+    client_id: str = str(config.get_setting("lfid", "clientid"))
+    client_secret: str = str(config.get_setting("lfid", "client_secret"))
+    refresh_token: str = str(config.get_setting("lfid", "refresh_token"))
+    token_uri: str = str(config.get_setting("lfid", "token_uri"))
+    url: str = str(config.get_setting("lfid", "url"))
 
     credentials: client.Oauth2Credentials = client.OAuth2Credentials(
         access_token=None,  # set access_token to None since we use a refresh token