45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
from collections import defaultdict
|
|
from pathlib import Path
|
|
|
|
from ..common.template import PathTemplate
|
|
from ..entry.detector import EntryDetector
|
|
from ..entry.entry import Entry, TaskKey
|
|
from .loader import PresetLoader, PresetLoaderConfig
|
|
from .model import Variables
|
|
|
|
|
|
class PresetEntryDetector(EntryDetector):
|
|
|
|
def __init__(self, preset_loader_config: PresetLoaderConfig | None = None):
|
|
self._preset_loader_config = preset_loader_config
|
|
|
|
def detect_entry(self, basepath: Path) -> Entry | None:
|
|
template = PathTemplate(basepath)
|
|
preset = PresetLoader(basepath, self._preset_loader_config).load()
|
|
if preset.skip:
|
|
return None
|
|
tasks = defaultdict(list)
|
|
for tag, preset in preset.presets.items():
|
|
preset_variables: Variables = {"entry_path": basepath}
|
|
if preset.variables:
|
|
template.update_variables(preset.variables, preset_variables)
|
|
if preset.conditions and not template.check_conditions(
|
|
preset.conditions, preset_variables
|
|
):
|
|
continue
|
|
for task_name, task_pack in preset.tasks.items():
|
|
for task in task_pack.values():
|
|
if not task.conditions or template.check_conditions(
|
|
task.conditions, preset_variables
|
|
):
|
|
variables = preset_variables.copy()
|
|
if task.variables:
|
|
template.update_variables(task.variables, variables)
|
|
tasks[TaskKey(task_name, tag)] += [
|
|
template.fill_template(command, variables)
|
|
for command in task.commands
|
|
]
|
|
if tasks:
|
|
return Entry(tasks, basepath)
|
|
return None
|