# 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:
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