nidhal baccouri
commited on
Commit
·
c228ada
1
Parent(s):
4a2e5df
added unit tests for cli
Browse files- deep_translator/__main__.py +1 -38
- deep_translator/cli.py +44 -4
- tests/test_cli.py +19 -27
- tests/test_google_trans.py +2 -0
deep_translator/__main__.py
CHANGED
|
@@ -4,44 +4,7 @@ from .cli import CLI
|
|
| 4 |
|
| 5 |
|
| 6 |
def main():
|
| 7 |
-
|
| 8 |
-
function responsible for parsing terminal arguments and provide them for further use in the translation process
|
| 9 |
-
"""
|
| 10 |
-
parser = argparse.ArgumentParser(add_help=True,
|
| 11 |
-
description="Official CLI for deep-translator",
|
| 12 |
-
usage="dt --help")
|
| 13 |
-
|
| 14 |
-
parser.add_argument('--translator',
|
| 15 |
-
'-trans',
|
| 16 |
-
default='google',
|
| 17 |
-
type=str,
|
| 18 |
-
help="name of the translator you want to use")
|
| 19 |
-
parser.add_argument('--source',
|
| 20 |
-
'-src',
|
| 21 |
-
default='auto',
|
| 22 |
-
type=str,
|
| 23 |
-
help="source language to translate from")
|
| 24 |
-
parser.add_argument('--target',
|
| 25 |
-
'-tg',
|
| 26 |
-
type=str,
|
| 27 |
-
help="target language to translate to")
|
| 28 |
-
parser.add_argument('--text',
|
| 29 |
-
'-txt',
|
| 30 |
-
type=str,
|
| 31 |
-
help="text you want to translate")
|
| 32 |
-
parser.add_argument('--languages',
|
| 33 |
-
'-lang',
|
| 34 |
-
action='store_true',
|
| 35 |
-
help="all the languages available with the translator"
|
| 36 |
-
"Run the command deep_translator -trans <translator service> -lang")
|
| 37 |
-
|
| 38 |
-
args = parser.parse_args()
|
| 39 |
-
|
| 40 |
-
cli = CLI(args)
|
| 41 |
-
if args.languages:
|
| 42 |
-
cli.get_supported_languages()
|
| 43 |
-
else:
|
| 44 |
-
cli.translate()
|
| 45 |
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def main():
|
| 7 |
+
CLI().run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
if __name__ == "__main__":
|
deep_translator/cli.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
"""Console script for deep_translator."""
|
| 2 |
-
import sys
|
| 3 |
|
| 4 |
from .google_trans import GoogleTranslator
|
| 5 |
from .mymemory import MyMemoryTranslator
|
|
@@ -11,6 +10,7 @@ from .qcri import QCRI
|
|
| 11 |
from .papago import PapagoTranslator
|
| 12 |
from .microsoft import MicrosoftTranslator
|
| 13 |
from .libre import LibreTranslator
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
class CLI(object):
|
|
@@ -18,7 +18,7 @@ class CLI(object):
|
|
| 18 |
translators_dict = None
|
| 19 |
translator = None
|
| 20 |
|
| 21 |
-
def __init__(self,
|
| 22 |
self.translators_dict = {
|
| 23 |
'google': GoogleTranslator,
|
| 24 |
'pons': PonsTranslator,
|
|
@@ -31,12 +31,14 @@ class CLI(object):
|
|
| 31 |
'qcri': QCRI,
|
| 32 |
'papago': PapagoTranslator
|
| 33 |
}
|
| 34 |
-
self.
|
|
|
|
|
|
|
| 35 |
translator_class = self.translators_dict.get(self.args.translator, None)
|
| 36 |
if not translator_class:
|
| 37 |
raise Exception(f"Translator {self.args.translator} is not supported."
|
| 38 |
f"Supported translators: {list(self.translators_dict.keys())}")
|
| 39 |
-
self.translator = translator_class(source=args.source, target=args.target)
|
| 40 |
|
| 41 |
def translate(self):
|
| 42 |
"""
|
|
@@ -58,5 +60,43 @@ class CLI(object):
|
|
| 58 |
print(f'Languages supported by \'{self.args.translator}\' are :\n')
|
| 59 |
print(translator_supported_languages)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""Console script for deep_translator."""
|
|
|
|
| 2 |
|
| 3 |
from .google_trans import GoogleTranslator
|
| 4 |
from .mymemory import MyMemoryTranslator
|
|
|
|
| 10 |
from .papago import PapagoTranslator
|
| 11 |
from .microsoft import MicrosoftTranslator
|
| 12 |
from .libre import LibreTranslator
|
| 13 |
+
import argparse
|
| 14 |
|
| 15 |
|
| 16 |
class CLI(object):
|
|
|
|
| 18 |
translators_dict = None
|
| 19 |
translator = None
|
| 20 |
|
| 21 |
+
def __init__(self, custom_args=None):
|
| 22 |
self.translators_dict = {
|
| 23 |
'google': GoogleTranslator,
|
| 24 |
'pons': PonsTranslator,
|
|
|
|
| 31 |
'qcri': QCRI,
|
| 32 |
'papago': PapagoTranslator
|
| 33 |
}
|
| 34 |
+
self.custom_args = custom_args
|
| 35 |
+
self.args = self.parse_args()
|
| 36 |
+
|
| 37 |
translator_class = self.translators_dict.get(self.args.translator, None)
|
| 38 |
if not translator_class:
|
| 39 |
raise Exception(f"Translator {self.args.translator} is not supported."
|
| 40 |
f"Supported translators: {list(self.translators_dict.keys())}")
|
| 41 |
+
self.translator = translator_class(source=self.args.source, target=self.args.target)
|
| 42 |
|
| 43 |
def translate(self):
|
| 44 |
"""
|
|
|
|
| 60 |
print(f'Languages supported by \'{self.args.translator}\' are :\n')
|
| 61 |
print(translator_supported_languages)
|
| 62 |
|
| 63 |
+
def parse_args(self):
|
| 64 |
+
"""
|
| 65 |
+
function responsible for parsing terminal arguments and provide them for further use in the translation process
|
| 66 |
+
"""
|
| 67 |
+
parser = argparse.ArgumentParser(add_help=True,
|
| 68 |
+
description="Official CLI for deep-translator",
|
| 69 |
+
usage="dt --help")
|
| 70 |
|
| 71 |
+
parser.add_argument('--translator',
|
| 72 |
+
'-trans',
|
| 73 |
+
default='google',
|
| 74 |
+
type=str,
|
| 75 |
+
help="name of the translator you want to use")
|
| 76 |
+
parser.add_argument('--source',
|
| 77 |
+
'-src',
|
| 78 |
+
default='auto',
|
| 79 |
+
type=str,
|
| 80 |
+
help="source language to translate from")
|
| 81 |
+
parser.add_argument('--target',
|
| 82 |
+
'-tg',
|
| 83 |
+
type=str,
|
| 84 |
+
help="target language to translate to")
|
| 85 |
+
parser.add_argument('--text',
|
| 86 |
+
'-txt',
|
| 87 |
+
type=str,
|
| 88 |
+
help="text you want to translate")
|
| 89 |
+
parser.add_argument('--languages',
|
| 90 |
+
'-lang',
|
| 91 |
+
action='store_true',
|
| 92 |
+
help="all the languages available with the translator"
|
| 93 |
+
"Run the command deep_translator -trans <translator service> -lang")
|
| 94 |
+
parsed_args = parser.parse_args(self.custom_args) if self.custom_args else parser.parse_args()
|
| 95 |
+
print(f"parsed args: {parsed_args}")
|
| 96 |
+
return parsed_args
|
| 97 |
|
| 98 |
+
def run(self):
|
| 99 |
+
if self.args.languages:
|
| 100 |
+
self.get_supported_languages()
|
| 101 |
+
else:
|
| 102 |
+
self.translate()
|
tests/test_cli.py
CHANGED
|
@@ -2,30 +2,22 @@
|
|
| 2 |
|
| 3 |
"""Tests for the CLI interface."""
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# result = runner.invoke(cli, ['languages', 'google'])
|
| 25 |
-
# assert result.exit_code == 0
|
| 26 |
-
#
|
| 27 |
-
# def test_invalid_language_languages(self):
|
| 28 |
-
# runner = CliRunner()
|
| 29 |
-
# result = runner.invoke(cli, ['languages', 'notValidTranslator'])
|
| 30 |
-
# assert 'The given translator is not supported.' in str(result.exception)
|
| 31 |
-
# assert result.exit_code == 1
|
|
|
|
| 2 |
|
| 3 |
"""Tests for the CLI interface."""
|
| 4 |
|
| 5 |
+
from deep_translator.cli import CLI
|
| 6 |
+
import pytest
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def mock_args():
|
| 12 |
+
sys.argv[1:] = ['--source', 'en', '--target', 'de', '--text', 'hello']
|
| 13 |
+
return CLI(sys.argv[1:]).parse_args()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_source(mock_args):
|
| 17 |
+
assert mock_args.source == 'en'
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_target(mock_args):
|
| 21 |
+
assert mock_args.target == 'de'
|
| 22 |
+
|
| 23 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_google_trans.py
CHANGED
|
@@ -135,6 +135,7 @@ def test_content(google_translator):
|
|
| 135 |
# assert 'GitHub' in BeautifulSoup(response.content).title.string
|
| 136 |
assert google_translator.translate(text='좋은') == "good"
|
| 137 |
|
|
|
|
| 138 |
def test_abbreviations_and_languages_mapping():
|
| 139 |
for abb, lang in GOOGLE_CODES_TO_LANGUAGES.items():
|
| 140 |
if abb != 'en':
|
|
@@ -142,6 +143,7 @@ def test_abbreviations_and_languages_mapping():
|
|
| 142 |
g2 = GoogleTranslator(lang)
|
| 143 |
assert g1._source == g2._source
|
| 144 |
|
|
|
|
| 145 |
def test_inputs():
|
| 146 |
with pytest.raises(exceptions.LanguageNotSupportedException):
|
| 147 |
GoogleTranslator(source="", target="")
|
|
|
|
| 135 |
# assert 'GitHub' in BeautifulSoup(response.content).title.string
|
| 136 |
assert google_translator.translate(text='좋은') == "good"
|
| 137 |
|
| 138 |
+
|
| 139 |
def test_abbreviations_and_languages_mapping():
|
| 140 |
for abb, lang in GOOGLE_CODES_TO_LANGUAGES.items():
|
| 141 |
if abb != 'en':
|
|
|
|
| 143 |
g2 = GoogleTranslator(lang)
|
| 144 |
assert g1._source == g2._source
|
| 145 |
|
| 146 |
+
|
| 147 |
def test_inputs():
|
| 148 |
with pytest.raises(exceptions.LanguageNotSupportedException):
|
| 149 |
GoogleTranslator(source="", target="")
|