diff --git a/dlccore/project/validation/baseline/__init__.py b/dlccore/project/validation/baseline/__init__.py new file mode 100644 index 0000000..5d7e99e --- /dev/null +++ b/dlccore/project/validation/baseline/__init__.py @@ -0,0 +1,24 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + +from .api import ( + iter_baseline_check_registrations, + run_baseline_checks, +) +from .base import ( + BaselineValidator, +) + +__all__ = [ + "BaselineValidator", + "iter_baseline_check_registrations", + "run_baseline_checks", +] diff --git a/dlccore/project/validation/baseline/api.py b/dlccore/project/validation/baseline/api.py new file mode 100644 index 0000000..7e22107 --- /dev/null +++ b/dlccore/project/validation/baseline/api.py @@ -0,0 +1,44 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + +from __future__ import annotations + +from dlccore.project.schemas.base.findings import Finding +from dlccore.project.schemas.snapshots import ProjectSnapshot +from dlccore.project.validation.context import ValidationContext + +from .base import BaselineCheckRegistration +from .checklist import BASELINE_CHECKS + + +def iter_baseline_check_registrations() -> tuple[BaselineCheckRegistration, ...]: + """Return built-in baseline checks in execution order.""" + + return BASELINE_CHECKS + + +def run_baseline_checks( + snapshot: ProjectSnapshot, + context: ValidationContext | None, +) -> tuple[Finding, ...]: + """Run all built-in baseline checks in execution order.""" + + findings: list[Finding] = [] + + for registration in BASELINE_CHECKS: + findings.extend( + registration.validator( + snapshot, + context, + ) + ) + + return tuple(findings) diff --git a/dlccore/project/validation/baseline/base.py b/dlccore/project/validation/baseline/base.py new file mode 100644 index 0000000..cd94055 --- /dev/null +++ b/dlccore/project/validation/baseline/base.py @@ -0,0 +1,36 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from dlccore.project.schemas.base.findings import Finding +from dlccore.project.schemas.snapshots import ProjectSnapshot +from dlccore.project.validation.context import ValidationContext + +BaselineValidator = Callable[ + [ + ProjectSnapshot, + ValidationContext | None, + ], + tuple[Finding, ...], +] + + +@dataclass(frozen=True) +class BaselineCheckRegistration: + """One automatic, lightweight project-level check.""" + + code: str + description: str + validator: BaselineValidator diff --git a/dlccore/project/validation/baseline/checklist.py b/dlccore/project/validation/baseline/checklist.py new file mode 100644 index 0000000..f9c4296 --- /dev/null +++ b/dlccore/project/validation/baseline/checklist.py @@ -0,0 +1,92 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + +from __future__ import annotations + +from dlccore.project.schemas.base.findings import Finding +from dlccore.project.schemas.snapshots import ProjectSnapshot +from dlccore.project.validation.baseline.base import ( + BaselineCheckRegistration, +) +from dlccore.project.validation.context import ValidationContext +from dlccore.project.validation.deeplabcut.project_path import ( + validate_project_path, +) +from dlccore.project.validation.structure import ( + validate_project_structure, +) + + +def _config_validation_already_applied( + snapshot: ProjectSnapshot, + context: ValidationContext | None, +) -> tuple[Finding, ...]: + """No-op empty validator: config is validated eagerly, before a snapshot exists. + + This registration exists only so ``get_project_manager_capabilities()`` + accurately advertises that config validation always happens. + + ``inspect_project`` reads and validates the config file via + ``validate_project_config`` before ``ProjectSnapshot.from_project`` runs, + so its findings are already present in ``snapshot.findings`` by the time + baseline checks run here. + """ + + del snapshot, context + + return () + + +def _validate_structure( + snapshot: ProjectSnapshot, + context: ValidationContext | None, +) -> tuple[Finding, ...]: + """Run baseline structural validation.""" + + del context + + return validate_project_structure( + snapshot.project, + project_root=snapshot.project_root, + ) + + +def _validate_config_project_path( + snapshot: ProjectSnapshot, + context: ValidationContext | None, +) -> tuple[Finding, ...]: + """Compare declared and inspected project paths when applicable.""" + + del snapshot + + if context is None: + return () + + return validate_project_path(context) + + +BASELINE_CHECKS = ( + BaselineCheckRegistration( + code="PROJECT_CONFIG_VALIDATION", + description=("Reads and validates config.yaml against the canonical schema."), + validator=_config_validation_already_applied, + ), + BaselineCheckRegistration( + code="PROJECT_STRUCTURE", + description=("Checks required-node presence, filesystem type, and readability."), + validator=_validate_structure, + ), + BaselineCheckRegistration( + code="PROJECT_CONFIG_PATH_RELATIONSHIP", + description=("Compares the project path declared by a valid config with the inspected project root."), + validator=_validate_config_project_path, + ), +) diff --git a/tests/project/validation/test_baseline.py b/tests/project/validation/test_baseline.py new file mode 100644 index 0000000..a44f9c8 --- /dev/null +++ b/tests/project/validation/test_baseline.py @@ -0,0 +1,28 @@ +# +# DeepLabCut Toolbox (deeplabcut.org) +# © A. & M.W. Mathis Labs +# https://github.com/DeepLabCut/dlccore +# +# Contributors: +# https://github.com/DeepLabCut/dlccore/graphs/contributors +# +# Licensed under GNU Lesser General Public License v3.0 +# + + +from dlccore.project.validation.baseline import ( + iter_baseline_check_registrations, +) + + +def test_baseline_check_registrations_are_well_formed() -> None: + registrations = iter_baseline_check_registrations() + codes = tuple(registration.code for registration in registrations) + + assert registrations + assert len(codes) == len(set(codes)) + + for registration in registrations: + assert registration.code + assert registration.description + assert callable(registration.validator)