From 5c2ff14839f04146216585fe3e1f92889b619e72 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Mon, 20 Apr 2020 14:29:28 +0200 Subject: [PATCH] Add "prompt_yes_no" to utils --- PFERD/utils.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/PFERD/utils.py b/PFERD/utils.py index 42f4205..43bdd33 100644 --- a/PFERD/utils.py +++ b/PFERD/utils.py @@ -14,24 +14,56 @@ def move(path: PurePath, from_folders: Tuple[str], to_folders: Tuple[str]) -> Op return PurePath(*to_folders, *path.parts[l:]) return None + def rename(path: PurePath, to_name: str) -> PurePath: return PurePath(*path.parts[:-1], to_name) + def stream_to_path(response: requests.Response, to_path: Path, chunk_size: int = 1024 ** 2) -> None: with open(to_path, 'wb') as fd: for chunk in response.iter_content(chunk_size=chunk_size): fd.write(chunk) + +def prompt_yes_no(question: str, default: Optional[bool] = None) -> bool: + """Prompts the user and returns their choice.""" + if default is True: + prompt = "[Y/n]" + elif default is False: + prompt = "[y/N]" + else: + prompt = "[y/n]" + + text = f"{question} {prompt} " + WRONG_REPLY = "Please reply with 'yes'/'y' or 'no'/'n'." + + while True: + response = input(text).strip().lower() + if response in {"yes", "ye", "y"}: + return True + elif response in {"no", "n"}: + return False + elif response == "": + if default is None: + print(WRONG_REPLY) + else: + return default + else: + print(WRONG_REPLY) + + class PrettyLogger: def __init__(self, logger: logging.Logger) -> None: self.logger = logger def modified_file(self, file_name: Path) -> None: - self.logger.info(f"{Fore.MAGENTA}{Style.BRIGHT}Modified {file_name}.{Style.RESET_ALL}") + self.logger.info( + f"{Fore.MAGENTA}{Style.BRIGHT}Modified {file_name}.{Style.RESET_ALL}") def new_file(self, file_name: Path) -> None: - self.logger.info(f"{Fore.GREEN}{Style.BRIGHT}Created {file_name}.{Style.RESET_ALL}") + self.logger.info( + f"{Fore.GREEN}{Style.BRIGHT}Created {file_name}.{Style.RESET_ALL}") def ignored_file(self, file_name: Path) -> None: self.logger.info(f"{Style.DIM}Ignored {file_name}.{Style.RESET_ALL}")