Skip to content

Commit

Permalink
Update user prompts and code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Nov 7, 2024
1 parent bb4bf6d commit b173d11
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
30 changes: 15 additions & 15 deletions docs/source/scribe_data/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Examples:
$ scribe-data get -l English --data-type verbs -od ~/path/for/output
Getting and formatting English verbs
Data updated: 100%|████████████████████████| 1/1 [00:29<00:00, 29.73s/process]
Data updated: 100%|████████████████████████| 1/1 [00:XY<00:00, XY.Zs/process]
Behavior and Output:
^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -197,7 +197,7 @@ Behavior and Output:
.. code-block:: text
Getting and formatting English verbs
Data updated: 100%|████████████████████████| 1/1 [00:29<00:00, 29.73s/process]
Data updated: 100%|████████████████████████| 1/1 [00:XY<00:00, XY.Zs/process]
4. If no data is found, you'll see a warning:

Expand Down Expand Up @@ -272,8 +272,8 @@ Examples:
==============================================
Language Data Type Total Lexemes
==============================================
English nouns 123456
verbs 234567
English nouns 123,456
verbs 234,567
...
.. code-block:: text
Expand All @@ -283,9 +283,9 @@ Examples:
Language Data Type Total Wikidata Lexemes
================================================================
English adjectives 12,848
adverbs 19,998
nouns 30,786
English adjectives 12,345
adverbs 23,456
nouns 34,567
...
.. code-block:: text
Expand All @@ -295,27 +295,27 @@ Examples:
Language Data Type Total Wikidata Lexemes
================================================================
Q1860 adjectives 12,848
adverbs 19,998
articles 0
conjunctions 72
nouns 30,786
personal pronouns 32
Q1860 adjectives 12,345
adverbs 23,456
articles 30
conjunctions 40
nouns 56,789
personal pronouns 60
...
.. code-block:: text
$ scribe-data total --language English -dt nouns
Language: English
Data type: nouns
Total number of lexemes: 12345
Total number of lexemes: 12,345
.. code-block:: text
$ scribe-data total --language Q1860 -dt verbs
Language: Q1860
Data type: verbs
Total number of lexemes: 23456
Total number of lexemes: 23,456
Convert Command
~~~~~~~~~~~~~~~
Expand Down
36 changes: 22 additions & 14 deletions src/scribe_data/cli/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
from rich.logging import RichHandler
from rich.table import Table
from tqdm import tqdm
from scribe_data.cli.total import total_wrapper

