feat: moved cli helpers to seperate file
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2024-05-27 13:41:24 +02:00
parent 0f2229b0f2
commit 49145a7d93
4 changed files with 177 additions and 154 deletions

View File

@@ -1,11 +1,13 @@
import random
import re
from pathlib import Path
from typing import Dict, List, Set, Type
from typing import Dict, Generator, List, Set, Type
from .. import console
from ..download import Downloader, Page
from ..download.results import GoToResults, Option, PageResults, Results
from ..download import Downloader, Page, components
from ..download.results import GoToResults
from ..download.results import Option as ResultOption
from ..download.results import PageResults, Results
from ..objects import Album, Artist, DatabaseObject, Song
from ..utils import BColors, output
from ..utils.config import main_settings, write_config
@@ -17,7 +19,7 @@ from ..utils.string_processing import fit_to_file_system
from ..utils.support_classes.download_result import DownloadResult
from ..utils.support_classes.query import Query
from .options.first_config import initial_config
from .utils import cli_function
from .utils import ask_for_bool, cli_function
EXIT_COMMANDS = {"q", "quit", "exit", "abort"}
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
@@ -25,50 +27,19 @@ PAGE_NAME_FILL = "-"
MAX_PAGE_LEN = 21
def get_existing_genre() -> List[str]:
"""
gets the name of all subdirectories of shared.MUSIC_DIR,
but filters out all directories, where the name matches with any patern
from shared.NOT_A_GENRE_REGEX.
"""
existing_genres: List[str] = []
# get all subdirectories of MUSIC_DIR, not the files in the dir.
existing_subdirectories: List[Path] = [f for f in main_settings["music_directory"].iterdir() if f.is_dir()]
for subdirectory in existing_subdirectories:
name: str = subdirectory.name
if not any(re.match(regex_pattern, name) for regex_pattern in main_settings["not_a_genre_regex"]):
existing_genres.append(name)
existing_genres.sort()
return existing_genres
def get_genre():
existing_genres = get_existing_genre()
for i, genre_option in enumerate(existing_genres):
print(f"{i + 1:0>2}: {genre_option}")
select_genre = components.GenreSelect()
select_genre._ask_for_creating_option = lambda key: ask_for_bool(f"Create the genre \"{key}\"")
while True:
genre = input("Id or new genre: ")
genre: Optional[components.Option] = None
if genre.isdigit():
genre_id = int(genre) - 1
if genre_id >= len(existing_genres):
print(f"No genre under the id {genre_id + 1}.")
continue
while genre is None:
for genre in select_genre:
print(genre)
return existing_genres[genre_id]
genre = select_genre.choose(input("Id or new genre: "))
new_genre = fit_to_file_system(genre)
agree_inputs = {"y", "yes", "ok"}
verification = input(f"create new genre \"{new_genre}\"? (Y/N): ").lower()
if verification in agree_inputs:
return new_genre
return genre.value
def help_message():
@@ -111,7 +82,7 @@ class CliDownloader:
page_count = 0
for option in self.current_results.formatted_generator():
if isinstance(option, Option):
if isinstance(option, ResultOption):
r = f"{BColors.GREY.value}{option.index:0{self.option_digits}}{BColors.ENDC.value} {option.music_object.option_string}"
print(r)
else:

View File

@@ -39,4 +39,8 @@ def print_cute_message():
print(message)
AGREE_INPUTS = {"y", "yes", "ok"}
def ask_for_bool(msg: str) -> bool:
i = input(msg + " (Y/N):").lower()
return i in AGREE_INPUTS