import re from pathlib import Path from typing import Dict, Generator, List, Set, Type, Union from ..download import Downloader, Page, components from ..utils.config import main_settings from .utils import ask_for_bool, cli_function class GenreIO(components.HumanIO): @staticmethod def ask_to_create(option: components.Option) -> bool: output() return ask_for_bool(f"create the genre {BColors.OKBLUE.value}{option.value}{BColors.ENDC.value}") @staticmethod def not_found(key: str) -> None: output(f"\ngenre {BColors.BOLD.value}{key}{BColors.ENDC.value} not found\n", color=BColors.FAIL) def _genre_generator() -> Generator[str, None, None]: def is_valid_genre(genre: Path) -> bool: """ 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. """ if not genre.is_dir(): return False if any(re.match(regex_pattern, genre.name) for regex_pattern in main_settings["not_a_genre_regex"]): return False return True for genre in filter(is_valid_genre, main_settings["music_directory"].iterdir()): yield genre.name def get_genre() -> str: select_genre = components.Select( human_io=GenreIO, can_create_options=True, data=_genre_generator(), ) genre: Optional[components.Option[str]] = None while genre is None: print(select_genre.pprint()) print() genre = select_genre.choose(input("> ")) return genre.value