feat: implemented better components
This commit is contained in:
158
music_kraken/download/components/__init__.py
Normal file
158
music_kraken/download/components/__init__.py
Normal file
@@ -0,0 +1,158 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from typing import (Any, Callable, Dict, Generator, Generic, Hashable, List,
|
||||
Optional, Tuple, TypeVar, Union)
|
||||
|
||||
from ...objects import OuterProxy as DataObject
|
||||
from ...utils import BColors
|
||||
from ...utils.config import main_settings
|
||||
from ...utils.enums import SourceType
|
||||
from ...utils.exception import MKComposeException
|
||||
from ...utils.shared import ALPHABET
|
||||
from ...utils.string_processing import unify
|
||||
|
||||
P = TypeVar('P')
|
||||
|
||||
|
||||
class HumanIO:
|
||||
@staticmethod
|
||||
def ask_to_create(option: Option) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def not_found(key: Any) -> None:
|
||||
return None
|
||||
|
||||
|
||||
class Option(Generic[P]):
|
||||
"""
|
||||
This could represent a data object, a string or a page.
|
||||
"""
|
||||
TEXT_TEMPLATE: str = f"{BColors.BOLD.value}{{index}}{BColors.ENDC.value}: {{value}}"
|
||||
ATTRIBUTES_FORMATTING: Tuple[str, ...] = ("index", "value")
|
||||
ATTRIBUTES_KEY: Tuple[str, ...] = ("index", )
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
value: P,
|
||||
hidden: bool = False,
|
||||
additional_keys: List[Hashable] = None,
|
||||
**kwargs
|
||||
):
|
||||
self.value = value
|
||||
self.hidden = hidden
|
||||
self._additional_keys = set(self._to_hash(key) for key in additional_keys or [])
|
||||
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
def _to_option_string(self, value: Any) -> str:
|
||||
if hasattr(value, "option_string"):
|
||||
return value.option_string
|
||||
|
||||
return str(value)
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
text = self.TEXT_TEMPLATE
|
||||
|
||||
for attribute_key in self.ATTRIBUTES_FORMATTING:
|
||||
text = text.replace(f"{{{attribute_key}}}", self._to_option_string(getattr(self, attribute_key)))
|
||||
|
||||
return text
|
||||
|
||||
def _to_hash(self, key: Any) -> int:
|
||||
try:
|
||||
key = int(key)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if isinstance(key, str):
|
||||
return hash(unify(key))
|
||||
|
||||
return hash(key)
|
||||
|
||||
@property
|
||||
def keys(self) -> set:
|
||||
keys = self._additional_keys.copy()
|
||||
|
||||
for key in self.ATTRIBUTES_KEY:
|
||||
keys.add(self._to_hash(getattr(self, key)))
|
||||
|
||||
def __contains__(self, key: Any) -> bool:
|
||||
return self._to_hash(key) in self.keys
|
||||
|
||||
def __str__(self):
|
||||
return self.text
|
||||
|
||||
|
||||
class Select(Generic[P]):
|
||||
OPTION: Type[Option[P]] = Option
|
||||
HUMAN_IO: Type[HumanIO] = HumanIO
|
||||
CAN_CREATE_OPTIONS: bool = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: Generator[P, None, None],
|
||||
**kwargs
|
||||
):
|
||||
self.option: Type[Option[P]] = kwargs.get("option", self.OPTION)
|
||||
self.human_io: Type[HumanIO] = kwargs.get("human_io", self.HUMAN_IO)
|
||||
self.can_create_options: bool = kwargs.get("can_create_options", self.CAN_CREATE_OPTIONS)
|
||||
|
||||
self._options: List[Option[P]] = []
|
||||
self.extend(data)
|
||||
|
||||
def append(self, value: P) -> Option[P]:
|
||||
option = self.option(value)
|
||||
self._options.append(option)
|
||||
return option
|
||||
|
||||
def extend(self, values: Generator[P, None, None]):
|
||||
for value in values:
|
||||
self.append(value)
|
||||
|
||||
@property
|
||||
def _options_to_show(self) -> Generator[Option[P], None, None]:
|
||||
for option in self._options:
|
||||
if option.hidden:
|
||||
continue
|
||||
|
||||
yield option
|
||||
|
||||
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
|
||||
|
||||
def __contains__(self, key: Any) -> bool:
|
||||
for option in self._options:
|
||||
if key in option:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def __getitem__(self, key: Any) -> Option[P]:
|
||||
for option in self._options:
|
||||
if key in option:
|
||||
return option
|
||||
|
||||
raise KeyError(key)
|
||||
|
||||
def choose(self, key: Any) -> Optional[Option[P]]:
|
||||
try:
|
||||
return self[key]
|
||||
except KeyError:
|
||||
if self.can_create_options:
|
||||
return self.append(key)
|
||||
|
||||
self.human_io.not_found(key)
|
||||
|
||||
def pprint(self) -> str:
|
||||
return "\n".join(str(option) for option in self)
|
||||
1
music_kraken/download/components/generic.py
Normal file
1
music_kraken/download/components/generic.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import Option, Select
|
||||
Reference in New Issue
Block a user