mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Use PathLike everywhere
This commit is contained in:
@ -3,6 +3,7 @@ A few utility bobs and bits.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path, PurePath
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
@ -10,25 +11,22 @@ import bs4
|
||||
import requests
|
||||
from colorama import Fore, Style
|
||||
|
||||
|
||||
def move(path: PurePath, from_folders: Tuple[str], to_folders: Tuple[str]) -> Optional[PurePath]:
|
||||
"""
|
||||
If the input path is located anywhere within from_folders, replace the
|
||||
from_folders with to_folders. Returns None otherwise.
|
||||
"""
|
||||
|
||||
length = len(from_folders)
|
||||
if path.parts[:length] == from_folders:
|
||||
return PurePath(*to_folders, *path.parts[length:])
|
||||
return None
|
||||
PathLike = Union[PurePath, str, Tuple[str, ...]]
|
||||
|
||||
|
||||
def rename(path: PurePath, to_name: str) -> PurePath:
|
||||
"""
|
||||
Set the file name of the input path to to_name.
|
||||
"""
|
||||
def to_path(pathlike: PathLike) -> Path:
|
||||
if isinstance(pathlike, tuple):
|
||||
return Path(*pathlike)
|
||||
return Path(pathlike)
|
||||
|
||||
return PurePath(*path.parts[:-1], to_name)
|
||||
|
||||
Regex = Union[str, re.Pattern]
|
||||
|
||||
|
||||
def to_pattern(regex: Regex) -> re.Pattern:
|
||||
if isinstance(regex, re.Pattern):
|
||||
return regex
|
||||
return re.compile(regex)
|
||||
|
||||
|
||||
def soupify(response: requests.Response) -> bs4.BeautifulSoup:
|
||||
@ -39,7 +37,7 @@ def soupify(response: requests.Response) -> bs4.BeautifulSoup:
|
||||
return bs4.BeautifulSoup(response.text, "html.parser")
|
||||
|
||||
|
||||
def stream_to_path(response: requests.Response, to_path: Path, chunk_size: int = 1024 ** 2) -> None:
|
||||
def stream_to_path(response: requests.Response, target: Path, chunk_size: int = 1024 ** 2) -> None:
|
||||
"""
|
||||
Download a requests response content to a file by streaming it. This
|
||||
function avoids excessive memory usage when downloading large files. The
|
||||
@ -47,7 +45,7 @@ def stream_to_path(response: requests.Response, to_path: Path, chunk_size: int =
|
||||
"""
|
||||
|
||||
with response:
|
||||
with open(to_path, 'wb') as file_descriptor:
|
||||
with open(target, 'wb') as file_descriptor:
|
||||
for chunk in response.iter_content(chunk_size=chunk_size):
|
||||
file_descriptor.write(chunk)
|
||||
|
||||
@ -86,54 +84,58 @@ class PrettyLogger:
|
||||
def __init__(self, logger: logging.Logger) -> None:
|
||||
self.logger = logger
|
||||
|
||||
def modified_file(self, path: PurePath) -> None:
|
||||
@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 {str(path)!r}.{Style.RESET_ALL}"
|
||||
f"{Fore.MAGENTA}{Style.BRIGHT}Modified {self._format_path(path)}.{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def new_file(self, path: PurePath) -> None:
|
||||
def new_file(self, path: PathLike) -> None:
|
||||
"""
|
||||
A new file has been downloaded.
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
f"{Fore.GREEN}{Style.BRIGHT}Created {str(path)!r}.{Style.RESET_ALL}"
|
||||
f"{Fore.GREEN}{Style.BRIGHT}Created {self._format_path(path)}.{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def ignored_file(self, path: PurePath, reason: str) -> None:
|
||||
def ignored_file(self, path: PathLike, reason: str) -> None:
|
||||
"""
|
||||
File was not downloaded or modified.
|
||||
"""
|
||||
|
||||
self.logger.info(
|
||||
f"{Style.DIM}Ignored {str(path)!r} "
|
||||
f"{Style.DIM}Ignored {self._format_path(path)} "
|
||||
f"({Style.NORMAL}{reason}{Style.DIM}).{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def searching(self, path: PurePath) -> None:
|
||||
def searching(self, path: PathLike) -> None:
|
||||
"""
|
||||
A crawler searches a particular object.
|
||||
"""
|
||||
|
||||
self.logger.info(f"Searching {str(path)!r}")
|
||||
self.logger.info(f"Searching {self._format_path(path)}")
|
||||
|
||||
def not_searching(self, path: PurePath, reason: str) -> None:
|
||||
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 {str(path)!r} "
|
||||
f"{Style.DIM}Not searching {self._format_path(path)} "
|
||||
f"({Style.NORMAL}{reason}{Style.DIM}).{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def starting_synchronizer(
|
||||
self,
|
||||
target_directory: Union[Path, str],
|
||||
target_directory: PathLike,
|
||||
synchronizer_name: str,
|
||||
subject: Optional[str] = None,
|
||||
) -> None:
|
||||
@ -144,6 +146,7 @@ class PrettyLogger:
|
||||
subject_str = f"{subject} " if subject else ""
|
||||
self.logger.info("")
|
||||
self.logger.info((
|
||||
f"{Fore.CYAN}{Style.BRIGHT}Synchronizing {subject_str}to {str(target_directory)!r}"
|
||||
f" using the {synchronizer_name} synchronizer.{Style.RESET_ALL}"
|
||||
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