171 lines
5.9 KiB
Python
171 lines
5.9 KiB
Python
"""__main__.py"""
|
|
|
|
# Standard Library
|
|
from pathlib import Path
|
|
|
|
# Local
|
|
from .api import (
|
|
folder_proccess,
|
|
is_file,
|
|
list_input_files,
|
|
pre_process_a_dictionary_file,
|
|
proccess_a_completed_file,
|
|
process_a_dictation_file,
|
|
process_a_dictionary_file,
|
|
process_a_phrases_file,
|
|
select_file,
|
|
select_folder,
|
|
)
|
|
from .constants import (
|
|
COMPLETED_TYPE,
|
|
DICTATION_TYPE,
|
|
DICT_TYPE,
|
|
INPUT,
|
|
LANGUAGES,
|
|
PHRASES_TYPE,
|
|
)
|
|
from .utility import ProcessFile, ProcessFolder
|
|
|
|
|
|
def cli_choose_work_type() -> str:
|
|
"""entry point for interactive interface"""
|
|
option = None
|
|
while not option in ["s", "f", "e"]:
|
|
option = input(
|
|
"Please select the work option:\n" "s:single file, f:folder, e:exit\n"
|
|
)
|
|
return option
|
|
|
|
|
|
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_folder() -> ProcessFolder:
|
|
"""Loops until it finds a valid input_folder"""
|
|
print("Select data folder:")
|
|
in_folder = None
|
|
level = Path()
|
|
while not in_folder:
|
|
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]
|
|
print(f"Selected {selected}")
|
|
s = None
|
|
while s not in ("yes", "y", "no", "n"):
|
|
s = input("if this the folder? (yes / no, go inside) : ")
|
|
if s in ("yes", "y"):
|
|
in_folder = selected
|
|
else:
|
|
level = selected
|
|
return select_folder(in_folder)
|
|
|
|
|
|
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:
|
|
if languages and language_id in languages:
|
|
print(f"{language_id} - {language}")
|
|
print("all - All languages")
|
|
s = None
|
|
while not s or s not in LANGUAGES.AvailableLanguages + ("all",):
|
|
lan_codes = [lan_id for lan_id, lan in avaliable_languages] + ["all"]
|
|
s = input(f"Please select the language {', '.join(lan_codes)}: ")
|
|
return s
|
|
|
|
|
|
def main():
|
|
"""CLI interface for the module"""
|
|
while True:
|
|
option = cli_choose_work_type()
|
|
if option == "e":
|
|
break
|
|
elif option == "s":
|
|
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()
|
|
print(
|
|
f"pre-processing file {input_file} with language {language_id}"
|
|
)
|
|
pre_process_a_dictionary_file(input_file, language_id)
|
|
else:
|
|
print(f"Processing file {input_file} with language {language_id}")
|
|
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} with language {language_id}")
|
|
process_a_phrases_file(input_file, language_id)
|
|
elif DICTATION_TYPE in input_file.input_file.suffixes:
|
|
language_id = cli_select_language()
|
|
print(f"processing file {input_file} with language {language_id}")
|
|
process_a_dictation_file(input_file, language_id)
|
|
elif COMPLETED_TYPE in input_file.input_file.suffixes:
|
|
language_id = input_file.input_file.suffixes[1][1:]
|
|
print(f"processing file {input_file} with language {language_id}")
|
|
proccess_a_completed_file(input_file, language_id)
|
|
elif option == "f":
|
|
language_id = cli_select_language()
|
|
langs = (
|
|
LANGUAGES.AvailableLanguages if language_id == "all" else [language_id]
|
|
)
|
|
for language_id in langs:
|
|
in_folder = cli_select_folder()
|
|
print(f"Selected: {in_folder} with language {language_id}")
|
|
for dirpath, dirnames, filenames in tuple(
|
|
in_folder.absolute_input_folder.walk()
|
|
)[1:]:
|
|
if not dirnames and filenames:
|
|
folder = select_folder(dirpath.relative_to(INPUT))
|
|
folder_proccess(folder, language_id=language_id)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|