refactor(progress): return bound callable from start_download (#30)

start_download(book) now returns a per-book bound callable
instead of raw task_id. Task identity is fully internal to
progress.py — no caller ever sees or passes task_id.

- Orchestrator: removed closure pattern, now threads
  progress_cb = start_download(book) directly
- downloader: download_book() accepts optional progress= kwarg,
  uses it when provided (falls back to reporter.update)
- cli.py: forwards progress kwarg through _download_fn
- Public update() drops task_id param; bound callable calls
  _progress.update(task_id_internal) directly

382 tests pass (+11 new), 6/6 AC met.
This commit is contained in:
AlexKucera committed 2026-06-05 11:58:04 +02:00
1 parent 9ac581e5b7
commit 1f46467f2c
10 files changed
+398 -95

No files matched your search

+1 -1
View File
@@ -157,7 +157,7 @@ def run(
book, config.output_dir, config=config, format_strategy=config.format,
)
result = download_book(book, client, plan, reporter)
result = download_book(book, client, plan, reporter, progress=progress)
# Write history as caller (no longer inside download_book)
if result.status == "downloaded":
+5 -1
View File
@@ -236,6 +236,8 @@ def download_book(
session: "LibroFmSession",
plan: "OutputPlan",
reporter: "DownloadReporter",
*,
progress: "Callable[[int], None] | None" = None,
) -> DownloadResult:
"""Orchestrate a single book download with format strategy.
@@ -253,7 +255,9 @@ def download_book(
output_dir.mkdir(parents=True, exist_ok=True)
format_strategy = plan.format_strategy
progress = reporter.update
# Use provided bound callback (from start_download) or fall back to reporter.update
if progress is None:
progress = reporter.update
cancel_event = reporter.cancel_event
transport = session._client._transport
+5 -9
View File
@@ -69,17 +69,13 @@ def _download_one(
- error: exception string if download_fn raised, else None
- was_skipped: True if download_fn returned None (skipped)
"""
task_id = reporter.start_download(book)
# Create a per-book progress callback bound to this book's task_id.
# Without this, reporter.update() falls back to the most-recently-started
# bar (see ProgressReporter.update), so all parallel downloads would
# update the same progress bar.
def _progress(completed: int, *, total: int | None = None) -> None:
reporter.update(completed, total=total, task_id=task_id)
# Issue #30: start_download() returns a bound callable that already carries
# task identity internally. For TTY reporters this is a per-book update fn;
# for plain-text reporters it is None. No closure needed here.
progress_cb = reporter.start_download(book)
try:
result = download_fn(book, progress=_progress)
result = download_fn(book, progress=progress_cb)
if result is None:
# Skipped (no format available)
reporter.complete(book) # still "complete" the progress tracking
+24 -15
View File
@@ -94,11 +94,12 @@ class ProgressReporter:
self._book_ids: dict[int, int] = {} # id(book) -> task_id
self.cancel_event: threading.Event | None = None
def start_download(self, book, total_bytes: int = 0) -> int:
def start_download(self, book, total_bytes: int = 0) -> callable:
"""Start a progress bar for this book's download.
Returns:
The Rich task ID for this book's progress bar.
A bound callable ``(completed, *, total=None) -> None`` that updates
only this book's progress bar. Returns ``None`` for PlainTextReporter.
"""
from rich.progress import (
BarColumn,
@@ -128,26 +129,34 @@ class ProgressReporter:
self._tasks[task_id] = book
self._book_ids[id(book)] = task_id
return task_id
# Return a bound callable that carries task_id internally.
# Calls _progress.update() directly — bypasses the public update().
task_id_internal = task_id
_progress_obj = self._progress
def update(self, completed: int, *, total: int | None = None, task_id: int | None = None) -> None:
"""Update progress bar with bytes completed.
def _update(completed: int, *, total: int | None = None) -> None:
kwargs: dict = {"completed": completed}
if total is not None:
kwargs["total"] = total
_progress_obj.update(task_id_internal, **kwargs)
If *total* is provided (e.g. from a Content-Length header), also
updates the task's total so that percentage and ETA can be computed.
return _update
def update(self, completed: int, *, total: int | None = None) -> None:
"""Update progress for the most recently started download.
This is a convenience fallback for callers that don't have a bound
callable from start_download(). It always targets the most-recently-started
task. For per-book targeting, use the bound callable returned by
start_download() instead.
Args:
completed: Bytes downloaded so far.
total: Optional total bytes (from Content-Length).
task_id: Specific task to update. Falls back to most recent if omitted.
total: Optional new total (e.g. from Content-Length header).
"""
if not self._progress:
return
if task_id is None:
# Backward compat: use the most recently started task
task_id = next(reversed(self._tasks), None)
if task_id is None or task_id not in self._tasks:
if not self._progress or not self._tasks:
return
task_id = next(reversed(self._tasks))
kwargs: dict = {"completed": completed}
if total is not None:
kwargs["total"] = total