This commit is contained in:
2026-04-02 13:37:35 +02:00
parent 4979b4f582
commit b05f6b8857
2 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import logging
import os
fmt = "%(asctime)s [%(levelname)s] %(module)s: %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
LOG_NIVEAUER = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL,
}
def opsaet_logging(log_fil: str, niveau: str = "info") -> None:
"""
Opsætter global logging til både fil og konsol.
niveau styres fra config (debug/info/warning/error/critical).
"""
log_niveau = LOG_NIVEAUER.get(niveau.lower(), logging.INFO)
os.makedirs(os.path.dirname(log_fil), exist_ok=True)
logging.basicConfig(
level=log_niveau,
format=fmt,
datefmt=datefmt,
handlers=[
logging.FileHandler(log_fil, encoding="utf-8"),
logging.StreamHandler(),
]
)
def hent_logger(navn: str) -> logging.Logger:
"""Returnerer en logger til det givne modul."""
return logging.getLogger(navn)

View File

@@ -0,0 +1,53 @@
import os
import re
from typing import Any
EMPTY_SENTINELS = (None, "", [], {})
def er_tom(v: Any) -> bool:
return v in EMPTY_SENTINELS
def evaluer_værdi_token(raw_value: str, global_config: dict) -> str:
vtype, vkey = parse_value_token(raw_value)
if vtype == "env":
return os.environ.get(vkey, "")
elif vtype == "prog":
return global_config.get(vkey, "")
else:
return vkey
def generer_filnavn(tekst: str, config: dict) -> str:
"""Erstatter dato-tokens i et filnavn med værdier fra config."""
return tekst.format(
yy=config["dato"].strftime('%y'),
yyyy=config["dato"].strftime('%Y'),
mm=config["dato"].strftime('%m'),
dd=config["dato"].strftime('%d')
)
def parse_value_token(value: Any) -> tuple[str, Any]:
"""
Parser en værdi fra YAML og returnerer (type, key/value)
type:
- 'env' → miljøvariabel ${VAR}
- 'prog' → programvariabel {var}
- 'const' → fast værdi (ingen placeholders)
key/value:
- For env/prog → selve navnet uden {} / ${}
- For const → hele strengen som den er
"""
if not isinstance(value, str):
return "const", value # ikke-strenge behandles som faste værdier
env_match = re.fullmatch(r"\$\{([^}]+)\}", value)
if env_match:
return "env", env_match.group(1)
prog_match = re.fullmatch(r"\{([^}]+)\}", value)
if prog_match:
return "prog", prog_match.group(1)
return "const", value