mirror of
https://github.com/dtomlinson91/panaetius.git
synced 2025-12-22 04:55:44 +00:00
adding latest working
This commit is contained in:
@@ -17,6 +17,7 @@ class Config:
|
|||||||
else pathlib.Path.home() / ".config"
|
else pathlib.Path.home() / ".config"
|
||||||
)
|
)
|
||||||
self._missing_config = False
|
self._missing_config = False
|
||||||
|
self.logging_path: str | None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def config(self) -> dict[str, Any]:
|
def config(self) -> dict[str, Any]:
|
||||||
@@ -28,50 +29,41 @@ class Config:
|
|||||||
self._missing_config = True
|
self._missing_config = True
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def get_value(
|
def get_value(self, key: str, default: Any, coerce: bool = False) -> Any:
|
||||||
self, key: str, default: Any, mask: bool, coerce: bool = False
|
|
||||||
) -> Any:
|
|
||||||
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
|
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
|
||||||
|
|
||||||
if not self._missing_config:
|
if not self._missing_config:
|
||||||
# look in the config file
|
# 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
|
# no config file, look for env vars
|
||||||
return self._get_env_value(env_key, default, coerce)
|
return self._get_env_value(env_key, default, coerce)
|
||||||
|
|
||||||
def _get_config_value(
|
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:
|
) -> Any:
|
||||||
try:
|
try:
|
||||||
# look under top header
|
# look under top header
|
||||||
|
# REVIEW: could this be auto handled for a key of arbitrary length?
|
||||||
if len(key.split(".")) == 1:
|
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:
|
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):
|
except (KeyError, TypeError):
|
||||||
value = os.environ.get(env_key.replace("-", "_"))
|
value = os.environ.get(env_key.replace("-", "_"))
|
||||||
if value is None:
|
if value is None:
|
||||||
return self.__get_config_value_missing_key_value_is_none(default)
|
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
|
# 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)
|
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:
|
def __get_config_value_key_split_once(self, key: str) -> Any:
|
||||||
if mask:
|
name = key.lower()
|
||||||
# TODO: write mask logic here
|
return self.config[self.header_variable][name]
|
||||||
pass
|
|
||||||
else:
|
|
||||||
name = key.lower()
|
|
||||||
return self.config[self.header_variable][name]
|
|
||||||
|
|
||||||
def __get_config_value_key_split_twice(self, key: str, mask: bool) -> Any:
|
def __get_config_value_key_split_twice(self, key: str) -> Any:
|
||||||
if mask:
|
section, name = key.lower().split(".")
|
||||||
# TODO: write mask logic here
|
return self.config[self.header_variable][section][name]
|
||||||
pass
|
|
||||||
else:
|
|
||||||
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:
|
def __get_config_value_missing_key_value_is_none(self, default: Any) -> Any:
|
||||||
return self.__load_default_value(default)
|
return self.__load_default_value(default)
|
||||||
@@ -91,6 +83,7 @@ class Config:
|
|||||||
return self.__load_value(value, coerce)
|
return self.__load_value(value, coerce)
|
||||||
|
|
||||||
def __load_value(self, value: str, coerce: bool) -> Any:
|
def __load_value(self, value: str, coerce: bool) -> Any:
|
||||||
|
value = str(value).lower() if isinstance(value, bool) else value
|
||||||
return (
|
return (
|
||||||
toml.loads(f"value = {value}")["value"]
|
toml.loads(f"value = {value}")["value"]
|
||||||
if coerce
|
if coerce
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ def set_config(
|
|||||||
config_inst: Config,
|
config_inst: Config,
|
||||||
key: str,
|
key: str,
|
||||||
default: Any = None,
|
default: Any = None,
|
||||||
mask: bool = False,
|
|
||||||
coerce: bool = False,
|
coerce: bool = False,
|
||||||
):
|
):
|
||||||
config_var = key.lower().replace(".", "_")
|
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))
|
||||||
|
|||||||
@@ -1,2 +1,53 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
import logging
|
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
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ Coding:
|
|||||||
needs to be lower case in the toml, need a check for this
|
needs to be lower case in the toml, need a check for this
|
||||||
|
|
||||||
Logging:
|
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 simply have the different logging strings to output
|
||||||
should both specify whether to save to file or not
|
should both specify whether to save to file or not
|
||||||
|
☐ Logging path should take by default the config path unless overwritten?
|
||||||
|
|
||||||
Documentation:
|
Documentation:
|
||||||
☐ Rewrite documentation using `mkdocs` and using `.md`. @2h
|
☐ Rewrite documentation using `mkdocs` and using `.md`. @2h
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ if __name__ == "__main__":
|
|||||||
set_config(c, key="logging.path", default="some logging path")
|
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", default="some nonexistent item")
|
||||||
set_config(c, key="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="toml.points_config")
|
||||||
set_config(c, key="float", default=2.0)
|
set_config(c, key="float", default=2.0)
|
||||||
set_config(c, key="float_str", default="2.0")
|
set_config(c, key="float_str", default="2.0")
|
||||||
|
|||||||
Reference in New Issue
Block a user