From: Andrew Grimberg Date: Tue, 10 Oct 2023 13:37:36 +0000 (-0700) Subject: Refactor: Add annotations to lftools.config X-Git-Tag: v0.37.7~2^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F39%2F72239%2F1;p=releng%2Flftools.git Refactor: Add annotations to lftools.config * Update lftools.oauth2_helper for new type hints out of config Issue: RELENG-4933 Signed-off-by: Andrew Grimberg Change-Id: I7659432c722b1074c41f801170bac1b5b2c6b5d6 --- diff --git a/lftools/config.py b/lftools/config.py index 96860498..370e872c 100644 --- a/lftools/config.py +++ b/lftools/config.py @@ -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: diff --git a/lftools/oauth2_helper.py b/lftools/oauth2_helper.py index ef73ecd6..858eca91 100644 --- a/lftools/oauth2_helper.py +++ b/lftools/oauth2_helper.py @@ -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