Skip to content

Commit

Permalink
feat interactive v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
axif0 committed Oct 31, 2024
1 parent 135f32c commit 49141ce
Show file tree
Hide file tree
Showing 5 changed files with 566 additions and 76 deletions.
72 changes: 61 additions & 11 deletions docs/source/scribe_data/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,31 @@ Options:
- ``-ot, --output-type {json,csv,tsv}``: The output file type.
- ``-ope, --outputs-per-entry OUTPUTS_PER_ENTRY``: How many outputs should be generated per data entry.
- ``-o, --overwrite``: Whether to overwrite existing files (default: False).
- ``-a, --all ALL``: Get all languages and data types.
- ``-a, --all``: Get all languages and data types. Can be combined with `-dt` to get all languages for a specific data type, or with `-lang` to get all data types for a specific language.
- ``-i, --interactive``: Run in interactive mode.

Example:
Examples:

.. code-block:: bash
$ scribe-data get --all
Getting data for all languages and all data types...
.. code-block:: bash
$ scribe-data get --all -dt nouns
Getting all nouns for all languages...
.. code-block:: bash
$ scribe-data get --all -lang English
Getting all data types for English...
.. code-block:: bash
$ 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]
Behavior and Output:
^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -242,31 +259,64 @@ Usage:
Options:
^^^^^^^^

- ``-lang, --language LANGUAGE``: The language(s) to check totals for.
- ``-lang, --language LANGUAGE``: The language(s) to check totals for. Can be a language name or QID.
- ``-dt, --data-type DATA_TYPE``: The data type(s) to check totals for.
- ``-a, --all ALL``: Get totals for all languages and data types.
- ``-a, --all``: Get totals for all languages and data types.

Examples:

.. code-block:: text
$scribe-data total -dt nouns # verbs, adjectives, etc
Data type: nouns
Total number of lexemes: 123456
$ scribe-data total --all
Total lexemes for all languages and data types:
==============================================
Language Data Type Total Lexemes
==============================================
English nouns 123456
verbs 234567
...
.. code-block:: text
$scribe-data total -lang English
Language: English
Total number of lexemes: 123456
$ scribe-data total --language English
Returning total counts for English data types...
Language Data Type Total Wikidata Lexemes
================================================================
English adjectives 12,848
adverbs 19,998
nouns 30,786
...
.. code-block:: text
$scribe-data total -lang English -dt nouns # verbs, adjectives, etc
$ scribe-data total --language Q1860
Wikidata QID Q1860 passed. Checking all data types.
Language Data Type Total Wikidata Lexemes
================================================================
Q1860 adjectives 12,848
adverbs 19,998
articles 0
conjunctions 72
nouns 30,786
personal pronouns 32
...
.. code-block:: text
$ scribe-data total --language English -dt nouns
Language: English
Data type: nouns
Total number of lexemes: 12345
.. code-block:: text
$ scribe-data total --language Q1860 -dt verbs
Language: Q1860
Data type: verbs
Total number of lexemes: 23456
Convert Command
~~~~~~~~~~~~~~~

Expand Down
173 changes: 145 additions & 28 deletions src/scribe_data/cli/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
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.version import get_version_message
from scribe_data.utils import (
Expand All @@ -53,6 +54,7 @@
)
console = Console()
logger = logging.getLogger("rich")
MESSAGE = "[bold cyan]Thank you for using Scribe-Data![/bold cyan]"


class ScribeDataConfig:
Expand All @@ -64,6 +66,7 @@ def __init__(self):
self.output_type: str = "json"
self.output_dir: Path = Path(DEFAULT_JSON_EXPORT_DIR)
self.overwrite: bool = False
self.configured: bool = False


config = ScribeDataConfig()
Expand Down Expand Up @@ -110,30 +113,32 @@ def configure_settings():
)
# MARK: Languages
language_completer = WordCompleter(["All"] + config.languages, ignore_case=True)
if not config.selected_languages:
selected_languages = prompt(
"Select languages (comma-separated or type 'All'): ",
completer=language_completer,
)
initial_language_selection = ", ".join(config.selected_languages)
selected_languages = prompt(
"Select languages (comma-separated or type 'All'): ",
default=initial_language_selection,
completer=language_completer,
)

if "All" in selected_languages:
config.selected_languages = config.languages
else:
config.selected_languages = [
lang.strip()
for lang in selected_languages.split(",")
if lang.strip() in config.languages
]
if "All" in selected_languages:
config.selected_languages = config.languages
else:
config.selected_languages = [
lang.strip()
for lang in selected_languages.split(",")
if lang.strip() in config.languages
]

if not config.selected_languages:
rprint("[yellow]No language selected. Please try again.[/yellow]")
return configure_settings()

# MARK: Data Types

data_type_completer = WordCompleter(["All"] + config.data_types, ignore_case=True)
initial_data_type_selection = ", ".join(config.selected_data_types)
selected_data_types = prompt(
"Select data types (comma-separated or type 'All'): ",
default=initial_data_type_selection,
completer=data_type_completer,
)

