Files
votify/votify/utils.py
T
GladistonXDandGitHub 62293b15c2 Add optional parameter to prompt_path function
Modify prompt_path function to add optional parameter for path existence check.
2026-01-28 10:10:24 -03:00

56 lines
1.6 KiB
Python

from __future__ import annotations
from pathlib import Path
import click
import colorama
import requests
from .constants import X_NOT_FOUND_STRING
def check_response(response: requests.Response):
try:
response.raise_for_status()
except requests.HTTPError:
_raise_response_exception(response)
def _raise_response_exception(response: requests.Response):
raise Exception(
f"Request failed with status code {response.status_code}: {response.text}"
)
def prompt_path(is_file: bool, initial_path: Path, description: str, optional: bool = False) -> Path:
path_validator = click.Path(
exists=not optional,
file_okay=is_file,
dir_okay=not is_file,
path_type=Path,
)
path_type = "file" if is_file else "folder"
while True:
try:
path_obj = path_validator.convert(initial_path, None, None)
break
except click.BadParameter as e:
path_str = click.prompt(
(
f"{X_NOT_FOUND_STRING.format(description, initial_path.absolute())} or "
"the specified path is not valid. "
f"Move the {path_type} to that location, type a new path "
f"or drag and drop the {path_type} here. "
"Then, press enter to continue"
),
default=str(initial_path),
show_default=False,
)
path_str = path_str.strip('"')
initial_path = Path(path_str)
return path_obj
def color_text(text: str, color) -> str:
return color + text + colorama.Style.RESET_ALL