From d9ea6881457a24ba26f58bd4cf52725cf7c32e04 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Fri, 26 Jun 2020 15:52:07 +0200 Subject: [PATCH] Use pretty logger for summaries --- PFERD/download_summary.py | 55 +++++++++------------------------------ PFERD/logging.py | 23 +++++++++++++++- PFERD/pferd.py | 5 +++- 3 files changed, 38 insertions(+), 45 deletions(-) diff --git a/PFERD/download_summary.py b/PFERD/download_summary.py index da59285..ae011c8 100644 --- a/PFERD/download_summary.py +++ b/PFERD/download_summary.py @@ -1,12 +1,9 @@ """ Provides a summary that keeps track of new modified or deleted files. """ -import logging from pathlib import Path from typing import List -from .logging import PrettyLogger - class DownloadSummary: """ @@ -14,66 +11,38 @@ class DownloadSummary: """ def __init__(self) -> None: - self._new_files: List[Path] = [] - self._modified_files: List[Path] = [] - self._deleted_files: List[Path] = [] + self.new_files: List[Path] = [] + self.modified_files: List[Path] = [] + self.deleted_files: List[Path] = [] def merge(self, summary: 'DownloadSummary') -> None: """ Merges ourselves with the passed summary. Modifies this object, but not the passed one. """ - # This is our own class! - # pylint: disable=protected-access - self._new_files += summary._new_files - self._modified_files += summary._modified_files - self._deleted_files += summary._deleted_files + self.new_files += summary.new_files + self.modified_files += summary.modified_files + self.deleted_files += summary.deleted_files def add_deleted_file(self, path: Path) -> None: """ Registers a file as deleted. """ - self._deleted_files.append(path) + self.deleted_files.append(path) def add_modified_file(self, path: Path) -> None: """ Registers a file as changed. """ - self._modified_files.append(path) + self.modified_files.append(path) def add_new_file(self, path: Path) -> None: """ Registers a file as new. """ - self._new_files.append(path) + self.new_files.append(path) - def _has_updates(self) -> bool: - return bool(self._new_files or self._modified_files or self._deleted_files) - - def print(self, logger: logging.Logger, pretty: PrettyLogger) -> None: + def has_updates(self) -> bool: """ - Prints this summary. + Returns whether this summary has any updates. """ - logger.info("") - logger.info("Summary: ") - if not self._has_updates(): - logger.info("Nothing changed!") - return - - if self._new_files: - logger.info("New Files:") - for file in self._new_files: - pretty.new_file(file) - - logger.info("") - - if self._modified_files: - logger.info("Modified Files:") - for file in self._modified_files: - pretty.modified_file(file) - - logger.info("") - - if self._deleted_files: - logger.info("Deleted Files:") - for file in self._deleted_files: - pretty.deleted_file(file) + return bool(self.new_files or self.modified_files or self.deleted_files) diff --git a/PFERD/logging.py b/PFERD/logging.py index 0db3153..76741f7 100644 --- a/PFERD/logging.py +++ b/PFERD/logging.py @@ -3,14 +3,18 @@ Contains a few logger utility functions and implementations. """ import logging -from typing import Optional +from pathlib import Path +from typing import List, Optional +from rich import print as rich_print from rich._log_render import LogRender from rich.console import Console +from rich.panel import Panel from rich.style import Style from rich.text import Text from rich.theme import Theme +from .download_summary import DownloadSummary from .utils import PathLike, to_path STYLE = "{" @@ -147,6 +151,23 @@ class PrettyLogger: f"([/dim]{reason}[dim]).[/dim]" ) + def summary(self, download_summary: DownloadSummary) -> None: + """ + Prints a download summary. + """ + self.logger.info("") + self.logger.info("[bold cyan]Download Summary[/bold cyan]") + if not download_summary.has_updates(): + self.logger.info("[bold dim]Nothing changed![/bold dim]") + return + + for new_file in download_summary.new_files: + self.new_file(new_file) + for modified_file in download_summary.modified_files: + self.modified_file(modified_file) + for deleted_files in download_summary.deleted_files: + self.deleted_file(deleted_files) + def starting_synchronizer( self, target_directory: PathLike, diff --git a/PFERD/pferd.py b/PFERD/pferd.py index 3384e4d..95427cb 100644 --- a/PFERD/pferd.py +++ b/PFERD/pferd.py @@ -159,7 +159,10 @@ class Pferd(Location): return organizer def print_summary(self) -> None: - self._download_summary.print(LOGGER, PRETTY) + """ + Prints the accumulated download summary. + """ + PRETTY.summary(self._download_summary) @swallow_and_print_errors def ilias_kit_personal_desktop(