mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Move logging into its own file
This commit is contained in:
@ -2,19 +2,20 @@
|
||||
A few utility bobs and bits.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path, PurePath
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
import bs4
|
||||
import requests
|
||||
from colorama import Fore, Style
|
||||
|
||||
PathLike = Union[PurePath, str, Tuple[str, ...]]
|
||||
|
||||
|
||||
def to_path(pathlike: PathLike) -> Path:
|
||||
"""
|
||||
Convert a given PathLike into a Path.
|
||||
"""
|
||||
if isinstance(pathlike, tuple):
|
||||
return Path(*pathlike)
|
||||
return Path(pathlike)
|
||||
@ -24,6 +25,9 @@ Regex = Union[str, re.Pattern]
|
||||
|
||||
|
||||
def to_pattern(regex: Regex) -> re.Pattern:
|
||||
"""
|
||||
Convert a regex to a re.Pattern.
|
||||
"""
|
||||
if isinstance(regex, re.Pattern):
|
||||
return regex
|
||||
return re.compile(regex)
|
||||
@ -74,79 +78,3 @@ def prompt_yes_no(question: str, default: Optional[bool] = None) -> bool:
|
||||
if response == "" and default is not None:
|
||||
return default
|
||||
print(wrong_reply)
|
||||
|
||||
|
||||
class PrettyLogger:
|
||||
"""
|
||||
A logger that prints some specially formatted log messages in color.
|
||||
"""
|
||||
|
||||
def __init__(self, logger: logging.Logger) -> None:
|
||||
self.logger = logger
|
||||
|
||||
@staticmethod
|
||||
def _format_path(path: PathLike) -> str:
|
||||
return repr(str(to_path(path)))
|
||||
|
||||
def modified_file(self, path: PathLike) -> None:
|
||||
"""
|
||||
An existing file has changed.
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
f"{Fore.MAGENTA}{Style.BRIGHT}Modified {self._format_path(path)}.{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def new_file(self, path: PathLike) -> None:
|
||||
"""
|
||||
A new file has been downloaded.
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
f"{Fore.GREEN}{Style.BRIGHT}Created {self._format_path(path)}.{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def ignored_file(self, path: PathLike, reason: str) -> None:
|
||||
"""
|
||||
File was not downloaded or modified.
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
f"{Style.DIM}Ignored {self._format_path(path)} "
|
||||
f"({Style.NORMAL}{reason}{Style.DIM}).{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def searching(self, path: PathLike) -> None:
|
||||
"""
|
||||
A crawler searches a particular object.
|
||||
"""
|
||||
|
||||
self.logger.info(f"Searching {self._format_path(path)}")
|
||||
|
||||
def not_searching(self, path: PathLike, reason: str) -> None:
|
||||
"""
|
||||
A crawler does not search a particular object.
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
f"{Style.DIM}Not searching {self._format_path(path)} "
|
||||
f"({Style.NORMAL}{reason}{Style.DIM}).{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def starting_synchronizer(
|
||||
self,
|
||||
target_directory: PathLike,
|
||||
synchronizer_name: str,
|
||||
subject: Optional[str] = None,
|
||||
) -> None:
|
||||
"""
|
||||
A special message marking that a synchronizer has been started.
|
||||
"""
|
||||
|
||||
subject_str = f"{subject} " if subject else ""
|
||||
self.logger.info("")
|
||||
self.logger.info((
|
||||
f"{Fore.CYAN}{Style.BRIGHT}Synchronizing "
|
||||
f"{subject_str}to {self._format_path(target_directory)} "
|
||||
f"using the {synchronizer_name} synchronizer.{Style.RESET_ALL}"
|
||||
))
|
||||
|
Reference in New Issue
Block a user