Skip to content

Commit

Permalink
Merge changes from topic "modernize_lftools"
Browse files Browse the repository at this point in the history
* changes:
  Refactor: Add annotations to lftools.config
  Refactor: Add annotations to lftools.helpers
  Refactor: Add annotations to schema validator
  • Loading branch information
tykeal authored and Gerrit Code Review committed Oct 13, 2023
2 parents c26314b + b9ad6a6 commit 54e5713
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 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
7 changes: 4 additions & 3 deletions lftools/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
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
15 changes: 9 additions & 6 deletions lftools/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,36 @@
##############################################################################
"""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.
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)
Expand Down

0 comments on commit 54e5713

Please sign in to comment.