Skip to content

Commit

Permalink
Fix interactive mode given new data types setup
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Sep 20, 2024
1 parent 13c2bd3 commit 31f75eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/scribe_data/cli/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import csv
import json
import shutil
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -107,17 +108,16 @@ def convert_to_csv_or_tsv(
try:
with file_path.open("r") as f:
data = json.load(f)

except (IOError, json.JSONDecodeError) as e:
print(f"Error reading '{file_path}': {e}")
continue

delimiter = "," if output_type == "csv" else "\t"
file_extension = output_type

final_output_dir = output_dir / normalized_language["language"].capitalize()
final_output_dir.mkdir(parents=True, exist_ok=True)

output_file = final_output_dir / f"{dtype}.{file_extension}"
output_file = final_output_dir / f"{dtype}.{output_type}"
if output_file.exists() and not overwrite:
user_input = input(
f"File '{output_file}' already exists. Overwrite? (y/n): "
Expand All @@ -132,14 +132,17 @@ def convert_to_csv_or_tsv(
if isinstance(data, dict):
writer.writerow(data.keys())
writer.writerow(data.values())

elif isinstance(data, list):
for item in data:
if isinstance(item, dict):
writer.writerow(item.values())

else:
writer.writerow([item])
else:
print(f"Unsupported data format for {output_type} export.")

except IOError as e:
print(f"Error writing to '{output_file}': {e}")
continue
Expand Down Expand Up @@ -174,12 +177,13 @@ def convert_to_sqlite(
if source_path.exists():
if target_path.exists() and not overwrite:
print(f"File {target_path} already exists. Use --overwrite to replace.")
else:
import shutil

else:
shutil.copy(source_path, target_path)
print(f"SQLite database copied to: {target_path}")

else:
print(f"Warning: SQLite file not found at {source_path}")

else:
print("No output directory specified. SQLite file remains in default location.")
13 changes: 8 additions & 5 deletions src/scribe_data/cli/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
)
from scribe_data.cli.get import get_data
from scribe_data.utils import (
DEFAULT_JSON_EXPORT_DIR,
DEFAULT_CSV_EXPORT_DIR,
DEFAULT_JSON_EXPORT_DIR,
DEFAULT_TSV_EXPORT_DIR,
)

Expand All @@ -56,7 +56,8 @@ def get_selection(user_input: str, options: list[str]) -> list[str]:
return options

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

return [options[i] for i in indices]

except (ValueError, IndexError):
Expand All @@ -72,7 +73,7 @@ def select_languages() -> list[str]:
List[str]
The languages available in Scribe-Data and CLI directions.
"""
print("Language options:")
print("\nLanguage options:")
languages = [
lang["language"].capitalize() for lang in language_metadata["languages"]
]
Expand Down Expand Up @@ -105,7 +106,7 @@ def select_data_types() -> list[str]:
"\nPlease enter the data types to get, their numbers or (a) for all data types: "
)

return get_selection(dt_input, data_types)
return get_selection(dt_input, list(data_types.keys()))


def get_output_options() -> dict:
Expand Down Expand Up @@ -133,8 +134,10 @@ def get_output_options() -> dict:

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

Expand Down Expand Up @@ -188,7 +191,7 @@ def run_interactive_mode():

# This function can be called from main.py when the -i or --interactive flag is used.
def start_interactive_mode():
print("Welcome to Scribe-Data interactive mode!\n")
print("Welcome to Scribe-Data interactive mode!")
run_interactive_mode()


Expand Down

0 comments on commit 31f75eb

Please sign in to comment.