2021-05-18 22:43:46 +02:00
|
|
|
import asyncio
|
2021-05-22 16:47:24 +02:00
|
|
|
import sys
|
|
|
|
import traceback
|
2021-05-18 22:43:46 +02:00
|
|
|
from contextlib import asynccontextmanager, contextmanager
|
2021-05-23 12:47:30 +02:00
|
|
|
# TODO In Python 3.9 and above, ContextManager is deprecated
|
2021-05-18 22:43:46 +02:00
|
|
|
from typing import AsyncIterator, ContextManager, Iterator, List, Optional
|
|
|
|
|
|
|
|
from rich.console import Console, RenderGroup
|
|
|
|
from rich.live import Live
|
|
|
|
from rich.markup import escape
|
|
|
|
from rich.progress import (BarColumn, DownloadColumn, Progress, TaskID, TextColumn, TimeRemainingColumn,
|
|
|
|
TransferSpeedColumn)
|
|
|
|
from rich.table import Column
|
|
|
|
|
|
|
|
|
|
|
|
class ProgressBar:
|
|
|
|
def __init__(self, progress: Progress, taskid: TaskID):
|
|
|
|
self._progress = progress
|
|
|
|
self._taskid = taskid
|
|
|
|
|
|
|
|
def advance(self, amount: float = 1) -> None:
|
|
|
|
self._progress.advance(self._taskid, advance=amount)
|
|
|
|
|
|
|
|
def set_total(self, total: float) -> None:
|
|
|
|
self._progress.update(self._taskid, total=total)
|
|
|
|
self._progress.start_task(self._taskid)
|
|
|
|
|
|
|
|
|
|
|
|
class Log:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.console = Console(highlight=False)
|
|
|
|
|
|
|
|
self._crawl_progress = Progress(
|
|
|
|
TextColumn("{task.description}", table_column=Column(ratio=1)),
|
|
|
|
BarColumn(),
|
|
|
|
TimeRemainingColumn(),
|
|
|
|
expand=True,
|
|
|
|
)
|
|
|
|
self._download_progress = Progress(
|
|
|
|
TextColumn("{task.description}", table_column=Column(ratio=1)),
|
|
|
|
TransferSpeedColumn(),
|
|
|
|
DownloadColumn(),
|
|
|
|
BarColumn(),
|
|
|
|
TimeRemainingColumn(),
|
|
|
|
expand=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
self._live = Live(console=self.console, transient=True)
|
|
|
|
self._update_live()
|
|
|
|
|
|
|
|
self._showing_progress = False
|
|
|
|
self._progress_suspended = False
|
|
|
|
self._lock = asyncio.Lock()
|
|
|
|
self._lines: List[str] = []
|
|
|
|
|
|
|
|
# Whether different parts of the output are enabled or disabled
|
2021-05-19 17:48:51 +02:00
|
|
|
self.output_explain = False
|
2021-05-23 22:39:07 +02:00
|
|
|
self.output_status = True
|
2021-05-19 17:48:51 +02:00
|
|
|
self.output_report = True
|
2021-05-18 22:43:46 +02:00
|
|
|
|
|
|
|
def _update_live(self) -> None:
|
|
|
|
elements = []
|
|
|
|
if self._crawl_progress.task_ids:
|
|
|
|
elements.append(self._crawl_progress)
|
|
|
|
if self._download_progress.task_ids:
|
|
|
|
elements.append(self._download_progress)
|
|
|
|
|
|
|
|
group = RenderGroup(*elements) # type: ignore
|
|
|
|
self._live.update(group)
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def show_progress(self) -> Iterator[None]:
|
|
|
|
if self._showing_progress:
|
|
|
|
raise RuntimeError("Calling 'show_progress' while already showing progress")
|
|
|
|
|
|
|
|
self._showing_progress = True
|
|
|
|
try:
|
|
|
|
with self._live:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self._showing_progress = False
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
async def exclusive_output(self) -> AsyncIterator[None]:
|
|
|
|
if not self._showing_progress:
|
|
|
|
raise RuntimeError("Calling 'exclusive_output' while not showing progress")
|
|
|
|
|
|
|
|
async with self._lock:
|
|
|
|
self._progress_suspended = True
|
|
|
|
self._live.stop()
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self._live.start()
|
|
|
|
self._progress_suspended = False
|
|
|
|
for line in self._lines:
|
|
|
|
self.print(line)
|
|
|
|
self._lines = []
|
|
|
|
|
2021-05-22 18:58:00 +02:00
|
|
|
def unlock(self) -> None:
|
|
|
|
"""
|
|
|
|
Get rid of an exclusive output state.
|
|
|
|
|
|
|
|
This function is meant to let PFERD print log messages after the event
|
|
|
|
loop was forcibly stopped and if it will not be started up again. After
|
|
|
|
this is called, it is not safe to use any functions except the logging
|
|
|
|
functions (print, warn, ...).
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._progress_suspended = False
|
|
|
|
for line in self._lines:
|
|
|
|
self.print(line)
|
|
|
|
|
2021-05-18 22:43:46 +02:00
|
|
|
def print(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print a normal message. Allows markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-18 22:43:46 +02:00
|
|
|
if self._progress_suspended:
|
|
|
|
self._lines.append(text)
|
|
|
|
else:
|
|
|
|
self.console.print(text)
|
|
|
|
|
2021-05-22 18:58:00 +02:00
|
|
|
# TODO Print errors (and warnings?) to stderr
|
|
|
|
|
2021-05-19 18:10:17 +02:00
|
|
|
def warn(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print a warning message. Allows no markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-19 18:10:17 +02:00
|
|
|
self.print(f"[bold bright_red]Warning[/] {escape(text)}")
|
|
|
|
|
2021-05-23 18:12:34 +02:00
|
|
|
def warn_contd(self, text: str) -> None:
|
|
|
|
"""
|
|
|
|
Print further lines of a warning message. Allows no markup.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.print(f"{escape(text)}")
|
|
|
|
|
2021-05-19 18:10:17 +02:00
|
|
|
def error(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print an error message. Allows no markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-19 18:10:17 +02:00
|
|
|
self.print(f"[bold bright_red]Error[/] [red]{escape(text)}")
|
|
|
|
|
|
|
|
def error_contd(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print further lines of an error message. Allows no markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-19 18:10:17 +02:00
|
|
|
self.print(f"[red]{escape(text)}")
|
|
|
|
|
2021-05-22 16:47:24 +02:00
|
|
|
def unexpected_exception(self) -> None:
|
2021-05-22 18:36:25 +02:00
|
|
|
"""
|
|
|
|
Call this in an "except" clause to log an unexpected exception.
|
|
|
|
"""
|
2021-05-22 16:47:24 +02:00
|
|
|
|
2021-05-22 18:36:25 +02:00
|
|
|
t, v, tb = sys.exc_info()
|
|
|
|
if t is None or v is None or tb is None:
|
|
|
|
# We're not currently handling an exception, so somebody probably
|
|
|
|
# called this function where they shouldn't.
|
|
|
|
self.error("Something unexpected happened")
|
|
|
|
self.error_contd("")
|
|
|
|
for line in traceback.format_stack():
|
|
|
|
self.error_contd(line[:-1]) # Without the newline
|
|
|
|
self.error_contd("")
|
2021-05-22 16:47:24 +02:00
|
|
|
else:
|
2021-05-22 18:36:25 +02:00
|
|
|
self.error("An unexpected exception occurred")
|
|
|
|
self.error_contd("")
|
|
|
|
self.error_contd(traceback.format_exc())
|
2021-05-22 16:47:24 +02:00
|
|
|
|
|
|
|
self.error_contd("""
|
2021-05-22 18:36:25 +02:00
|
|
|
Please copy your program output and send it to the PFERD maintainers, either
|
|
|
|
directly or as a GitHub issue: https://github.com/Garmelon/PFERD/issues/new
|
2021-05-22 16:47:24 +02:00
|
|
|
""".strip())
|
|
|
|
|
2021-05-18 22:43:46 +02:00
|
|
|
def explain_topic(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print a top-level explain text. Allows no markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-19 17:48:51 +02:00
|
|
|
if self.output_explain:
|
2021-05-23 21:27:37 +02:00
|
|
|
self.print(f"[yellow]{escape(text)}")
|
2021-05-18 22:43:46 +02:00
|
|
|
|
|
|
|
def explain(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print an indented explain text. Allows no markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-19 17:48:51 +02:00
|
|
|
if self.output_explain:
|
2021-05-18 22:43:46 +02:00
|
|
|
self.print(f" {escape(text)}")
|
|
|
|
|
2021-05-23 22:39:07 +02:00
|
|
|
def status(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print a status update while crawling. Allows markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-23 22:39:07 +02:00
|
|
|
if self.output_status:
|
2021-05-18 22:43:46 +02:00
|
|
|
self.print(text)
|
|
|
|
|
|
|
|
def report(self, text: str) -> None:
|
2021-05-23 11:30:16 +02:00
|
|
|
"""
|
|
|
|
Print a report after crawling. Allows markup.
|
|
|
|
"""
|
|
|
|
|
2021-05-19 17:48:51 +02:00
|
|
|
if self.output_report:
|
2021-05-18 22:43:46 +02:00
|
|
|
self.print(text)
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def _bar(
|
|
|
|
self,
|
|
|
|
progress: Progress,
|
|
|
|
description: str,
|
|
|
|
total: Optional[float],
|
|
|
|
) -> Iterator[ProgressBar]:
|
|
|
|
if total is None:
|
|
|
|
# Indeterminate progress bar
|
|
|
|
taskid = progress.add_task(description, start=False)
|
|
|
|
else:
|
|
|
|
taskid = progress.add_task(description, total=total)
|
|
|
|
self._update_live()
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield ProgressBar(progress, taskid)
|
|
|
|
finally:
|
|
|
|
progress.remove_task(taskid)
|
|
|
|
self._update_live()
|
|
|
|
|
|
|
|
def crawl_bar(
|
|
|
|
self,
|
|
|
|
description: str,
|
|
|
|
total: Optional[float] = None,
|
|
|
|
) -> ContextManager[ProgressBar]:
|
|
|
|
return self._bar(self._crawl_progress, description, total)
|
|
|
|
|
|
|
|
def download_bar(
|
|
|
|
self,
|
|
|
|
description: str,
|
|
|
|
total: Optional[float] = None,
|
|
|
|
) -> ContextManager[ProgressBar]:
|
|
|
|
return self._bar(self._download_progress, description, total)
|
|
|
|
|
|
|
|
|
|
|
|
log = Log()
|