From 7dd574304011679a8333e14e8a32624c6981d79b Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Wed, 25 Nov 2026 17:15:08 +0100 Subject: [PATCH 1/3] Copy project layout nodes --- .../schemas/project_layout/__init__.py | 0 .../schemas/project_layout/labeled_data.py | 97 ++++++++++ .../project/schemas/project_layout/project.py | 39 ++++ .../project/schemas/project_layout/videos.py | 28 +++ tests/project/schemas/test_layout_suffixes.py | 21 ++ tests/project/schemas/test_layout_walk.py | 179 ++++++++++++++++++ 6 files changed, 364 insertions(+) create mode 100644 dlccore/project/schemas/project_layout/__init__.py create mode 100644 dlccore/project/schemas/project_layout/labeled_data.py create mode 100644 dlccore/project/schemas/project_layout/project.py create mode 100644 dlccore/project/schemas/project_layout/videos.py create mode 100644 tests/project/schemas/test_layout_suffixes.py create mode 100644 tests/project/schemas/test_layout_walk.py diff --git a/dlccore/project/schemas/project_layout/__init__.py b/dlccore/project/schemas/project_layout/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dlccore/project/schemas/project_layout/labeled_data.py b/dlccore/project/schemas/project_layout/labeled_data.py new file mode 100644 index 0000000..c3b8f97 --- /dev/null +++ b/dlccore/project/schemas/project_layout/labeled_data.py @@ -0,0 +1,97 @@ +# schemas/labeled_data.py + +from dlc_octopus.io.suffixes import H5_SUFFIX, HUMAN_LABELS_PREFIX, LABELED_DATA_TABLE_SUFFIXES +from dlc_octopus.schemas.base.filesystem import ( + DirectorySchema, + FileSchema, + NamePattern, + RepeatedNodeSchema, +) +from dlc_octopus.schemas.base.resources import ( + DirectoryResourceKind, + FileResourceKind, + ResourceManagement, +) + +# FIXME: @C-Achard review valid file types +LABELED_FRAME = FileSchema( + schema_id="dlc.labeled_data.frame", + kind=FileResourceKind.LABELED_FRAME, + name=NamePattern( + suffixes=frozenset( + { + ".png", + ".jpg", + ".jpeg", + ".bmp", + ".tif", + ".tiff", + } + ) + ), + description="An extracted frame available for labeling.", +) +HUMAN_LABELS = FileSchema( + schema_id="dlc.labeled_data.human_labels", + kind=FileResourceKind.HUMAN_LABELS, + name=NamePattern( + prefix=HUMAN_LABELS_PREFIX, + suffixes=LABELED_DATA_TABLE_SUFFIXES, + ), + description="Human keypoint labels for a labeled dataset.", +) + +MACHINE_LABELS = FileSchema( + schema_id="dlc.labeled_data.machine_labels", + kind=FileResourceKind.MACHINE_LABELS, + name=NamePattern( + prefix="machinelabels-iter", + suffixes=frozenset({H5_SUFFIX}), + ), + description="Machine keypoint labels for a labeled dataset, per training iteration.", +) + +MACHINE_LABELS_CSV = FileSchema( + schema_id="dlc.labeled_data.machine_labels_csv", + kind=FileResourceKind.MACHINE_LABELS, + name=NamePattern(exact="machinelabels.csv"), + description="Machine keypoint labels as CSV, written beside the latest iteration's H5.", +) + +NAPARI_DLC_METADATA = FileSchema( + schema_id="dlc.labeled_data.napari_metadata", + kind=FileResourceKind.NAPARI_DLC_METADATA, + name=NamePattern( + exact=".napari-deeplabcut.json", + ), + required=False, + description="JSON metadata for the napari DeepLabCut plugin.", + management=ResourceManagement.KNOWN_UNMANAGED, +) + + +# ``name`` is deliberately unconstrained: a directory here cannot be told apart from +# DeepLabCut ``check_labels`` plot output, or from a dataset not labeled yet, by structure +# alone. Discovery declines to guess; the judgement belongs to validation. +LABELED_DATASET = DirectorySchema( + schema_id="dlc.labeled_data.dataset", + kind=DirectoryResourceKind.LABELED_DATASET, + name=NamePattern(), + description=("A labeled dataset associated with one source video or another config-defined dataset identity."), + children=(NAPARI_DLC_METADATA,), + repeated_children=( + RepeatedNodeSchema(child=LABELED_FRAME), + RepeatedNodeSchema(child=HUMAN_LABELS), + RepeatedNodeSchema(child=MACHINE_LABELS), + RepeatedNodeSchema(child=MACHINE_LABELS_CSV), + ), +) + + +LABELED_DATA = DirectorySchema( + schema_id="dlc.labeled_data", + kind=DirectoryResourceKind.LABELED_DATA_DIRECTORY, + name=NamePattern(exact="labeled-data"), + description="Root directory containing labeled datasets.", + repeated_children=(RepeatedNodeSchema(child=LABELED_DATASET),), +) diff --git a/dlccore/project/schemas/project_layout/project.py b/dlccore/project/schemas/project_layout/project.py new file mode 100644 index 0000000..2ea6556 --- /dev/null +++ b/dlccore/project/schemas/project_layout/project.py @@ -0,0 +1,39 @@ +# schemas/project.py + +from dlc_octopus.schemas.base.filesystem import ( + DirectorySchema, + FileSchema, + NamePattern, + index_layout, +) +from dlc_octopus.schemas.base.resources import DirectoryResourceKind, FileResourceKind +from dlc_octopus.schemas.project_layout.labeled_data import LABELED_DATA +from dlc_octopus.schemas.project_layout.videos import VIDEOS + +PROJECT_CONFIG = FileSchema( + schema_id="dlc.project.config", + kind=FileResourceKind.PROJECT_CONFIG, + name=NamePattern(exact="config.yaml"), + required=True, + description="The authoritative DeepLabCut project configuration.", +) + + +PROJECT = DirectorySchema( + schema_id="dlc.project", + kind=DirectoryResourceKind.PROJECT_ROOT, + name=NamePattern(), + required=True, + description="A canonical DeepLabCut project root.", + children=( + # Config file + PROJECT_CONFIG, + ## Input and labeled data + VIDEOS, + LABELED_DATA, + ## Training datasets + ### Extend later... + ), +) + +PROJECT_INDEX = index_layout(PROJECT) diff --git a/dlccore/project/schemas/project_layout/videos.py b/dlccore/project/schemas/project_layout/videos.py new file mode 100644 index 0000000..78d9a70 --- /dev/null +++ b/dlccore/project/schemas/project_layout/videos.py @@ -0,0 +1,28 @@ +# dlc_octopus/schemas/project_layout/videos.py + +from dlc_octopus.io.suffixes import VIDEO_SUFFIXES +from dlc_octopus.schemas.base.filesystem import ( + DirectorySchema, + FileSchema, + NamePattern, + RepeatedNodeSchema, +) +from dlc_octopus.schemas.base.resources import DirectoryResourceKind, FileResourceKind + +SOURCE_VIDEO = FileSchema( + schema_id="dlc.videos.source_video", + kind=FileResourceKind.SOURCE_VIDEO, + name=NamePattern( + suffixes=VIDEO_SUFFIXES, + ), + description="A source video registered with the project.", +) + + +VIDEOS = DirectorySchema( + schema_id="dlc.videos", + kind=DirectoryResourceKind.VIDEOS_DIRECTORY, + name=NamePattern(exact="videos"), + description="Root directory for project source videos.", + repeated_children=(RepeatedNodeSchema(child=SOURCE_VIDEO),), +) diff --git a/tests/project/schemas/test_layout_suffixes.py b/tests/project/schemas/test_layout_suffixes.py new file mode 100644 index 0000000..9f888ff --- /dev/null +++ b/tests/project/schemas/test_layout_suffixes.py @@ -0,0 +1,21 @@ +from pathlib import Path + +import pytest + +from dlc_octopus.io.suffixes import LABELED_DATA_TABLE_SUFFIXES, VIDEO_SUFFIXES +from dlc_octopus.schemas.base.filesystem import FileSchema +from dlc_octopus.schemas.project_layout.labeled_data import HUMAN_LABELS, MACHINE_LABELS +from dlc_octopus.schemas.project_layout.videos import SOURCE_VIDEO + + +@pytest.mark.parametrize( + ("schema", "suffixes"), + [(HUMAN_LABELS, LABELED_DATA_TABLE_SUFFIXES), (MACHINE_LABELS, {".h5"}), (SOURCE_VIDEO, VIDEO_SUFFIXES)], +) +def test_the_layout_claims_the_suffixes_io_reads(schema: FileSchema, suffixes: frozenset[str]) -> None: + assert schema.name.suffixes == suffixes + + +@pytest.mark.parametrize("name", ["session.flv", "session.MPV", "session.qt", "session.yuv"]) +def test_every_video_deeplabcut_reads_is_claimed_as_a_video(name: str) -> None: + assert SOURCE_VIDEO.name.matches(Path(name)) diff --git a/tests/project/schemas/test_layout_walk.py b/tests/project/schemas/test_layout_walk.py new file mode 100644 index 0000000..85edd16 --- /dev/null +++ b/tests/project/schemas/test_layout_walk.py @@ -0,0 +1,179 @@ +from __future__ import annotations + +import pytest +from pydantic import ValidationError + +from dlc_octopus.schemas.base.filesystem import ( + DirectorySchema, + FileSchema, + NamePattern, + RepeatedNodeSchema, + child_schemas, + index_layout, + walk_schema, +) +from dlc_octopus.schemas.base.resources import DirectoryResourceKind, FileResourceKind, ResourceManagement +from dlc_octopus.schemas.project_layout.labeled_data import LABELED_FRAME +from dlc_octopus.schemas.project_layout.project import PROJECT + + +def test_the_walk_flags_exactly_the_repeated_children() -> None: + flags = {schema.schema_id: repeated for schema, repeated in walk_schema(PROJECT)} + + assert flags["dlc.project"] is False + assert flags["dlc.labeled_data"] is False + assert flags["dlc.labeled_data.dataset"] is True + assert flags["dlc.labeled_data.napari_metadata"] is False + assert flags["dlc.videos.source_video"] is True + + +def test_the_walk_visits_a_parent_before_its_children() -> None: + order = [schema.schema_id for schema, _ in walk_schema(PROJECT)] + + assert order[0] == "dlc.project" + assert order.index("dlc.labeled_data") < order.index("dlc.labeled_data.dataset") + assert order.index("dlc.labeled_data.dataset") < order.index("dlc.labeled_data.frame") + + +def test_a_file_has_no_child_schemas() -> None: + assert child_schemas(LABELED_FRAME) == () + + +def test_the_project_layout_indexes_every_node_once() -> None: + assert list(index_layout(PROJECT).schemas) == [schema.schema_id for schema, _ in walk_schema(PROJECT)] + + +def test_the_layout_index_records_each_parent() -> None: + parents = index_layout(PROJECT).parents + + assert parents["dlc.project"] is None + assert parents["dlc.project.config"] == "dlc.project" + assert parents["dlc.videos.source_video"] == "dlc.videos" + assert parents["dlc.labeled_data.napari_metadata"] == "dlc.labeled_data.dataset" + assert parents["dlc.labeled_data.frame"] == "dlc.labeled_data.dataset" + + +def test_the_layout_index_flags_every_node_inside_a_repeated_node() -> None: + # The sidecar is a plain child, but of a repeated dataset, so it lies inside one. + assert index_layout(PROJECT).inside_repeated == { + "dlc.videos.source_video", + "dlc.labeled_data.dataset", + "dlc.labeled_data.napari_metadata", + "dlc.labeled_data.frame", + "dlc.labeled_data.human_labels", + "dlc.labeled_data.machine_labels", + "dlc.labeled_data.machine_labels_csv", + } + + +def test_the_layout_index_is_read_only() -> None: + index = index_layout(PROJECT) + + with pytest.raises(TypeError): + index.schemas["dlc.elsewhere"] = PROJECT # type: ignore[index] + + +def test_a_schema_id_names_one_position() -> None: + assert "schema_id" not in RepeatedNodeSchema.model_fields + + readme = FileSchema( + schema_id="dlc.readme", + kind=FileResourceKind.NAPARI_DLC_METADATA, + name=NamePattern(exact="README.txt"), + ) + dataset = DirectorySchema( + schema_id="dlc.dataset", + kind=DirectoryResourceKind.LABELED_DATASET, + name=NamePattern(), + children=(readme,), + ) + layout = DirectorySchema( + schema_id="dlc.project", + kind=DirectoryResourceKind.PROJECT_ROOT, + name=NamePattern(), + children=(readme,), + repeated_children=(RepeatedNodeSchema(child=dataset),), + ) + + with pytest.raises(ValueError, match=r"dlc\.readme"): + index_layout(layout) + + +@pytest.mark.parametrize( + ("schema_type", "kind"), + [ + (FileSchema, FileResourceKind.NAPARI_DLC_METADATA), + (DirectorySchema, DirectoryResourceKind.LABELED_DATASET), + ], +) +def test_a_known_unmanaged_node_cannot_be_required( + schema_type: type[FileSchema | DirectorySchema], + kind: FileResourceKind | DirectoryResourceKind, +) -> None: + with pytest.raises(ValidationError, match="cannot be required"): + schema_type( + schema_id="dlc.unmanaged", + kind=kind, + name=NamePattern(exact="unmanaged"), + management=ResourceManagement.KNOWN_UNMANAGED, + required=True, + ) + + +def test_a_known_unmanaged_node_may_be_optional_and_a_managed_node_required() -> None: + sidecar = FileSchema( + schema_id="dlc.sidecar", + kind=FileResourceKind.NAPARI_DLC_METADATA, + name=NamePattern(exact="sidecar.json"), + management=ResourceManagement.KNOWN_UNMANAGED, + ) + config = FileSchema( + schema_id="dlc.config", + kind=FileResourceKind.PROJECT_CONFIG, + name=NamePattern(exact="config.yaml"), + required=True, + ) + + assert (sidecar.required, config.required) == (False, True) + + +def test_the_ancestry_runs_from_a_node_up_to_the_root() -> None: + index = index_layout(PROJECT) + + assert index.ancestry("dlc.labeled_data.frame") == ( + "dlc.labeled_data.frame", + "dlc.labeled_data.dataset", + "dlc.labeled_data", + "dlc.project", + ) + assert index.ancestry("dlc.project") == ("dlc.project",) + + +def test_the_subtree_holds_the_node_and_its_descendants_only() -> None: + index = index_layout(PROJECT) + + assert index.subtree("dlc.labeled_data.dataset") == ( + "dlc.labeled_data.dataset", + "dlc.labeled_data.napari_metadata", + "dlc.labeled_data.frame", + "dlc.labeled_data.human_labels", + "dlc.labeled_data.machine_labels", + "dlc.labeled_data.machine_labels_csv", + ) + assert index.subtree("dlc.project.config") == ("dlc.project.config",) + + +@pytest.mark.parametrize("query", ["ancestry", "subtree"]) +def test_an_undeclared_schema_id_is_refused(query: str) -> None: + with pytest.raises(KeyError): + getattr(index_layout(PROJECT), query)("dlc.not_declared") + + +def test_the_layout_lists_its_nodes_of_one_type_in_walk_order() -> None: + index = index_layout(PROJECT) + files = index.schema_ids_of(FileSchema) + + assert files == tuple(schema_id for schema_id in index.schemas if schema_id in files) + assert "dlc.project.config" in files + assert "dlc.labeled_data" not in files + assert set(files) | set(index.schema_ids_of(DirectorySchema)) == set(index.schemas) From 84d2f7942d9934e04eb5ce8df7c21d5dc806af45 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Wed, 25 Nov 2026 17:15:46 +0100 Subject: [PATCH 2/3] Adapt project layout nodes to dlccore - Rename the packages to dlccore.project and dlccore.io, and add the dlccore license header - Drop the stale path comments on line 1 --- .../schemas/project_layout/__init__.py | 10 ++++++++++ .../schemas/project_layout/labeled_data.py | 17 +++++++++++++---- .../project/schemas/project_layout/project.py | 19 ++++++++++++++----- .../project/schemas/project_layout/videos.py | 17 +++++++++++++---- tests/project/schemas/test_layout_suffixes.py | 19 +++++++++++++++---- tests/project/schemas/test_layout_walk.py | 19 +++++++++++++++---- 6 files changed, 80 insertions(+), 21 deletions(-) diff --git a/dlccore/project/schemas/project_layout/__init__.py b/dlccore/project/schemas/project_layout/__init__.py index e69de29..d329f1d 100644 --- a/dlccore/project/schemas/project_layout/__init__.py +++ b/dlccore/project/schemas/project_layout/__init__.py @@ -0,0 +1,10 @@ +# +# 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 +# diff --git a/dlccore/project/schemas/project_layout/labeled_data.py b/dlccore/project/schemas/project_layout/labeled_data.py index c3b8f97..677d546 100644 --- a/dlccore/project/schemas/project_layout/labeled_data.py +++ b/dlccore/project/schemas/project_layout/labeled_data.py @@ -1,13 +1,22 @@ -# schemas/labeled_data.py +# +# 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 dlc_octopus.io.suffixes import H5_SUFFIX, HUMAN_LABELS_PREFIX, LABELED_DATA_TABLE_SUFFIXES -from dlc_octopus.schemas.base.filesystem import ( +from dlccore.io.suffixes import H5_SUFFIX, HUMAN_LABELS_PREFIX, LABELED_DATA_TABLE_SUFFIXES +from dlccore.project.schemas.base.filesystem import ( DirectorySchema, FileSchema, NamePattern, RepeatedNodeSchema, ) -from dlc_octopus.schemas.base.resources import ( +from dlccore.project.schemas.base.resources import ( DirectoryResourceKind, FileResourceKind, ResourceManagement, diff --git a/dlccore/project/schemas/project_layout/project.py b/dlccore/project/schemas/project_layout/project.py index 2ea6556..c23658d 100644 --- a/dlccore/project/schemas/project_layout/project.py +++ b/dlccore/project/schemas/project_layout/project.py @@ -1,14 +1,23 @@ -# schemas/project.py +# +# 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 dlc_octopus.schemas.base.filesystem import ( +from dlccore.project.schemas.base.filesystem import ( DirectorySchema, FileSchema, NamePattern, index_layout, ) -from dlc_octopus.schemas.base.resources import DirectoryResourceKind, FileResourceKind -from dlc_octopus.schemas.project_layout.labeled_data import LABELED_DATA -from dlc_octopus.schemas.project_layout.videos import VIDEOS +from dlccore.project.schemas.base.resources import DirectoryResourceKind, FileResourceKind +from dlccore.project.schemas.project_layout.labeled_data import LABELED_DATA +from dlccore.project.schemas.project_layout.videos import VIDEOS PROJECT_CONFIG = FileSchema( schema_id="dlc.project.config", diff --git a/dlccore/project/schemas/project_layout/videos.py b/dlccore/project/schemas/project_layout/videos.py index 78d9a70..63a7f05 100644 --- a/dlccore/project/schemas/project_layout/videos.py +++ b/dlccore/project/schemas/project_layout/videos.py @@ -1,13 +1,22 @@ -# dlc_octopus/schemas/project_layout/videos.py +# +# 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 dlc_octopus.io.suffixes import VIDEO_SUFFIXES -from dlc_octopus.schemas.base.filesystem import ( +from dlccore.io.suffixes import VIDEO_SUFFIXES +from dlccore.project.schemas.base.filesystem import ( DirectorySchema, FileSchema, NamePattern, RepeatedNodeSchema, ) -from dlc_octopus.schemas.base.resources import DirectoryResourceKind, FileResourceKind +from dlccore.project.schemas.base.resources import DirectoryResourceKind, FileResourceKind SOURCE_VIDEO = FileSchema( schema_id="dlc.videos.source_video", diff --git a/tests/project/schemas/test_layout_suffixes.py b/tests/project/schemas/test_layout_suffixes.py index 9f888ff..7c03bbf 100644 --- a/tests/project/schemas/test_layout_suffixes.py +++ b/tests/project/schemas/test_layout_suffixes.py @@ -1,11 +1,22 @@ +# +# 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 pathlib import Path import pytest -from dlc_octopus.io.suffixes import LABELED_DATA_TABLE_SUFFIXES, VIDEO_SUFFIXES -from dlc_octopus.schemas.base.filesystem import FileSchema -from dlc_octopus.schemas.project_layout.labeled_data import HUMAN_LABELS, MACHINE_LABELS -from dlc_octopus.schemas.project_layout.videos import SOURCE_VIDEO +from dlccore.io.suffixes import LABELED_DATA_TABLE_SUFFIXES, VIDEO_SUFFIXES +from dlccore.project.schemas.base.filesystem import FileSchema +from dlccore.project.schemas.project_layout.labeled_data import HUMAN_LABELS, MACHINE_LABELS +from dlccore.project.schemas.project_layout.videos import SOURCE_VIDEO @pytest.mark.parametrize( diff --git a/tests/project/schemas/test_layout_walk.py b/tests/project/schemas/test_layout_walk.py index 85edd16..8705d7f 100644 --- a/tests/project/schemas/test_layout_walk.py +++ b/tests/project/schemas/test_layout_walk.py @@ -1,9 +1,20 @@ +# +# 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 import pytest from pydantic import ValidationError -from dlc_octopus.schemas.base.filesystem import ( +from dlccore.project.schemas.base.filesystem import ( DirectorySchema, FileSchema, NamePattern, @@ -12,9 +23,9 @@ index_layout, walk_schema, ) -from dlc_octopus.schemas.base.resources import DirectoryResourceKind, FileResourceKind, ResourceManagement -from dlc_octopus.schemas.project_layout.labeled_data import LABELED_FRAME -from dlc_octopus.schemas.project_layout.project import PROJECT +from dlccore.project.schemas.base.resources import DirectoryResourceKind, FileResourceKind, ResourceManagement +from dlccore.project.schemas.project_layout.labeled_data import LABELED_FRAME +from dlccore.project.schemas.project_layout.project import PROJECT def test_the_walk_flags_exactly_the_repeated_children() -> None: From bcac88a4e747bf57e466c18635e86196bf94b7f8 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Thu, 26 Nov 2026 14:55:11 +0100 Subject: [PATCH 3/3] Enforce that the project layer reaches io only through content - Add the prototype's project-side boundary tests: only content imports io, the layout imports only its suffixes, and CollectedData_ is spelled once, in io.suffixes - The control that content does import io lands with content --- tests/io/test_boundaries.py | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/io/test_boundaries.py b/tests/io/test_boundaries.py index c8a4583..c862e6b 100644 --- a/tests/io/test_boundaries.py +++ b/tests/io/test_boundaries.py @@ -11,6 +11,9 @@ """``dlccore.io``'s import boundaries: nothing from ``dlccore`` outside io, suffixes only the standard library, and no format module loaded until it is imported. + +``dlccore.project`` reaches io only through ``dlccore.project.content``, except for the layout, which imports the +data in ``dlccore.io.suffixes``. """ import ast @@ -27,6 +30,9 @@ PACKAGE_ROOT = Path(dlccore.__file__).parent IO = "dlccore.io" SUFFIXES = f"{IO}.suffixes" +PROJECT = "dlccore.project" +CONTENT = f"{PROJECT}.content" +SCHEMAS = f"{PROJECT}.schemas" def _within(name: str, package: str) -> bool: @@ -88,6 +94,50 @@ def test_only_io_imports_the_private_labeled_data_modules() -> None: assert {module: names for module, names in found.items() if names and not _within(module, IO)} == {} +def _may_import_from_io(module: str, name: str) -> bool: + """Content may import any of io; the layout may import its suffixes, which are data only.""" + + return _within(module, CONTENT) or (_within(module, SCHEMAS) and _within(name, SUFFIXES)) + + +def test_only_content_imports_io_and_the_layout_only_its_suffixes() -> None: + offending = { + module: sorted(name for name in imports if _within(name, IO) and not _may_import_from_io(module, name)) + for module, imports in _package_modules() + if _within(module, PROJECT) + } + + assert {module: names for module, names in offending.items() if names} == {} + + +def test_the_layout_may_import_no_other_part_of_io() -> None: + # Control: the exception for suffixes must not let the layout import io's readers. + assert _may_import_from_io(f"{SCHEMAS}.project_layout.videos", SUFFIXES) + assert not _may_import_from_io(f"{SCHEMAS}.project_layout.videos", f"{IO}.labeled_data") + assert not _may_import_from_io(f"{PROJECT}.validation.structure", SUFFIXES) + + +def test_the_scan_sees_the_layout_import_io_suffixes() -> None: + # Control: the layout does import suffixes, so the exception above is exercised. + assert SUFFIXES in dict(_package_modules())[f"{SCHEMAS}.project_layout.videos"] + + +def _modules_spelling(literal: str) -> set[str]: + """Modules holding ``literal`` as a code constant; docstrings, which only mention it, are longer strings.""" + + return { + path.relative_to(PACKAGE_ROOT).as_posix() + for path in PACKAGE_ROOT.rglob("*.py") + for node in ast.walk(ast.parse(path.read_text(encoding="utf-8"))) + if isinstance(node, ast.Constant) and node.value == literal + } + + +def test_the_human_labels_prefix_is_spelled_once_in_io() -> None: + # The layout imports it from io.suffixes, so the project layer and the I/O layer can never claim different files. + assert _modules_spelling("CollectedData_") == {"io/suffixes.py"} + + def test_the_scan_reaches_io() -> None: # Control: io's own __init__ imports its errors module, so an empty scan cannot pass the test above. assert f"{IO}.errors" in dict(_package_modules())[IO]