Expand All @@ -151,7 +156,6 @@ def configure_settings():
return configure_settings()

# MARK: Output Type

output_type_completer = WordCompleter(["json", "csv", "tsv"], ignore_case=True)
config.output_type = prompt(
"Select output type (json/csv/tsv): ", completer=output_type_completer
Expand All @@ -163,19 +167,18 @@ def configure_settings():
)

# MARK: Output Directory

if output_dir := prompt(f"Enter output directory (default: {config.output_dir}): "):
config.output_dir = Path(output_dir)

# MARK: Overwrite Confirmation

overwrite_completer = WordCompleter(["Y", "n"], ignore_case=True)
overwrite = (
prompt("Overwrite existing files? (Y/n): ", completer=overwrite_completer)
or "y"
)
config.overwrite = overwrite.lower() == "y"

config.configured = True
display_summary()


Expand Down Expand Up @@ -226,34 +229,148 @@ def run_request():
# MARK: Start


def start_interactive_mode():
def request_total_lexeme():
"""
Provides base options and forwarding to other interactive mode functionality.
Requests language and data type for lexeme totals.
"""
rprint(
f"[bold cyan]Welcome to {get_version_message()} interactive mode![/bold cyan]"
# MARK: Language Selection
language_completer = WordCompleter(["All"] + config.languages, ignore_case=True)
initial_language_selection = ", ".join(config.selected_languages)
selected_languages = prompt(
"Select languages (comma-separated or 'All'): ",
default=initial_language_selection,
completer=language_completer,
)
if "All" in selected_languages:
config.selected_languages = config.languages
elif selected_languages.strip(): # Check if input is not just whitespace
config.selected_languages = [
lang.strip()
for lang in selected_languages.split(",")
if lang.strip() in config.languages
]

if not config.selected_languages:
rprint("[yellow]No language selected. Please try again.[/yellow]")
return request_total_lexeme()

# MARK: Data Type Selection
data_type_completer = WordCompleter(["All"] + config.data_types, ignore_case=True)
initial_data_type_selection = ", ".join(config.selected_data_types)
selected_data_types = prompt(
"Select data types (comma-separated or 'All'): ",
default=initial_data_type_selection,
completer=data_type_completer,
)
if "All" in selected_data_types.capitalize():
config.selected_data_types = config.data_types
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 not config.selected_data_types:
rprint("[yellow]No data type selected. Please try again.[/yellow]")
return request_total_lexeme()


def request_total_lexeme_loop():
"""
Continuously prompts for lexeme requests until exit.
"""
while True:
choice = questionary.select(
"What would you like to do?",
choices=[
Choice("Configure request", "configure"),
Choice("Run configured data request", "run"),
Choice("Request total lexeme", "total"),
Choice("Run for total lexeme", "run"),
Choice("Exit", "exit"),
],
).ask()

if choice == "run":
total_wrapper(
language=config.selected_languages,
data_type=config.selected_data_types,
all_bool=False,
)
config.selected_languages, config.selected_data_types = [], []
rprint(MESSAGE)
break
elif choice == "exit":
return
else:
# config.selected_languages, config.selected_data_types = [], []
request_total_lexeme()


def see_list_languages():
"""
See list of languages.
"""

choice = questionary.select(
"What would you like to list?",
choices=[
Choice("All languages", "all_languages"),
Choice("Languages for a specific data type", "languages_for_data_type"),
Choice("Data types for a specific language", "data_types_for_language"),
],
).ask()

if choice == "all_languages":
list_wrapper(all_bool=True)
elif choice == "languages_for_data_type":
list_wrapper(data_type=True)
elif choice == "data_types_for_language":
list_wrapper(language=True)


def start_interactive_mode():
"""
Entry point for interactive mode.
"""
rprint(
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
if not config.selected_languages and not config.selected_data_types:
choices = [
Choice("Request get data", "configure"),
Choice("Request total lexeme", "total"),
Choice("See list of languages", "languages"),
Choice("Exit", "exit"),
]
else:
choices = [
Choice("Request get data", "configure"),
Choice("Exit", "exit"),
]
if config.configured:
choices.insert(1, Choice("Run configured data request", "run"))
else:
choices.insert(1, Choice("Request total lexeme", "total"))

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

if choice == "configure":
configure_settings()

elif choice == "total":
request_total_lexeme()
request_total_lexeme_loop()
break
elif choice == "languages":
see_list_languages()
break
elif choice == "run":
run_request()
rprint("[bold cyan]Thank you for using Scribe-Data![/bold cyan]")
rprint(MESSAGE)
break

else:
rprint("[bold cyan]Thank you for using Scribe-Data![/bold cyan]")
rprint(MESSAGE)
break


Expand Down
Loading

0 comments on commit 49141ce

Please sign in to comment.