"""Schritt 7: Export (export.py).""" import csv import json import subprocess import sys from conftest import PROJECT from dataset import load_dataset from deletions import DeletionList from export import ORIGIN_FILE, check_target, plan_export, run_export, suggest_target def notebook_read(folder): """So liest das Trainings-Notebook: sorted(glob('**/*.jpg')), JSON mit gleichem Pfad.""" return [(jpg, json.loads(jpg.with_suffix(".json").read_text())["angle"]) for jpg in sorted(folder.glob("**/*.jpg"))] def test_export_skips_deleted_and_incomplete_and_renumbers(make_recording, tmp_path): source = make_recording([float(i) for i in range(10)]) (source / "9_00000007.json").unlink() # unvollständig ds = load_dataset(source) deletions = DeletionList(ds) deletions.delete(ds.frames[0:3]) # 0, 1, 2 gelöscht plan = plan_export(ds, deletions) assert (len(plan.items), plan.deleted, plan.incomplete) == (6, 3, 1) target = tmp_path / "export" assert run_export(plan, target) names = sorted(p.stem for p in target.glob("*.jpg")) assert names == [f"9_{i:08d}" for i in range(6)] rows = list(csv.reader(open(target / ORIGIN_FILE, encoding="utf-8"), delimiter=";")) assert rows[0] == ["neu", "original"] assert [orig for _, orig in rows[1:]] == ["9_00000003", "9_00000004", "9_00000005", "9_00000006", "9_00000008", "9_00000009"] for new, orig in rows[1:]: # Byte für Byte gleich assert (target / f"{new}.jpg").read_bytes() == (source / f"{orig}.jpg").read_bytes() assert (target / f"{new}.json").read_bytes() == (source / f"{orig}.json").read_bytes() assert [angle for _, angle in notebook_read(target)] == [3.0, 4.0, 5.0, 6.0, 8.0, 9.0] exported = load_dataset(target) assert all(f.is_complete for f in exported.frames) assert [p.name for p in exported.ignored_files] == [ORIGIN_FILE] assert not (tmp_path / "export.unfertig").exists() def test_check_target(make_recording, tmp_path): source = make_recording([0]) assert check_target(source, source) is not None assert check_target(source, source / "x") is not None assert check_target(source, tmp_path) is not None # enthält die Quelle (tmp_path / "voll").mkdir() (tmp_path / "voll" / "x").write_text("x") assert check_target(source, tmp_path / "voll") is not None (tmp_path / "leer").mkdir() assert check_target(source, tmp_path / "leer") is None assert check_target(source, tmp_path / "neu") is None def test_suggest_target_avoids_used_folders(tmp_path): assert suggest_target(tmp_path / "6", tmp_path).name == "6_bereinigt" (tmp_path / "6_bereinigt").mkdir() assert suggest_target(tmp_path / "6", tmp_path).name == "6_bereinigt" # leer = ok (tmp_path / "6_bereinigt" / "x").write_text("x") assert suggest_target(tmp_path / "6", tmp_path).name == "6_bereinigt_2" def test_cancel_leaves_nothing(make_recording, tmp_path): ds = load_dataset(make_recording([0] * 10)) plan = plan_export(ds, DeletionList(ds)) assert run_export(plan, tmp_path / "abbruch", lambda done, total: done < 5) is False assert not (tmp_path / "abbruch").exists() assert not (tmp_path / "abbruch.unfertig").exists() def test_command_line(make_recording, tmp_path): source = make_recording([1, 2, 3]) result = subprocess.run([sys.executable, str(PROJECT / "app" / "export.py"), str(source), str(tmp_path / "out")], capture_output=True, text=True, encoding="utf-8", env={**__import__("os").environ, "PYTHONIOENCODING": "utf-8"}) assert result.returncode == 0, result.stderr assert "3 Frames werden exportiert" in result.stdout assert len(list((tmp_path / "out").glob("*.jpg"))) == 3