Nidhal Baccouri commited on
Commit
c9fc8b6
·
unverified ·
2 Parent(s): 5891228 6bf8aa6

Merge pull request #220 from nidhaloff/hotfix/digit-translation

Browse files
deep_translator/deepl.py CHANGED
@@ -17,7 +17,7 @@ from deep_translator.exceptions import (
17
  ServerException,
18
  TranslationNotFound,
19
  )
20
- from deep_translator.validate import is_empty, is_input_valid
21
 
22
 
23
  class DeeplTranslator(BaseTranslator):
@@ -85,7 +85,7 @@ class DeeplTranslator(BaseTranslator):
85
  # If the answer is not success, raise server exception.
86
  if response.status_code == 403:
87
  raise AuthorizationException(self.api_key)
88
- elif response.status_code != 200:
89
  raise ServerException(response.status_code)
90
  # Get the response and check is not empty.
91
  res = response.json()
 
17
  ServerException,
18
  TranslationNotFound,
19
  )
20
+ from deep_translator.validate import is_empty, is_input_valid, request_failed
21
 
22
 
23
  class DeeplTranslator(BaseTranslator):
 
85
  # If the answer is not success, raise server exception.
86
  if response.status_code == 403:
87
  raise AuthorizationException(self.api_key)
88
+ if request_failed(status_code=response.status_code):
89
  raise ServerException(response.status_code)
90
  # Get the response and check is not empty.
91
  res = response.json()
deep_translator/google.py CHANGED
@@ -16,7 +16,7 @@ from deep_translator.exceptions import (
16
  TooManyRequests,
17
  TranslationNotFound,
18
  )
19
- from deep_translator.validate import is_empty, is_input_valid
20
 
21
 
22
  class GoogleTranslator(BaseTranslator):
@@ -70,7 +70,7 @@ class GoogleTranslator(BaseTranslator):
70
  if response.status_code == 429:
71
  raise TooManyRequests()
72
 
73
- if response.status_code != 200:
74
  raise RequestError()
75
 
76
  soup = BeautifulSoup(response.text, "html.parser")
 
16
  TooManyRequests,
17
  TranslationNotFound,
18
  )
19
+ from deep_translator.validate import is_empty, is_input_valid, request_failed
20
 
21
 
22
  class GoogleTranslator(BaseTranslator):
 
70
  if response.status_code == 429:
71
  raise TooManyRequests()
72
 
73
+ if request_failed(status_code=response.status_code):
74
  raise RequestError()
75
 
76
  soup = BeautifulSoup(response.text, "html.parser")
deep_translator/libre.py CHANGED
@@ -20,7 +20,7 @@ from deep_translator.exceptions import (
20
  ServerException,
21
  TranslationNotFound,
22
  )
23
- from deep_translator.validate import is_empty, is_input_valid
24
 
25
 
26
  class LibreTranslator(BaseTranslator):
@@ -95,7 +95,7 @@ class LibreTranslator(BaseTranslator):
95
 
96
  if response.status_code == 403:
97
  raise AuthorizationException(self.api_key)
98
- elif response.status_code != 200:
99
  raise ServerException(response.status_code)
100
  # Get the response and check is not empty.
101
  res = response.json()
 
20
  ServerException,
21
  TranslationNotFound,
22
  )
23
+ from deep_translator.validate import is_empty, is_input_valid, request_failed
24
 
25
 
26
  class LibreTranslator(BaseTranslator):
 
95
 
96
  if response.status_code == 403:
97
  raise AuthorizationException(self.api_key)
98
+ elif request_failed(status_code=response.status_code):
99
  raise ServerException(response.status_code)
100
  # Get the response and check is not empty.
101
  res = response.json()
deep_translator/linguee.py CHANGED
@@ -19,7 +19,7 @@ from deep_translator.exceptions import (
19
  TooManyRequests,
20
  TranslationNotFound,
21
  )
22
- from deep_translator.validate import is_empty, is_input_valid
23
 
24
 
25
  class LingueeTranslator(BaseTranslator):
@@ -72,8 +72,9 @@ class LingueeTranslator(BaseTranslator):
72
  if response.status_code == 429:
73
  raise TooManyRequests()
74
 
75
- if response.status_code != 200:
76
  raise RequestError()
 
77
  soup = BeautifulSoup(response.text, "html.parser")
78
  elements = soup.find_all(self._element_tag, self._element_query)
79
  response.close()
 
19
  TooManyRequests,
20
  TranslationNotFound,
21
  )
22
+ from deep_translator.validate import is_empty, is_input_valid, request_failed
23
 
24
 
25
  class LingueeTranslator(BaseTranslator):
 
72
  if response.status_code == 429:
73
  raise TooManyRequests()
74
 
75
+ if request_failed(status_code=response.status_code):
76
  raise RequestError()
77
+
78
  soup = BeautifulSoup(response.text, "html.parser")
