Skip to content

Commit

Permalink
Refactor: Add annotations to lftools.config
Browse files Browse the repository at this point in the history
* Update lftools.oauth2_helper for new type hints out of config

Issue: RELENG-4933
Signed-off-by: Andrew Grimberg <[email protected]>
Change-Id: I7659432c722b1074c41f801170bac1b5b2c6b5d6
  • Loading branch information
tykeal committed Oct 10, 2023
1 parent bcb4bb7 commit b9ad6a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 14 additions & 13 deletions lftools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions lftools/oauth2_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
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
Expand Down

0 comments on commit b9ad6a6

Please sign in to comment.