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

+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