pferd/PFERD/utils.py

153 lines
4.0 KiB
Python
Raw Normal View History

2020-04-20 17:15:47 +02:00
"""
A few utility bobs and bits.
"""
2020-04-20 03:54:47 +02:00
import logging
2020-04-24 20:39:30 +02:00
import re
2020-04-20 03:54:47 +02:00
from pathlib import Path, PurePath
from typing import Optional, Tuple, Union
2020-04-20 18:38:18 +02:00
import bs4
2020-04-20 03:54:47 +02:00
import requests
from colorama import Fore, Style
2020-04-24 20:39:30 +02:00
PathLike = Union[PurePath, str, Tuple[str, ...]]
2020-04-20 03:54:47 +02:00
2020-04-20 17:15:47 +02:00
2020-04-24 20:39:30 +02:00
def to_path(pathlike: PathLike) -> Path:
if isinstance(pathlike, tuple):
return Path(*pathlike)
return Path(pathlike)
2020-04-20 14:29:28 +02:00
2020-04-24 20:39:30 +02:00
Regex = Union[str, re.Pattern]
2020-04-20 17:15:47 +02:00
2020-04-24 20:39:30 +02:00
def to_pattern(regex: Regex) -> re.Pattern:
if isinstance(regex, re.Pattern):
return regex
return re.compile(regex)
2020-04-20 14:29:28 +02:00
2020-04-20 18:38:18 +02:00
def soupify(response: requests.Response) -> bs4.BeautifulSoup:
2020-04-20 19:27:26 +02:00
"""
Wrap a requests response in a bs4 object.
"""
2020-04-20 18:38:18 +02:00
return bs4.BeautifulSoup(response.text, "html.parser")
2020-04-24 20:39:30 +02:00
def stream_to_path(response: requests.Response, target: Path, chunk_size: int = 1024 ** 2) -> None:
2020-04-20 17:15:47 +02:00
"""
Download a requests response content to a file by streaming it. This
function avoids excessive memory usage when downloading large files. The
chunk_size is in bytes.
"""
2020-04-20 19:27:26 +02:00
with response:
2020-04-24 20:39:30 +02:00
with open(target, 'wb') as file_descriptor:
2020-04-20 19:27:26 +02:00
for chunk in response.iter_content(chunk_size=chunk_size):
file_descriptor.write(chunk)
2020-04-20 14:29:28 +02:00
def prompt_yes_no(question: str, default: Optional[bool] = None) -> bool:
2020-04-20 17:15:47 +02:00
"""
Prompts the user a yes/no question and returns their choice.
"""
2020-04-20 14:29:28 +02:00
if default is True:
prompt = "[Y/n]"
elif default is False:
prompt = "[y/N]"
else:
prompt = "[y/n]"
text = f"{question} {prompt} "
2020-04-20 17:15:47 +02:00
wrong_reply = "Please reply with 'yes'/'y' or 'no'/'n'."
2020-04-20 14:29:28 +02:00
while True:
response = input(text).strip().lower()
if response in {"yes", "ye", "y"}:
return True
2020-04-20 17:15:47 +02:00
if response in {"no", "n"}:
2020-04-20 14:29:28 +02:00
return False
2020-04-20 17:15:47 +02:00
if response == "" and default is not None:
return default
print(wrong_reply)
2020-04-20 14:29:28 +02:00
class PrettyLogger:
2020-04-20 17:15:47 +02:00
"""
A logger that prints some specially formatted log messages in color.
"""
2020-04-20 03:54:47 +02:00
def __init__(self, logger: logging.Logger) -> None:
self.logger = logger
2020-04-24 20:39:30 +02:00
@staticmethod
def _format_path(path: PathLike) -> str:
return repr(str(to_path(path)))
def modified_file(self, path: PathLike) -> None:
2020-04-20 17:15:47 +02:00
"""
An existing file has changed.
"""
2020-04-20 14:29:28 +02:00
self.logger.info(
2020-04-24 20:39:30 +02:00
f"{Fore.MAGENTA}{Style.BRIGHT}Modified {self._format_path(path)}.{Style.RESET_ALL}"
)
2020-04-24 20:39:30 +02:00
def new_file(self, path: PathLike) -> None:
2020-04-20 17:15:47 +02:00
"""
A new file has been downloaded.
"""
2020-04-20 14:29:28 +02:00
self.logger.info(
2020-04-24 20:39:30 +02:00
f"{Fore.GREEN}{Style.BRIGHT}Created {self._format_path(path)}.{Style.RESET_ALL}"
2020-04-24 20:24:44 +02:00
)
2020-04-24 20:39:30 +02:00
def ignored_file(self, path: PathLike, reason: str) -> None:
2020-04-24 20:24:44 +02:00
"""
File was not downloaded or modified.
"""
self.logger.info(
2020-04-24 20:39:30 +02:00
f"{Style.DIM}Ignored {self._format_path(path)} "
2020-04-24 20:24:44 +02:00
f"({Style.NORMAL}{reason}{Style.DIM}).{Style.RESET_ALL}"
)
2020-04-24 20:39:30 +02:00
def searching(self, path: PathLike) -> None:
2020-04-20 17:15:47 +02:00
"""
2020-04-24 20:24:44 +02:00
A crawler searches a particular object.
2020-04-20 17:15:47 +02:00
"""
2020-04-24 20:39:30 +02:00
self.logger.info(f"Searching {self._format_path(path)}")
2020-04-24 20:39:30 +02:00
def not_searching(self, path: PathLike, reason: str) -> None:
"""
2020-04-24 20:24:44 +02:00
A crawler does not search a particular object.
"""
self.logger.info(
2020-04-24 20:39:30 +02:00
f"{Style.DIM}Not searching {self._format_path(path)} "
2020-04-24 20:24:44 +02:00
f"({Style.NORMAL}{reason}{Style.DIM}).{Style.RESET_ALL}"
)
2020-04-20 17:15:47 +02:00
def starting_synchronizer(
self,
2020-04-24 20:39:30 +02:00
target_directory: PathLike,
2020-04-20 17:15:47 +02:00
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((
2020-04-24 20:39:30 +02:00
f"{Fore.CYAN}{Style.BRIGHT}Synchronizing "
f"{subject_str}to {self._format_path(target_directory)} "
f"using the {synchronizer_name} synchronizer.{Style.RESET_ALL}"
))