Skip to content

Commit

Permalink
Merge pull request #220 from nidhaloff/hotfix/digit-translation
Browse files Browse the repository at this point in the history
Hotfix/digit translation
  • Loading branch information
nidhaloff authored Jun 28, 2023
2 parents 5891228 + 6bf8aa6 commit c9fc8b6
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions deep_translator/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ServerException,
TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid
from deep_translator.validate import is_empty, is_input_valid, request_failed


class DeeplTranslator(BaseTranslator):
Expand Down Expand Up @@ -85,7 +85,7 @@ def translate(self, text: str, **kwargs) -> str:
# If the answer is not success, raise server exception.
if response.status_code == 403:
raise AuthorizationException(self.api_key)
elif response.status_code != 200:
if request_failed(status_code=response.status_code):
raise ServerException(response.status_code)
# Get the response and check is not empty.
res = response.json()
Expand Down
4 changes: 2 additions & 2 deletions deep_translator/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
TooManyRequests,
TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid
from deep_translator.validate import is_empty, is_input_valid, request_failed


class GoogleTranslator(BaseTranslator):
Expand Down Expand Up @@ -70,7 +70,7 @@ def translate(self, text: str, **kwargs) -> str:
if response.status_code == 429:
raise TooManyRequests()

if response.status_code != 200:
if request_failed(status_code=response.status_code):
raise RequestError()

soup = BeautifulSoup(response.text, "html.parser")
Expand Down
4 changes: 2 additions & 2 deletions deep_translator/libre.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ServerException,
TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid
from deep_translator.validate import is_empty, is_input_valid, request_failed


class LibreTranslator(BaseTranslator):
Expand Down Expand Up @@ -95,7 +95,7 @@ def translate(self, text: str, **kwargs) -> str:

if response.status_code == 403:
raise AuthorizationException(self.api_key)
elif response.status_code != 200:
elif request_failed(status_code=response.status_code):
raise ServerException(response.status_code)
# Get the response and check is not empty.
res = response.json()
Expand Down
5 changes: 3 additions & 2 deletions deep_translator/linguee.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
TooManyRequests,
TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid
from deep_translator.validate import is_empty, is_input_valid, request_failed