79
  elements = soup.find_all(self._element_tag, self._element_query)
80
  response.close()
deep_translator/mymemory.py CHANGED
@@ -15,7 +15,7 @@ from deep_translator.exceptions import (
15
  TooManyRequests,
16
  TranslationNotFound,
17
  )
18
- from deep_translator.validate import is_empty, is_input_valid
19
 
20
 
21
  class MyMemoryTranslator(BaseTranslator):
@@ -71,7 +71,7 @@ class MyMemoryTranslator(BaseTranslator):
71
 
72
  if response.status_code == 429:
73
  raise TooManyRequests()
74
- if response.status_code != 200:
75
  raise RequestError()
76
 
77
  data = response.json()
 
15
  TooManyRequests,
16
  TranslationNotFound,
17
  )
18
+ from deep_translator.validate import is_empty, is_input_valid, request_failed
19
 
20
 
21
  class MyMemoryTranslator(BaseTranslator):
 
71
 
72
  if response.status_code == 429:
73
  raise TooManyRequests()
74
+ if request_failed(status_code=response.status_code):
75
  raise RequestError()
76
 
77
  data = response.json()
deep_translator/papago.py CHANGED
@@ -12,7 +12,7 @@ import requests
12
  from deep_translator.base import BaseTranslator
13
  from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
14
  from deep_translator.exceptions import TranslationNotFound
15
- from deep_translator.validate import is_input_valid
16
 
17
 
18
  class PapagoTranslator(BaseTranslator):
@@ -67,7 +67,7 @@ class PapagoTranslator(BaseTranslator):
67
  response = requests.post(
68
  self._base_url, headers=headers, data=payload
69
  )
70
- if response.status_code != 200:
71
  raise Exception(
72
  f"Translation error! -> status code: {response.status_code}"
73
  )
 
12
  from deep_translator.base import BaseTranslator
13
  from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
14
  from deep_translator.exceptions import TranslationNotFound
15
+ from deep_translator.validate import is_input_valid, request_failed
16
 
17
 
18
  class PapagoTranslator(BaseTranslator):
 
67
  response = requests.post(
68
  self._base_url, headers=headers, data=payload
69
  )
70
+ if request_failed(status_code=response.status_code):
71
  raise Exception(
72
  f"Translation error! -> status code: {response.status_code}"
73
  )
deep_translator/pons.py CHANGED
@@ -19,7 +19,7 @@ from deep_translator.exceptions import (
19
  TooManyRequests,
20
  TranslationNotFound,
21
  )
22
- from deep_translator.validate import is_empty, is_input_valid
23
 
24
 
25
  class PonsTranslator(BaseTranslator):
@@ -71,7 +71,7 @@ class PonsTranslator(BaseTranslator):
71
  if response.status_code == 429:
72
  raise TooManyRequests()
73
 
74
- if response.status_code != 200:
75
  raise RequestError()
76
 
77
  soup = BeautifulSoup(response.text, "html.parser")
 
19
  TooManyRequests,
20
  TranslationNotFound,
21
  )
22
+ from deep_translator.validate import is_empty, is_input_valid, request_failed
23
 
24
 
25
  class PonsTranslator(BaseTranslator):
 
71
  if response.status_code == 429:
72
  raise TooManyRequests()
73
 
74
+ if request_failed(status_code=response.status_code):
75
  raise RequestError()
76
 
77
  soup = BeautifulSoup(response.text, "html.parser")
deep_translator/qcri.py CHANGED
@@ -16,6 +16,7 @@ from deep_translator.exceptions import (
16
  ServerException,
17
  TranslationNotFound,
18
  )
 
19
 
20
 
21
  class QcriTranslator(BaseTranslator):
@@ -96,7 +97,7 @@ class QcriTranslator(BaseTranslator):
96
  raise ServerException(503)
97
 
98
  else:
99
- if response.status_code != 200:
100
  ServerException(response.status_code)
101
  else:
102
  res = response.json()
 
16
  ServerException,
17
  TranslationNotFound,
18
  )
19
+ from deep_translator.validate import request_failed
20
 
21
 
22
  class QcriTranslator(BaseTranslator):
 
97
  raise ServerException(503)
98
 
99
  else:
100
+ if request_failed(status_code=response.status_code):
101
  ServerException(response.status_code)
102
  else:
103
  res = response.json()
deep_translator/validate.py CHANGED
@@ -9,6 +9,21 @@ def is_empty(text: str) -> bool:
9
  return text == ""
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def is_input_valid(
13
  text: str, min_chars: int = 0, max_chars: Optional[int] = None
14
  ) -> bool:
