Add logger

This commit is contained in:
CPD 2025-03-10 12:04:27 +01:00
parent 068ea6022f
commit e071906d58

View File

@ -1,6 +1,8 @@
from os import environ, makedirs, path
import yaml
import logging
log = logging.getLogger(__name__)
class ConfigFile:
"""
Class managing a yaml config file.
@ -15,6 +17,7 @@ class ConfigFile:
self.values = init_values
self.filepath = filepath
if path.isfile(self.filepath):
log.debug(f"[{self.filepath}] loading from file")
with open(self.filepath, "r") as file:
self.values |= yaml.safe_load(file)
@ -23,6 +26,7 @@ class ConfigFile:
directory = path.dirname(self.filepath)
if not path.isdir(directory):
makedirs(directory)
log.debug(f"[{self.filepath}] saving to file")
with open(self.filepath, "w") as file:
yaml.dump(self.values, file)
@ -35,10 +39,12 @@ class ConfigFile:
raise KeyError(f"Key '{name}' not found in config file '{self.filepath}'")
def set(self, name: str, value):
log.debug(f"[{self.filepath}] set {name} = {value}")
self.values[name] = value
def get_values(self):
return self.values.copy()
def set_values(self, values):
log.debug(f"[{self.filepath}] set values = {values}")
self.values = values