Skip to content

Commit d589ccf

Browse files
translate_document and translate_document_from_filepath return final DocumentStatus, allowing the number of billed characters to be queried
1 parent b73dec8 commit d589ccf

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* Add `error_message` property to `DocumentStatus`, describing the error in case of document translation failure.
1111
### Changed
1212
* Improve error message if `translate_text_with_glossary` is called without an instance of `GlossaryInfo`.
13+
* `translate_document` and `translate_document_from_filepath` return final `DocumentStatus`, allowing the number of
14+
billed characters to be queried.
1315
### Deprecated
1416
### Removed
1517
### Fixed

deepl/translator.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ def translate_document_from_filepath(
779779
target_lang: str,
780780
formality: Union[str, Formality] = Formality.DEFAULT,
781781
glossary: Union[str, GlossaryInfo, None] = None,
782-
) -> None:
782+
) -> DocumentStatus:
783783
"""Upload document at given input path, translate it into the target
784784
language, and download result to given output path.
785785
@@ -794,6 +794,8 @@ def translate_document_from_filepath(
794794
Formality enum, "less" or "more".
795795
:param glossary: (Optional) glossary or glossary ID to use for
796796
translation. Must match specified source_lang and target_lang.
797+
:return: DocumentStatus when document translation completed, this
798+
allows the number of billed characters to be queried.
797799
798800
:raises DocumentTranslationException: If an error occurs during
799801
translation. The exception includes information about the document
@@ -802,7 +804,7 @@ def translate_document_from_filepath(
802804
with open(input_path, "rb") as in_file:
803805
with open(output_path, "wb") as out_file:
804806
try:
805-
self.translate_document(
807+
return self.translate_document(
806808
in_file,
807809
out_file,
808810
target_lang=target_lang,
@@ -824,7 +826,7 @@ def translate_document(
824826
target_lang: str,
825827
formality: Union[str, Formality] = Formality.DEFAULT,
826828
glossary: Union[str, GlossaryInfo, None] = None,
827-
) -> None:
829+
) -> DocumentStatus:
828830
"""Upload document, translate it into the target language, and download
829831
result.
830832
@@ -841,6 +843,8 @@ def translate_document(
841843
Formality enum, "less" or "more".
842844
:param glossary: (Optional) glossary or glossary ID to use for
843845
translation. Must match specified source_lang and target_lang.
846+
:return: DocumentStatus when document translation completed, this
847+
allows the number of billed characters to be queried.
844848
845849
:raises DocumentTranslationException: If an error occurs during
846850
translation, the exception includes the document handle.
@@ -877,6 +881,7 @@ def translate_document(
877881
f"Error occurred while translating document: {error_message}",
878882
handle,
879883
)
884+
return status
880885

881886
def translate_document_upload(
882887
self,

tests/test_translate_document.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ def test_translate_document_from_filepath(
1919
example_document_translation,
2020
output_document_path,
2121
):
22-
translator.translate_document_from_filepath(
22+
status = translator.translate_document_from_filepath(
2323
example_document_path,
2424
output_path=output_document_path,
2525
**default_lang_args,
2626
)
2727
assert example_document_translation == output_document_path.read_text()
28+
assert status.billed_characters == len(example_text["EN"])
29+
assert status.status == deepl.DocumentStatus.Status.DONE
30+
assert status.done
2831

2932
# Note: cases with invalid file paths are not tested, because standard
3033
# library functions are used.

0 commit comments

Comments
 (0)