Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Feb 29, 2024
1 parent 5139e5d commit 1680068
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Initialisation of the application."""

import locale
import logging
import sys
import time
import typing as t
from os import environ, getenv, path
from os import environ, getenv
from pathlib import Path

import yaml

Expand All @@ -15,17 +19,17 @@

# LOGGING CONFIGURATION
config = {}
CONFIG_PATH = path.join(APPLICATION_PATH_DATA, "config.yaml")
if path.exists(CONFIG_PATH):
with open(CONFIG_PATH) as file:
config = yaml.load(file, Loader=yaml.FullLoader)
CONFIG_PATH = Path(APPLICATION_PATH_DATA) / "config.yaml"
if Path(CONFIG_PATH).exists():
with Path(CONFIG_PATH).open() as file:
config = yaml.safe_load(file)

if "DEBUG" in environ and str2bool(getenv("DEBUG")):
logging_level = logging.DEBUG
else:
logging_level = logging.INFO

if "log2file" in config and config["log2file"]:
if config.get("log2file"):
logging.basicConfig(
filename=f"{APPLICATION_PATH_LOG}/myelectricaldata.log",
format=LOG_FORMAT,
Expand All @@ -40,12 +44,14 @@
else:
logging.basicConfig(format=LOG_FORMAT, datefmt=LOG_FORMAT_DATE, level=logging_level)

if not path.exists(CONFIG_PATH):
if not Path(CONFIG_PATH).exists():
logging.critical(f"Config file is not found ({CONFIG_PATH})")
exit()
sys.exit()


class EndpointFilter(logging.Filter):
"""Filter class for filtering log records based on the path."""

def __init__(
self,
path: str,
Expand All @@ -56,6 +62,7 @@ def __init__(
self._path = path

def filter(self, record: logging.LogRecord) -> bool:
"""Filter log records based on the path."""
return record.getMessage().find(self._path) == -1


Expand Down

0 comments on commit 1680068

Please sign in to comment.