__all__ = ("PresetUtil",) import importlib from pathlib import Path from typing import Any, Dict import tomli from .model import Preset class PresetUtil: @classmethod def resolve_loader(cls, loader: str) -> Any: module_name, item_name = loader.split(":") module = importlib.import_module(module_name) return getattr(module, item_name) @classmethod def get_key_path_value(cls, key_path: str, data: dict): path = key_path.split(".") value = data.get(path[0]) for key in path[1:]: if value: value = value.get(key) return value @classmethod def match_condition_value(cls, value: Any, pattern: str) -> bool: if pattern == "*": return bool(value) return value == pattern @classmethod def check_conditions(cls, data: dict, conditions: Dict[str, str]) -> bool: for key, value in conditions.items(): if not cls.match_condition_value(cls.get_key_path_value(key, data), value): return False return True @classmethod def resolve_preset(cls, filename: Path) -> Preset: with open(filename, "rb") as fd: preset_data = tomli.load(fd) return Preset(**preset_data)