# from scribe_data.cli.list import list_wrapper
from scribe_data.cli.get import get_data
from scribe_data.cli.total import total_wrapper
from scribe_data.cli.version import get_version_message
from scribe_data.utils import (
DEFAULT_JSON_EXPORT_DIR,
Expand Down Expand Up @@ -98,7 +98,7 @@ def display_summary():
console.print("\n")


# Helper function to create a WordCompleter
# Helper function to create a WordCompleter.
def create_word_completer(
options: List[str], include_all: bool = False
) -> WordCompleter:
Expand All @@ -108,11 +108,12 @@ def create_word_completer(


# MARK: Language Selection


def prompt_for_languages():
"""
Requests language and data type for lexeme totals.
"""
# MARK: Language Selection
language_completer = create_word_completer(config.languages, include_all=True)
initial_language_selection = ", ".join(config.selected_languages)
selected_languages = prompt(
Expand All @@ -122,7 +123,7 @@ def prompt_for_languages():
)
if "All" in selected_languages:
config.selected_languages = config.languages
elif selected_languages.strip(): # Check if input is not just whitespace
elif selected_languages.strip(): # check if input is not just whitespace
config.selected_languages = [
lang.strip()
for lang in selected_languages.split(",")
Expand All @@ -135,6 +136,8 @@ def prompt_for_languages():


# MARK: Data Type Selection


def prompt_for_data_types():
data_type_completer = create_word_completer(config.data_types, include_all=True)
initial_data_type_selection = ", ".join(config.selected_data_types)
Expand All @@ -147,14 +150,14 @@ def prompt_for_data_types():
if "All" in selected_data_types.capitalize():
config.selected_data_types = config.data_types
break
elif selected_data_types.strip(): # Check if input is not just whitespace
elif selected_data_types.strip(): # check if input is not just whitespace
config.selected_data_types = [
dt.strip()
for dt in selected_data_types.split(",")
if dt.strip() in config.data_types
]
if config.selected_data_types:
break # Exit loop if valid data types are selected
break # exit loop if valid data types are selected

rprint("[yellow]No data type selected. Please try again.[/yellow]")

Expand All @@ -173,12 +176,11 @@ def configure_settings():
rprint(
"[cyan]Follow the prompts below. Press tab for completions and enter to select.[/cyan]"
)
# MARK: Languages
prompt_for_languages()
# MARK: Data Types
prompt_for_data_types()

# MARK: Output Type

output_type_completer = create_word_completer(["json", "csv", "tsv"])
config.output_type = prompt(
"Select output type (json/csv/tsv): ", completer=output_type_completer
Expand Down Expand Up @@ -257,8 +259,8 @@ def request_total_lexeme_loop():
choice = questionary.select(
"What would you like to do?",
choices=[
Choice("Request total lexeme", "total"),
Choice("Run for total lexeme", "run"),
Choice("Configure total lexemes request", "total"),
Choice("Run total lexemes request", "run"),
Choice("Exit", "exit"),
],
).ask()
Expand Down Expand Up @@ -312,47 +314,53 @@ def start_interactive_mode(selectMode: str = None):
f"[bold cyan]Welcome to {get_version_message()} interactive mode![/bold cyan]"
)
while True:
# Check if both selected_languages and selected_data_types are empty
# Check if both selected_languages and selected_data_types are empty.
if not config.selected_languages and not config.selected_data_types:
if selectMode == "Get":
choices = [
Choice("Request get data", "configure"),
Choice("Configure get data request", "configure"),
# Choice("See list of languages", "languages"),
Choice("Exit", "exit"),
]

elif selectMode == "Total":
choices = [
Choice("Request total lexeme", "total"),
Choice("Configure total lexemes request", "total"),
# Choice("See list of languages", "languages"),
Choice("Exit", "exit"),
]

else:
choices = [
Choice("Request get data", "configure"),
Choice("Configure get data request", "configure"),
Choice("Exit", "exit"),
]
if config.configured:
choices.insert(1, Choice("Request for get data", "run"))

else:
choices.insert(1, Choice("Request for total lexeme", "total"))

choice = questionary.select("What would you like to do?", choices=choices).ask()

if choice == "configure":
configure_settings()

elif choice == "total":
prompt_for_languages()
prompt_for_data_types()
request_total_lexeme_loop()
break

# elif choice == "languages":
# see_list_languages()
# break

elif choice == "run":
run_request()
rprint(THANK_YOU_MESSAGE)
break

else:
rprint(THANK_YOU_MESSAGE)
break
Expand Down
22 changes: 11 additions & 11 deletions tests/cli/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
"""

import unittest
from unittest.mock import patch, MagicMock, call
from pathlib import Path
from unittest.mock import MagicMock, call, patch

from scribe_data.cli.interactive import (
ScribeDataConfig,
configure_settings,
display_summary,
run_request,
prompt_for_languages,
prompt_for_data_types,
prompt_for_languages,
run_request,
)


Expand All @@ -54,7 +55,7 @@ def test_scribe_data_config_initialization(self):
@patch("scribe_data.cli.interactive.rprint")
def test_configure_settings_all_languages(self, mock_rprint, mock_prompt):
"""Test configure_settings with 'All' languages selection."""
# Set up mock responses
# Set up mock responses.
responses = iter(
[
"All", # languages
Expand All @@ -79,7 +80,7 @@ def test_configure_settings_all_languages(self, mock_rprint, mock_prompt):
@patch("scribe_data.cli.interactive.rprint")
def test_configure_settings_specific_languages(self, mock_rprint, mock_prompt):
"""Test configure_settings with specific language selection."""
# Set up mock responses
# Set up mock responses.
responses = iter(
[
"english, spanish", # languages
Expand All @@ -106,7 +107,6 @@ def test_configure_settings_specific_languages(self, mock_rprint, mock_prompt):
@patch("scribe_data.cli.interactive.logger")
def test_run_request(self, mock_logger, mock_tqdm, mock_get_data):
"""Test run_request functionality."""
# Setup
self.config.selected_languages = ["english"]
self.config.selected_data_types = ["nouns"]
self.config.configured = True
Expand All @@ -131,10 +131,10 @@ def test_run_request(self, mock_logger, mock_tqdm, mock_get_data):
@patch("scribe_data.cli.interactive.rprint")
def test_request_total_lexeme(self, mock_rprint, mock_prompt):
"""Test request_total_lexeme functionality."""
# Set up mock responses
# Set up mock responses.
mock_prompt.side_effect = [
"english, french", # First call for languages
"nouns", # First call for data types
"english, french", # first call for languages
"nouns", # first call for data types
]

with patch("scribe_data.cli.interactive.config", self.config):
Expand All @@ -145,11 +145,11 @@ def test_request_total_lexeme(self, mock_rprint, mock_prompt):
prompt_for_languages()
prompt_for_data_types()

# Verify the config was updated correctly
# Verify the config was updated correctly.
self.assertEqual(self.config.selected_languages, ["english", "french"])
self.assertEqual(self.config.selected_data_types, ["nouns"])

# Verify prompt was called with correct arguments
# Verify prompt was called with correct arguments.
expected_calls = [
call(
"Select languages (comma-separated or 'All'): ",
Expand Down
8 changes: 4 additions & 4 deletions tests/cli/test_total.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from unittest.mock import MagicMock, call, patch

from scribe_data.cli.total import (
check_qid_is_language,
get_datatype_list,
get_qid_by_input,
get_total_lexemes,
get_datatype_list,
check_qid_is_language,
total_wrapper,
)

Expand Down Expand Up @@ -140,7 +140,7 @@ def test_get_total_lexemes_various_data_types(self, mock_query, mock_get_qid):
@patch("scribe_data.cli.total.sparql.query")
@patch("scribe_data.cli.total.LANGUAGE_DATA_EXTRACTION_DIR")
def test_get_total_lexemes_sub_languages(self, mock_dir, mock_query, mock_get_qid):
# Setup for sub-languages
# Setup for sub-languages.
mock_get_qid.side_effect = lambda x: {
"bokmål": "Q25167",
"nynorsk": "Q25164",
Expand All @@ -151,7 +151,7 @@ def test_get_total_lexemes_sub_languages(self, mock_dir, mock_query, mock_get_qi
}
mock_query.return_value = mock_results

# Mocking directory paths and contents
# Mocking directory paths and contents.
mock_dir.__truediv__.return_value.exists.return_value = True
mock_dir.__truediv__.return_value.iterdir.return_value = [
MagicMock(name="verbs", is_dir=lambda: True),
Expand Down

0 comments on commit b173d11

Please sign in to comment.