Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1. Modified the get_selection() , select_languages() and select_data_types() functions #216

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 55 additions & 33 deletions src/scribe_data/cli/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

def get_selection(user_input: str, options: list[str]) -> list[str]:
"""
Parse user input to get selected options.
Parse user input to get selected options. Re-asks the question if input is empty.

Parameters
----------
Expand All @@ -52,12 +52,14 @@ def get_selection(user_input: str, options: list[str]) -> list[str]:
List[str]
The options available in interactive mode and CLI directions.
"""
while not user_input.strip(): # Keep asking if input is empty
user_input = input("\nYou must provide a valid input. Please try again: ").strip()

if user_input.lower() == "a":
return options

try:
indices = [int(i.strip()) - 1 for i in user_input.split(",")]

return [options[i] for i in indices]

except (ValueError, IndexError):
Expand All @@ -66,85 +68,104 @@ def get_selection(user_input: str, options: list[str]) -> list[str]:

def select_languages() -> list[str]:
"""
Display language options and get user selection.
Display language options and get user selection, re-asking for input if none is provided.

Returns
-------
List[str]
The languages available in Scribe-Data and CLI directions.
"""
print("\nLanguage options:")
languages = [
lang["language"].capitalize() for lang in language_metadata["languages"]
]
for i, lang in enumerate(languages, 1):
print(f"{i}. {lang}")

lang_input = input(
"\nPlease enter the languages to get data for, their numbers or (a) for all languages: "
)
while True: # Loop to re-ask for input if necessary
print("\nLanguage options:")
for i, lang in enumerate(languages, 1):
print(f"{i}. {lang}")

return get_selection(lang_input, languages)
lang_input = input(
"\nPlease enter the languages to get data for, their numbers or (a) for all languages: "
).strip()

if lang_input:
selected_languages = get_selection(lang_input, languages)
if selected_languages:
return selected_languages
else:
print("Invalid selection. Please try again.")
else:
print("No input provided. Please try again.")


def select_data_types() -> list[str]:
"""
Display data type options and get user selection.
Display data type options and get user selection, re-asking for input if none is provided.

Returns
-------
List[str]
The data types available in Scribe-Data and CLI directions.
"""
print("\nData type options:")
data_types = data_type_metadata["data-types"]

for i, dt in enumerate(data_types, 1):
print(f"{i}. {dt}")
while True: # Loop to re-ask for input if necessary
print("\nData type options:")
for i, dt in enumerate(data_types, 1):
print(f"{i}. {dt}")

dt_input = input(
"\nPlease enter the data types to get, their numbers or (a) for all data types: "
)
dt_input = input(
"\nPlease enter the data types to get, their numbers or (a) for all data types: "
).strip()

return get_selection(dt_input, list(data_types.keys()))
if dt_input: # Proceed if user provides a non-empty input
selected_data_types = get_selection(dt_input, list(data_types.keys()))
if selected_data_types:
return selected_data_types
else:
print("Invalid selection. Please try again.")
else:
print("No input provided. Please try again.")


def get_output_options() -> dict:
"""
Get output options from user.
Get output options from user, re-asking for input if none is provided.

Returns
-------
dict
Output options including type, directory, and overwrite flag
Output options including type, directory, and overwrite flag.
"""
valid_types = ["json", "csv", "tsv"]
output_type = (
input("File type to export (json, csv, tsv) [json]: ").strip().lower() or "json"
)

while output_type not in valid_types:
print(
f"Invalid output type '{output_type}'. Please choose from 'json', 'csv', or 'tsv'."
)
while True: # Loop to re-ask for valid output type
output_type = (
input("File type to export (json, csv, tsv) [json]: ").strip().lower()
or "json"
)
if output_type in valid_types:
break
print(
f"Invalid output type '{output_type}'. Please choose from 'json', 'csv', or 'tsv'."
)

if output_type == "csv":
default_export_dir = DEFAULT_CSV_EXPORT_DIR

elif output_type == "tsv":
default_export_dir = DEFAULT_TSV_EXPORT_DIR

else:
default_export_dir = DEFAULT_JSON_EXPORT_DIR

output_dir = (
input(f"Export directory path [./{default_export_dir}]: ").strip()
or f"./{default_export_dir}"
)
while True: # Loop to ensure a non-empty directory path is provided
output_dir = (
input(f"Export directory path [./{default_export_dir}]: ").strip()
or f"./{default_export_dir}"
)
if output_dir:
break
print("You must provide a valid directory path. Please try again.")

overwrite = (
input("Overwrite existing data without asking (y/n) [n]: ").strip().lower()
== "y"
Expand All @@ -153,6 +174,7 @@ def get_output_options() -> dict:
return {"type": output_type, "dir": output_dir, "overwrite": overwrite}



def run_interactive_mode():
"""
Run the interactive mode for Scribe-Data CLI.
Expand Down
Loading