Fix navigation commands in TVShowManager class

- Ensure `TVShowManager` correctly handles `next` and `prev` commands during global search.
- Update `run` method to properly interpret and process user commands.
- Improve user input handling and command validation in the `run` method.
This commit is contained in:
Cristian Porzio 2024-12-29 19:51:17 +01:00
parent 3f53015487
commit 836be2b1bb

View File

@ -20,209 +20,208 @@ from .call_stack import get_call_stack
class TVShowManager:
def __init__(self):
"""
Initialize TVShowManager with provided column information.
"""
self.console = Console()
self.tv_shows: List[Dict[str, Any]] = [] # List to store TV show data as dictionaries
def __init__(self, console=Console(), global_search=False):
self.isGlobal = global_search
self.console = console
self.tv_shows: List[Dict[str, Any]] = []
self.slice_start: int = 0
self.slice_end: int = 5
self.step: int = self.slice_end
self.column_info = []
def set_slice_end(self, new_slice: int) -> None:
"""
Set the end of the slice for displaying TV shows.
Parameters:
- new_slice (int): The new value for the slice end.
"""
self.slice_end = new_slice
self.step = new_slice
def add_column(self, column_info: Dict[str, Dict[str, str]]) -> None:
"""
Add column information.
Parameters:
- column_info (Dict[str, Dict[str, str]]): Dictionary containing column names, their colors, and justification.
"""
self.column_info = column_info
def add_tv_show(self, tv_show: Dict[str, Any]):
"""
Add a TV show to the list of TV shows.
Parameters:
- tv_show (Dict[str, Any]): Dictionary containing TV show details.
"""
self.tv_shows.append(tv_show)
def display_data(self, data_slice: List[Dict[str, Any]]):
"""
Display TV show data in a tabular format.
Parameters:
- data_slice (List[Dict[str, Any]]): List of dictionaries containing TV show details to display.
"""
table = Table(border_style="white")
# Add columns dynamically based on provided column information
for col_name, col_style in self.column_info.items():
color = col_style.get("color", None)
if color:
style = Style(color=color)
else:
style = None
style = Style(color=color) if color else None
table.add_column(col_name, style=style, justify='center')
# Add rows dynamically based on available TV show data
for entry in data_slice:
# Create row data while handling missing keys
row_data = [entry.get(col_name, '') for col_name in self.column_info.keys()]
table.add_row(*row_data)
self.console.print(table)
def run_back_command(self, research_func: dict):
"""
Executes a back-end search command by dynamically importing a module and invoking its search function.
Args:
research_func (dict): A dictionary containing:
- 'folder' (str): The absolute path to the directory containing the module to be executed.
"""
try:
# Get site name from folder
site_name = (os.path.basename(research_func['folder']))
# Find the project root directory
site_name = os.path.basename(research_func['folder'])
current_path = research_func['folder']
while not os.path.exists(os.path.join(current_path, 'StreamingCommunity')):
current_path = os.path.dirname(current_path)
# Add project root to Python path
project_root = current_path
#print(f"[DEBUG] Project Root: {project_root}")
if project_root not in sys.path:
sys.path.insert(0, project_root)
# Import using full absolute import
module_path = f'StreamingCommunity.Api.Site.{site_name}'
#print(f"[DEBUG] Importing module: {module_path}")
# Import the module
module = importlib.import_module(module_path)
# Get the search function
search_func = getattr(module, 'search')
# Call the search function with the search string
search_func(None)
except Exception as e:
self.console.print(f"[red]Error during search: {e}")
# Print detailed traceback
import traceback
traceback.print_exc()
# Optionally remove the path if you want to clean up
if project_root in sys.path:
sys.path.remove(project_root)
'''
def run(self, force_int_input: bool = False, max_int_input: int = 0) -> str:
"""
Run the TV show manager application.
Parameters:
- force_int_input(bool): If True, only accept integer inputs from 0 to max_int_input
- max_int_input (int): range of row to show
Returns:
str: Last command executed before breaking out of the loop.
"""
total_items = len(self.tv_shows)
last_command = "" # Variable to store the last command executed
last_command = ""
while True:
start_message()
# Display table
self.display_data(self.tv_shows[self.slice_start:self.slice_end])
research_func = None
for reverse_fun in get_call_stack():
if reverse_fun['function'] == 'search' and reverse_fun['script'] == '__init__.py':
research_func = reverse_fun
logging.info(f"Found research_func: {research_func}")
if self.slice_end < total_items:
self.console.print(f"\n[green]Press [red]Enter [green]for next page, [red]'q' [green]to quit, [red]'back' [green]to search, [red]'next' [green]for next manager, or [red]'prev' [green]for previous manager.")
if not force_int_input:
key = Prompt.ask(
"\n[cyan]Insert media index [yellow](e.g., 1), [red]* [cyan]to download all media, "
"[yellow](e.g., 1-2) [cyan]for a range of media, or [yellow](e.g., 3-*) [cyan]to download from a specific index to the end"
)
else:
choices = [str(i) for i in range(0, max_int_input)]
choices.extend(["q", "", "back", "next", "prev"])
key = Prompt.ask("[cyan]Insert media [red]index", choices=choices, show_choices=False)
last_command = key
if key.lower() == "q":
return "quit"
elif key == "":
self.slice_start += self.step
self.slice_end += self.step
if self.slice_end > total_items:
self.slice_end = total_items
elif key.lower() == "back" and research_func:
self.run_back_command(research_func)
elif key.lower() == "next":
return "next"
elif key.lower() == "prev":
return "prev"
else:
break
else:
self.console.print(f"\n [green]You've reached the end. [red]Enter [green]for first page, [red]'q' [green]to quit, [red]'back' [green]to search, [red]'next' [green]for next manager, or [red]'prev' [green]for previous manager.")
if not force_int_input:
key = Prompt.ask(
"\n[cyan]Insert media index [yellow](e.g., 1), [red]* [cyan]to download all media, "
"[yellow](e.g., 1-2) [cyan]for a range of media, or [yellow](e.g., 3-*) [cyan]to download from a specific index to the end"
)
else:
choices = [str(i) for i in range(0, max_int_input)]
choices.extend(["q", "", "back", "next", "prev"])
key = Prompt.ask("[cyan]Insert media [red]index", choices=choices, show_choices=False)
last_command = key
if key.lower() == "q":
return "quit"
elif key == "":
self.slice_start = 0
self.slice_end = self.step
elif key.lower() == "back" and research_func:
self.run_back_command(research_func)
elif key.lower() == "next":
return "next"
elif key.lower() == "prev":
return "prev"
else:
break
return last_command
'''
# Find research function from call stack
def run(self, force_int_input: bool = False, max_int_input: int = 0) -> str:
total_items = len(self.tv_shows)
last_command = ""
while True:
start_message()
self.display_data(self.tv_shows[self.slice_start:self.slice_end])
research_func = None
for reverse_fun in get_call_stack():
if reverse_fun['function'] == 'search' and reverse_fun['script'] == '__init__.py':
research_func = reverse_fun
logging.info(f"Found research_func: {research_func}")
# Handling user input for loading more items or quitting
if self.slice_end < total_items:
self.console.print(f"\n[green]Press [red]Enter [green]for next page, [red]'q' [green]to quit, or [red]'back' [green]to search.")
# Display different options based on whether it's a global search or not
if self.isGlobal:
self.console.print(
f"\n[green]Press [red]Enter [green]for next page, [red]'q' [green]to quit, [red]'back' [green]to search, [red]'next' [green]for next provider, or [red]'prev' [green]for previous provider.")
else:
self.console.print(
f"\n[green]Press [red]Enter [green]for next page, [red]'q' [green]to quit, or [red]'back' [green]to search.")
if not force_int_input:
key = Prompt.ask(
"\n[cyan]Insert media index [yellow](e.g., 1), [red]* [cyan]to download all media, "
"[yellow](e.g., 1-2) [cyan]for a range of media, or [yellow](e.g., 3-*) [cyan]to download from a specific index to the end"
)
else:
choices = [str(i) for i in range(0, max_int_input)]
choices.extend(["q", "", "back"])
if self.isGlobal:
choices.extend(["q", "", "back", "next", "prev"])
else:
choices.extend(["q", "", "back"])
key = Prompt.ask("[cyan]Insert media [red]index", choices=choices, show_choices=False)
last_command = key
if key.lower() == "q":
break
return "quit"
elif key == "":
self.slice_start += self.step
self.slice_end += self.step
if self.slice_end > total_items:
self.slice_end = total_items
elif key.lower() == "back" and research_func:
self.run_back_command(research_func)
elif key.lower() == "next" and self.isGlobal:
return "next"
elif key.lower() == "prev" and self.isGlobal:
return "prev"
else:
break
else:
# Last slice, ensure all remaining items are shown
self.console.print(f"\n [green]You've reached the end. [red]Enter [green]for first page, [red]'q' [green]to quit, or [red]'back' [green]to search.")
# Display different options based on whether it's a global search or not
if self.isGlobal:
self.console.print(
f"\n [green]You've reached the end. [red]Enter [green]for first page, [red]'q' [green]to quit, [red]'back' [green]to search, [red]'next' [green]for next manager, or [red]'prev' [green]for previous manager.")
else:
self.console.print(
f"\n [green]You've reached the end. [red]Enter [green]for first page, [red]'q' [green]to quit, or [red]'back' [green]to search.")
if not force_int_input:
key = Prompt.ask(
"\n[cyan]Insert media index [yellow](e.g., 1), [red]* [cyan]to download all media, "
"[yellow](e.g., 1-2) [cyan]for a range of media, or [yellow](e.g., 3-*) [cyan]to download from a specific index to the end"
)
else:
choices = [str(i) for i in range(0, max_int_input)]
choices.extend(["q", "", "back"])
if self.isGlobal:
choices.extend(["q", "", "back", "next", "prev"])
else:
choices.extend(["q", "", "back"])
key = Prompt.ask("[cyan]Insert media [red]index", choices=choices, show_choices=False)
last_command = key
if key.lower() == "q":
break
return "quit"
elif key == "":
self.slice_start = 0
self.slice_end = self.step
elif key.lower() == "back" and research_func:
self.run_back_command(research_func)
elif key.lower() == "next" and self.isGlobal:
return "next"
elif key.lower() == "prev" and self.isGlobal:
return "prev"
else:
break
return last_command
def clear(self):