Skip to content

Commit

Permalink
Minor updates to interactive fxn args and code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Nov 8, 2024
1 parent b173d11 commit 8c598c8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
11 changes: 8 additions & 3 deletions src/scribe_data/cli/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,29 @@ def request_total_lexeme_loop():


# MARK: Start
def start_interactive_mode(selectMode: str = None):
def start_interactive_mode(operation: str = None):
"""
Entry point for interactive mode.
Parameters
----------
operation : str
The type of operation that interactive mode is being ran with.
"""
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:
if selectMode == "Get":
if operation == "get":
choices = [
Choice("Configure get data request", "configure"),
# Choice("See list of languages", "languages"),
Choice("Exit", "exit"),
]

elif selectMode == "Total":
elif operation == "total":
choices = [
Choice("Configure total lexemes request", "total"),
# Choice("See list of languages", "languages"),
Expand Down
4 changes: 2 additions & 2 deletions src/scribe_data/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def main() -> None:

elif args.command in ["get", "g"]:
if args.interactive:
start_interactive_mode(selectMode="Get")
start_interactive_mode(operation="get")

else:
get_data(
Expand All @@ -291,7 +291,7 @@ def main() -> None:

elif args.command in ["total", "t"]:
if args.interactive:
start_interactive_mode(selectMode="Total")
start_interactive_mode(operation="total")
else:
total_wrapper(
language=args.language, data_type=args.data_type, all_bool=args.all
Expand Down
24 changes: 17 additions & 7 deletions src/scribe_data/cli/total.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
"""

from http.client import IncompleteRead
from typing import List, Union
from urllib.error import HTTPError

import requests
from SPARQLWrapper import JSON
from typing import List, Union

from scribe_data.utils import (
LANGUAGE_DATA_EXTRACTION_DIR,
data_type_metadata,
Expand Down Expand Up @@ -76,19 +77,20 @@ def get_datatype_list(language):
data_types : list[str] or None
A list of the corresponding data types.
"""
language_key = language.strip().lower() # Normalize input
language_key = language.strip().lower() # normalize input
languages = list_all_languages(language_metadata)

# Adjust language_key for sub-languages using the format_sublanguage_name function
# Adjust language_key for sub-languages using the format_sublanguage_name function.
formatted_language = format_sublanguage_name(language_key, language_metadata)
language_key = formatted_language.split("/")[
0
].lower() # Use the main language part if formatted
].lower() # use the main language part if formatted

if language_key in languages:
if "sub_languages" in language_metadata[language_key]:
sub_languages = language_metadata[language_key]["sub_languages"]
data_types = []

for sub_lang_key in sub_languages:
sub_lang_dir = (
LANGUAGE_DATA_EXTRACTION_DIR / sub_languages[sub_lang_key]["iso"]
Expand All @@ -97,20 +99,26 @@ def get_datatype_list(language):
data_types.extend(
[f.name for f in sub_lang_dir.iterdir() if f.is_dir()]
)

if not data_types:
raise ValueError(
f"No data types available for sub-languages of '{formatted_language}'."
)
return sorted(set(data_types)) # Remove duplicates and sort

return sorted(set(data_types)) # remove duplicates and sort

else:
language_dir = LANGUAGE_DATA_EXTRACTION_DIR / language_key
if not language_dir.exists():
raise ValueError(f"Directory '{language_dir}' does not exist.")

data_types = [f.name for f in language_dir.iterdir() if f.is_dir()]

if not data_types:
raise ValueError(
f"No data types available for language '{formatted_language}'."
)

return sorted(data_types)

else: # return all data types
Expand Down Expand Up @@ -368,8 +376,10 @@ def total_wrapper(
----------
language : Union[str, List[str]]
The language(s) to potentially total data types for.
data_type : Union[str, List[str]]
The data type(s) to check for.
all_bool : boolean
Whether all languages and data types should be listed.
"""
Expand All @@ -386,7 +396,7 @@ def total_wrapper(

for lang in languages:
first_row = (
True # Flag to check if it's the first data type for the language
True # flag to check if it's the first data type for the language
)
for dt in data_types:
total_lexemes = get_total_lexemes(lang, dt, False)
Expand All @@ -399,7 +409,7 @@ def total_wrapper(
else:
print(
f"{'':<20} {dt:<25} {total_lexemes:<25}"
) # Print empty space for language
) # print empty space for language
print()

elif language is not None and data_type is None:
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TestScribeDataInteractive(unittest.TestCase):
def setUp(self):
"""Set up test fixtures before each test method."""
self.config = ScribeDataConfig()
# Mock the language_metadata and data_type_metadata
# Mock the language_metadata and data_type_metadata.
self.config.languages = ["english", "spanish", "french"]
self.config.data_types = ["nouns", "verbs"]

Expand Down

0 comments on commit 8c598c8

Please sign in to comment.