Use pretty logger for summaries

This commit is contained in:
I-Al-Istannen 2020-06-26 15:52:07 +02:00
parent e8be6e498e
commit d9ea688145
3 changed files with 38 additions and 45 deletions

View File

@ -1,12 +1,9 @@
""" """
Provides a summary that keeps track of new modified or deleted files. Provides a summary that keeps track of new modified or deleted files.
""" """
import logging
from pathlib import Path from pathlib import Path
from typing import List from typing import List
from .logging import PrettyLogger
class DownloadSummary: class DownloadSummary:
""" """
@ -14,66 +11,38 @@ class DownloadSummary:
""" """
def __init__(self) -> None: def __init__(self) -> None:
self._new_files: List[Path] = [] self.new_files: List[Path] = []
self._modified_files: List[Path] = [] self.modified_files: List[Path] = []
self._deleted_files: List[Path] = [] self.deleted_files: List[Path] = []
def merge(self, summary: 'DownloadSummary') -> None: def merge(self, summary: 'DownloadSummary') -> None:
""" """
Merges ourselves with the passed summary. Modifies this object, but not the passed one. Merges ourselves with the passed summary. Modifies this object, but not the passed one.
""" """
# This is our own class! self.new_files += summary.new_files
# pylint: disable=protected-access self.modified_files += summary.modified_files
self._new_files += summary._new_files self.deleted_files += summary.deleted_files
self._modified_files += summary._modified_files
self._deleted_files += summary._deleted_files
def add_deleted_file(self, path: Path) -> None: def add_deleted_file(self, path: Path) -> None:
""" """
Registers a file as deleted. Registers a file as deleted.
""" """
self._deleted_files.append(path) self.deleted_files.append(path)
def add_modified_file(self, path: Path) -> None: def add_modified_file(self, path: Path) -> None:
""" """
Registers a file as changed. Registers a file as changed.
""" """
self._modified_files.append(path) self.modified_files.append(path)
def add_new_file(self, path: Path) -> None: def add_new_file(self, path: Path) -> None:
""" """
Registers a file as new. Registers a file as new.
""" """
self._new_files.append(path) self.new_files.append(path)
def _has_updates(self) -> bool: 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:
""" """
Prints this summary. Returns whether this summary has any updates.
""" """
logger.info("") return bool(self.new_files or self.modified_files or self.deleted_files)
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)

View File

@ -3,14 +3,18 @@ Contains a few logger utility functions and implementations.
""" """
import logging 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._log_render import LogRender
from rich.console import Console from rich.console import Console
from rich.panel import Panel
from rich.style import Style from rich.style import Style
from rich.text import Text from rich.text import Text
from rich.theme import Theme from rich.theme import Theme
from .download_summary import DownloadSummary
from .utils import PathLike, to_path from .utils import PathLike, to_path
STYLE = "{" STYLE = "{"
@ -147,6 +151,23 @@ class PrettyLogger:
f"([/dim]{reason}[dim]).[/dim]" 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( def starting_synchronizer(
self, self,
target_directory: PathLike, target_directory: PathLike,

View File

@ -159,7 +159,10 @@ class Pferd(Location):
return organizer return organizer
def print_summary(self) -> None: 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 @swallow_and_print_errors
def ilias_kit_personal_desktop( def ilias_kit_personal_desktop(