feat: add presets

This commit is contained in:
2023-03-16 15:07:11 +03:00
parent 23ca8c0cac
commit 8b718fda79
15 changed files with 357 additions and 142 deletions
+45
View File
@@ -0,0 +1,45 @@
__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)