fixed rpr

This commit is contained in:
Hellow 2023-03-28 18:40:56 +02:00
parent c67d8fba61
commit 0a7dfeb5ea
2 changed files with 48 additions and 48 deletions

View File

@ -7,7 +7,7 @@ def search_pages():
print("audio", search.audio_pages) print("audio", search.audio_pages)
print() print()
print(search._current_option) print(search)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -9,114 +9,114 @@ from ...objects import Options, DatabaseObject
class MultiPageOptions: class MultiPageOptions:
def __init__( def __init__(
self, self,
max_displayed_options: int = 10, max_displayed_options: int = 10,
option_digits: int = 3 option_digits: int = 3
) -> None: ) -> None:
self.max_displayed_options = max_displayed_options self.max_displayed_options = max_displayed_options
self.option_digits: int = option_digits self.option_digits: int = option_digits
self._current_option_dict: Dict[Type[Page], Options] = defaultdict(lambda: Options()) self._current_option_dict: Dict[Type[Page], Options] = defaultdict(lambda: Options())
def __getitem__(self, key: Type[Page]): def __getitem__(self, key: Type[Page]):
return self._current_option_dict[key] return self._current_option_dict[key]
def __setitem__(self, key: Type[Page], value: Options): def __setitem__(self, key: Type[Page], value: Options):
self._current_option_dict[key] = value self._current_option_dict[key] = value
def string_from_all_pages(self) -> str: def string_from_all_pages(self) -> str:
lines: List[str] = [] lines: List[str] = []
page_name_fill = "-" page_name_fill = "-"
max_page_len = 21 max_page_len = 21
j = 0 j = 0
for page, options in self._current_option_dict.items(): for page, options in self._current_option_dict.items():
lines.append(f"----------{page.__name__:{page_name_fill}<{max_page_len}}----------") lines.append(f"----------{page.__name__:{page_name_fill}<{max_page_len}}----------")
option_obj: DatabaseObject option_obj: DatabaseObject
for i, option_obj in enumerate(options): for i, option_obj in enumerate(options):
if i >= self.max_displayed_options: if i >= self.max_displayed_options:
lines.append("...") lines.append("...")
break break
lines.append(f"{j+i:0{self.option_digits}} {option_obj.option_string}") lines.append(f"{j + i:0{self.option_digits}} {option_obj.option_string}")
j += i + 1 j += i + 1
return "\n".join(lines) return "\n".join(lines)
def choose_from_all_pages(self, index: int) -> DatabaseObject: def choose_from_all_pages(self, index: int) -> DatabaseObject:
j = 0 j = 0
for page, options in self._current_option_dict.items(): for page, options in self._current_option_dict.items():
option_len = len(options) option_len = len(options)
if option_len > self.max_displayed_options: if option_len > self.max_displayed_options:
option_len = self.max_displayed_options option_len = self.max_displayed_options
if index < j + option_len: if index < j + option_len:
return options[j+option_len-1] return options[j + option_len - 1]
j += option_len - 1 j += option_len - 1
raise KeyError("index is out of range") raise KeyError("index is out of range")
def string_from_single_page(self, page: Type[Page]) -> str: def string_from_single_page(self, page: Type[Page]) -> str:
lines: List[str] = [] lines: List[str] = []
page_name_fill = "-" page_name_fill = "-"
max_page_len = 21 max_page_len = 21
lines.append(f"----------{page.__name__:{page_name_fill}<{max_page_len}}----------") lines.append(f"----------{page.__name__:{page_name_fill}<{max_page_len}}----------")
option_obj: DatabaseObject option_obj: DatabaseObject
for i, option_obj in enumerate(self._current_option_dict[page]): for i, option_obj in enumerate(self._current_option_dict[page]):
lines.append(f"{i:0{self.option_digits}} {option_obj.option_string}") lines.append(f"{i:0{self.option_digits}} {option_obj.option_string}")
return "\n".join(lines) return "\n".join(lines)
def choose_from_single_page(self, page: Type[Page], index: int) -> DatabaseObject: def choose_from_single_page(self, page: Type[Page], index: int) -> DatabaseObject:
return self._current_option_dict[page][index] return self._current_option_dict[page][index]
def __repr__(self) -> str: def __repr__(self) -> str:
return self.string_from_all_pages() return self.string_from_all_pages()
class Search: class Search:
def __init__( def __init__(
self, self,
query: str, query: str,
pages: Tuple[Type[Page]] = page_attributes.ALL_PAGES, pages: Tuple[Type[Page]] = page_attributes.ALL_PAGES,
exclude_pages: Set[Type[Page]] = set(), exclude_pages: Set[Type[Page]] = set(),
exclude_shady: bool = False, exclude_shady: bool = False,
max_displayed_options: int = 10, max_displayed_options: int = 10,
option_digits: int = 3 option_digits: int = 3
) -> None: ) -> None:
_page_list: List[Type[Page]] = [] _page_list: List[Type[Page]] = []
_audio_page_list: List[Type[Page]] = [] _audio_page_list: List[Type[Page]] = []
for page in pages: for page in pages:
if exclude_shady and page in page_attributes.SHADY_PAGES: if exclude_shady and page in page_attributes.SHADY_PAGES:
continue continue
if page in exclude_pages: if page in exclude_pages:
continue continue
_page_list.append(page) _page_list.append(page)
if page in page_attributes.AUDIO_PAGES: if page in page_attributes.AUDIO_PAGES:
_audio_page_list.append(page) _audio_page_list.append(page)
self.pages: Tuple[Type[Page]] = tuple(_page_list) self.pages: Tuple[Type[Page]] = tuple(_page_list)
self.audio_pages: Tuple[Type[Page]] = tuple(_audio_page_list) self.audio_pages: Tuple[Type[Page]] = tuple(_audio_page_list)
self.max_displayed_options = max_displayed_options self.max_displayed_options = max_displayed_options
self.option_digits: int = option_digits self.option_digits: int = option_digits
self._option_history: List[MultiPageOptions] = [] self._option_history: List[MultiPageOptions] = []
self._current_option: MultiPageOptions = self.next_options self._current_option: MultiPageOptions = self.next_options
self.search(query) self.search(query)
@property @property
def next_options(self) -> MultiPageOptions: def next_options(self) -> MultiPageOptions:
mpo = MultiPageOptions( mpo = MultiPageOptions(
@ -125,13 +125,12 @@ class Search:
) )
self._option_history.append(mpo) self._option_history.append(mpo)
return mpo return mpo
@property @property
def previous_options(self) -> MultiPageOptions: def previous_options(self) -> MultiPageOptions:
self._option_history.pop() self._option_history.pop()
return self._option_history[-1] return self._option_history[-1]
def search(self, query: str): def search(self, query: str):
""" """
# The Query # The Query
@ -141,8 +140,9 @@ class Search:
followed by a space "#a Psychonaut 4 #r Tired, Numb and #t Drop by Drop" followed by a space "#a Psychonaut 4 #r Tired, Numb and #t Drop by Drop"
if no # is in the query it gets treated as "unspecified query" if no # is in the query it gets treated as "unspecified query"
""" """
for page in self.pages: for page in self.pages:
self._current_option[page] = page.search_by_query(query=query) self._current_option[page] = page.search_by_query(query=query)
def __repr__(self):
return self._current_option.__repr__()