mirror of
https://github.com/glomatico/votify.git
synced 2026-10-14 03:42:36 +02:00
Refactor prompt_path to improve path validation
This commit is contained in:
+2
-2
@@ -400,7 +400,7 @@ def main(
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(CustomLoggerFormatter())
|
||||
logger.addHandler(stream_handler)
|
||||
cookies_path = prompt_path("Cookies file", cookies_path)
|
||||
cookies_path = prompt_path(True, cookies_path, "Cookies file")
|
||||
logger.info("Starting Votify")
|
||||
spotify_api = SpotifyApi.from_cookies_file(cookies_path)
|
||||
if spotify_api.session_info["isAnonymous"]:
|
||||
@@ -489,7 +489,7 @@ def main(
|
||||
X_NOT_FOUND_STRING.format("mp4decrypt", mp4decrypt_path)
|
||||
)
|
||||
return
|
||||
wvd_path = prompt_path(".wvd file", wvd_path)
|
||||
wvd_path = prompt_path(False, wvd_path, ".wvd file")
|
||||
downloader.set_cdm()
|
||||
if download_mode == DownloadMode.ARIA2C and not downloader.aria2c_path_full:
|
||||
logger.critical(X_NOT_FOUND_STRING.format("aria2c", aria2c_path))
|
||||
|
||||
+24
-9
@@ -22,15 +22,30 @@ def _raise_response_exception(response: requests.Response):
|
||||
)
|
||||
|
||||
|
||||
def prompt_path(path_description: str, path_obj: Path) -> Path:
|
||||
while not path_obj.exists():
|
||||
path_obj_str = click.prompt(
|
||||
X_NOT_FOUND_STRING.format(path_description, path_obj.absolute())
|
||||
+ ". Move it to that location or drag and drop it here. Then, press enter to continue",
|
||||
default=str(path_obj),
|
||||
show_default=False,
|
||||
)
|
||||
path_obj = Path(path_obj_str.strip('"'))
|
||||
def prompt_path(is_file: bool, initial_path: Path, description: str) -> Path:
|
||||
path_validator = click.Path(
|
||||
exists=True,
|
||||
file_okay=is_file,
|
||||
dir_okay=not is_file,
|
||||
path_type=Path,
|
||||
)
|
||||
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. "
|
||||
"Move it to that location, type the path or drag and drop it 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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user