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
+28
View File
@@ -0,0 +1,28 @@
from collections import defaultdict
from pathlib import Path
from typing import Optional
from ..entry import Entry, EntryDetector
from .util import PresetUtil
__all__ = ("PresetEntryDetector",)
class PresetEntryDetector(EntryDetector):
def __init__(self, filename: Path):
self._preset = PresetUtil.resolve_preset(filename)
def detect_entry(self, basepath: Path) -> Optional["Entry"]:
tasks = defaultdict(list)
for preset_name, preset in self._preset.presets.items():
filename = basepath / preset.filename
if filename.exists():
loader = PresetUtil.resolve_loader(preset.loader)
with open(filename, "rb") as fd:
data = loader(fd)
for task_name, task_pack in preset.tasks.items():
for task_item_name, task in task_pack.items():
if PresetUtil.check_conditions(data, task.conditions):
tasks[task_name] += task.commands
if tasks:
return Entry(tasks, basepath)