From 035d2b4bef1fd040f8b555c3b8743920f4c64ae1 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sat, 16 Oct 2021 16:36:15 +0100 Subject: [PATCH] adding latest working --- panaetius/config.py | 37 ++++++++++++---------------- panaetius/library.py | 3 +-- panaetius/logging.py | 53 ++++++++++++++++++++++++++++++++++++++++- rewrite.todo | 3 ++- tests/test_panaetius.py | 1 - 5 files changed, 70 insertions(+), 27 deletions(-) diff --git a/panaetius/config.py b/panaetius/config.py index 75152fb..e2a147d 100644 --- a/panaetius/config.py +++ b/panaetius/config.py @@ -17,6 +17,7 @@ class Config: else pathlib.Path.home() / ".config" ) self._missing_config = False + self.logging_path: str | None = None @property def config(self) -> dict[str, Any]: @@ -28,50 +29,41 @@ class Config: self._missing_config = True return {} - def get_value( - self, key: str, default: Any, mask: bool, coerce: bool = False - ) -> Any: + def get_value(self, key: str, default: Any, coerce: bool = False) -> 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, mask, coerce) + return self._get_config_value(env_key, key, default, coerce) # no config file, look for env vars return self._get_env_value(env_key, default, coerce) def _get_config_value( - self, env_key: str, key: str, default: Any, mask: bool, coerce: bool = False + self, env_key: str, key: str, default: Any, coerce: bool = False ) -> Any: try: # look under top header + # REVIEW: could this be auto handled for a key of arbitrary length? if len(key.split(".")) == 1: - return self.__get_config_value_key_split_once(key, mask) + return self.__get_config_value_key_split_once(key) if len(key.split(".")) == 2: - return self.__get_config_value_key_split_twice(key, mask) + return self.__get_config_value_key_split_twice(key) + 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 - value = self.__get_config_value_missing_key_value_is_not_none(value, coerce) return self.__get_config_value_missing_key_value_is_not_none(value, coerce) - def __get_config_value_key_split_once(self, key: str, mask: bool) -> Any: - if mask: - # TODO: write mask logic here - pass - else: - name = key.lower() - return self.config[self.header_variable][name] + def __get_config_value_key_split_once(self, key: str) -> Any: + name = key.lower() + return self.config[self.header_variable][name] - def __get_config_value_key_split_twice(self, key: str, mask: bool) -> Any: - if mask: - # TODO: write mask logic here - pass - else: - section, name = key.lower().split(".") - return self.config[self.header_variable][section][name] + def __get_config_value_key_split_twice(self, key: str) -> Any: + section, name = key.lower().split(".") + return self.config[self.header_variable][section][name] def __get_config_value_missing_key_value_is_none(self, default: Any) -> Any: return self.__load_default_value(default) @@ -91,6 +83,7 @@ class Config: return self.__load_value(value, coerce) def __load_value(self, value: str, coerce: bool) -> Any: + value = str(value).lower() if isinstance(value, bool) else value return ( toml.loads(f"value = {value}")["value"] if coerce diff --git a/panaetius/library.py b/panaetius/library.py index d01fc0f..2f76417 100644 --- a/panaetius/library.py +++ b/panaetius/library.py @@ -9,8 +9,7 @@ def set_config( config_inst: Config, key: str, default: Any = None, - mask: bool = False, coerce: bool = False, ): config_var = key.lower().replace(".", "_") - setattr(config_inst, config_var, config_inst.get_value(key, default, mask, coerce)) + setattr(config_inst, config_var, config_inst.get_value(key, default, coerce)) diff --git a/panaetius/logging.py b/panaetius/logging.py index f050abf..181fe78 100644 --- a/panaetius/logging.py +++ b/panaetius/logging.py @@ -1,2 +1,53 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod import logging -from logging.handlers import RotatingFileHandler +import pathlib +import sys + +from panaetius import Config + + +def set_logger(config_inst: Config, logging_format: Logger): + logger = logging.getLogger(config_inst.header_variable) + loghandler_sys = logging.StreamHandler(sys.stdout) + + # check if log path is set + if config_inst.logging_path is not None: + logging_file = pathlib.Path(config_inst.logging_path) + + +class Logger(ABC): + @property + @abstractmethod + def format(self): + pass + + +class SimpleLogger(Logger): + @property + def format(self): + return ( + '{\n\t"time": "%(asctime)s",\n\t"logging_level":' + '"%(levelname)s",\n\t"message": "%(message)s"\n}', + ) + + +class AdvancedLogger(Logger): + @property + def format(self): + return ( + '{\n\t"time": "%(asctime)s",\n\t"file_name": "%(filename)s",' + '\n\t"module": "%(module)s",\n\t"function":"%(funcName)s",\n\t' + '"line_number": "%(lineno)s",\n\t"logging_level":' + '"%(levelname)s",\n\t"message": "%(message)s"\n}', + ) + + +class CustomLogger(Logger): + @property + def format(self): + return self._format + + def __init__(self, logging_format: str): + self._format = logging_format diff --git a/rewrite.todo b/rewrite.todo index 6214ac2..69d7948 100644 --- a/rewrite.todo +++ b/rewrite.todo @@ -8,9 +8,10 @@ Coding: needs to be lower case in the toml, need a check for this Logging: - ☐ Create SimpleLogger, AdvancedLogger, CustomLogger classes + ✔ Create SimpleLogger, AdvancedLogger, CustomLogger classes @done(21-10-16 16:22) should simply have the different logging strings to output should both specify whether to save to file or not + ☐ Logging path should take by default the config path unless overwritten? Documentation: ☐ Rewrite documentation using `mkdocs` and using `.md`. @2h diff --git a/tests/test_panaetius.py b/tests/test_panaetius.py index 954bc31..42d5cb6 100644 --- a/tests/test_panaetius.py +++ b/tests/test_panaetius.py @@ -26,7 +26,6 @@ if __name__ == "__main__": set_config(c, key="logging.path", default="some logging path") set_config(c, key="nonexistent.item", default="some nonexistent item") set_config(c, key="nonexistent.item") - # FIXME: this isn't working after the refactor... set_config(c, key="toml.points_config") set_config(c, key="float", default=2.0) set_config(c, key="float_str", default="2.0")