Align paths in status messages and progress bars

Also print "Ignored" when paths are ignored due to transforms
This commit is contained in:
Joscha 2021-05-31 12:21:05 +02:00
parent 17879a7f69
commit 64a2960751
3 changed files with 39 additions and 23 deletions

View File

@ -5,8 +5,6 @@ from datetime import datetime
from pathlib import Path, PurePath
from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Set, Tuple, TypeVar
from rich.markup import escape
from ..auth import Authenticator
from ..config import Config, Section
from ..deduplicator import Deduplicator
@ -104,12 +102,9 @@ class CrawlToken(ReusableAsyncContextManager[ProgressBar]):
return self._path
async def _on_aenter(self) -> ProgressBar:
bar_desc = f"[bold bright_cyan]Crawling[/] {escape(fmt_path(self._path))}"
after_desc = f"[bold cyan]Crawled[/] {escape(fmt_path(self._path))}"
self._stack.callback(lambda: log.status(after_desc))
self._stack.callback(lambda: log.status("[bold cyan]", "Crawled", fmt_path(self._path)))
await self._stack.enter_async_context(self._limiter.limit_crawl())
bar = self._stack.enter_context(log.crawl_bar(bar_desc))
bar = self._stack.enter_context(log.crawl_bar("[bold bright_cyan]", "Crawling", fmt_path(self._path)))
return bar
@ -127,12 +122,11 @@ class DownloadToken(ReusableAsyncContextManager[Tuple[ProgressBar, FileSink]]):
return self._path
async def _on_aenter(self) -> Tuple[ProgressBar, FileSink]:
bar_desc = f"[bold bright_cyan]Downloading[/] {escape(fmt_path(self._path))}"
# The "Downloaded ..." message is printed in the output dir, not here
await self._stack.enter_async_context(self._limiter.limit_download())
sink = await self._stack.enter_async_context(self._fs_token)
bar = self._stack.enter_context(log.download_bar(bar_desc))
# The "Downloaded ..." message is printed in the output dir, not here
bar = self._stack.enter_context(log.download_bar("[bold bright_cyan]", "Downloading",
fmt_path(self._path)))
return bar, sink
@ -273,6 +267,7 @@ class Crawler(ABC):
if self._transformer.transform(path) is None:
log.explain("Answer: No")
log.status("[bold bright_black]", "Ignored", fmt_path(path))
return None
log.explain("Answer: Yes")
@ -291,6 +286,7 @@ class Crawler(ABC):
transformed_path = self._transformer.transform(path)
if transformed_path is None:
log.explain("Answer: No")
log.status("[bold bright_black]", "Ignored", fmt_path(path))
return None
fs_token = await self._output_dir.download(path, transformed_path, mtime, redownload, on_conflict)

View File

@ -28,6 +28,8 @@ class ProgressBar:
class Log:
STATUS_WIDTH = 11
def __init__(self) -> None:
self.console = Console(highlight=False)
@ -195,13 +197,15 @@ directly or as a GitHub issue: https://github.com/Garmelon/PFERD/issues/new
if self.output_explain:
self.print(f" {escape(text)}")
def status(self, text: str) -> None:
def status(self, style: str, action: str, text: str) -> None:
"""
Print a status update while crawling. Allows markup.
Print a status update while crawling. Allows markup in the "style"
argument which will be applied to the "action" string.
"""
if self.output_status:
self.print(text)
action = escape(f"{action:<{self.STATUS_WIDTH}}")
self.print(f"{style}{action}[/] {escape(text)}")
def report(self, text: str) -> None:
"""
@ -233,16 +237,34 @@ directly or as a GitHub issue: https://github.com/Garmelon/PFERD/issues/new
def crawl_bar(
self,
description: str,
style: str,
action: str,
text: str,
total: Optional[float] = None,
) -> ContextManager[ProgressBar]:
"""
Allows markup in the "style" argument which will be applied to the
"action" string.
"""
action = escape(f"{action:<{self.STATUS_WIDTH}}")
description = f"{style}{action}[/] {text}"
return self._bar(self._crawl_progress, description, total)
def download_bar(
self,
description: str,
style: str,
action: str,
text: str,
total: Optional[float] = None,
) -> ContextManager[ProgressBar]:
"""
Allows markup in the "style" argument which will be applied to the
"action" string.
"""
action = escape(f"{action:<{self.STATUS_WIDTH}}")
description = f"{style}{action}[/] {text}"
return self._bar(self._download_progress, description, total)

View File

@ -11,8 +11,6 @@ from enum import Enum
from pathlib import Path, PurePath
from typing import BinaryIO, Iterator, Optional, Tuple
from rich.markup import escape
from .logging import log
from .report import Report, ReportLoadError
from .utils import ReusableAsyncContextManager, fmt_path, fmt_real_path, prompt_yes_no
@ -425,7 +423,7 @@ class OutputDirectory:
async def _after_download(self, info: DownloadInfo) -> None:
with self._ensure_deleted(info.tmp_path):
log.status(f"[bold cyan]Downloaded[/] {fmt_path(info.remote_path)}")
log.status("[bold cyan]", "Downloaded", fmt_path(info.remote_path))
log.explain_topic(f"Processing downloaded file for {fmt_path(info.path)}")
changed = False
@ -456,10 +454,10 @@ class OutputDirectory:
self._update_metadata(info)
if changed:
log.status(f"[bold bright_yellow]Changed[/] {escape(fmt_path(info.path))}")
log.status("[bold bright_yellow]", "Changed", fmt_path(info.path))
self._report.change_file(info.path)
else:
log.status(f"[bold bright_green]Added[/] {escape(fmt_path(info.path))}")
log.status("[bold bright_green]", "Added", fmt_path(info.path))
self._report.add_file(info.path)
async def cleanup(self) -> None:
@ -489,12 +487,12 @@ class OutputDirectory:
if await self._conflict_delete_lf(self._on_conflict, pure):
try:
path.unlink()
log.status(f"[bold bright_magenta]Deleted[/] {escape(fmt_path(pure))}")
log.status("[bold bright_magenta]", "Deleted", fmt_path(pure))
self._report.delete_file(pure)
except OSError:
pass
else:
log.status(f"[bold bright_magenta]Not deleted[/] {escape(fmt_path(pure))}")
log.status("[bold bright_magenta]", "Not deleted", fmt_path(pure))
self._report.not_delete_file(pure)
def load_prev_report(self) -> None: