Files
AlexKucera 4aaae68a2e feat(cli): enhanced summary, graceful Ctrl+C, integration tests (closes #9)
Summary output now lists each failed book (ISBN + title + reason)
and each skipped book (ISBN + title) in both plain-text and rich/TTY
formats. Ctrl+C produces a clean shutdown message with exit code 130
instead of a Python traceback. Three integration tests exercise the
full run() pipeline: happy path, mixed results, and fatal auth failure.

12 new tests (168 total), 15/15 acceptance criteria met.
2026-06-03 15:00:27 +02:00

202 lines
7.5 KiB
Python

"""User-facing output polish — Issue #8.
TTY-aware download reporting:
- TTY (interactive): rich.Progress bars with %, speed, ETA, size
- Non-TTY (cron/pipe): plain log lines
All output goes through DownloadReporter so cli.py stays clean.
"""
from __future__ import annotations
import sys
import time
from typing import IO, TextIO
class PlainTextReporter:
"""Plain-text log lines for non-TTY output (cron, piped)."""
def __init__(self, stdout: TextIO | None = None) -> None:
self._out: TextIO = stdout or sys.stdout
def start_download(self, book, total_bytes: int = 0) -> None:
"""Log download start with author, title, and file size."""
authors = ", ".join(book.authors) if book.authors else "Unknown"
size_str = _fmt_size(total_bytes)
self._print(f"Downloading: {authors} - {book.title} ({size_str})")
def complete(self, book) -> None:
"""Log successful completion."""
authors = ", ".join(book.authors) if book.authors else "Unknown"
self._print(f"Completed: {authors} - {book.title}")
def fail(self, book, reason: str = "") -> None:
"""Log failure with ISBN, title, and error reason."""
authors = ", ".join(book.authors) if book.authors else "Unknown"
detail = f" ({reason})" if reason else ""
self._print(f"Failed: {authors} - {book.title} [{book.isbn}]{detail}")
def update(self, completed: int, *, total: int | None = None) -> None:
"""No-op progress update — plain text mode has no progress bar."""
def summary(
self,
downloaded: int,
skipped: int,
failed: int,
*,
failed_books: list[tuple] | None = None,
skipped_books: list | None = None,
) -> None:
"""Print summary line with optional per-book details.
Args:
downloaded: Count of successfully downloaded books.
skipped: Count of skipped books.
failed: Count of failed books.
failed_books: List of (book, reason) tuples for each failure.
skipped_books: List of Book objects that were skipped.
"""
self._print(f"\nSummary: {downloaded} downloaded, {skipped} skipped, {failed} failed")
if failed_books:
self._print(" Failed:")
for book, reason in failed_books:
authors = ", ".join(book.authors) if book.authors else "Unknown"
self._print(f" ✗ {authors} - {book.title} [{book.isbn}] ({reason})")
if skipped_books:
self._print(" Skipped:")
for book in skipped_books:
authors = ", ".join(book.authors) if book.authors else "Unknown"
self._print(f" ⏭ {authors} - {book.title} [{book.isbn}]")
def _print(self, message: str) -> None:
self._out.write(message + "\n")
self._out.flush()
class ProgressReporter:
"""Rich Progress-based reporter for TTY (interactive) output."""
def __init__(self, stdout: TextIO | None = None) -> None:
from rich.console import Console
self._console = Console(file=stdout or sys.stdout)
self._progress = None # Lazy init on first download
def start_download(self, book, total_bytes: int = 0) -> None:
"""Start a progress bar for this book's download."""
from rich.progress import (
BarColumn,
DownloadColumn,
Progress,
TimeRemainingColumn,
TransferSpeedColumn,
)
if self._progress is None:
self._progress = Progress(
"[progress.description]{task.description}",
BarColumn(),
"[progress.percentage]{task.percentage:>3.1f}%",
TransferSpeedColumn(),
TimeRemainingColumn(),
DownloadColumn(),
console=self._console,
)
self._progress.start()
authors = ", ".join(book.authors) if book.authors else "Unknown"
description = f"{authors} - {book.title}"
task_id = self._progress.add_task(description, total=total_bytes or None)
# Store task_id on self for update() to find it
# In practice we'd track per-book; for now store as current
self._current_task = task_id
self._current_book = book
self._start_time = time.monotonic()
def update(self, completed: int, *, total: int | None = None) -> None:
"""Update progress bar with bytes completed.
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.
"""
if self._progress and hasattr(self, "_current_task"):
kwargs: dict = {"completed": completed}
if total is not None:
kwargs["total"] = total
self._progress.update(self._current_task, **kwargs)
def complete(self, book) -> None:
"""Mark current progress bar as complete."""
if self._progress and hasattr(self, "_current_task"):
self._progress.update(self._current_task, completed=self._progress.tasks[self._current_task].total or 0)
self._console.print(f" [green]✓[/green] {book.title}")
self._current_task = None
def fail(self, book, reason: str = "") -> None:
"""Mark current progress bar as failed."""
if self._progress and hasattr(self, "_current_task"):
self._progress.stop_task(self._current_task)
detail = f" ({reason})" if reason else ""
self._console.print(f" [red]✗[/red] {book.title}[dim] [{book.isbn}]{detail}[/dim]")
self._current_task = None
def summary(
self,
downloaded: int,
skipped: int,
failed: int,
*,
failed_books: list[tuple] | None = None,
skipped_books: list | None = None,
) -> None:
"""Print summary line with optional per-book details and stop progress."""
if self._progress:
self._progress.stop()
self._console.print(f"\n[bold]Summary:[/bold] {downloaded} downloaded, {skipped} skipped, {failed} failed")
if failed_books:
self._console.print(" [red]Failed:[/red]")
for book, reason in failed_books:
authors = ", ".join(book.authors) if book.authors else "Unknown"
self._console.print(f" ✗ {authors} - {book.title} [{book.isbn}] ({reason})")
if skipped_books:
self._console.print(" [yellow]Skipped:[/yellow]")
for book in skipped_books:
authors = ", ".join(book.authors) if book.authors else "Unknown"
self._console.print(f" ⏭ {authors} - {book.title} [{book.isbn}]")
def _fmt_size(bytes_val: int) -> str:
"""Format byte count as human-readable size string."""
if bytes_val <= 0:
return "unknown size"
for unit in ("B", "KB", "MB", "GB"):
if bytes_val < 1024:
return f"{bytes_val:.1f} {unit}" if unit != "B" else f"{bytes_val} {unit}"
bytes_val /= 1024
return f"{bytes_val:.1f} TB"
def DownloadReporter(stdout=None):
"""Factory: returns appropriate reporter based on TTY detection.
Args:
stdout: File-like object to check for TTY and write to.
Defaults to sys.stdout.
Returns:
ProgressReporter if stdout is a TTY, else PlainTextReporter.
"""
if stdout is None:
stdout = sys.stdout
if getattr(stdout, "isatty", lambda: False)():
return ProgressReporter(stdout=stdout)
return PlainTextReporter(stdout=stdout)