Skip to content

Commit

Permalink
fix: make wizard state work in resume mode too
Browse files Browse the repository at this point in the history
prompt() cannot have any side effects since it is not called
in resume mode.
  • Loading branch information
joanise committed Oct 2, 2024
1 parent d3ed1dc commit af7d71b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion everyvoice/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
):
Expand Down
5 changes: 4 additions & 1 deletion everyvoice/wizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
Expand Down
39 changes: 26 additions & 13 deletions everyvoice/wizard/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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."
)
Expand All @@ -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?",
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit af7d71b

Please sign in to comment.