Chris Trenthem
commited on
Commit
·
41742b2
1
Parent(s):
5ef2374
resolved issue66 and modified cli tests to compensate
Browse files- deep_translator/__main__.py +43 -39
- deep_translator/tests/test_cli.py +9 -3
- setup.cfg +9 -9
- setup.py +2 -2
deep_translator/__main__.py
CHANGED
@@ -12,36 +12,31 @@ from .microsoft import MicrosoftTranslator
|
|
12 |
from .papago import PapagoTranslator
|
13 |
|
14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
|
|
|
|
|
|
15 |
|
16 |
-
@
|
17 |
@click.argument('translator', required=True, default='google', type=str)
|
18 |
@click.option("--source", "-src", required=True, type=str, help="source language to translate from")
|
19 |
@click.option("--target", "-tgt", required=True, type=str, help="target language to translate to")
|
20 |
@click.option("--text", "-txt", type=str,required = True,prompt="Enter the text you want to translate",help="text you want to translate")
|
21 |
@click.option("--api-key",type=str,help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators")
|
22 |
-
|
23 |
-
# @click.option("--languages","-lang",is_flag=True,help="list all the languages available with the translator.\nRun with deep_translator <translator service> -lang",)
|
24 |
-
def main(translator, source, target, text, api_key, languages):
|
25 |
"""
|
26 |
Use TRANSLATOR to translate source material into another language.
|
27 |
-
|
|
|
|
|
|
|
28 |
"""
|
29 |
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
30 |
if translator in api_key_required and not api_key:
|
31 |
click.echo(
|
32 |
-
"This translator requires an api key provided through --api-key"
|
33 |
-
)
|
34 |
-
# elif languages: #TODO: This needs to be moved to its own sub-command. I have to figure out how sub-commands work in Click.
|
35 |
-
# print_supported_languages(translator, api_key)
|
36 |
else:
|
37 |
-
|
38 |
|
39 |
-
def translate(translator, source, target, text, api_key):
|
40 |
-
"""
|
41 |
-
Directory function to send arguments to the correct translator.
|
42 |
-
@param translator: translator name parsed from terminal arguments
|
43 |
-
@return: None
|
44 |
-
"""
|
45 |
if translator == "google":
|
46 |
translator = GoogleTranslator(source=source, target=target)
|
47 |
elif translator == "mymemory":
|
@@ -70,51 +65,60 @@ def translate(translator, source, target, text, api_key):
|
|
70 |
target=target,
|
71 |
api_key=api_key)
|
72 |
else:
|
73 |
-
|
74 |
-
"The given translator is not supported."
|
75 |
-
" Please use a translator supported by the deep_translator tool")
|
76 |
-
return
|
77 |
|
78 |
res = translator.translate(text)
|
79 |
click.echo(f" | Translation from {source} to {target} |")
|
80 |
click.echo(f"Translated text: \n {res}")
|
|
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
"""
|
85 |
Retrieve the list of available languages from the given translator.
|
86 |
-
@param
|
87 |
@param api_key: Optional API key given by the user. Required for some translators.
|
88 |
@return: None
|
89 |
"""
|
90 |
-
translator =
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
translator = GoogleTranslator
|
93 |
-
elif
|
94 |
translator = MyMemoryTranslator
|
95 |
-
elif
|
96 |
translator = QCRI(api_key=api_key)
|
97 |
-
elif
|
98 |
translator = LingueeTranslator
|
99 |
-
elif
|
100 |
translator = PonsTranslator
|
101 |
-
elif
|
102 |
translator = YandexTranslator(api_key=api_key)
|
103 |
-
elif
|
104 |
translator = MicrosoftTranslator(api_key=api_key)
|
105 |
-
elif
|
106 |
translator = PapagoTranslator(api_key=api_key)
|
107 |
else:
|
108 |
-
|
109 |
-
"The given translator is not supported."
|
110 |
-
" Please use a translator supported by the deep_translator tool"
|
111 |
-
)
|
112 |
-
return
|
113 |
|
114 |
supported_languages = translator.get_supported_languages(as_dict=True)
|
115 |
-
click.echo(f"Languages supported by '{
|
116 |
for k, v in supported_languages.items():
|
117 |
click.echo(f"|- {k}: {v}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
-
|
|
|
12 |
from .papago import PapagoTranslator
|
13 |
|
14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
15 |
+
@click.group()
|
16 |
+
def cli():
|
17 |
+
pass
|
18 |
|
19 |
+
@cli.command(context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
|
20 |
@click.argument('translator', required=True, default='google', type=str)
|
21 |
@click.option("--source", "-src", required=True, type=str, help="source language to translate from")
|
22 |
@click.option("--target", "-tgt", required=True, type=str, help="target language to translate to")
|
23 |
@click.option("--text", "-txt", type=str,required = True,prompt="Enter the text you want to translate",help="text you want to translate")
|
24 |
@click.option("--api-key",type=str,help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators")
|
25 |
+
def translate(translator, source, target, text, api_key, languages):
|
|
|
|
|
26 |
"""
|
27 |
Use TRANSLATOR to translate source material into another language.
|
28 |
+
\f
|
29 |
+
Directory function to send arguments to the correct translator.
|
30 |
+
@param translator: translator name parsed from terminal arguments
|
31 |
+
@return: None
|
32 |
"""
|
33 |
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
34 |
if translator in api_key_required and not api_key:
|
35 |
click.echo(
|
36 |
+
"This translator requires an api key provided through --api-key")
|
|
|
|
|
|
|
37 |
else:
|
38 |
+
pass
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if translator == "google":
|
41 |
translator = GoogleTranslator(source=source, target=target)
|
42 |
elif translator == "mymemory":
|
|
|
65 |
target=target,
|
66 |
api_key=api_key)
|
67 |
else:
|
68 |
+
raise AttributeError("The given translator is not supported.")
|
|
|
|
|
|
|
69 |
|
70 |
res = translator.translate(text)
|
71 |
click.echo(f" | Translation from {source} to {target} |")
|
72 |
click.echo(f"Translated text: \n {res}")
|
73 |
+
return 0
|
74 |
|
75 |
+
@cli.command(context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
|
76 |
+
@click.argument('translator')
|
77 |
+
@click.argument('api_key', required=False)
|
78 |
+
def languages(translator, api_key):
|
79 |
"""
|
80 |
Retrieve the list of available languages from the given translator.
|
81 |
+
@param translator: Translator given by the user.
|
82 |
@param api_key: Optional API key given by the user. Required for some translators.
|
83 |
@return: None
|
84 |
"""
|
85 |
+
translator = translator.lower()
|
86 |
+
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
87 |
+
if translator in api_key_required and not api_key:
|
88 |
+
click.echo("This translator requires an api key provided through --api-key")
|
89 |
+
else:
|
90 |
+
pass
|
91 |
+
|
92 |
+
if translator == "google":
|
93 |
translator = GoogleTranslator
|
94 |
+
elif translator == "mymemory":
|
95 |
translator = MyMemoryTranslator
|
96 |
+
elif translator == "qcri":
|
97 |
translator = QCRI(api_key=api_key)
|
98 |
+
elif translator == "linguee":
|
99 |
translator = LingueeTranslator
|
100 |
+
elif translator == "pons":
|
101 |
translator = PonsTranslator
|
102 |
+
elif translator == "yandex":
|
103 |
translator = YandexTranslator(api_key=api_key)
|
104 |
+
elif translator == "microsoft":
|
105 |
translator = MicrosoftTranslator(api_key=api_key)
|
106 |
+
elif translator == "papago":
|
107 |
translator = PapagoTranslator(api_key=api_key)
|
108 |
else:
|
109 |
+
raise AttributeError("The given translator is not supported.")
|
|
|
|
|
|
|
|
|
110 |
|
111 |
supported_languages = translator.get_supported_languages(as_dict=True)
|
112 |
+
click.echo(f"Languages supported by '{translator}' are :")
|
113 |
for k, v in supported_languages.items():
|
114 |
click.echo(f"|- {k}: {v}")
|
115 |
+
return 0
|
116 |
+
|
117 |
+
@cli.command()
|
118 |
+
def list():
|
119 |
+
"""Lists available translators."""
|
120 |
+
click.echo("Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.")
|
121 |
+
return 0
|
122 |
|
123 |
if __name__ == "__main__":
|
124 |
+
cli()
|
deep_translator/tests/test_cli.py
CHANGED
@@ -7,10 +7,16 @@ from deep_translator import __main__
|
|
7 |
|
8 |
def results_test():
|
9 |
runner = CliRunner()
|
10 |
-
result = runner.invoke(__main__.
|
11 |
assert result.exit_code == 0
|
12 |
assert result == 'good'
|
13 |
|
14 |
-
api_error = runner.invoke(__main__.
|
15 |
assert api_error.exit_code == 0
|
16 |
-
assert api_error == "This translator requires an api key provided through --api-key"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def results_test():
|
9 |
runner = CliRunner()
|
10 |
+
result = runner.invoke(__main__.translate, [ 'google', 'auto', 'en', '좋은'])
|
11 |
assert result.exit_code == 0
|
12 |
assert result == 'good'
|
13 |
|
14 |
+
api_error = runner.invoke(__main__.translate, ['microsoft','auto','en','Zwei minimale Dellchen auf der Rückseite.'])
|
15 |
assert api_error.exit_code == 0
|
16 |
+
assert api_error == "This translator requires an api key provided through --api-key"
|
17 |
+
|
18 |
+
language_list_test = runner.invoke(__main__.languages, ['google'])
|
19 |
+
assert language_list_test.exit_code == 0
|
20 |
+
|
21 |
+
language_list_invalid_test = runner.invoke(__main__.languages, ['notValidTranslator'])
|
22 |
+
assert language_list_invalid_test.exception == AttributeError
|
setup.cfg
CHANGED
@@ -66,15 +66,15 @@ replace = version='{new_version}'
|
|
66 |
search = __version__ = '{current_version}'
|
67 |
replace = __version__ = '{new_version}'
|
68 |
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
#
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
|
|
66 |
search = __version__ = '{current_version}'
|
67 |
replace = __version__ = '{new_version}'
|
68 |
|
69 |
+
[bdist_wheel]
|
70 |
+
universal = 1
|
71 |
|
72 |
+
[flake8]
|
73 |
+
exclude = docs
|
74 |
|
75 |
+
[aliases]
|
76 |
+
# Define setup.py command aliases here
|
77 |
+
test = pytest
|
78 |
|
79 |
+
[tool:pytest]
|
80 |
+
collect_ignore = [setup.py]
|
setup.py
CHANGED
@@ -11,7 +11,7 @@ setup(
|
|
11 |
include_package_data=True,
|
12 |
packages=find_packages(include=['deep_translator'],),
|
13 |
entry_points={'console_scripts':[
|
14 |
-
'deep-translator=deep_translator.__main__:
|
15 |
-
'dt=deep_translator.__main__:
|
16 |
],},
|
17 |
)
|
|
|
11 |
include_package_data=True,
|
12 |
packages=find_packages(include=['deep_translator'],),
|
13 |
entry_points={'console_scripts':[
|
14 |
+
'deep-translator=deep_translator.__main__:cli',
|
15 |
+
'dt=deep_translator.__main__:cli',
|
16 |
],},
|
17 |
)
|