From af7d71bc6182bade42dbc997004f489c8eeeb208 Mon Sep 17 00:00:00 2001 From: Eric Joanis Date: Wed, 2 Oct 2024 15:03:28 -0400 Subject: [PATCH] fix: make wizard state work in resume mode too prompt() cannot have any side effects since it is not called in resume mode. --- everyvoice/cli.py | 2 +- everyvoice/wizard/__init__.py | 5 ++++- everyvoice/wizard/dataset.py | 39 +++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/everyvoice/cli.py b/everyvoice/cli.py index 44793537..40796222 100644 --- a/everyvoice/cli.py +++ b/everyvoice/cli.py @@ -316,7 +316,7 @@ def new_project( exists=True, dir_okay=False, file_okay=True, - help="Saved Q&A list to resume from", + help="Resume from previously saved progress.", autocompletion=complete_path, ), ): diff --git a/everyvoice/wizard/__init__.py b/everyvoice/wizard/__init__.py index da3c855b..d46dd2df 100644 --- a/everyvoice/wizard/__init__.py +++ b/everyvoice/wizard/__init__.py @@ -35,7 +35,10 @@ def __init__(self, name: str): self.name = name def prompt(self): - """Implement this method to prompt the user and return the step's response.""" + """Implement this method to prompt the user and return the step's response. + + This method must not have side effects, as it will not get called in resume mode. + """ raise NotImplementedError( f"This step ({self.name}) doesn't have a prompt method implemented. Please implement one." ) diff --git a/everyvoice/wizard/dataset.py b/everyvoice/wizard/dataset.py index 6c757747..c345e483 100644 --- a/everyvoice/wizard/dataset.py +++ b/everyvoice/wizard/dataset.py @@ -527,18 +527,22 @@ class HasSpeakerStep(Step): REVERSIBLE = True choices = ("no", "yes") - def prompt(self): + def can_have_speaker_column(self) -> bool: + assert self.state is not None if self.state[StepNames.filelist_format_step] == "festival": - self.AUTOMATIC = True - return "no" + return False elif len(self.state.get("selected_headers", [])) >= len( self.state["filelist_data_list"][0] ): - self.AUTOMATIC = True - rich_print("No columns left, we will assume you have no speaker column.") + return False + else: + return True + + def prompt(self): + if not self.can_have_speaker_column(): + rich_print("No columns available to have a speaker column.") return "no" else: - self.AUTOMATIC = False return get_response_from_menu_prompt( prompt_text="Does your data have a column/value for the speaker?", choices=self.choices, @@ -548,6 +552,7 @@ def validate(self, response): return response in self.choices def effect(self): + self.AUTOMATIC = not self.can_have_speaker_column() rich_print( "Note: if your dataset has speakers with names matching with speakers from other provided datasets, they will be considered the same. If this is not the desired behaviour, you will have to alter the speaker IDs in the relevant datasets to indicate that they are different." ) @@ -570,8 +575,11 @@ class KnowSpeakerStep(Step): REVERSIBLE = True choices = ("no", "yes") + @property + def dataset_index(self) -> str: + return self.state_subset.split("_")[-1] + def prompt(self): - self.dataset_index = self.state_subset.split("_")[-1] return get_response_from_menu_prompt( choices=self.choices, title=f"Since your data does not have a speaker column, we will use a default ID of 'speaker_{self.dataset_index}'. Would you like to specify an alternative speaker ID for this dataset instead?", @@ -621,18 +629,22 @@ class HasLanguageStep(Step): REVERSIBLE = True choices = ("no", "yes") - def prompt(self): + def can_have_language_column(self) -> bool: + assert self.state is not None if self.state[StepNames.filelist_format_step] == "festival": - self.AUTOMATIC = True - return "no" + return False elif len(self.state.get("selected_headers", [])) >= len( self.state["filelist_data_list"][0] ): - rich_print("No columns left, we will assume you have no language column.") - self.AUTOMATIC = True + return False + else: + return True + + def prompt(self): + if not self.can_have_language_column(): + rich_print("No columns available to have a speaker column.") return "no" else: - self.AUTOMATIC = False return get_response_from_menu_prompt( prompt_text="Does your data have a column/value for the language?", choices=self.choices, @@ -642,6 +654,7 @@ def validate(self, response): return response in self.choices def effect(self): + self.AUTOMATIC = not self.can_have_language_column() if self.state[StepNames.data_has_language_value_step] == "yes": self.tour.add_step( LanguageHeaderStep(