Skip to content

Commit

Permalink
Remove language cache and glossary language check
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jones-dev committed Oct 6, 2021
1 parent dc17787 commit bbdcf96
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 73 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* Add `Translator.get_glossary_languages()` to query language pairs supported for glossaries.
### Changed
* Internal language caching and client-side checking of language codes are removed.
### Deprecated
* Some optional arguments related to language caching are now deprecated, and will be removed in a future version:
* `Translator()`: the `skip_language_check` argument
* `Translator.get_source_languages()` and `Translator.get_target_languages()`: the `skip_cache` argument
### Removed
### Fixed
* Fix HTTP request retries for document uploads.
Expand Down
107 changes: 36 additions & 71 deletions deepl/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,21 +302,6 @@ def remove_regional_variant(language: Union[str]) -> str:
"""Removes the regional variant from a language, e.g. EN-US gives EN"""
return str(language).upper()[0:2]

@staticmethod
def check_supported_glossary_languages(source_lang: str, target_lang: str):
if f"{source_lang}>{target_lang}" not in [
"DE>EN",
"EN>DE",
"EN>ES",
"EN>FR",
"ES>EN",
"FR>EN",
]:
raise DeepLException(
"Invalid source or target language, glossaries are supported "
"for the following language pairs: EN<->DE, EN<->FR, EN<->ES"
)


class GlossaryLanguagePair:
"""Information about a pair of languages supported for DeepL glossaries.
Expand Down Expand Up @@ -380,8 +365,9 @@ class Translator:
:param auth_key: Authentication key as found in your DeepL API account.
:param server_url: (Optional) Base URL of DeepL API, can be overridden e.g.
for testing purposes.
:param skip_language_check: (Optional) Set to True to override automatic
request of available languages.
:param skip_language_check: Deprecated, and now has no effect as the
corresponding internal functionality has been removed. This parameter
will be removed in a future version.
All functions may raise DeepLException or a subclass if a connection error
occurs.
Expand Down Expand Up @@ -415,10 +401,6 @@ def __init__(
self._client = http_client.HttpClient()
self.headers = {"Authorization": f"DeepL-Auth-Key {auth_key}"}

self._skip_language_check = skip_language_check
self._source_languages_cached = None
self._target_languages_cached = None

def __del__(self):
self.close()

Expand Down Expand Up @@ -515,26 +497,10 @@ def _raise_for_status(
f"content: {content}."
)

def _request_languages(self, target: bool) -> List[Language]:
"""Internal function to make a /languages request and cache the result."""
data = {"type": "target"} if target else {}
status, content, json = self._api_call("v2/languages", data=data)

self._raise_for_status(status, content, json)

return [
Language(
language["language"],
language["name"],
language.get("supports_formality", None),
)
for language in json
]

def _check_valid_languages(
self, source_lang: Optional[str], target_lang: str
):
"""Internal function to check given languages match available languages."""
"""Internal function to check given languages are valid."""
if target_lang == "EN":
raise DeepLException(
'target_lang="EN" is deprecated, please use "EN-GB" or "EN-US" instead.'
Expand All @@ -544,24 +510,6 @@ def _check_valid_languages(
'target_lang="PT" is deprecated, please use "PT-PT" or "PT-BR" instead.'
)

if self._skip_language_check:
return

if source_lang is not None and not any(
source_lang == lang.code for lang in self.get_source_languages()
):
raise DeepLException(
f"source_lang ({source_lang}) must be one of the supported "
"language codes, or None for auto-detection"
)

if not any(
target_lang == lang.code for lang in self.get_target_languages()
):
raise DeepLException(
f"target_lang ({target_lang}) must be one of the supported language codes"
)

def _check_language_and_formality(
self,
source_lang: Union[str, Language, None],
Expand All @@ -586,10 +534,6 @@ def _check_language_and_formality(
raise ValueError(
"source_lang and target_lang must match glossary"
)
elif glossary is not None:
Language.check_supported_glossary_languages(
source_lang, Language.remove_regional_variant(target_lang)
)

self._check_valid_languages(source_lang, target_lang)

Expand Down Expand Up @@ -944,20 +888,42 @@ def translate_document_download(
return response

def get_source_languages(self, skip_cache=False) -> List[Language]:
"""Request the list of available source languages."""
if self._source_languages_cached is None or skip_cache:
self._source_languages_cached = self._request_languages(
target=False
"""Request the list of available source languages.
:param skip_cache: Deprecated, and now has no effect as the
corresponding internal functionality has been removed. This
parameter will be removed in a future version.
:return: List of supported source languages.
"""
status, content, json = self._api_call("v2/languages")
self._raise_for_status(status, content, json)
return [
Language(
language["language"],
language["name"],
)
return self._source_languages_cached
for language in json
]

def get_target_languages(self, skip_cache=False) -> List[Language]:
"""Request the list of available target languages."""
if self._target_languages_cached is None or skip_cache:
self._target_languages_cached = self._request_languages(
target=True
"""Request the list of available target languages.
:param skip_cache: Deprecated, and now has no effect as the
corresponding internal functionality has been removed. This
parameter will be removed in a future version.
:return: List of supported target languages.
"""
data = {"type": "target"}
status, content, json = self._api_call("v2/languages", data=data)
self._raise_for_status(status, content, json)
return [
Language(
language["language"],
language["name"],
language.get("supports_formality", None),
)
return self._target_languages_cached
for language in json
]

def get_glossary_languages(self) -> List[GlossaryLanguagePair]:
"""Request the list of language pairs supported for glossaries."""
Expand Down Expand Up @@ -1013,7 +979,6 @@ def create_glossary(
# glossaries are only supported for base language types
target_lang = Language.remove_regional_variant(target_lang)
source_lang = Language.remove_regional_variant(source_lang)
Language.check_supported_glossary_languages(source_lang, target_lang)

if not name:
raise ValueError("glossary name must not be empty")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_translate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def check_result(result):
def test_invalid_language(translator):
with pytest.raises(
deepl.DeepLException,
match="target_lang.*must be one of the supported language codes",
match="target_lang.*not supported",
):
translator.translate_text(example_text["EN"], target_lang="XX")

with pytest.raises(
deepl.DeepLException,
match="source_lang.*must be one of the supported language codes",
match="source_lang.*not supported",
):
translator.translate_text(
example_text["EN"], source_lang="XX", target_lang="DE"
Expand Down

0 comments on commit bbdcf96

Please sign in to comment.