Skip to content

Commit

Permalink
feat: adding sanitize_input() to sanitize user's input during a step
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelLarkin committed Mar 20, 2024
1 parent 33c9101 commit 5a42c11
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
10 changes: 4 additions & 6 deletions everyvoice/tests/test_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,14 @@ def test_leading_white_space_in_outpath(self):
with capture_stdout(), tempfile.TemporaryDirectory() as tmpdirname:
with create_pipe_input() as pipe_input:
# NOTE: we use `` to replace the `.` with our path with leading spaces.
pipe_input.send_text(
f"output_dir_with_leading_spaces\n {tmpdirname}\n"
)
pipe_input.send_text(f" {tmpdirname}\n")
with create_app_session(input=pipe_input, output=DummyOutput()):
tour = Tour(
"trimming leading spaces",
[
basic.NameStep(),
name="trimming leading spaces",
steps=[
basic.OutputPathStep(),
],
state={SN.name_step.value: "output_dir_with_leading_spaces"},
)
tour.run()
self.assertFalse(tour.state[SN.output_step.value].startswith(" "))
Expand Down
7 changes: 7 additions & 0 deletions everyvoice/wizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def prompt(self):
f"This step ({self.name}) doesn't have a prompt method implemented. Please implement one."
)

def sanitize_input(self, response):
"""
Perform data sanitization of user provided input.
"""
return response

def validate(self, response) -> bool:
"""Validate the response.
Expand Down Expand Up @@ -92,6 +98,7 @@ def run(self):
If this method returns something truthy, continue, otherwise ask the prompt again.
"""
self.response = self.prompt()
self.response = self.sanitize_input(self.response)
if self.validate(self.response):
self.completed = True
try:
Expand Down
10 changes: 6 additions & 4 deletions everyvoice/wizard/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class NameStep(Step):
DEFAULT_NAME = StepNames.name_step

def prompt(self):
return questionary.text(
return input(
"What would you like to call this project? This name should reflect the model you intend to train, e.g. 'my-sinhala-project' or 'english-french-model' or something similarly descriptive of your project: "
).unsafe_ask()
)

def validate(self, response):
if len(response) == 0:
Expand Down Expand Up @@ -118,14 +118,16 @@ class OutputPathStep(Step):
DEFAULT_NAME = StepNames.output_step

def prompt(self):
path = questionary.path(
return questionary.path(
"Where should the Configuration Wizard save your files?",
default=".",
style=CUSTOM_QUESTIONARY_STYLE,
only_directories=True,
).unsafe_ask()

return path.strip()
def sanitize_input(self, response):
response = super().sanitize_input(response)
return response.strip()

def validate(self, response) -> bool:
path = Path(response)
Expand Down
12 changes: 8 additions & 4 deletions everyvoice/wizard/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ class WavsDirStep(Step):
DEFAULT_NAME = StepNames.wavs_dir_step

def prompt(self):
path = questionary.path(
return questionary.path(
"Where are your audio files?",
style=CUSTOM_QUESTIONARY_STYLE,
only_directories=True,
).unsafe_ask()

return path.strip()
def sanitize_input(self, response):
response = super().sanitize_input(response)
return response.strip()

def validate(self, response) -> bool:
valid_path = validate_path(response, is_dir=True, exists=True)
Expand Down Expand Up @@ -106,11 +108,13 @@ class FilelistStep(Step):
DEFAULT_NAME = StepNames.filelist_step

def prompt(self):
path = questionary.path(
return questionary.path(
"Where is your data filelist?", style=CUSTOM_QUESTIONARY_STYLE
).unsafe_ask()

return path.strip()
def sanitize_input(self, response):
response = super().sanitize_input(response)
return response.strip()

def validate(self, response) -> bool:
return validate_path(response, is_file=True, exists=True)
Expand Down

0 comments on commit 5a42c11

Please sign in to comment.