Chris
commited on
Commit
·
92cc291
1
Parent(s):
a48a547
started the refactoring & added a test_cli.py
Browse files- deep_translator/cli.py +27 -15
- deep_translator/deepl.py +2 -2
- deep_translator/detection.py +1 -1
- deep_translator/exceptions.py +2 -2
- deep_translator/google_trans.py +3 -3
- deep_translator/linguee.py +3 -3
- deep_translator/microsoft.py +2 -2
- deep_translator/mymemory.py +3 -3
- deep_translator/papago.py +2 -2
- deep_translator/parent.py +1 -1
- deep_translator/pons.py +3 -3
- deep_translator/qcri.py +2 -4
- deep_translator/tests/test_cli.py +12 -0
- deep_translator/yandex.py +3 -3
deep_translator/cli.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
"""Console script for deep_translator."""
|
2 |
|
3 |
import click
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from
|
10 |
-
from
|
11 |
-
from
|
12 |
-
from
|
13 |
|
14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
15 |
|
@@ -104,23 +104,33 @@ def print_supported_languages(requested_translator, api_key):
|
|
104 |
|
105 |
|
106 |
@click.command(context_settings=CONTEXT_SETTINGS)
|
107 |
-
@click.option(
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
type=str,
|
112 |
-
help="name of the translator you want to use",
|
113 |
-
show_default=True,
|
114 |
)
|
|
|
115 |
@click.option(
|
116 |
"--source",
|
117 |
"-src",
|
|
|
118 |
type=str,
|
119 |
help="source language to translate from"
|
120 |
)
|
121 |
@click.option(
|
122 |
"--target",
|
123 |
"-tg",
|
|
|
124 |
type=str,
|
125 |
help="target language to translate to"
|
126 |
)
|
@@ -144,6 +154,8 @@ def print_supported_languages(requested_translator, api_key):
|
|
144 |
)
|
145 |
def main(translator, source, target, text, api_key, languages):
|
146 |
"""
|
|
|
|
|
147 |
\f
|
148 |
function responsible for parsing terminal arguments and provide them for
|
149 |
further use in the translation process
|
|
|
1 |
"""Console script for deep_translator."""
|
2 |
|
3 |
import click
|
4 |
+
from google_trans import GoogleTranslator
|
5 |
+
from mymemory import MyMemoryTranslator
|
6 |
+
from deepl import DeepL
|
7 |
+
from qcri import QCRI
|
8 |
+
from linguee import LingueeTranslator
|
9 |
+
from pons import PonsTranslator
|
10 |
+
from yandex import YandexTranslator
|
11 |
+
from microsoft import MicrosoftTranslator
|
12 |
+
from papago import PapagoTranslator
|
13 |
|
14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
15 |
|
|
|
104 |
|
105 |
|
106 |
@click.command(context_settings=CONTEXT_SETTINGS)
|
107 |
+
# @click.option(
|
108 |
+
# "--translator",
|
109 |
+
# "-trans",
|
110 |
+
# default="google",
|
111 |
+
# type=str,
|
112 |
+
# help="name of the translator you want to use",
|
113 |
+
# show_default=True,
|
114 |
+
# )
|
115 |
+
|
116 |
+
@click.argument(
|
117 |
+
'translator',
|
118 |
+
required=True,
|
119 |
+
default='google',
|
120 |
type=str,
|
|
|
|
|
121 |
)
|
122 |
+
|
123 |
@click.option(
|
124 |
"--source",
|
125 |
"-src",
|
126 |
+
required=True
|
127 |
type=str,
|
128 |
help="source language to translate from"
|
129 |
)
|
130 |
@click.option(
|
131 |
"--target",
|
132 |
"-tg",
|
133 |
+
required=True
|
134 |
type=str,
|
135 |
help="target language to translate to"
|
136 |
)
|
|
|
154 |
)
|
155 |
def main(translator, source, target, text, api_key, languages):
|
156 |
"""
|
157 |
+
Use TRANSLATOR to translate source material into another language.
|
158 |
+
Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.
|
159 |
\f
|
160 |
function responsible for parsing terminal arguments and provide them for
|
161 |
further use in the translation process
|
deep_translator/deepl.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import requests
|
2 |
-
from
|
3 |
-
from
|
4 |
TranslationNotFound,
|
5 |
LanguageNotSupportedException,
|
6 |
AuthorizationException)
|
|
|
1 |
import requests
|
2 |
+
from constants import BASE_URLS, DEEPL_LANGUAGE_TO_CODE
|
3 |
+
from exceptions import (ServerException,
|
4 |
TranslationNotFound,
|
5 |
LanguageNotSupportedException,
|
6 |
AuthorizationException)
|
deep_translator/detection.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
language detection API
|
3 |
"""
|
4 |
import requests
|
5 |
-
from
|
6 |
from requests.exceptions import HTTPError
|
7 |
|
8 |
|
|
|
2 |
language detection API
|
3 |
"""
|
4 |
import requests
|
5 |
+
from configs import config
|
6 |
from requests.exceptions import HTTPError
|
7 |
|
8 |
|
deep_translator/exceptions.py
CHANGED
@@ -82,7 +82,7 @@ class NotValidLength(BaseError):
|
|
82 |
|
83 |
class RequestError(Exception):
|
84 |
"""
|
85 |
-
exception thrown if an error
|
86 |
"""
|
87 |
|
88 |
def __init__(self, message="Request exception can happen due to an api connection error. "
|
@@ -108,7 +108,7 @@ class MicrosoftAPIerror(Exception):
|
|
108 |
|
109 |
class TooManyRequests(Exception):
|
110 |
"""
|
111 |
-
exception thrown if an error
|
112 |
"""
|
113 |
|
114 |
def __init__(self, message="Server Error: You made too many requests to the server. According to google, you are allowed to make 5 requests per second and up to 200k requests per day. You can wait and try again later or you can try the translate_batch function"):
|
|
|
82 |
|
83 |
class RequestError(Exception):
|
84 |
"""
|
85 |
+
exception thrown if an error occurred during the request call, e.g a connection problem.
|
86 |
"""
|
87 |
|
88 |
def __init__(self, message="Request exception can happen due to an api connection error. "
|
|
|
108 |
|
109 |
class TooManyRequests(Exception):
|
110 |
"""
|
111 |
+
exception thrown if an error occurred during the request call, e.g a connection problem.
|
112 |
"""
|
113 |
|
114 |
def __init__(self, message="Server Error: You made too many requests to the server. According to google, you are allowed to make 5 requests per second and up to 200k requests per day. You can wait and try again later or you can try the translate_batch function"):
|
deep_translator/google_trans.py
CHANGED
@@ -2,9 +2,9 @@
|
|
2 |
google translator API
|
3 |
"""
|
4 |
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
from bs4 import BeautifulSoup
|
9 |
import requests
|
10 |
from time import sleep
|
|
|
2 |
google translator API
|
3 |
"""
|
4 |
|
5 |
+
from constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
6 |
+
from exceptions import TooManyRequests, LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
|
7 |
+
from parent import BaseTranslator
|
8 |
from bs4 import BeautifulSoup
|
9 |
import requests
|
10 |
from time import sleep
|
deep_translator/linguee.py
CHANGED
@@ -2,14 +2,14 @@
|
|
2 |
linguee translator API
|
3 |
"""
|
4 |
|
5 |
-
from
|
6 |
-
from
|
7 |
TranslationNotFound,
|
8 |
NotValidPayload,
|
9 |
ElementNotFoundInGetRequest,
|
10 |
RequestError,
|
11 |
TooManyRequests)
|
12 |
-
from
|
13 |
from bs4 import BeautifulSoup
|
14 |
import requests
|
15 |
from requests.utils import requote_uri
|
|
|
2 |
linguee translator API
|
3 |
"""
|
4 |
|
5 |
+
from constants import BASE_URLS, LINGUEE_LANGUAGES_TO_CODES, LINGUEE_CODE_TO_LANGUAGE
|
6 |
+
from exceptions import (LanguageNotSupportedException,
|
7 |
TranslationNotFound,
|
8 |
NotValidPayload,
|
9 |
ElementNotFoundInGetRequest,
|
10 |
RequestError,
|
11 |
TooManyRequests)
|
12 |
+
from parent import BaseTranslator
|
13 |
from bs4 import BeautifulSoup
|
14 |
import requests
|
15 |
from requests.utils import requote_uri
|
deep_translator/microsoft.py
CHANGED
@@ -4,8 +4,8 @@ import requests
|
|
4 |
import logging
|
5 |
import sys
|
6 |
|
7 |
-
from
|
8 |
-
from
|
9 |
|
10 |
|
11 |
class MicrosoftTranslator:
|
|
|
4 |
import logging
|
5 |
import sys
|
6 |
|
7 |
+
from constants import BASE_URLS, MICROSOFT_CODES_TO_LANGUAGES
|
8 |
+
from exceptions import LanguageNotSupportedException, ServerException, MicrosoftAPIerror
|
9 |
|
10 |
|
11 |
class MicrosoftTranslator:
|
deep_translator/mymemory.py
CHANGED
@@ -4,13 +4,13 @@ mymemory translator API
|
|
4 |
import logging
|
5 |
import warnings
|
6 |
|
7 |
-
from
|
8 |
-
from
|
9 |
TranslationNotFound,
|
10 |
LanguageNotSupportedException,
|
11 |
RequestError,
|
12 |
TooManyRequests)
|
13 |
-
from
|
14 |
import requests
|
15 |
from time import sleep
|
16 |
|
|
|
4 |
import logging
|
5 |
import warnings
|
6 |
|
7 |
+
from constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
8 |
+
from exceptions import (NotValidPayload,
|
9 |
TranslationNotFound,
|
10 |
LanguageNotSupportedException,
|
11 |
RequestError,
|
12 |
TooManyRequests)
|
13 |
+
from parent import BaseTranslator
|
14 |
import requests
|
15 |
from time import sleep
|
16 |
|
deep_translator/papago.py
CHANGED
@@ -2,8 +2,8 @@
|
|
2 |
google translator API
|
3 |
"""
|
4 |
import json
|
5 |
-
from
|
6 |
-
from
|
7 |
import requests
|
8 |
import warnings
|
9 |
import logging
|
|
|
2 |
google translator API
|
3 |
"""
|
4 |
import json
|
5 |
+
from constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
|
6 |
+
from exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload
|
7 |
import requests
|
8 |
import warnings
|
9 |
import logging
|
deep_translator/parent.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
"""parent translator class"""
|
2 |
|
3 |
-
from
|
4 |
from abc import ABC, abstractmethod
|
5 |
import string
|
6 |
|
|
|
1 |
"""parent translator class"""
|
2 |
|
3 |
+
from exceptions import NotValidPayload, NotValidLength, InvalidSourceOrTargetLanguage
|
4 |
from abc import ABC, abstractmethod
|
5 |
import string
|
6 |
|
deep_translator/pons.py
CHANGED
@@ -3,14 +3,14 @@ pons translator API
|
|
3 |
"""
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
6 |
-
from
|
7 |
-
from
|
8 |
TranslationNotFound,
|
9 |
NotValidPayload,
|
10 |
ElementNotFoundInGetRequest,
|
11 |
RequestError,
|
12 |
TooManyRequests)
|
13 |
-
from
|
14 |
from requests.utils import requote_uri
|
15 |
|
16 |
|
|
|
3 |
"""
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
6 |
+
from constants import BASE_URLS, PONS_LANGUAGES_TO_CODES, PONS_CODES_TO_LANGUAGES
|
7 |
+
from exceptions import (LanguageNotSupportedException,
|
8 |
TranslationNotFound,
|
9 |
NotValidPayload,
|
10 |
ElementNotFoundInGetRequest,
|
11 |
RequestError,
|
12 |
TooManyRequests)
|
13 |
+
from parent import BaseTranslator
|
14 |
from requests.utils import requote_uri
|
15 |
|
16 |
|
deep_translator/qcri.py
CHANGED
@@ -1,9 +1,7 @@
|
|
1 |
|
2 |
import requests
|
3 |
-
from
|
4 |
-
from
|
5 |
-
ServerException, TranslationNotFound)
|
6 |
-
|
7 |
|
8 |
class QCRI(object):
|
9 |
"""
|
|
|
1 |
|
2 |
import requests
|
3 |
+
from constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
|
4 |
+
from exceptions import (ServerException, TranslationNotFound)
|
|
|
|
|
5 |
|
6 |
class QCRI(object):
|
7 |
"""
|
deep_translator/tests/test_cli.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
"""Tests for `deep_translator` package."""
|
4 |
+
|
5 |
+
from click.testing import CliRunner
|
6 |
+
import cli
|
7 |
+
|
8 |
+
def cli():
|
9 |
+
runner = CliRunner()
|
10 |
+
result = runner.invoke(cli.translate, [ 'google', 'auto', 'en', '좋은'])
|
11 |
+
assert result.exit_code == 0
|
12 |
+
assert result == 'good'
|
deep_translator/yandex.py
CHANGED
@@ -2,9 +2,9 @@
|
|
2 |
Yandex translator API
|
3 |
"""
|
4 |
import requests
|
5 |
-
from
|
6 |
-
from
|
7 |
-
|
8 |
|
9 |
|
10 |
class YandexTranslator(object):
|
|
|
2 |
Yandex translator API
|
3 |
"""
|
4 |
import requests
|
5 |
+
from constants import BASE_URLS
|
6 |
+
from exceptions import (RequestError,
|
7 |
+
ServerException, TranslationNotFound, TooManyRequests)
|
8 |
|
9 |
|
10 |
class YandexTranslator(object):
|