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"
|
||||
)
|
||||
self._missing_config = False
|
||||
self.logging_path: str | None = None
|
||||
|
||||
@property
|
||||
def config(self) -> dict[str, Any]:
|
||||
@@ -28,48 +29,39 @@ 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:
|
||||
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:
|
||||
def __get_config_value_key_split_twice(self, key: str) -> Any:
|
||||
section, name = key.lower().split(".")
|
||||
return self.config[self.header_variable][section][name]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user