refactored to print the search in the main file for easyer editing of cli things

This commit is contained in:
Lars Noack 2022-11-08 10:39:27 +01:00
parent d8b0a771cf
commit 273f94e814
2 changed files with 29 additions and 27 deletions

View File

@ -62,12 +62,15 @@ def search_for_metadata():
if input_.lower() == "q": if input_.lower() == "q":
break break
if input_.lower() == "..": if input_.lower() == "..":
search.get_previous_options() print()
print(search.get_previous_options())
continue continue
if input_.isdigit(): if input_.isdigit():
search.choose(int(input_)) print()
print(search.choose(int(input_)))
continue continue
search.search_from_query(input_) print()
print(search.search_from_query(input_))
return search.current_option return search.current_option

View File

@ -44,6 +44,13 @@ class Option:
} }
return f"{type_repr[self.type]}: \"{self.name}\"{self.additional_info}" return f"{type_repr[self.type]}: \"{self.name}\"{self.additional_info}"
class MultipleOptions:
def __init__(self, option_list: List[Option]) -> None:
self.option_list = option_list
def __repr__(self) -> str:
return "\n".join([f"{str(i).zfill(2)}) {choice.__repr__()}" for i, choice in enumerate(self.option_list)])
class Search: class Search:
def __init__(self, logger: logging.Logger) -> None: def __init__(self, logger: logging.Logger) -> None:
@ -52,18 +59,13 @@ class Search:
self.options_history = [] self.options_history = []
self.current_option: Option self.current_option: Option
def append_new_choices(self, new_choices: List[Option]): def append_new_choices(self, new_choices: List[Option]) -> MultipleOptions:
print()
for i, choice in enumerate(new_choices):
print(f"{str(i).zfill(2)}) {choice}")
self.options_history.append(new_choices) self.options_history.append(new_choices)
return MultipleOptions(new_choices)
def get_previous_options(self): def get_previous_options(self):
self.options_history.pop(-1) self.options_history.pop(-1)
print() return MultipleOptions(self.options_history[-1])
for i, choice in enumerate(self.options_history[-1]):
print(f"{str(i).zfill(2)}) {choice}")
@staticmethod @staticmethod
def fetch_new_options_from_artist(artist: Option): def fetch_new_options_from_artist(artist: Option):
@ -195,7 +197,7 @@ class Search:
return results return results
def fetch_new_options(self): def fetch_new_options(self) -> MultipleOptions:
if self.current_option is None: if self.current_option is None:
return -1 return -1
@ -209,17 +211,17 @@ class Search:
elif self.current_option.type == 'recording': elif self.current_option.type == 'recording':
result = self.fetch_new_options_from_record(self.current_option) result = self.fetch_new_options_from_record(self.current_option)
self.append_new_choices(result) return self.append_new_choices(result)
def choose(self, index: int): def choose(self, index: int) -> MultipleOptions:
if len(self.options_history) == 0: if len(self.options_history) == 0:
logging.error("initial query neaded before choosing") logging.error("initial query neaded before choosing")
return -1 return MultipleOptions([])
latest_options = self.options_history[-1] latest_options = self.options_history[-1]
if index >= len(latest_options): if index >= len(latest_options):
logging.error("index outside of options") logging.error("index outside of options")
return -1 return MultipleOptions([])
self.current_option = latest_options[index] self.current_option = latest_options[index]
return self.fetch_new_options() return self.fetch_new_options()
@ -256,14 +258,12 @@ class Search:
for artist_ in artist_list] for artist_ in artist_list]
return resulting_options return resulting_options
def search_from_text(self, artist: str = None, release_group: str = None, recording: str = None): def search_from_text(self, artist: str = None, release_group: str = None, recording: str = None) -> MultipleOptions:
self.logger.info(f"searching specified artist: \"{artist}\", release group: \"{release_group}\", recording: \"{recording}\"") self.logger.info(f"searching specified artist: \"{artist}\", release group: \"{release_group}\", recording: \"{recording}\"")
if artist is None and release_group is None and recording is None: if artist is None and release_group is None and recording is None:
self.logger.error("either artist, release group or recording has to be set") self.logger.error("either artist, release group or recording has to be set")
return -1 return -1
print()
if recording is not None: if recording is not None:
self.logger.info("search for recording") self.logger.info("search for recording")
results = self.search_recording_from_text(artist=artist, release_group=release_group, recording=recording) results = self.search_recording_from_text(artist=artist, release_group=release_group, recording=recording)
@ -274,9 +274,9 @@ class Search:
self.logger.info("search for artist") self.logger.info("search for artist")
results = self.search_artist_from_text(artist=artist) results = self.search_artist_from_text(artist=artist)
self.append_new_choices(results) return self.append_new_choices(results)
def search_from_text_unspecified(self, query: str): def search_from_text_unspecified(self, query: str) -> MultipleOptions:
self.logger.info(f"searching unspecified: \"{query}\"") self.logger.info(f"searching unspecified: \"{query}\"")
results = [] results = []
@ -284,11 +284,11 @@ class Search:
results.extend(self.search_release_group_from_text(query=query)) results.extend(self.search_release_group_from_text(query=query))
results.extend(self.search_recording_from_text(query=query)) results.extend(self.search_recording_from_text(query=query))
self.append_new_choices(results) return self.append_new_choices(results)
def search_from_query(self, query: str): def search_from_query(self, query: str) -> MultipleOptions:
if query is None: if query is None:
return return MultipleOptions([])
""" """
mit # wird ein neuer Parameter gestartet mit # wird ein neuer Parameter gestartet
der Buchstabe dahinter legt die Art des Parameters fest der Buchstabe dahinter legt die Art des Parameters fest
@ -299,8 +299,7 @@ class Search:
""" """
if not '#' in query: if not '#' in query:
self.search_from_text_unspecified(query) return self.search_from_text_unspecified(query)
return
artist = None artist = None
release_group = None release_group = None
@ -328,7 +327,7 @@ class Search:
recording = input_ recording = input_
continue continue
self.search_from_text(artist=artist, release_group=release_group, recording=recording) return self.search_from_text(artist=artist, release_group=release_group, recording=recording)
def automated_demo(): def automated_demo():