Skip to content

Commit daab05f

Browse files
hive-botclaude
andcommitted
feat: add the TranslationMemoryJobStatus enum
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 07f6326 commit daab05f

5 files changed

Lines changed: 92 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
137137
when creating the import job and sent with the upload; extra body parameters
138138
are added to the request that creates the import job and replace keys of
139139
the same name.
140+
- Added the `TranslationMemoryJobStatus` enum. `TranslationMemoryJob.status`
141+
and `TranslationMemoryJobResult.status` now return its members for the
142+
statuses `awaiting_input`, `processing`, `completed`, `failed` and
143+
`expired`, and the plain string for any other value. Members are strings
144+
that compare equal to the API value, so existing comparisons with strings
145+
and the `TranslationMemoryJobResult.STATUS_*` constants keep working.
140146

141147
### Changed
142148
- HTTP 404 responses raise `NotFoundException` instead of `DeepLException`

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,16 @@ job = deepl_client.wait_until_translation_memory_job_done(
12521252
)
12531253
```
12541254

1255+
`TranslationMemoryJob.status` returns a `TranslationMemoryJobStatus` member
1256+
(`AWAITING_INPUT`, `PROCESSING`, `COMPLETED`, `FAILED` or `EXPIRED`), or the
1257+
plain string for a status the library does not know. Members are strings and
1258+
compare equal to the API value, so `job.status == "completed"` keeps working.
1259+
1260+
```python
1261+
if job.status == deepl.TranslationMemoryJobStatus.FAILED:
1262+
print(f"Import failed: {job.result.error_message}")
1263+
```
1264+
12551265
#### Exporting a translation memory
12561266

12571267
`export_translation_memory_to_filepath()` exports a translation memory to a TMX

‎deepl/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
SpokenTermsEntries,
4848
SpokenTermsList,
4949
StyleProfile,
50+
TranslationMemoryJobStatus,
5051
)
5152

5253
from .translator import ( # noqa
@@ -105,6 +106,7 @@
105106
"TagHandlingMode",
106107
"TagHandlingVersion",
107108
"TextResult",
109+
"TranslationMemoryJobStatus",
108110
"Translator",
109111
"Usage",
110112
"http_client",

‎deepl/api_data.py‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,11 +2027,47 @@ def reused_existing(self) -> bool:
20272027
return self._reused_existing
20282028

20292029

2030+
class TranslationMemoryJobStatus(str, Enum):
2031+
"""Status of a translation memory import or export job.
2032+
2033+
Members are strings and compare equal to the status string the API
2034+
returns, for example ``TranslationMemoryJobStatus.COMPLETED ==
2035+
"completed"``.
2036+
"""
2037+
2038+
AWAITING_INPUT = "awaiting_input"
2039+
PROCESSING = "processing"
2040+
COMPLETED = "completed"
2041+
FAILED = "failed"
2042+
EXPIRED = "expired"
2043+
2044+
def __str__(self) -> str:
2045+
return self.value
2046+
2047+
def __format__(self, format_spec: str) -> str:
2048+
return format(self.value, format_spec)
2049+
2050+
# Hash like the plain status string, so members and strings are
2051+
# interchangeable as dict keys and set members.
2052+
__hash__ = str.__hash__
2053+
2054+
2055+
def _parse_translation_memory_job_status(
2056+
status: Any,
2057+
) -> Union[TranslationMemoryJobStatus, str]:
2058+
try:
2059+
return TranslationMemoryJobStatus(status)
2060+
except ValueError:
2061+
# Unknown statuses are kept as given.
2062+
return status
2063+
2064+
20302065
class TranslationMemoryJobResult:
20312066
"""The outcome of a translation memory import or export job.
20322067
2033-
:param status: Job status, one of "awaiting_input", "processing",
2034-
"completed", "downloaded", "failed" or "expired".
2068+
:param status: Job status. A known status is returned as a
2069+
TranslationMemoryJobStatus member, any other value as the plain
2070+
string.
20352071
:param required_action: Human-readable action the caller must take, set
20362072
while the job is waiting on the caller.
20372073
:param download_url: Download URL of the exported TMX file, set once an
@@ -2061,7 +2097,7 @@ def __init__(
20612097
translation_memory_id: Optional[str] = None,
20622098
skipped_segment_count: Optional[int] = None,
20632099
):
2064-
self._status = status
2100+
self._status = _parse_translation_memory_job_status(status)
20652101
self._required_action = required_action
20662102
self._download_url = download_url
20672103
self._expires_at = expires_at
@@ -2089,8 +2125,9 @@ def from_json(json) -> "TranslationMemoryJobResult":
20892125
)
20902126

20912127
@property
2092-
def status(self) -> str:
2093-
"""Returns the job status."""
2128+
def status(self) -> Union[TranslationMemoryJobStatus, str]:
2129+
"""Returns the job status: a TranslationMemoryJobStatus member, or the
2130+
plain string for a status this library does not know."""
20942131
return self._status
20952132

20962133
@property
@@ -2238,8 +2275,9 @@ def result(self) -> Optional[TranslationMemoryJobResult]:
22382275
return self._results[0] if self._results else None
22392276

22402277
@property
2241-
def status(self) -> Optional[str]:
2242-
"""Returns the status of the job result."""
2278+
def status(self) -> Optional[Union[TranslationMemoryJobStatus, str]]:
2279+
"""Returns the status of the job result, see
2280+
TranslationMemoryJobResult.status."""
22432281
return self.result.status if self.result else None
22442282

22452283
@property

‎tests/offline/test_translation_memories.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,35 @@ def test_derives_job_progress_from_status(mocked, status, done, ok):
431431
assert job.result.error_message == "bad"
432432

433433

434+
@pytest.mark.parametrize(
435+
"wire, status",
436+
[
437+
("awaiting_input", deepl.TranslationMemoryJobStatus.AWAITING_INPUT),
438+
("processing", deepl.TranslationMemoryJobStatus.PROCESSING),
439+
("completed", deepl.TranslationMemoryJobStatus.COMPLETED),
440+
("failed", deepl.TranslationMemoryJobStatus.FAILED),
441+
("expired", deepl.TranslationMemoryJobStatus.EXPIRED),
442+
],
443+
)
444+
def test_parses_job_status_as_enum_member(mocked, wire, status):
445+
reply_with_job(mocked, wire)
446+
447+
job = make_deepl_client().get_translation_memory_job("job-1")
448+
449+
assert job.status is status
450+
assert job.result.status is status
451+
assert job.status == wire
452+
453+
454+
def test_keeps_unknown_job_status_as_string(mocked):
455+
reply_with_job(mocked, "paused")
456+
457+
job = make_deepl_client().get_translation_memory_job("job-1")
458+
459+
assert type(job.status) is str
460+
assert job.status == "paused"
461+
462+
434463
def test_parses_required_action_of_awaiting_job(mocked):
435464
reply_with_job(
436465
mocked, "awaiting_input", status_metadata={"required_action": "upload"}

0 commit comments

Comments
 (0)