WIP: feature/cleanup_programming_interface #40
@ -1,3 +1,12 @@
|
||||
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:
|
||||
@ -42,4 +51,3 @@ def get_genre() -> str:
|
||||
genre = select_genre.choose(input("> "))
|
||||
|
||||
return genre.value
|
||||
|
@ -49,6 +49,8 @@ class Option(Generic[P]):
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
super(Option, self).__init__()
|
||||
|
||||
def _to_option_string(self, value: Any) -> str:
|
||||
if hasattr(value, "option_string"):
|
||||
return value.option_string
|
||||
@ -88,6 +90,9 @@ class Option(Generic[P]):
|
||||
def __str__(self):
|
||||
return self.text
|
||||
|
||||
def __iter__(self) -> Generator[Option[P], None, None]:
|
||||
yield self
|
||||
|
||||
|
||||
class Select(Generic[P]):
|
||||
OPTION: Type[Option[P]] = Option
|
||||
@ -106,6 +111,8 @@ class Select(Generic[P]):
|
||||
self._options: List[Option[P]] = []
|
||||
self.extend(data)
|
||||
|
||||
super(Select, self).__init__(**kwargs)
|
||||
|
||||
def append(self, value: P) -> Option[P]:
|
||||
option = self.option(value)
|
||||
self._options.append(option)
|
||||
@ -126,10 +133,11 @@ class Select(Generic[P]):
|
||||
def __iter__(self) -> Generator[Option, None, None]:
|
||||
_index = 0
|
||||
|
||||
for i, option in enumerate(self._options_to_show):
|
||||
option.index = _index
|
||||
yield option
|
||||
_index += 1
|
||||
for i, o in enumerate(self._options_to_show):
|
||||
for option in o:
|
||||
option.index = _index
|
||||
yield option
|
||||
_index += 1
|
||||
|
||||
def __contains__(self, key: Any) -> bool:
|
||||
for option in self._options:
|
||||
@ -156,3 +164,18 @@ class Select(Generic[P]):
|
||||
|
||||
def pprint(self) -> str:
|
||||
return "\n".join(str(option) for option in self)
|
||||
|
||||
|
||||
class OptionGroup(Option[P], Select[P]):
|
||||
ALPHABET: str = "abcdefghijklmnopqrstuvwxyz"
|
||||
ATTRIBUTES_FORMATTING: Tuple[str, ...] = ("alphabetic_index", "value")
|
||||
|
||||
TEXT_TEMPLATE: str = f"{BColors.HEADER.value}{{alphabetic_index}}) {{value}}{BColors.ENDC.value}"
|
||||
|
||||
@property
|
||||
def alphabetic_index(self) -> str:
|
||||
return self.ALPHABET[self.index % len(self.ALPHABET)]
|
||||
|
||||
def __init__(self, value: P, data: Generator[P, None, None] **kwargs):
|
||||
super(OptionGroup, self).__init__(value=value, data=data, **kwargs)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user