refactor(reporters): remove cancel_event from progress reporters
Thread cancel_event explicitly through the parameter chain (sync_run closure → download_book() → streaming calls) instead of stashing it on reporter objects as shared state. Remove the cancel_event attribute from both PlainTextReporter and ProgressReporter — cancellation is not a reporting concern. Closes #39 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
caca930c7c
commit
2930d163b2
9 files changed
+154
-9
No files matched your search
@@ -239,6 +239,7 @@ def download_book(
|
||||
reporter: "DownloadReporter",
|
||||
*,
|
||||
progress: "Callable[[int], None] | None" = None,
|
||||
cancel_event: threading.Event | None = None,
|
||||
rename_chapters: bool = False,
|
||||
) -> DownloadResult:
|
||||
"""Orchestrate a single book download with format strategy.
|
||||
@@ -260,7 +261,7 @@ def download_book(
|
||||
# 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
|
||||
# Cooperative cancellation: explicit parameter (Issue #39 — no longer via reporter)
|
||||
transport = session.transport
|
||||
|
||||
# --- m4b_mp3_fallback: try M4B first, fall back to MP3 ---
|
||||
|
||||
@@ -19,7 +19,6 @@ class PlainTextReporter:
|
||||
|
||||
def __init__(self, stdout: TextIO | None = None) -> None:
|
||||
self._out: TextIO = stdout or sys.stdout
|
||||
self.cancel_event: threading.Event | None = None
|
||||
|
||||
def start_download(self, book, total_bytes: int = 0) -> None:
|
||||
"""Log download start with author, title, and file size."""
|
||||
@@ -96,7 +95,6 @@ class ProgressReporter:
|
||||
# Per-book identity mapping for concurrent downloads
|
||||
self._tasks: dict[int, object] = {} # task_id -> book
|
||||
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) -> callable:
|
||||
"""Start a progress bar for this book's download.
|
||||
|
||||
@@ -191,13 +191,14 @@ def sync_run(
|
||||
# 6. Download loop with TTY-aware reporting
|
||||
reporter = DownloadReporter()
|
||||
cancel_event = threading.Event()
|
||||
reporter.cancel_event = cancel_event
|
||||
# cancel_event passed explicitly via closure to download_book() (Issue #39)
|
||||
console.print(f"\n[bold]{len(new_books)} book(s) to download:[/bold]\n")
|
||||
|
||||
# --- Download loop (parallel via orchestrator — Issue #16) ---
|
||||
|
||||
def _make_download_fn():
|
||||
_rc = resolved_rename_chapters # capture resolved value
|
||||
_ce = cancel_event # capture for closure (Issue #39 — explicit param, not via reporter)
|
||||
|
||||
def _download_fn(
|
||||
book: Book, *, progress: Callable[[int], None] | None = None
|
||||
@@ -209,7 +210,7 @@ def sync_run(
|
||||
config=config,
|
||||
format_strategy=config.format,
|
||||
)
|
||||
result = download_book(book, client, plan, reporter, progress=progress, rename_chapters=_rc)
|
||||
result = download_book(book, client, plan, reporter, progress=progress, rename_chapters=_rc, cancel_event=_ce)
|
||||
|
||||
# Write history as caller (no longer inside download_book)
|
||||
if result.status == "downloaded":
|
||||
|
||||
Reference in new issue
Block a user