Skip to content

Commit

Permalink
Merge pull request #7 from ShadowXBoss696/fix-method-complexity-issue…
Browse files Browse the repository at this point in the history
…-in-logging

Fixed method complexity in configure_logging() method
  • Loading branch information
ShadowXBoss696 authored Mar 22, 2024
2 parents c483d20 + 0ba2d0a commit 3638ff7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repos:
- id: poetry-lock

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.3
rev: v0.3.4
hooks:
- id: ruff

Expand Down
87 changes: 63 additions & 24 deletions fastboot/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from logging.config import dictConfig, fileConfig
from os import path
from typing import Any

# Define default logging configuration
Expand Down Expand Up @@ -26,27 +27,65 @@ def configure_logging(config: dict[str, Any] | str | None = None) -> None:
dictConfig(DEFAULT_LOGGING_CONFIG)

# load the given config
if config: # pragma: no cover
if isinstance(config, dict):
# Read the logging configuration from a dictionary.
dictConfig(config)

elif config.endswith(".json"):
# Read the logging configuration from a JSON file.
import json

with open(config) as f:
loaded_config: dict[str, Any] = json.load(f)
dictConfig(loaded_config)

elif config.endswith((".yaml", ".yml")):
# Read the logging configuration from a YAML file.
import yaml

with open(config) as f:
loaded_config: dict[str, Any] = yaml.safe_load(f)
dictConfig(loaded_config)

else:
# Read the logging configuration from a ConfigParser-format file.
fileConfig(config, disable_existing_loggers=False)
if not config:
return # no-op

elif isinstance(config, dict):
# Read the logging configuration from a dictionary.
dictConfig(config)

elif config.endswith(".json"):
# Read the logging configuration from a JSON file.
jsonConfig(config)

elif config.endswith((".yaml", ".yml")):
# Read the logging configuration from a YAML file.
yamlConfig(config)

else:
# Read the logging configuration from a ConfigParser-format file.
fileConfig(config, disable_existing_loggers=False)


# noinspection PyPep8Naming
def jsonConfig(fname: str) -> None:
"""Reads the logging configuration from a JSON file"""
import json

# Validate config file
if not path.exists(fname):
raise FileNotFoundError(f"{fname} doesn't exist")
elif not path.getsize(fname):
raise RuntimeError(f"{fname} is an empty file")

# Read configuration
try:
with open(fname) as f:
loaded_config: dict[str, Any] = json.load(f)
except ValueError as e:
raise RuntimeError(f"{fname} is invalid: {e}") from e

# Apply configuration
dictConfig(loaded_config)


# noinspection PyPep8Naming
def yamlConfig(fname: str) -> None:
"""Reads the logging configuration from a YAML file"""
import yaml

# Validate config file
if not path.exists(fname):
raise FileNotFoundError(f"{fname} doesn't exist")
elif not path.getsize(fname):
raise RuntimeError(f"{fname} is an empty file")

# Read configuration
try:
with open(fname) as f:
loaded_config: dict[str, Any] = yaml.safe_load(f)
except ValueError as e:
raise RuntimeError(f"{fname} is invalid: {e}") from e

# Apply configuration
dictConfig(loaded_config)
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ select = [
ignore = [
"E501", # LineTooLong
"E731", # DoNotAssignLambda
"TRY003", # Avoid specifying long messages outside the exception class
]

[tool.ruff.lint.per-file-ignores]
Expand Down

0 comments on commit 3638ff7

Please sign in to comment.