# SPDX-FileCopyrightText: Copyright (C) Shaun Wilson
# SPDX-License-Identifier: MIT

import logging


class _ConfigFilterSettings:

    def __init__(self, source:str, settings:dict[str,str]):
        self.source = source
        self.level = getattr(logging, settings.get("level", "DEBUG").upper())


class ConfigFilter(logging.Filter):

    def __init__(self, config:dict[str,dict[str,str]]):
        self.__settings = [_ConfigFilterSettings(k, v) for k,v in config.items()]
        super().__init__()

    def filter(self, record:logging.LogRecord) -> bool:
        for e in self.__settings:
            if record.name == e.source:
                if record.levelno < e.level:
                    return False
        return True
