draft: improved searching one page only

This commit is contained in:
2024-05-27 16:37:55 +02:00
parent a0e42fc6ee
commit 999299c32a
2 changed files with 54 additions and 33 deletions

View File

@@ -125,7 +125,11 @@ class Select:
return self._parse_option_key(key) in self._key_to_option
def __getitem__(self, key: Any) -> Option:
return self._key_to_option[self._parse_option_key(key)]
r = self._key_to_option[self._parse_option_key(key)]
# check if is callable
if callable(r.value):
r.value = r.value()
return r
def create_option(self, key: Any, **kwargs) -> Option:
if not self.can_create_options:
@@ -186,20 +190,36 @@ class GenreSelect(StringSelect):
super().__init__(sort=True, raw_options=(genre.name for genre in filter(self.is_valid_genre, main_settings["music_directory"].iterdir())))
class SourceTypeToOption(dict):
def __init__(self, callback):
super().__init__()
self.callback = callback
def __missing__(self, key):
self[key] = self.callback(key)
return self[key]
class DataObjectSelect(Select):
def __init__(self, data_objects: Generator[DataObject]):
self._source_type_to_data_objects: Dict[SourceType, List[Option]] = defaultdict(list)
self._source_type_to_option: Dict[SourceType, Option] = SourceTypeToOption(self.option_from_source_type)
self._data_object_index: int = 0
self._source_type_index: int = 0
super().__init__()
super().__init__(
parse_option_key=lambda x: unify(str(x)),
)
self.extend(data_objects)
def option_from_data_object(self, data_object: DataObject) -> Option:
index = self._data_object_index
self._data_object_index += 1
return Option(
value=data_object,
keys=[index, data_object.option_string, data_object.title_string],
@@ -210,12 +230,16 @@ class DataObjectSelect(Select):
index = ALPHABET[self._source_type_index % len(ALPHABET)]
self._source_type_index += 1
return Option(
value=self._source_type_to_data_objects[source_type],
o = Option(
value=lambda: DataObjectSelect(self._source_type_to_data_objects[source_type]),
keys=[index, source_type],
text=f"{BColors.HEADER.value}({index}) --------------------------------{source_type.name:{'-'}<{21}}--------------------{BColors.ENDC.value}",
)
super().append(o)
return o
def append(self, option: Union[Option, DataObject]):
if isinstance(option, DataObject):
data_object = option
@@ -224,15 +248,19 @@ class DataObjectSelect(Select):
data_object = option.value
for source_type in data_object.source_collection.source_types(only_with_page=True):
if source_type not in self._source_type_to_data_objects:
st_option = self.option_from_source_type(source_type)
self._source_type_to_data_objects[source_type].append(st_option)
super().append(st_option)
self._source_type_to_data_objects[source_type].append(option)
super().append(option)
def __iter__(self):
for options in self._source_type_to_data_objects.values():
yield from options
source_types = list(sorted(self._source_type_to_data_objects.keys(), key=lambda x: x.name))
has_limit = len(source_types) > 1
for st in source_types:
if has_limit:
yield self._source_type_to_option[st]
limit = min(15, len(self._source_type_to_data_objects[st])) if has_limit else len(self._source_type_to_data_objects[st])
for i in range(limit):
yield self._source_type_to_data_objects[st][i]