@@ -20,7 +35,7 @@ def is_input_valid(
20
  @return: bool
21
  """
22
 
23
- if not isinstance(text, str) or text.isdigit():
24
  raise NotValidPayload(text)
25
  if max_chars and (not min_chars <= len(text) < max_chars):
26
  raise NotValidLength(text, min_chars, max_chars)
 
9
  return text == ""
10
 
11
 
12
+ def request_failed(status_code: int) -> bool:
13
+ """Check if a request has failed or not.
14
+ A request is considered successfull if the status code is in the 2** range.
15
+
16
+ Args:
17
+ status_code (int): status code of the request
18
+
19
+ Returns:
20
+ bool: indicates request failure
21
+ """
22
+ if status_code > 299 or status_code < 200:
23
+ return True
24
+ return False
25
+
26
+
27
  def is_input_valid(
28
  text: str, min_chars: int = 0, max_chars: Optional[int] = None
29
  ) -> bool:
 
35
  @return: bool
36
  """
37
 
38
+ if not isinstance(text, str):
39
  raise NotValidPayload(text)
40
  if max_chars and (not min_chars <= len(text) < max_chars):
41
  raise NotValidLength(text, min_chars, max_chars)
deep_translator/yandex.py CHANGED
@@ -18,7 +18,7 @@ from deep_translator.exceptions import (
18
  TooManyRequests,
19
  TranslationNotFound,
20
  )
21
- from deep_translator.validate import is_input_valid
22
 
23
 
24
  class YandexTranslator(BaseTranslator):
@@ -75,7 +75,7 @@ class YandexTranslator(BaseTranslator):
75
  else:
76
  data = response.json()
77
 
78
- if response.status_code != 200:
79
  raise ServerException(response.status_code)
80
  return data.get("dirs")
81
 
 
18
  TooManyRequests,
19
  TranslationNotFound,
20
  )
21
+ from deep_translator.validate import is_input_valid, request_failed
22
 
23
 
24
  class YandexTranslator(BaseTranslator):
 
75
  else:
76
  data = response.json()
77
 
78
+ if request_failed(status_code=response.status_code):
79
  raise ServerException(response.status_code)
80
  return data.get("dirs")
81
 
tests/test_google.py CHANGED
@@ -46,14 +46,6 @@ def test_empty_text(google_translator):
46
 
47
 
48
  def test_payload(google_translator):
49
- with pytest.raises(exceptions.NotValidPayload):
50
- google_translator.translate(text="1234")
51
- google_translator.translate(text="{}")
52
- google_translator.translate(text="%@")
53
-
54
- with pytest.raises(exceptions.NotValidPayload):
55
- google_translator.translate(text=123)
56
-
57
  with pytest.raises(exceptions.NotValidPayload):
58
  google_translator.translate(text={})
59
 
 
46
 
47
 
48
  def test_payload(google_translator):
 
 
 
 
 
 
 
 
49
  with pytest.raises(exceptions.NotValidPayload):
50
  google_translator.translate(text={})
51
 
tests/test_libre.py CHANGED
@@ -32,9 +32,6 @@ def test_abbreviations_and_languages_mapping():
32
 
33
 
34
  def test_payload(libre):
35
- with pytest.raises(exceptions.NotValidPayload):
36
- libre.translate(123)
37
-
38
  with pytest.raises(exceptions.NotValidPayload):
39
  libre.translate({})
40
 
 
32
 
33
 
34
  def test_payload(libre):
 
 
 
35
  with pytest.raises(exceptions.NotValidPayload):
36
  libre.translate({})
37
 
tests/test_linguee.py CHANGED
@@ -38,9 +38,6 @@ def test_inputs():
38
 
39
 
40
  def test_payload(linguee):
41
- with pytest.raises(exceptions.NotValidPayload):
42
- linguee.translate(123)
43
-
44
  with pytest.raises(exceptions.NotValidPayload):
45
  linguee.translate({})
46
 
 
38
 
39
 
40
  def test_payload(linguee):
 
 
 
41
  with pytest.raises(exceptions.NotValidPayload):
42
  linguee.translate({})
43
 
tests/test_mymemory.py CHANGED
@@ -36,9 +36,6 @@ def test_inputs():
36
 
37
 
38
  def test_payload(mymemory):
39
- with pytest.raises(exceptions.NotValidPayload):
40
- mymemory.translate(text=123)
41
-
42
  with pytest.raises(exceptions.NotValidPayload):
43
  mymemory.translate(text={})
44
 
 
36
 
37
 
38
  def test_payload(mymemory):
 
 
 
39
  with pytest.raises(exceptions.NotValidPayload):
40
  mymemory.translate(text={})
41
 
tests/test_pons.py CHANGED
@@ -36,9 +36,6 @@ def test_inputs():
36
 
37
 
38
  def test_payload(pons):
39
- with pytest.raises(exceptions.NotValidPayload):
40
- pons.translate(123)
41
-
42
  with pytest.raises(exceptions.NotValidPayload):
43
  pons.translate({})
44
 
 
36
 
37
 
38
  def test_payload(pons):
 
 
 
39
  with pytest.raises(exceptions.NotValidPayload):
40
  pons.translate({})
41