Files
anki-hsk-creator/src/anki_hsk_creator/__main__.py
Wolfang Torres a23e0dc34e fix cli
2026-06-20 10:19:59 +08:00

96 lines
3.0 KiB
Python

"""__main__.py"""
# Standard Library
from pathlib import Path
# Local
from .api import (
is_file,
list_input_files,
pre_process_a_dictionary_file,
process_a_dictionary_file,
process_a_phrases_file,
select_file,
)
from .constants import DICT_TYPE, LANGUAGES, PHRASES_TYPE
from .utility import ProcessFile
def cli_select_files() -> ProcessFile:
"""Loops until it finds a valid input_file"""
print("Select data file:")
in_file = None
level = Path()
while not in_file:
files = list_input_files(level)
for n, file in enumerate(files):
print(f"{n+1} - {file}")
s = None
while not s or not s.isnumeric() or not 1 <= int(s) <= len(files):
s = input(f"Please select the file [1-{len(files)}]: ")
selected = files[int(s) - 1]
if is_file(selected):
in_file = selected
else:
level = selected
input_file = select_file(in_file)
return input_file
def cli_select_dictionay_tsv() -> bool:
"""If a dictionary file is selected, ask if the user wants to proccess it"""
s = None
while s not in ("y", "yes", "no", "n"):
s = input("Do you want to Pre-Process a dictionary (y/n): ")
r = s in ("y", "yes")
return r
def cli_select_language(languages: list = None) -> str:
"""Selects a language for the trasnlatation"""
if languages:
avaliable_languages = {
lan_id: lan
for lan_id, lan in LANGUAGES.LanguageNames.items()
if lan_id in languages
}
else:
avaliable_languages = LANGUAGES.LanguageNames.items()
if not avaliable_languages:
raise ValueError("""No languages are avaliable,
if this is a dictionay file, you must preprocess it first""")
print("Select a language:")
for language_id, language in avaliable_languages.items():
if languages and language_id in languages:
print(f"{language_id} - {language}")
s = None
while not s or s not in LANGUAGES.AvailableLanguages:
s = input(f"Please select the language {avaliable_languages.keys()}: ")
return s
def main():
"""CLI interface for the module"""
while True:
input_file = cli_select_files()
if DICT_TYPE in input_file.input_file.suffixes:
dict_selected = cli_select_dictionay_tsv()
if dict_selected:
language_id = cli_select_language()
pre_process_a_dictionary_file(input_file, language_id)
else:
language_id = cli_select_language(
input_file.available_dictionary_languages
)
process_a_dictionary_file(input_file, language_id)
elif PHRASES_TYPE in input_file.input_file.suffixes:
language_id = cli_select_language()
print(
f"processing file {input_file.input_file} with language {language_id}"
)
process_a_phrases_file(input_file, language_id)
if __name__ == "__main__":
main()