class LingueeTranslator(BaseTranslator):
Expand Down Expand Up @@ -72,8 +72,9 @@ def translate(
if response.status_code == 429:
raise TooManyRequests()

if response.status_code != 200:
if request_failed(status_code=response.status_code):
raise RequestError()

soup = BeautifulSoup(response.text, "html.parser")
elements = soup.find_all(self._element_tag, self._element_query)
response.close()
Expand Down
4 changes: 2 additions & 2 deletions deep_translator/mymemory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
TooManyRequests,
TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid
from deep_translator.validate import is_empty, is_input_valid, request_failed


class MyMemoryTranslator(BaseTranslator):
Expand Down Expand Up @@ -71,7 +71,7 @@ def translate(

if response.status_code == 429:
raise TooManyRequests()
if response.status_code != 200:
if request_failed(status_code=response.status_code):
raise RequestError()

data = response.json()
Expand Down
4 changes: 2 additions & 2 deletions deep_translator/papago.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from deep_translator.base import BaseTranslator
from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
from deep_translator.exceptions import TranslationNotFound
from deep_translator.validate import is_input_valid
from deep_translator.validate import is_input_valid, request_failed


class PapagoTranslator(BaseTranslator):
Expand Down Expand Up @@ -67,7 +67,7 @@ def translate(self, text: str, **kwargs) -> str:
response = requests.post(
self._base_url, headers=headers, data=payload
)
if response.status_code != 200:
if request_failed(status_code=response.status_code):
raise Exception(
f"Translation error! -> status code: {response.status_code}"
)
Expand Down
4 changes: 2 additions & 2 deletions deep_translator/pons.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
TooManyRequests,
TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid
from deep_translator.validate import is_empty, is_input_valid, request_failed


class PonsTranslator(BaseTranslator):
Expand Down Expand Up @@ -71,7 +71,7 @@ def translate(
if response.status_code == 429:
raise TooManyRequests()

if response.status_code != 200:
if request_failed(status_code=response.status_code):
raise RequestError()

soup = BeautifulSoup(response.text, "html.parser")
Expand Down
3 changes: 2 additions & 1 deletion deep_translator/qcri.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ServerException,
TranslationNotFound,
)
from deep_translator.validate import request_failed


class QcriTranslator(BaseTranslator):
Expand Down Expand Up @@ -96,7 +97,7 @@ def translate(self, text: str, **kwargs) -> str:
raise ServerException(503)

else:
if response.status_code != 200:
if request_failed(status_code=response.status_code):
ServerException(response.status_code)
else:
res = response.json()
Expand Down
17 changes: 16 additions & 1 deletion deep_translator/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ def is_empty(text: str) -> bool:
return text == ""


def request_failed(status_code: int) -> bool:
"""Check if a request has failed or not.
A request is considered successfull if the status code is in the 2** range.
Args:
status_code (int): status code of the request
Returns:
bool: indicates request failure
"""
if status_code > 299 or status_code < 200:
return True
return False


def is_input_valid(
text: str, min_chars: int = 0, max_chars: Optional[int] = None
) -> bool:
Expand All @@ -20,7 +35,7 @@ def is_input_valid(
@return: bool
"""

if not isinstance(text, str) or text.isdigit():
if not isinstance(text, str):
raise NotValidPayload(text)
if max_chars and (not min_chars <= len(text) < max_chars):
raise NotValidLength(text, min_chars, max_chars)
Expand Down
4 changes: 2 additions & 2 deletions deep_translator/yandex.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
TooManyRequests,
TranslationNotFound,
)
from deep_translator.validate import is_input_valid
from deep_translator.validate import is_input_valid, request_failed


class YandexTranslator(BaseTranslator):
Expand Down Expand Up @@ -75,7 +75,7 @@ def dirs(self, proxies: Optional[dict] = None):
else:
data = response.json()

if response.status_code != 200:
if request_failed(status_code=response.status_code):
raise ServerException(response.status_code)
return data.get("dirs")

Expand Down
8 changes: 0 additions & 8 deletions tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ def test_empty_text(google_translator):


def test_payload(google_translator):
with pytest.raises(exceptions.NotValidPayload):
google_translator.translate(text="1234")
google_translator.translate(text="{}")
google_translator.translate(text="%@")

with pytest.raises(exceptions.NotValidPayload):
google_translator.translate(text=123)

with pytest.raises(exceptions.NotValidPayload):
google_translator.translate(text={})

Expand Down
3 changes: 0 additions & 3 deletions tests/test_libre.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def test_abbreviations_and_languages_mapping():


def test_payload(libre):
with pytest.raises(exceptions.NotValidPayload):
libre.translate(123)

with pytest.raises(exceptions.NotValidPayload):
libre.translate({})

Expand Down
3 changes: 0 additions & 3 deletions tests/test_linguee.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ def test_inputs():


def test_payload(linguee):
with pytest.raises(exceptions.NotValidPayload):
linguee.translate(123)

with pytest.raises(exceptions.NotValidPayload):
linguee.translate({})

Expand Down
3 changes: 0 additions & 3 deletions tests/test_mymemory.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ def test_inputs():


def test_payload(mymemory):
with pytest.raises(exceptions.NotValidPayload):
mymemory.translate(text=123)

with pytest.raises(exceptions.NotValidPayload):
mymemory.translate(text={})

Expand Down
3 changes: 0 additions & 3 deletions tests/test_pons.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ def test_inputs():


def test_payload(pons):
with pytest.raises(exceptions.NotValidPayload):
pons.translate(123)

with pytest.raises(exceptions.NotValidPayload):
pons.translate({})

Expand Down

0 comments on commit c9fc8b6

Please sign in to comment.