2020-04-20 17:15:47 +02:00
|
|
|
"""
|
|
|
|
A few utility bobs and bits.
|
|
|
|
"""
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
import logging
|
|
|
|
from pathlib import Path, PurePath
|
|
|
|
from typing import Optional, Tuple
|
2018-11-24 09:27:33 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def move(path: PurePath, from_folders: Tuple[str], to_folders: Tuple[str]) -> Optional[PurePath]:
|
2020-04-20 17:15:47 +02:00
|
|
|
"""
|
|
|
|
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:])
|
2020-04-20 03:54:47 +02:00
|
|
|
return None
|
2018-11-27 09:52:27 +01:00
|
|
|
|
2020-04-20 14:29:28 +02:00
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def rename(path: PurePath, to_name: str) -> PurePath:
|
2020-04-20 17:15:47 +02:00
|
|
|
"""
|
|
|
|
Set the file name of the input path to to_name.
|
|
|
|
"""
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
return PurePath(*path.parts[:-1], to_name)
|
2018-11-27 09:52:27 +01:00
|
|
|
|
2020-04-20 14:29:28 +02:00
|
|
|
|
2020-04-20 18:38:18 +02:00
|
|
|
def soupify(response: requests.Response) -> bs4.BeautifulSoup:
|
|
|
|
return bs4.BeautifulSoup(response.text, "html.parser")
|
|
|
|
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def stream_to_path(response: requests.Response, to_path: 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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with open(to_path, 'wb') as file_descriptor:
|
2019-04-25 20:52:48 +02:00
|
|
|
for chunk in response.iter_content(chunk_size=chunk_size):
|
2020-04-20 17:15:47 +02:00
|
|
|
file_descriptor.write(chunk)
|
2018-11-24 09:27:33 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-04-20 15:39:38 +02:00
|
|
|
class ResolveException(Exception):
|
|
|
|
"""An exception while resolving a file."""
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_path(directory: Path, target_file: Path) -> Path:
|
|
|
|
"""Resolve a file relative to the path of this organizer.
|
|
|
|
|
|
|
|
Raises a [ResolveException] if the file is outside the given directory.
|
|
|
|
"""
|
|
|
|
absolute_path = directory.joinpath(target_file).resolve()
|
|
|
|
|
2020-04-20 16:07:14 +02:00
|
|
|
if directory not in absolute_path.parents:
|
2020-04-20 15:39:38 +02:00
|
|
|
raise ResolveException(
|
|
|
|
f"Path resolved to file outside folder ({absolute_path})"
|
|
|
|
)
|
|
|
|
|
|
|
|
return absolute_path
|
|
|
|
|
|
|
|
|
2019-06-07 13:26:23 +02:00
|
|
|
class PrettyLogger:
|
2020-04-20 17:15:47 +02:00
|
|
|
"""
|
|
|
|
A logger that prints some specially formatted log messages in color.
|
|
|
|
"""
|
2019-06-07 13:26:23 +02:00
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def __init__(self, logger: logging.Logger) -> None:
|
2019-06-07 13:26:23 +02:00
|
|
|
self.logger = logger
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def modified_file(self, file_name: Path) -> 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(
|
|
|
|
f"{Fore.MAGENTA}{Style.BRIGHT}Modified {file_name}.{Style.RESET_ALL}")
|
2019-06-07 13:26:23 +02:00
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def new_file(self, file_name: Path) -> 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(
|
|
|
|
f"{Fore.GREEN}{Style.BRIGHT}Created {file_name}.{Style.RESET_ALL}")
|
2019-06-07 13:26:23 +02:00
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def ignored_file(self, file_name: Path) -> None:
|
2020-04-20 17:15:47 +02:00
|
|
|
"""
|
|
|
|
Nothing in particular happened to this file.
|
|
|
|
"""
|
|
|
|
|
2019-06-07 13:26:23 +02:00
|
|
|
self.logger.info(f"{Style.DIM}Ignored {file_name}.{Style.RESET_ALL}")
|
|
|
|
|
2020-04-20 17:15:47 +02:00
|
|
|
def starting_synchronizer(
|
|
|
|
self,
|
|
|
|
target_directory: Path,
|
|
|
|
synchronizer_name: str,
|
|
|
|
subject: Optional[str] = None,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
A special message marking that a synchronizer has been started.
|
|
|
|
"""
|
|
|
|
|
2019-06-07 13:26:23 +02:00
|
|
|
subject_str = f"{subject} " if subject else ""
|
|
|
|
self.logger.info("")
|
|
|
|
self.logger.info((
|
|
|
|
f"{Fore.CYAN}{Style.BRIGHT}Synchronizing {subject_str}to {target_directory}"
|
|
|
|
f" using the {synchronizer_name} synchronizer.{Style.RESET_ALL}"
|
|
|
|
))
|