"""Lecture/ecriture des artefacts du pipeline dans data//. Chaque etape ecrit un JSON ; les etapes suivantes les relisent. C'est aussi ce qui rend le pipeline reprenable : on peut detecter qu'un artefact existe deja. """ from __future__ import annotations from pathlib import Path from ..config import book_data_dir from ..models import Cast, ChapterAnalysis, Pronunciation def analysis_path(slug: str, chapter_index: int) -> Path: return book_data_dir(slug) / "analysis" / f"ch{chapter_index:02d}.json" def cast_path(slug: str) -> Path: return book_data_dir(slug) / "cast.json" def pronunciation_path(slug: str) -> Path: return book_data_dir(slug) / "pronunciation.json" def save_analysis(slug: str, analysis: ChapterAnalysis) -> Path: path = analysis_path(slug, analysis.index) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(analysis.model_dump_json(indent=2), encoding="utf-8") return path def load_analysis(slug: str, chapter_index: int) -> ChapterAnalysis: path = analysis_path(slug, chapter_index) return ChapterAnalysis.model_validate_json(path.read_text(encoding="utf-8")) def save_cast(slug: str, cast: Cast) -> Path: path = cast_path(slug) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(cast.model_dump_json(indent=2), encoding="utf-8") return path def load_cast(slug: str) -> Cast: path = cast_path(slug) if not path.exists(): return Cast() return Cast.model_validate_json(path.read_text(encoding="utf-8")) def save_pronunciation(slug: str, pron: Pronunciation) -> Path: path = pronunciation_path(slug) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(pron.model_dump_json(indent=2), encoding="utf-8") return path def load_pronunciation(slug: str) -> Pronunciation: path = pronunciation_path(slug) if not path.exists(): return Pronunciation() return Pronunciation.model_validate_json(path.read_text(encoding="utf-8"))