From f69da9a9d0b090444cfa82561750e8e25ea93509 Mon Sep 17 00:00:00 2001 From: Andrew Grimberg Date: Tue, 10 Oct 2023 06:13:21 -0700 Subject: [PATCH 1/3] Refactor: Add annotations to schema validator Issue: RELENG-4933 Signed-off-by: Andrew Grimberg Change-Id: I1b13c1ad1ad825cf29718dd15c0cc56c8a4a55cc --- lftools/schema.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lftools/schema.py b/lftools/schema.py index e3d67b99..6b25fc2b 100644 --- a/lftools/schema.py +++ b/lftools/schema.py @@ -9,15 +9,16 @@ ############################################################################## """Verify YAML Schema.""" -from __future__ import print_function +from __future__ import annotations, print_function import logging +from typing import Dict import jsonschema import yaml -def check_schema_file(yamlfile, schemafile): +def check_schema_file(yamlfile: str, schemafile: str) -> None: """Verify YAML Schema. YAMLFILE: Release YAML file to be validated. @@ -25,17 +26,19 @@ def check_schema_file(yamlfile, schemafile): SCHEMAFILE: SCHEMA file to validate against. """ with open(yamlfile) as _: - yaml_file = yaml.safe_load(_) + yaml_file: Dict = yaml.safe_load(_) with open(schemafile) as _: - schema_file = yaml.safe_load(_) + schema_file: Dict = yaml.safe_load(_) # Load the schema - validation = jsonschema.Draft4Validator(schema_file, format_checker=jsonschema.FormatChecker()) + validation: jsonschema.Draft4Validator = jsonschema.Draft4Validator( + schema_file, format_checker=jsonschema.FormatChecker() + ) validation.iter_errors(yaml_file) # Look for errors - errors = 0 + errors: int = 0 for error in validation.iter_errors(yaml_file): errors += 1 logging.error(error) From bcb4bb79569ececb5f66df5da2f04320846cfdda Mon Sep 17 00:00:00 2001 From: Andrew Grimberg Date: Tue, 10 Oct 2023 06:23:15 -0700 Subject: [PATCH 2/3] Refactor: Add annotations to lftools.helpers Issue: RELENG-4933 Signed-off-by: Andrew Grimberg Change-Id: I28a94f82c6fdb04380cb2bc731ebc13811157ceb --- lftools/helpers.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lftools/helpers.py b/lftools/helpers.py index be7ff868..fa36dc7a 100644 --- a/lftools/helpers.py +++ b/lftools/helpers.py @@ -9,12 +9,13 @@ ############################################################################## """Nexus3 REST API interface.""" +from __future__ import annotations import random import string -def generate_password(length=12): - punctuation = "!#$%&()*+,-.:;<=>?@[]^_{|}~" - password_characters = string.ascii_letters + string.digits + punctuation +def generate_password(length: int = 12) -> str: + punctuation: str = "!#$%&()*+,-.:;<=>?@[]^_{|}~" + password_characters: str = string.ascii_letters + string.digits + punctuation return "".join(random.choice(password_characters) for _ in range(length)) From b9ad6a69090503246e910b68ba487685e1a18664 Mon Sep 17 00:00:00 2001 From: Andrew Grimberg Date: Tue, 10 Oct 2023 06:37:36 -0700 Subject: [PATCH 3/3] 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 --- lftools/config.py | 27 ++++++++++++++------------- lftools/oauth2_helper.py | 10 +++++----- 2 files changed, 19 insertions(+), 18 deletions(-) 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 @@ 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