feat(path): add OutputPlan dataclass for centralized path resolution
Introduce frozen OutputPlan dataclass carrying all resolved file paths (audio, partial, cover, PDF) for a single Book. Add resolve_output_plan() function that centralizes every path computation so download functions receive pre-resolved Paths instead of computing them inline. Refactor download_book() to consume plan.audio_path (eliminating duplicated sanitize(title).m4b logic), download_accompanying_files() to accept an OutputPlan instead of raw output_dir, and _download_cover/_download_pdf to accept optional expected_path parameter. Closes #28
This commit is contained in:
1 parent
fe7642be7c
commit
0ee8186361
7 files changed
+467
-35
No files matched your search
@@ -17,6 +17,7 @@ from librofm_downloader.history import HistoryEntry
|
||||
from librofm_downloader.path import (
|
||||
_resolve_output_dir,
|
||||
needs_subdirectory,
|
||||
resolve_output_plan,
|
||||
resolve_path,
|
||||
sanitize,
|
||||
)
|
||||
@@ -240,7 +241,8 @@ def download_book(
|
||||
"""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
output_dir = _resolve_output_dir(book, output_base, config=config)
|
||||
plan = resolve_output_plan(book, output_base, config=config)
|
||||
output_dir = plan.audio_path.parent
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# --- m4b_mp3_fallback: try M4B first, fall back to MP3 ---
|
||||
@@ -248,12 +250,10 @@ def download_book(
|
||||
# Try M4B first
|
||||
try:
|
||||
m4b_url = client.fetch_m4b_url(book.isbn)
|
||||
title_sanitized = sanitize(book.title)
|
||||
m4b_path = output_dir / f"{title_sanitized}.m4b"
|
||||
result = download_m4b(m4b_url, m4b_path, transport=transport, progress=progress, cancel_event=cancel_event)
|
||||
result = download_m4b(m4b_url, plan.audio_path, transport=transport, progress=progress, cancel_event=cancel_event)
|
||||
_write_history(history, book, "m4b", str(result))
|
||||
if config:
|
||||
download_accompanying_files(book, output_dir, config, client=client, transport=transport)
|
||||
download_accompanying_files(book, plan, config, client=client, transport=transport)
|
||||
return result
|
||||
except M4BUnavailableError:
|
||||
logger.info("M4B not available for %s (%s), falling back to MP3", book.title, book.isbn)
|
||||
@@ -261,26 +261,24 @@ def download_book(
|
||||
# Fall back to MP3
|
||||
result = _download_mp3(book, client, output_dir, history, transport, progress=progress, cancel_event=cancel_event)
|
||||
if result and config:
|
||||
download_accompanying_files(book, output_dir, config, client=client, transport=transport)
|
||||
download_accompanying_files(book, plan, config, client=client, transport=transport)
|
||||
return result
|
||||
|
||||
# --- mp3_only: skip M4B entirely ---
|
||||
if format_strategy == "mp3_only":
|
||||
result = _download_mp3(book, client, output_dir, history, transport, progress=progress, cancel_event=cancel_event)
|
||||
if result and config:
|
||||
download_accompanying_files(book, output_dir, config, client=client, transport=transport)
|
||||
download_accompanying_files(book, plan, config, client=client, transport=transport)
|
||||
return result
|
||||
|
||||
# --- m4b_only: skip book if M4B unavailable ---
|
||||
if format_strategy == "m4b_only":
|
||||
try:
|
||||
m4b_url = client.fetch_m4b_url(book.isbn)
|
||||
title_sanitized = sanitize(book.title)
|
||||
m4b_path = output_dir / f"{title_sanitized}.m4b"
|
||||
result = download_m4b(m4b_url, m4b_path, transport=transport, progress=progress, cancel_event=cancel_event)
|
||||
result = download_m4b(m4b_url, plan.audio_path, transport=transport, progress=progress, cancel_event=cancel_event)
|
||||
_write_history(history, book, "m4b", str(result))
|
||||
if config:
|
||||
download_accompanying_files(book, output_dir, config, client=client, transport=transport)
|
||||
download_accompanying_files(book, plan, config, client=client, transport=transport)
|
||||
return result
|
||||
except M4BUnavailableError:
|
||||
logger.info("Skipping %s (%s): no M4B available (m4b_only mode)", book.title, book.isbn)
|
||||
@@ -342,6 +340,7 @@ def _cover_filename_from_url(url: str) -> str:
|
||||
def _download_cover(
|
||||
url: str,
|
||||
output_dir: Path,
|
||||
expected_path: Path | None = None,
|
||||
transport: httpx.BaseTransport | None = None,
|
||||
) -> Path | None:
|
||||
"""Download a cover art file via streaming .partial → atomic rename.
|
||||
@@ -353,13 +352,15 @@ def _download_cover(
|
||||
"""
|
||||
from librofm_downloader.session import LibroFmSession
|
||||
|
||||
filename = _cover_filename_from_url(url)
|
||||
if expected_path is not None:
|
||||
output_path = expected_path
|
||||
else:
|
||||
filename = _cover_filename_from_url(url)
|
||||
output_path = output_dir / filename
|
||||
|
||||
# Normalize protocol-relative URLs (//covers.libro.fm/... → https://covers.libro.fm/...)
|
||||
if url.startswith("//"):
|
||||
url = "https:" + url
|
||||
|
||||
output_path = output_dir / filename
|
||||
partial_path = output_path.with_suffix(output_path.suffix + ".partial")
|
||||
|
||||
try:
|
||||
@@ -390,14 +391,18 @@ def _download_pdf(
|
||||
url: str,
|
||||
filename: str,
|
||||
output_dir: Path,
|
||||
expected_path: Path | None = None,
|
||||
transport: httpx.BaseTransport | None = None,
|
||||
) -> Path | None:
|
||||
"""Download a PDF extra file via streaming .partial → atomic rename.
|
||||
|
||||
Returns Path on success, None on failure (logs warning).
|
||||
"""
|
||||
safe_name = sanitize(filename)
|
||||
output_path = output_dir / safe_name
|
||||
if expected_path is not None:
|
||||
output_path = expected_path
|
||||
else:
|
||||
safe_name = sanitize(filename)
|
||||
output_path = output_dir / safe_name
|
||||
partial_path = output_path.with_suffix(output_path.suffix + ".partial")
|
||||
|
||||
try:
|
||||
@@ -421,7 +426,7 @@ def _download_pdf(
|
||||
|
||||
def download_accompanying_files(
|
||||
book: Book,
|
||||
output_dir: Path | str,
|
||||
plan: "OutputPlan",
|
||||
config: "Config",
|
||||
client: "LibroFmSession | None" = None,
|
||||
transport: httpx.BaseTransport | None = None,
|
||||
@@ -433,7 +438,7 @@ def download_accompanying_files(
|
||||
|
||||
Args:
|
||||
book: The book metadata.
|
||||
output_dir: Directory where accompanying files are placed.
|
||||
plan: OutputPlan with resolved paths for cover/PDF files.
|
||||
config: Config with download_extras / download_covers toggles.
|
||||
client: Optional LibroFmSession for fetching PDF URLs.
|
||||
transport: Optional httpx transport override for testing.
|
||||
@@ -441,14 +446,15 @@ def download_accompanying_files(
|
||||
Returns:
|
||||
List of Paths successfully downloaded.
|
||||
"""
|
||||
from librofm_downloader.path import OutputPlan
|
||||
from librofm_downloader.session import LibroFmSession
|
||||
|
||||
output_dir = Path(output_dir)
|
||||
output_dir = plan.audio_path.parent
|
||||
downloaded: list[Path] = []
|
||||
|
||||
# Download cover art if enabled and available
|
||||
if config.download_covers and book.cover_url:
|
||||
result = _download_cover(book.cover_url, output_dir, transport=transport)
|
||||
result = _download_cover(book.cover_url, output_dir, expected_path=plan.cover_path, transport=transport)
|
||||
if result:
|
||||
downloaded.append(result)
|
||||
|
||||
@@ -457,7 +463,7 @@ def download_accompanying_files(
|
||||
try:
|
||||
pdf_url = client.fetch_pdf_extra_url(book.isbn, "map.pdf")
|
||||
if pdf_url:
|
||||
result = _download_pdf(pdf_url, "map.pdf", output_dir, transport=transport)
|
||||
result = _download_pdf(pdf_url, "map.pdf", output_dir, expected_path=plan.pdf_path, transport=transport)
|
||||
if result:
|
||||
downloaded.append(result)
|
||||
except Exception as exc:
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
Pure logic module — no I/O, no network. All deterministic string manipulation.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
@@ -129,6 +131,21 @@ def needs_subdirectory(book: Book) -> bool:
|
||||
return bool(book.pdf_extras)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class OutputPlan:
|
||||
"""Resolved output paths and flags for one Book.
|
||||
|
||||
Immutable snapshot of where every output file for a book should go.
|
||||
Computed once by :func:`resolve_output_plan`, then consumed by download
|
||||
functions so that no path computation happens inside downloaders.
|
||||
"""
|
||||
|
||||
audio_path: Path # Complete file path for audio (.m4b)
|
||||
partial_path: Path # .partial resume path for audio
|
||||
cover_path: Path | None # None when disabled or no cover_url
|
||||
pdf_path: Path | None # None when disabled or no pdf_extras
|
||||
|
||||
|
||||
def _resolve_output_dir(
|
||||
book: Book,
|
||||
output_base: Path | str,
|
||||
@@ -156,3 +173,50 @@ def _resolve_output_dir(
|
||||
|
||||
# Flat: file is leaf node under author dir
|
||||
return base / first_author
|
||||
|
||||
|
||||
def resolve_output_plan(
|
||||
book: Book,
|
||||
output_base: Path | str,
|
||||
config: "Config | None" = None,
|
||||
) -> OutputPlan:
|
||||
"""Resolve all output paths for a book into a single immutable plan.
|
||||
|
||||
Centralizes every path computation so download functions receive
|
||||
pre-resolved Paths instead of computing them inline.
|
||||
|
||||
Args:
|
||||
book: The book metadata.
|
||||
output_base: Base download directory (string or Path).
|
||||
config: Config with download_covers / download_extras toggles.
|
||||
When None, cover_path and pdf_path are both None.
|
||||
|
||||
Returns:
|
||||
Frozen OutputPlan with all resolved file paths.
|
||||
"""
|
||||
from urllib.parse import urlparse as _urlparse
|
||||
|
||||
output_dir = _resolve_output_dir(book, output_base, config=config)
|
||||
|
||||
title_sanitized = sanitize(book.title)
|
||||
audio_path = output_dir / f"{title_sanitized}.m4b"
|
||||
partial_path = Path(str(audio_path) + ".partial")
|
||||
|
||||
# Cover path: only when config provided AND enabled AND cover_url is present
|
||||
cover_path: Path | None = None
|
||||
if config is not None and config.download_covers and book.cover_url:
|
||||
parsed = _urlparse(book.cover_url)
|
||||
name = Path(parsed.path).name or "cover.jpg"
|
||||
cover_path = output_dir / sanitize(name)
|
||||
|
||||
# PDF path: only when enabled AND pdf_extras is truthy
|
||||
pdf_path: Path | None = None
|
||||
if config is not None and config.download_extras and book.pdf_extras:
|
||||
pdf_path = output_dir / "map.pdf"
|
||||
|
||||
return OutputPlan(
|
||||
audio_path=audio_path,
|
||||
partial_path=partial_path,
|
||||
cover_path=cover_path,
|
||||
pdf_path=pdf_path,
|
||||
)
|
||||
Reference in new issue
Block a user