2020-04-20 03:54:47 +02:00
|
|
|
import logging
|
2018-11-24 09:27:33 +01:00
|
|
|
import os
|
2019-06-07 13:26:23 +02:00
|
|
|
import sys
|
2020-04-20 03:54:47 +02:00
|
|
|
from pathlib import Path, PurePath
|
|
|
|
from typing import Optional, Tuple
|
2018-11-24 09:27:33 +01:00
|
|
|
|
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]:
|
2019-04-24 14:34:20 +02:00
|
|
|
l = len(from_folders)
|
|
|
|
if path.parts[:l] == from_folders:
|
2020-04-20 03:54:47 +02:00
|
|
|
return PurePath(*to_folders, *path.parts[l:])
|
|
|
|
return None
|
2018-11-27 09:52:27 +01:00
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def rename(path: PurePath, to_name: str) -> PurePath:
|
|
|
|
return PurePath(*path.parts[:-1], to_name)
|
2018-11-27 09:52:27 +01:00
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def stream_to_path(response: requests.Response, to_path: Path, chunk_size: int = 1024 ** 2) -> None:
|
2019-04-24 14:34:20 +02:00
|
|
|
with open(to_path, 'wb') as fd:
|
2019-04-25 20:52:48 +02:00
|
|
|
for chunk in response.iter_content(chunk_size=chunk_size):
|
2019-04-24 14:34:20 +02:00
|
|
|
fd.write(chunk)
|
2018-11-24 09:27:33 +01:00
|
|
|
|
2019-06-07 13:26:23 +02:00
|
|
|
class PrettyLogger:
|
|
|
|
|
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:
|
2019-06-07 13:26:23 +02:00
|
|
|
self.logger.info(f"{Fore.MAGENTA}{Style.BRIGHT}Modified {file_name}.{Style.RESET_ALL}")
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def new_file(self, file_name: Path) -> None:
|
2019-06-07 13:26:23 +02:00
|
|
|
self.logger.info(f"{Fore.GREEN}{Style.BRIGHT}Created {file_name}.{Style.RESET_ALL}")
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def ignored_file(self, file_name: Path) -> None:
|
2019-06-07 13:26:23 +02:00
|
|
|
self.logger.info(f"{Style.DIM}Ignored {file_name}.{Style.RESET_ALL}")
|
|
|
|
|
2020-04-20 03:54:47 +02:00
|
|
|
def starting_synchronizer(self, target_directory: Path, synchronizer_name: str, subject: Optional[str] = None) -> None:
|
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}"
|
|
|
|
))
|