Files
project-manager/project_manager/preset/detector.py
T
2026-08-10 13:36:12 +03:00

45 lines
1.8 KiB
Python

from collections import defaultdict
from pathlib import Path
from ..common.template import PathTemplate
from ..entry.detector import ENTRY_NONE, ENTRY_SKIP, 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:
template = PathTemplate(basepath)
preset = PresetLoader(basepath, self._preset_loader_config).load()
if preset.skip:
return ENTRY_SKIP
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 ENTRY_NONE