adding latest tests

This commit is contained in:
2021-10-18 00:12:20 +01:00
parent 4ae4eb085c
commit f73a6d2441
17 changed files with 402 additions and 95 deletions

View File

@@ -1,10 +1,14 @@
from __future__ import annotations
import ast
import os
import pathlib
from typing import Any
import toml
from panaetius.exceptions import KeyErrorTooDeepException
class Config:
"""docstring for Config()."""
@@ -16,7 +20,7 @@ class Config:
if config_path
else pathlib.Path.home() / ".config"
)
self._missing_config = False
self._missing_config = self._check_config_file_exists()
# default logging options
self.logging_path: str | None = None
@@ -24,42 +28,52 @@ class Config:
self.logging_backup_count: int = 0
@property
def config(self) -> dict[str, Any]:
def config(self) -> dict:
config_file_location = self.config_path / self.header_variable / "config.toml"
try:
with open(config_file_location, "r", encoding="utf-8") as config_file:
return dict(toml.load(config_file))
except FileNotFoundError:
self._missing_config = True
return {}
def get_value(self, key: str, default: Any, coerce: bool = False) -> Any:
def get_value(self, key: str, default: Any) -> Any:
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
if not self._missing_config:
# look in the config file
return self._get_config_value(env_key, key, default, coerce)
return self._get_config_value(env_key, key, default)
# no config file, look for env vars
return self._get_env_value(env_key, default, coerce)
return self._get_env_value(env_key, default)
def _get_config_value(
self, env_key: str, key: str, default: Any, coerce: bool = False
) -> Any:
def _check_config_file_exists(self) -> bool:
config_file_location = self.config_path / self.header_variable / "config.toml"
try:
with open(config_file_location, "r", encoding="utf-8"):
return False
except FileNotFoundError:
return True
def _get_config_value(self, env_key: str, key: str, default: Any) -> Any:
try:
# look under top header
# REVIEW: could this be auto handled for a key of arbitrary length?
if len(key.split(".")) > 2:
raise KeyErrorTooDeepException(
f"Your key of {key} can only be 2 levels deep maximum. "
f"You have {len(key.split('.'))}"
)
if len(key.split(".")) == 1:
return self.__get_config_value_key_split_once(key)
if len(key.split(".")) == 2:
return self.__get_config_value_key_split_twice(key)
raise KeyError
raise KeyError()
except (KeyError, TypeError):
value = os.environ.get(env_key.replace("-", "_"))
if value is None:
return self.__get_config_value_missing_key_value_is_none(default)
# if env var, coerce value if flag is set, else return a TOML string
return self.__get_config_value_missing_key_value_is_not_none(value, coerce)
return self.__get_config_value_missing_key_value_is_not_none(value)
def __get_config_value_key_split_once(self, key: str) -> Any:
name = key.lower()
@@ -72,33 +86,18 @@ class Config:
def __get_config_value_missing_key_value_is_none(self, default: Any) -> Any:
return self.__load_default_value(default)
def __get_config_value_missing_key_value_is_not_none(
self, value: str, coerce: bool
) -> Any:
return self.__load_value(value, coerce)
def __get_config_value_missing_key_value_is_not_none(self, value: str) -> Any:
return self.__load_value(value)
def _get_env_value( # noqa
self, env_key: str, default: Any, coerce: bool = False
) -> Any:
def _get_env_value(self, env_key: str, default: Any) -> Any: # noqa
# look for an environment variable, fallback to default
value = os.environ.get(env_key.replace("-", "_"))
if value is None:
return self.__load_default_value(default)
return self.__load_value(value, coerce)
return self.__load_value(value)
def __load_value(self, value: str, coerce: bool) -> Any: # noqa
value = str(value).lower() if isinstance(value, bool) else value
return (
toml.loads(f"value = {value}")["value"]
if coerce
else toml.loads(f'value = "{value}"')["value"]
)
def __load_value(self, value: str) -> Any: # noqa
return ast.literal_eval(value)
def __load_default_value(self, default: Any) -> Any: # noqa
if isinstance(default, str):
return toml.loads(f'value = "{default}"')["value"]
# if default is bool convert to lower case toml syntax
default = str(default).lower() if isinstance(default, bool) else default
return (
toml.loads(f"value = {default}")["value"] if default is not None else None
)
return default

6
panaetius/exceptions.py Normal file
View File

@@ -0,0 +1,6 @@
class KeyErrorTooDeepException(Exception):
pass
class LoggingDirectoryDoesNotExistException(Exception):
pass

View File

@@ -9,7 +9,6 @@ def set_config(
config_inst: Config,
key: str,
default: Any = None,
coerce: bool = False,
):
config_var = key.lower().replace(".", "_")
setattr(config_inst, config_var, config_inst.get_value(key, default, coerce))
setattr(config_inst, config_var, config_inst.get_value(key, default))

View File

@@ -8,6 +8,7 @@ import sys
from panaetius import Config
from panaetius.library import set_config
from panaetius.exceptions import LoggingDirectoryDoesNotExistException
def set_logger(config_inst: Config, logging_format_inst: LoggingData) -> logging.Logger:
@@ -22,6 +23,9 @@ def set_logger(config_inst: Config, logging_format_inst: LoggingData) -> logging
/ f"{config_inst.header_variable}.log"
).expanduser()
if not logging_file.parents[0].exists():
raise LoggingDirectoryDoesNotExistException()
if config_inst.logging_rotate_bytes == 0:
set_config(config_inst, "logging.rotate_bytes", 512000)
if config_inst.logging_backup_count == 0: