From d9fafb6e65dd7b3672bd6e3fd6cccc0f20f5d86a Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Fri, 19 Feb 2021 17:02:46 -0800 Subject: [PATCH 01/59] initial commit --- .../samples/sample_cancel_batch.py | 53 ++++++++++++ .../samples/sample_check_statuses.py | 80 +++++++++++++++++++ .../samples/sample_list_batches.py | 34 ++++++++ 3 files changed, 167 insertions(+) create mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py create mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py create mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py new file mode 100644 index 000000000000..fdb56a2856f7 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py @@ -0,0 +1,53 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def sample_cancel_batch(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + BatchDocumentInput, + StorageSourceInput, + StorageTargetInput + ) + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] + target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + batch = [ + BatchDocumentInput( + source=StorageSourceInput( + source_url=source_container_url, + language="en", + prefix="document_2021" + ), + targets=[ + StorageTargetInput( + target_url=target_container_url_es, + language="es" + ) + ], + storage_type="file" + ) + ] + + poller = client.begin_batch_translation(batch) + + batch_detail = client.get_batch_status(poller) # type: BatchStatusDetail + + print("Batch status: {}".format(batch_detail.status)) + print("Number of translations on documents: {}".format(batch_detail.summary.total)) + + client.cancel_batch(poller) + detail = client.get_batch_status(poller) # type: BatchStatusDetail + + if detail.status in ["Cancelled", "Cancelling"]: + print("We cancelled batch with ID: {}".format(detail.id)) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py new file mode 100644 index 000000000000..9c0093f0efaf --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -0,0 +1,80 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def sample_batch_translation(): + import os + import time + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + BatchDocumentInput, + StorageSourceInput, + StorageTargetInput + ) + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] + target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + batch = [ + BatchDocumentInput( + source=StorageSourceInput( + source_url=source_container_url, + language="en", + prefix="document_2021" + ), + targets=[ + StorageTargetInput( + target_url=target_container_url_es, + language="es" + ), + StorageTargetInput( + target_url=target_container_url_fr, + language="fr" + ) + ], + storage_type="folder" + ) + ] + + poller = client.begin_batch_translation(batch) + + while True: + batch_detail = client.get_batch_status(poller) # type: BatchStatusDetail + if batch_detail.status == ["NotStarted", "Running"]: + time.sleep(5) + continue + + if batch_detail.status == ["Failed", "ValidationFailed"]: + print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message)) + check_documents(client, batch_detail.id) + exit(1) + + if batch_detail.status == "Succeeded": + print("We translated our documents!") + if batch_detail.summary.failed > 0: + check_documents(client, batch_detail.id) + break + + +def check_documents(client, batch_id): + docs_to_retry = [] + doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + for document in doc_statuses: + if document.status == "Failed": + print("Document at {} failed to be translated to {} language".format( + document.document_url, document.translate_to + )) + print("Document ID: {}, Error Code: {}, Message: {}".format( + document.id, document.error.code, document.error.message + )) + if document.id not in docs_to_retry: + docs_to_retry.append(document.id) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py new file mode 100644 index 000000000000..c57fcfafbb99 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -0,0 +1,34 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def sample_list_all_batches(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + ) + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + batches = client.list_batches() + + print("Batches summary") + for batch in batches: + print("Batch ID: {}".format(batch.id)) + print("Batch status: {}".format(batch.status)) + print("Batch created on: {}".format(batch.created_on)) + print("Batch last updated on: {}".format(batch.last_updated)) + print("Batch number of translations on documents: {}".format(batch.summary.total)) + + print("Of total documents...") + print("{} failed".format(batch.summary.failed)) + print("{} succeeded".format(batch.summary.succeeded)) + print("{} in progress".format(batch.summary.in_progress)) + print("{} not yet started".format(batch.summary.not_yet_started)) + print("{} cancelled".format(batch.summary.cancelled)) From a4aa5b23c4554e4b8b6b49173e6e5363c3309695 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Mon, 22 Feb 2021 18:24:40 -0800 Subject: [PATCH 02/59] update samples --- .../samples/sample_azure_storage.py | 122 ++++++++++++++++++ .../samples/sample_check_statuses.py | 22 +++- .../samples/sample_list_batches.py | 2 +- 3 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py new file mode 100644 index 000000000000..7fc7212fe3ae --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py @@ -0,0 +1,122 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def batch_translation_with_storage(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + BatchDocumentInput, + StorageSourceInput, + StorageTargetInput + ) + from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_storage_endpoint = os.environ["AZURE_STORAGE_SOURCE_ENDPOINT"] + source_storage_account_name = os.environ["AZURE_STORAGE_SOURCE_ACCOUNT_NAME"] + source_storage_container_name = os.environ["AZURE_STORAGE_SOURCE_CONTAINER_NAME"] + source_storage_key = os.environ["AZURE_STORAGE_SOURCE_KEY"] + target_storage_endpoint = os.environ["AZURE_STORAGE_TARGET_ENDPOINT"] + target_storage_account_name = os.environ["AZURE_STORAGE_TARGET_ACCOUNT_NAME"] + target_storage_container_name = os.environ["AZURE_STORAGE_TARGET_CONTAINER_NAME"] + target_storage_key = os.environ["AZURE_STORAGE_TARGET_KEY"] + + batch_client = DocumentTranslationClient( + endpoint, AzureKeyCredential(key) + ) + + container_client = ContainerClient( + source_storage_endpoint, + container_name=source_storage_container_name, + credential=source_storage_key + ) + + with open("document.txt", "rb") as doc: + container_client.upload_blob("document.txt", doc) + + source_container_sas = generate_container_sas( + account_name=source_storage_account_name, + container_name=source_storage_container_name, + account_key=source_storage_key, + permission=ContainerSasPermissions.from_string("rl") + ) + + target_container_sas = generate_container_sas( + account_name=target_storage_account_name, + container_name=target_storage_container_name, + account_key=target_storage_key, + permission=ContainerSasPermissions.from_string("rlwd") + ) + + source_container_url = source_storage_endpoint + "/" + source_storage_container_name + "?" + source_container_sas + target_container_url = target_storage_endpoint + "/" + target_storage_container_name + "?" + target_container_sas + + batch = [ + BatchDocumentInput( + source=StorageSourceInput( + source_url=source_container_url, + language="en", + prefix="document" + ), + targets=[ + StorageTargetInput( + target_url=target_container_url, + language="es" + ) + ] + ) + ] + + poller = batch_client.begin_batch_translation(batch) + + batch_detail = poller.result() + if batch_detail.status == "Succeeded": + print("We translated our documents!") + if batch_detail.summary.failed > 0: + check_documents(batch_client, batch_detail.id) + + if batch_detail.status in ["Failed", "ValidationFailed"]: + if batch_detail.error: + print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message)) + check_documents(batch_client, batch_detail.id) + exit(1) + + container_client = ContainerClient( + target_storage_endpoint, + container_name=target_storage_container_name, + credential=target_storage_key + ) + + target_container_client = container_client.from_container_url(target_container_url) + + with open("translated.txt", "wb") as my_blob: + download_stream = target_container_client.download_blob("document.txt") + my_blob.write(download_stream.readall()) + + +def check_documents(client, batch_id): + from azure.core.exceptions import ResourceNotFoundError + + try: + doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + except ResourceNotFoundError as err: + print("Failed to process any documents in source/target container.") + raise err + + docs_to_retry = [] + for document in doc_statuses: + if document.status == "Failed": + print("Document at {} failed to be translated to {} language".format( + document.url, document.translate_to + )) + print("Document ID: {}, Error Code: {}, Message: {}".format( + document.id, document.error.code, document.error.message + )) + if document.url not in docs_to_retry: + docs_to_retry.append(document.url) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py index 9c0093f0efaf..76312472af2f 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -49,12 +49,13 @@ def sample_batch_translation(): while True: batch_detail = client.get_batch_status(poller) # type: BatchStatusDetail - if batch_detail.status == ["NotStarted", "Running"]: + if batch_detail.status in ["NotStarted", "Running"]: time.sleep(5) continue - if batch_detail.status == ["Failed", "ValidationFailed"]: - print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message)) + if batch_detail.status in ["Failed", "ValidationFailed"]: + if batch_detail.error: + print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message)) check_documents(client, batch_detail.id) exit(1) @@ -66,15 +67,22 @@ def sample_batch_translation(): def check_documents(client, batch_id): + from azure.core.exceptions import ResourceNotFoundError + + try: + doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + except ResourceNotFoundError as err: + print("Failed to process any documents in source/target container.") + raise err + docs_to_retry = [] - doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] for document in doc_statuses: if document.status == "Failed": print("Document at {} failed to be translated to {} language".format( - document.document_url, document.translate_to + document.url, document.translate_to )) print("Document ID: {}, Error Code: {}, Message: {}".format( document.id, document.error.code, document.error.message )) - if document.id not in docs_to_retry: - docs_to_retry.append(document.id) + if document.url not in docs_to_retry: + docs_to_retry.append(document.url) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py index c57fcfafbb99..5138d7bdef3d 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -23,7 +23,7 @@ def sample_list_all_batches(): print("Batch ID: {}".format(batch.id)) print("Batch status: {}".format(batch.status)) print("Batch created on: {}".format(batch.created_on)) - print("Batch last updated on: {}".format(batch.last_updated)) + print("Batch last updated on: {}".format(batch.last_updated_on)) print("Batch number of translations on documents: {}".format(batch.summary.total)) print("Of total documents...") From c6e34f0f731dfdf36d5a028b242f6bc4510fb0f2 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Tue, 23 Feb 2021 17:07:35 -0800 Subject: [PATCH 03/59] updates from feedback --- .../azure/ai/documenttranslation/__init__.py | 3 +- .../azure/ai/documenttranslation/_models.py | 2 +- .../azure/ai/documenttranslation/_polling.py | 38 +++++++++++++++++++ .../samples/sample_azure_storage.py | 23 +++++------ .../samples/sample_cancel_batch.py | 24 +++++------- .../samples/sample_check_statuses.py | 27 ++++++------- .../samples/sample_list_batches.py | 14 +++---- 7 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py index 6aa71f6e8164..c28e0fdadd35 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py @@ -19,6 +19,7 @@ BatchDocumentInput, FileFormat ) +from ._polling import DocumentTranslationPoller __VERSION__ = VERSION @@ -26,7 +27,7 @@ __all__ = [ "DocumentTranslationClient", "DocumentTranslationVersion", - "BatchDocumentInput", + "BatchTranslationInput", "TranslationGlossary", "StorageInputType", "FileFormat", diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index e5928093c103..9557d78eb13f 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -61,7 +61,7 @@ def __init__( self.storage_source = kwargs.get("storage_source", None) -class BatchDocumentInput(object): +class BatchTranslationInput(object): """Definition for the input batch translation request. :param source_url: Required. Location of the folder / container or single file with your diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py new file mode 100644 index 000000000000..f3c29e7dfa73 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -0,0 +1,38 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +import logging +from typing import TYPE_CHECKING, TypeVar +from azure.core.polling import LROPoller, PollingMethod +if TYPE_CHECKING: + from typing import Any + + +PollingReturnType = TypeVar("PollingReturnType") + +_LOGGER = logging.getLogger(__name__) + + +class DocumentTranslationPoller(LROPoller): + # TODO - this is temporary class. we will generate with the custom poller + + @property + def batch_id(self): + return self._polling_method._operation._async_url.split("/batches/")[1] + + @classmethod + def from_continuation_token(cls, polling_method, continuation_token, **kwargs): + # type: (PollingMethod[PollingReturnType], str, **Any) -> DocumentTranslationPoller[PollingReturnType] + """ + :param polling_method: + :type polling_method: ~azure.core.polling.PollingMethod + :param str continuation_token: + :return: DocumentTranslationPoller + """ + client, initial_response, deserialization_callback = polling_method.from_continuation_token( + continuation_token, **kwargs + ) + return cls(client, initial_response, deserialization_callback, polling_method) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py index 7fc7212fe3ae..01cf57a3c76a 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py @@ -10,9 +10,8 @@ def batch_translation_with_storage(): from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, - BatchDocumentInput, - StorageSourceInput, - StorageTargetInput + BatchTranslationInput, + StorageTarget ) from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions @@ -58,18 +57,16 @@ def batch_translation_with_storage(): target_container_url = target_storage_endpoint + "/" + target_storage_container_name + "?" + target_container_sas batch = [ - BatchDocumentInput( - source=StorageSourceInput( - source_url=source_container_url, - language="en", - prefix="document" - ), + BatchTranslationInput( + source_url=source_container_url, + source_language="en", targets=[ - StorageTargetInput( + StorageTarget( target_url=target_container_url, language="es" ) - ] + ], + prefix="document" ) ] @@ -78,7 +75,7 @@ def batch_translation_with_storage(): batch_detail = poller.result() if batch_detail.status == "Succeeded": print("We translated our documents!") - if batch_detail.summary.failed > 0: + if batch_detail.documents_failed_count > 0: check_documents(batch_client, batch_detail.id) if batch_detail.status in ["Failed", "ValidationFailed"]: @@ -104,7 +101,7 @@ def check_documents(client, batch_id): from azure.core.exceptions import ResourceNotFoundError try: - doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + doc_statuses = client.list_statuses_of_documents(batch_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: print("Failed to process any documents in source/target container.") raise err diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py index fdb56a2856f7..a53f1d0c0934 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py @@ -10,9 +10,8 @@ def sample_cancel_batch(): from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, - BatchDocumentInput, - StorageSourceInput, - StorageTargetInput + BatchTranslationInput, + StorageTarget ) endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] @@ -23,14 +22,11 @@ def sample_cancel_batch(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) batch = [ - BatchDocumentInput( - source=StorageSourceInput( - source_url=source_container_url, - language="en", - prefix="document_2021" - ), + BatchTranslationInput( + source_url=source_container_url, + source_language="en", targets=[ - StorageTargetInput( + StorageTarget( target_url=target_container_url_es, language="es" ) @@ -41,13 +37,13 @@ def sample_cancel_batch(): poller = client.begin_batch_translation(batch) - batch_detail = client.get_batch_status(poller) # type: BatchStatusDetail + batch_detail = client.get_batch_status(poller.batch_id) # type: BatchStatusDetail print("Batch status: {}".format(batch_detail.status)) - print("Number of translations on documents: {}".format(batch_detail.summary.total)) + print("Number of translations on documents: {}".format(batch_detail.documents_total_count)) - client.cancel_batch(poller) - detail = client.get_batch_status(poller) # type: BatchStatusDetail + client.cancel_batch(batch_detail.id) + detail = client.get_batch_status(batch_detail.id) # type: BatchStatusDetail if detail.status in ["Cancelled", "Cancelling"]: print("We cancelled batch with ID: {}".format(detail.id)) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py index 76312472af2f..1349975f9417 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -11,9 +11,8 @@ def sample_batch_translation(): from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, - BatchDocumentInput, - StorageSourceInput, - StorageTargetInput + BatchTranslationInput, + StorageTarget ) endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] @@ -25,30 +24,28 @@ def sample_batch_translation(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) batch = [ - BatchDocumentInput( - source=StorageSourceInput( - source_url=source_container_url, - language="en", - prefix="document_2021" - ), + BatchTranslationInput( + source_url=source_container_url, + source_language="en", targets=[ - StorageTargetInput( + StorageTarget( target_url=target_container_url_es, language="es" ), - StorageTargetInput( + StorageTarget( target_url=target_container_url_fr, language="fr" ) ], - storage_type="folder" + storage_type="folder", + prefix="document_2021" ) ] poller = client.begin_batch_translation(batch) while True: - batch_detail = client.get_batch_status(poller) # type: BatchStatusDetail + batch_detail = client.get_batch_status(poller.batch_id) # type: BatchStatusDetail if batch_detail.status in ["NotStarted", "Running"]: time.sleep(5) continue @@ -61,7 +58,7 @@ def sample_batch_translation(): if batch_detail.status == "Succeeded": print("We translated our documents!") - if batch_detail.summary.failed > 0: + if batch_detail.documents_failed_count > 0: check_documents(client, batch_detail.id) break @@ -70,7 +67,7 @@ def check_documents(client, batch_id): from azure.core.exceptions import ResourceNotFoundError try: - doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + doc_statuses = client.list_statuses_of_documents(batch_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: print("Failed to process any documents in source/target container.") raise err diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py index 5138d7bdef3d..5e0147d00718 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -16,7 +16,7 @@ def sample_list_all_batches(): key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - batches = client.list_batches() + batches = client.list_statuses_of_batches() print("Batches summary") for batch in batches: @@ -24,11 +24,11 @@ def sample_list_all_batches(): print("Batch status: {}".format(batch.status)) print("Batch created on: {}".format(batch.created_on)) print("Batch last updated on: {}".format(batch.last_updated_on)) - print("Batch number of translations on documents: {}".format(batch.summary.total)) + print("Batch number of translations on documents: {}".format(batch.documents_total_count)) print("Of total documents...") - print("{} failed".format(batch.summary.failed)) - print("{} succeeded".format(batch.summary.succeeded)) - print("{} in progress".format(batch.summary.in_progress)) - print("{} not yet started".format(batch.summary.not_yet_started)) - print("{} cancelled".format(batch.summary.cancelled)) + print("{} failed".format(batch.documents_failed_count)) + print("{} succeeded".format(batch.documents_succeeded_count)) + print("{} in progress".format(batch.documents_in_progress_count)) + print("{} not yet started".format(batch.documents_not_yet_started_count)) + print("{} cancelled".format(batch.documents_cancelled_count)) From d14fcaca621b09e495314c6a2ad64141f095ea65 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Wed, 24 Feb 2021 16:08:22 -0800 Subject: [PATCH 04/59] johans feedback --- .../azure/ai/documenttranslation/__init__.py | 1 - .../azure/ai/documenttranslation/_polling.py | 38 ------------------- .../samples/sample_azure_storage.py | 20 +++++----- .../samples/sample_cancel_batch.py | 4 +- .../samples/sample_check_statuses.py | 6 +-- .../samples/sample_list_batches.py | 2 +- 6 files changed, 15 insertions(+), 56 deletions(-) delete mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py index c28e0fdadd35..0469a9ddeb5e 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py @@ -19,7 +19,6 @@ BatchDocumentInput, FileFormat ) -from ._polling import DocumentTranslationPoller __VERSION__ = VERSION diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py deleted file mode 100644 index f3c29e7dfa73..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py +++ /dev/null @@ -1,38 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import logging -from typing import TYPE_CHECKING, TypeVar -from azure.core.polling import LROPoller, PollingMethod -if TYPE_CHECKING: - from typing import Any - - -PollingReturnType = TypeVar("PollingReturnType") - -_LOGGER = logging.getLogger(__name__) - - -class DocumentTranslationPoller(LROPoller): - # TODO - this is temporary class. we will generate with the custom poller - - @property - def batch_id(self): - return self._polling_method._operation._async_url.split("/batches/")[1] - - @classmethod - def from_continuation_token(cls, polling_method, continuation_token, **kwargs): - # type: (PollingMethod[PollingReturnType], str, **Any) -> DocumentTranslationPoller[PollingReturnType] - """ - :param polling_method: - :type polling_method: ~azure.core.polling.PollingMethod - :param str continuation_token: - :return: DocumentTranslationPoller - """ - client, initial_response, deserialization_callback = polling_method.from_continuation_token( - continuation_token, **kwargs - ) - return cls(client, initial_response, deserialization_callback, polling_method) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py index 01cf57a3c76a..65886cb83258 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py @@ -70,18 +70,18 @@ def batch_translation_with_storage(): ) ] - poller = batch_client.begin_batch_translation(batch) + batch_detail = batch_client.create_batch(batch) + batch_result = batch_client.wait_until_done(batch_detail.id) - batch_detail = poller.result() - if batch_detail.status == "Succeeded": + if batch_result.status == "Succeeded": print("We translated our documents!") - if batch_detail.documents_failed_count > 0: - check_documents(batch_client, batch_detail.id) + if batch_result.documents_failed_count > 0: + check_documents(batch_client, batch_result.id) - if batch_detail.status in ["Failed", "ValidationFailed"]: - if batch_detail.error: - print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message)) - check_documents(batch_client, batch_detail.id) + if batch_result.status in ["Failed", "ValidationFailed"]: + if batch_result.error: + print("Batch failed: {}: {}".format(batch_result.error.code, batch_result.error.message)) + check_documents(batch_client, batch_result.id) exit(1) container_client = ContainerClient( @@ -101,7 +101,7 @@ def check_documents(client, batch_id): from azure.core.exceptions import ResourceNotFoundError try: - doc_statuses = client.list_statuses_of_documents(batch_id) # type: ItemPaged[DocumentStatusDetail] + doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: print("Failed to process any documents in source/target container.") raise err diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py index a53f1d0c0934..cd97ebc878d3 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py @@ -35,9 +35,7 @@ def sample_cancel_batch(): ) ] - poller = client.begin_batch_translation(batch) - - batch_detail = client.get_batch_status(poller.batch_id) # type: BatchStatusDetail + batch_detail = client.create_batch(batch) # type: BatchStatusDetail print("Batch status: {}".format(batch_detail.status)) print("Number of translations on documents: {}".format(batch_detail.documents_total_count)) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py index 1349975f9417..c3d97b209953 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -42,10 +42,10 @@ def sample_batch_translation(): ) ] - poller = client.begin_batch_translation(batch) + batch_detail = client.create_batch(batch) while True: - batch_detail = client.get_batch_status(poller.batch_id) # type: BatchStatusDetail + batch_detail = client.get_batch_status(batch_detail.id) # type: BatchStatusDetail if batch_detail.status in ["NotStarted", "Running"]: time.sleep(5) continue @@ -67,7 +67,7 @@ def check_documents(client, batch_id): from azure.core.exceptions import ResourceNotFoundError try: - doc_statuses = client.list_statuses_of_documents(batch_id) # type: ItemPaged[DocumentStatusDetail] + doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: print("Failed to process any documents in source/target container.") raise err diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py index 5e0147d00718..0abb8a57a468 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -16,7 +16,7 @@ def sample_list_all_batches(): key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - batches = client.list_statuses_of_batches() + batches = client.list_batches_statuses() print("Batches summary") for batch in batches: From 48edd9e0949262941e7197da58cd44c31f99653e Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Wed, 24 Feb 2021 18:23:29 -0800 Subject: [PATCH 05/59] renaming to use job terminology --- .../azure/ai/documenttranslation/__init__.py | 2 +- .../azure/ai/documenttranslation/_models.py | 2 +- .../samples/sample_azure_storage.py | 28 +++++------ .../samples/sample_cancel_batch.py | 47 ------------------- .../samples/sample_check_statuses.py | 30 ++++++------ .../samples/sample_list_batches.py | 27 +++++------ 6 files changed, 44 insertions(+), 92 deletions(-) delete mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py index 0469a9ddeb5e..6aa71f6e8164 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/__init__.py @@ -26,7 +26,7 @@ __all__ = [ "DocumentTranslationClient", "DocumentTranslationVersion", - "BatchTranslationInput", + "BatchDocumentInput", "TranslationGlossary", "StorageInputType", "FileFormat", diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 9557d78eb13f..e5928093c103 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -61,7 +61,7 @@ def __init__( self.storage_source = kwargs.get("storage_source", None) -class BatchTranslationInput(object): +class BatchDocumentInput(object): """Definition for the input batch translation request. :param source_url: Required. Location of the folder / container or single file with your diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py index 65886cb83258..6501fa7b4d06 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py @@ -10,7 +10,7 @@ def batch_translation_with_storage(): from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, - BatchTranslationInput, + BatchDocumentInput, StorageTarget ) from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions @@ -26,7 +26,7 @@ def batch_translation_with_storage(): target_storage_container_name = os.environ["AZURE_STORAGE_TARGET_CONTAINER_NAME"] target_storage_key = os.environ["AZURE_STORAGE_TARGET_KEY"] - batch_client = DocumentTranslationClient( + translation_client = DocumentTranslationClient( endpoint, AzureKeyCredential(key) ) @@ -57,7 +57,7 @@ def batch_translation_with_storage(): target_container_url = target_storage_endpoint + "/" + target_storage_container_name + "?" + target_container_sas batch = [ - BatchTranslationInput( + BatchDocumentInput( source_url=source_container_url, source_language="en", targets=[ @@ -70,18 +70,18 @@ def batch_translation_with_storage(): ) ] - batch_detail = batch_client.create_batch(batch) - batch_result = batch_client.wait_until_done(batch_detail.id) + job_detail = translation_client.create_translation_job(batch) + job_result = translation_client.wait_until_done(job_detail.id) - if batch_result.status == "Succeeded": + if job_result.status == "Succeeded": print("We translated our documents!") - if batch_result.documents_failed_count > 0: - check_documents(batch_client, batch_result.id) + if job_result.documents_failed_count > 0: + check_documents(translation_client, job_result.id) - if batch_result.status in ["Failed", "ValidationFailed"]: - if batch_result.error: - print("Batch failed: {}: {}".format(batch_result.error.code, batch_result.error.message)) - check_documents(batch_client, batch_result.id) + if job_result.status in ["Failed", "ValidationFailed"]: + if job_result.error: + print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) + check_documents(translation_client, job_result.id) exit(1) container_client = ContainerClient( @@ -97,11 +97,11 @@ def batch_translation_with_storage(): my_blob.write(download_stream.readall()) -def check_documents(client, batch_id): +def check_documents(client, job_id): from azure.core.exceptions import ResourceNotFoundError try: - doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: print("Failed to process any documents in source/target container.") raise err diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py deleted file mode 100644 index cd97ebc878d3..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_batch.py +++ /dev/null @@ -1,47 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_cancel_batch(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - BatchTranslationInput, - StorageTarget - ) - - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] - target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - batch = [ - BatchTranslationInput( - source_url=source_container_url, - source_language="en", - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ) - ], - storage_type="file" - ) - ] - - batch_detail = client.create_batch(batch) # type: BatchStatusDetail - - print("Batch status: {}".format(batch_detail.status)) - print("Number of translations on documents: {}".format(batch_detail.documents_total_count)) - - client.cancel_batch(batch_detail.id) - detail = client.get_batch_status(batch_detail.id) # type: BatchStatusDetail - - if detail.status in ["Cancelled", "Cancelling"]: - print("We cancelled batch with ID: {}".format(detail.id)) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py index c3d97b209953..4d47f3ca8821 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -11,7 +11,7 @@ def sample_batch_translation(): from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, - BatchTranslationInput, + BatchDocumentInput, StorageTarget ) @@ -24,7 +24,7 @@ def sample_batch_translation(): client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) batch = [ - BatchTranslationInput( + BatchDocumentInput( source_url=source_container_url, source_language="en", targets=[ @@ -42,32 +42,32 @@ def sample_batch_translation(): ) ] - batch_detail = client.create_batch(batch) + job_detail = client.create_translation_job(batch) while True: - batch_detail = client.get_batch_status(batch_detail.id) # type: BatchStatusDetail - if batch_detail.status in ["NotStarted", "Running"]: - time.sleep(5) + job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail + if job_detail.status in ["NotStarted", "Running"]: + time.sleep(10) continue - if batch_detail.status in ["Failed", "ValidationFailed"]: - if batch_detail.error: - print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message)) - check_documents(client, batch_detail.id) + if job_detail.status in ["Failed", "ValidationFailed"]: + if job_detail.error: + print("Translation job failed: {}: {}".format(job_detail.error.code, job_detail.error.message)) + check_documents(client, job_detail.id) exit(1) - if batch_detail.status == "Succeeded": + if job_detail.status == "Succeeded": print("We translated our documents!") - if batch_detail.documents_failed_count > 0: - check_documents(client, batch_detail.id) + if job_detail.documents_failed_count > 0: + check_documents(client, job_detail.id) break -def check_documents(client, batch_id): +def check_documents(client, job_id): from azure.core.exceptions import ResourceNotFoundError try: - doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail] + doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: print("Failed to process any documents in source/target container.") raise err diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py index 0abb8a57a468..cc2495de8e9e 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -5,7 +5,7 @@ # ------------------------------------ -def sample_list_all_batches(): +def sample_list_all_jobs(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( @@ -16,19 +16,18 @@ def sample_list_all_batches(): key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - batches = client.list_batches_statuses() + jobs = client.list_submitted_jobs() - print("Batches summary") - for batch in batches: - print("Batch ID: {}".format(batch.id)) - print("Batch status: {}".format(batch.status)) - print("Batch created on: {}".format(batch.created_on)) - print("Batch last updated on: {}".format(batch.last_updated_on)) - print("Batch number of translations on documents: {}".format(batch.documents_total_count)) + for job in jobs: + print("Job ID: {}".format(job.id)) + print("Job status: {}".format(job.status)) + print("Job created on: {}".format(job.created_on)) + print("Job last updated on: {}".format(job.last_updated_on)) + print("Total number of translations on documents: {}".format(job.documents_total_count)) print("Of total documents...") - print("{} failed".format(batch.documents_failed_count)) - print("{} succeeded".format(batch.documents_succeeded_count)) - print("{} in progress".format(batch.documents_in_progress_count)) - print("{} not yet started".format(batch.documents_not_yet_started_count)) - print("{} cancelled".format(batch.documents_cancelled_count)) + print("{} failed".format(job.documents_failed_count)) + print("{} succeeded".format(job.documents_succeeded_count)) + print("{} in progress".format(job.documents_in_progress_count)) + print("{} not yet started".format(job.documents_not_yet_started_count)) + print("{} cancelled".format(job.documents_cancelled_count)) From f418339253e64a1ebacf95b9560c9b4dbf4da403 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Tue, 2 Mar 2021 07:58:52 -0800 Subject: [PATCH 06/59] update samples - optional src language --- .../samples/sample_azure_storage.py | 7 +++++-- .../samples/sample_check_statuses.py | 15 +++++++++------ .../samples/sample_list_batches.py | 10 +++++++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py index 6501fa7b4d06..dcbb9ec80298 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py @@ -59,7 +59,6 @@ def batch_translation_with_storage(): batch = [ BatchDocumentInput( source_url=source_container_url, - source_language="en", targets=[ StorageTarget( target_url=target_container_url, @@ -78,7 +77,7 @@ def batch_translation_with_storage(): if job_result.documents_failed_count > 0: check_documents(translation_client, job_result.id) - if job_result.status in ["Failed", "ValidationFailed"]: + elif job_result.status in ["Failed", "ValidationFailed"]: if job_result.error: print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) check_documents(translation_client, job_result.id) @@ -117,3 +116,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + + +if __name__ == '__main__': + batch_translation_with_storage() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py index 4d47f3ca8821..596b373db915 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -5,7 +5,7 @@ # ------------------------------------ -def sample_batch_translation(): +def sample_translation_status_checks(): import os import time from azure.core.credentials import AzureKeyCredential @@ -26,7 +26,6 @@ def sample_batch_translation(): batch = [ BatchDocumentInput( source_url=source_container_url, - source_language="en", targets=[ StorageTarget( target_url=target_container_url_es, @@ -47,16 +46,16 @@ def sample_batch_translation(): while True: job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail if job_detail.status in ["NotStarted", "Running"]: - time.sleep(10) + time.sleep(30) continue - if job_detail.status in ["Failed", "ValidationFailed"]: + elif job_detail.status in ["Failed", "ValidationFailed"]: if job_detail.error: print("Translation job failed: {}: {}".format(job_detail.error.code, job_detail.error.message)) check_documents(client, job_detail.id) exit(1) - if job_detail.status == "Succeeded": + elif job_detail.status == "Succeeded": print("We translated our documents!") if job_detail.documents_failed_count > 0: check_documents(client, job_detail.id) @@ -69,7 +68,7 @@ def check_documents(client, job_id): try: doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container.") + print("Failed to process any documents in source/target container due to insufficient permissions.") raise err docs_to_retry = [] @@ -83,3 +82,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + + +if __name__ == '__main__': + sample_translation_status_checks() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py index cc2495de8e9e..2961b9516301 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -16,14 +16,18 @@ def sample_list_all_jobs(): key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - jobs = client.list_submitted_jobs() + jobs = client.list_submitted_jobs() # type: ItemPaged[JobStatusDetail] for job in jobs: + if job.status in ["NotStarted", "Running"]: + job = client.wait_until_done(job.id) + print("Job ID: {}".format(job.id)) print("Job status: {}".format(job.status)) print("Job created on: {}".format(job.created_on)) print("Job last updated on: {}".format(job.last_updated_on)) print("Total number of translations on documents: {}".format(job.documents_total_count)) + print("Total number of characters charged: {}".format(job.total_characters_charged)) print("Of total documents...") print("{} failed".format(job.documents_failed_count)) @@ -31,3 +35,7 @@ def sample_list_all_jobs(): print("{} in progress".format(job.documents_in_progress_count)) print("{} not yet started".format(job.documents_not_yet_started_count)) print("{} cancelled".format(job.documents_cancelled_count)) + + +if __name__ == '__main__': + sample_list_all_jobs() From 7d763ab8fd7c4f4d6f891ac3a49d818b189a440a Mon Sep 17 00:00:00 2001 From: Mohamed Shaban Date: Wed, 3 Mar 2021 11:42:30 -0500 Subject: [PATCH 07/59] samples hero scenarios (#16936) * [samples] added 'batch_translation_async' sample * [samples] added 'batch_translation_with_storage_async' sample * [samples] added remianing async samples * [samples] update file names * [samples] added self to instance methods * [samples][async] fix import textanalytics :) * [samples] fix self. when calling instance methods * [samples] fixed async check status to use AsyncItemPaged used in Async Client * [samples] async -> some async operations instead of sync ones * [samples][async] use async blob operations * [samples][async] blob download async * [samples][async] check_documents async * [samples][async] added some missing await methods * [async samples] change await time to recommended period * [samples] updated async samples to comply with new changes --- .../samples/sample_azure_storage.py | 122 ------------------ .../samples/sample_check_statuses.py | 88 ------------- .../samples/sample_list_batches.py | 41 ------ 3 files changed, 251 deletions(-) delete mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py delete mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py delete mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py deleted file mode 100644 index dcbb9ec80298..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py +++ /dev/null @@ -1,122 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def batch_translation_with_storage(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - BatchDocumentInput, - StorageTarget - ) - from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions - - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_storage_endpoint = os.environ["AZURE_STORAGE_SOURCE_ENDPOINT"] - source_storage_account_name = os.environ["AZURE_STORAGE_SOURCE_ACCOUNT_NAME"] - source_storage_container_name = os.environ["AZURE_STORAGE_SOURCE_CONTAINER_NAME"] - source_storage_key = os.environ["AZURE_STORAGE_SOURCE_KEY"] - target_storage_endpoint = os.environ["AZURE_STORAGE_TARGET_ENDPOINT"] - target_storage_account_name = os.environ["AZURE_STORAGE_TARGET_ACCOUNT_NAME"] - target_storage_container_name = os.environ["AZURE_STORAGE_TARGET_CONTAINER_NAME"] - target_storage_key = os.environ["AZURE_STORAGE_TARGET_KEY"] - - translation_client = DocumentTranslationClient( - endpoint, AzureKeyCredential(key) - ) - - container_client = ContainerClient( - source_storage_endpoint, - container_name=source_storage_container_name, - credential=source_storage_key - ) - - with open("document.txt", "rb") as doc: - container_client.upload_blob("document.txt", doc) - - source_container_sas = generate_container_sas( - account_name=source_storage_account_name, - container_name=source_storage_container_name, - account_key=source_storage_key, - permission=ContainerSasPermissions.from_string("rl") - ) - - target_container_sas = generate_container_sas( - account_name=target_storage_account_name, - container_name=target_storage_container_name, - account_key=target_storage_key, - permission=ContainerSasPermissions.from_string("rlwd") - ) - - source_container_url = source_storage_endpoint + "/" + source_storage_container_name + "?" + source_container_sas - target_container_url = target_storage_endpoint + "/" + target_storage_container_name + "?" + target_container_sas - - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url, - language="es" - ) - ], - prefix="document" - ) - ] - - job_detail = translation_client.create_translation_job(batch) - job_result = translation_client.wait_until_done(job_detail.id) - - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - check_documents(translation_client, job_result.id) - - elif job_result.status in ["Failed", "ValidationFailed"]: - if job_result.error: - print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) - check_documents(translation_client, job_result.id) - exit(1) - - container_client = ContainerClient( - target_storage_endpoint, - container_name=target_storage_container_name, - credential=target_storage_key - ) - - target_container_client = container_client.from_container_url(target_container_url) - - with open("translated.txt", "wb") as my_blob: - download_stream = target_container_client.download_blob("document.txt") - my_blob.write(download_stream.readall()) - - -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail] - except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container.") - raise err - - docs_to_retry = [] - for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - - -if __name__ == '__main__': - batch_translation_with_storage() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py deleted file mode 100644 index 596b373db915..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ /dev/null @@ -1,88 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_translation_status_checks(): - import os - import time - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - BatchDocumentInput, - StorageTarget - ) - - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] - target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] - target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ), - StorageTarget( - target_url=target_container_url_fr, - language="fr" - ) - ], - storage_type="folder", - prefix="document_2021" - ) - ] - - job_detail = client.create_translation_job(batch) - - while True: - job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail - if job_detail.status in ["NotStarted", "Running"]: - time.sleep(30) - continue - - elif job_detail.status in ["Failed", "ValidationFailed"]: - if job_detail.error: - print("Translation job failed: {}: {}".format(job_detail.error.code, job_detail.error.message)) - check_documents(client, job_detail.id) - exit(1) - - elif job_detail.status == "Succeeded": - print("We translated our documents!") - if job_detail.documents_failed_count > 0: - check_documents(client, job_detail.id) - break - - -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail] - except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container due to insufficient permissions.") - raise err - - docs_to_retry = [] - for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - - -if __name__ == '__main__': - sample_translation_status_checks() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py deleted file mode 100644 index 2961b9516301..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ /dev/null @@ -1,41 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_list_all_jobs(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - ) - - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - jobs = client.list_submitted_jobs() # type: ItemPaged[JobStatusDetail] - - for job in jobs: - if job.status in ["NotStarted", "Running"]: - job = client.wait_until_done(job.id) - - print("Job ID: {}".format(job.id)) - print("Job status: {}".format(job.status)) - print("Job created on: {}".format(job.created_on)) - print("Job last updated on: {}".format(job.last_updated_on)) - print("Total number of translations on documents: {}".format(job.documents_total_count)) - print("Total number of characters charged: {}".format(job.total_characters_charged)) - - print("Of total documents...") - print("{} failed".format(job.documents_failed_count)) - print("{} succeeded".format(job.documents_succeeded_count)) - print("{} in progress".format(job.documents_in_progress_count)) - print("{} not yet started".format(job.documents_not_yet_started_count)) - print("{} cancelled".format(job.documents_cancelled_count)) - - -if __name__ == '__main__': - sample_list_all_jobs() From 4cc75d02ad3fecd3d943b7db3cc5c71f9c7fa0bb Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 3 Mar 2021 23:06:10 +0200 Subject: [PATCH 08/59] [client] modify the 'create_translation_job()' method --- .../azure/ai/documenttranslation/_client.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index e48ec7f1148b..6f29e33732df 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -55,12 +55,20 @@ def create_translation_job(self, batch, **kwargs): :rtype: JobStatusDetail """ - return self._client.document_translation.begin_submit_batch_request( + # submit translation job + response_headers = self._client.document_translation._submit_batch_request_initial( inputs=batch, - polling=True, + cls=lambda x,y,z: z, **kwargs ) + # get job id from response header + job_id = response_headers['Operation-Location'] + + # call + return self.get_job_status(job_id) + + @distributed_trace def get_job_status(self, job_id, **kwargs): # type: (str, **Any) -> JobStatusDetail From 1f10c77f58dfa4af522dc13ae3981aa06224f285 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 18:21:30 +0200 Subject: [PATCH 09/59] [models wrapping 'JobStatusDetail' --- .../azure/ai/documenttranslation/_models.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index e5928093c103..c55e2d4a9817 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -143,6 +143,23 @@ def __init__( self.documents_cancelled_count = kwargs.get('documents_cancelled_count', None) self.total_characters_charged = kwargs.get('total_characters_charged', None) + @classmethod + def _from_generated(cls, batch_status_details): + return cls( + id=batch_status_details.id, + created_on=batch_status_details.created_date_time_utc, + last_updated_on=batch_status_details.last_action_date_time_utc, + status=batch_status_details.status, + error=DocumentTranslationError._from_generated(batch_status_details.error), + documents_total_count=batch_status_details.summary.total, + documents_failed_count=batch_status_details.summary.failed, + documents_succeeded_count=batch_status_details.summary.success, + documents_in_progress_count=batch_status_details.summary.in_progress, + documents_not_yet_started_count=batch_status_details.summary.not_yet_started, + documents_cancelled_count=batch_status_details.summary.cancelled, + total_characters_charged=batch_status_details.summary.total_character_charged + ) + class DocumentStatusDetail(object): """DocumentStatusDetail. @@ -210,6 +227,14 @@ def __init__( self.message = None self.target = None + @classmethod + def _from_generated(cls, error): + return cls( + code=error.code, + message=error.message, + target=error.target + ) + class FileFormat(object): """FileFormat. From 2eed7154859b667d69d566be37311285b9c7df3d Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 18:25:12 +0200 Subject: [PATCH 10/59] [models wrapping] update client --- .../azure/ai/documenttranslation/_client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 6f29e33732df..6475ea4ee460 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -65,7 +65,7 @@ def create_translation_job(self, batch, **kwargs): # get job id from response header job_id = response_headers['Operation-Location'] - # call + # get job status return self.get_job_status(job_id) @@ -79,7 +79,8 @@ def get_job_status(self, job_id, **kwargs): :rtype: ~azure.ai.documenttranslation.JobStatusDetail """ - return self._client.document_translation.get_operation_status(job_id, **kwargs) + job_status = self._client.document_translation.get_operation_status(job_id, **kwargs) + return JobStatusDetail._from_generated(job_status) @distributed_trace def cancel_job(self, job_id, **kwargs): From d97b551444874e782c18bc76c62aba07c811a85a Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 19:36:27 +0200 Subject: [PATCH 11/59] [models wrapping] formats mapping [documents, glossaries, storage] --- .../azure/ai/documenttranslation/_client.py | 9 ++++++--- .../azure/ai/documenttranslation/_models.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 6475ea4ee460..b51095feb725 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -151,7 +151,8 @@ def get_supported_storage_sources(self, **kwargs): :rtype: List[str] """ - return self._client.document_translation.get_document_storage_source(**kwargs) + result = self._client.document_translation.get_document_storage_source(**kwargs) + return result.value @distributed_trace def get_supported_glossary_formats(self, **kwargs): @@ -161,7 +162,8 @@ def get_supported_glossary_formats(self, **kwargs): :rtype: List[FileFormat] """ - return self._client.document_translation.get_glossary_formats(**kwargs) + glossary_formats = self._client.document_translation.get_glossary_formats(**kwargs) + return FileFormat._from_generated_list(glossary_formats) @distributed_trace def get_supported_document_formats(self, **kwargs): @@ -171,4 +173,5 @@ def get_supported_document_formats(self, **kwargs): :rtype: List[FileFormat] """ - return self._client.document_translation.get_document_formats(**kwargs) + document_formats = self._client.document_translation.get_document_formats(**kwargs) + return FileFormat._from_generated_list(document_formats) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index c55e2d4a9817..99773889a648 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -258,3 +258,16 @@ def __init__( self.file_extensions = kwargs.get('file_extensions', None) self.content_types = kwargs.get('content_types', None) self.versions = kwargs.get('versions', None) + + @classmethod + def _from_generated(cls, file_format): + return cls( + format=file_format.format, + file_extentions=file_format.file_extentions, + content_types=file_format.content_types, + versions=file_format.versions + ) + + @classmethod + def _from_generated_list(cls, file_formats): + return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) From 5ebd7fa5ae3822eaa4e27aa16a64f7c70b9c1206 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 4 Mar 2021 20:51:33 +0200 Subject: [PATCH 12/59] [models wrapping] batch document input --- .../azure/ai/documenttranslation/_client.py | 4 +- .../azure/ai/documenttranslation/_models.py | 86 ++++++++++++++++--- 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index b51095feb725..75645df41cf7 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -57,8 +57,8 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( - inputs=batch, - cls=lambda x,y,z: z, + inputs = BatchDocumentInput._to_generated_list(batch), + cls = lambda x,y,z: z, **kwargs ) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 99773889a648..6159a0badd71 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -6,6 +6,14 @@ from typing import Any, List +from ._generated.models import ( + BatchRequest as _BatchRequest, + SourceInput as _SourceInput, + DocumentFilter as _DocumentFilter, + TargetInput as _TargetInput, + Glossary as _Glossary +) + class TranslationGlossary(object): """Glossary / translation memory for the request. @@ -32,6 +40,27 @@ def __init__( self.format_version = kwargs.get("format_version", None) self.storage_source = kwargs.get("storage_source", None) + @classmethod + def _to_generated_list(cls, glossaries): + result = list(_Glossary) + for glossary in glossaries: + if isinstance(TranslationGlossary): + result.append( + _Glossary( + glossary_url = glossary.glossary_url, + format = glossary.format, + version = glossary.version, + storage_source = glossary.storage_source + ) + ) + elif isinstance(str): + result.append( + _Glossary( + glossary_url = glossary, + ) + ) + return result + class StorageTarget(object): """Destination for the finished translated documents. @@ -60,6 +89,19 @@ def __init__( self.glossaries = kwargs.get("glossaries", None) self.storage_source = kwargs.get("storage_source", None) + @classmethod + def _to_generated_list(cls, targets): + return [ + _TargetInput( + target_url = target.target_url, + category = target.category_id, + language = target.language, + storage_source = target.storage_source, + glossaries = TranslationGlossary._to_generated_list(target.glossaries) + ) + for target in targets + ] + class BatchDocumentInput(object): """Definition for the input batch translation request. @@ -97,6 +139,26 @@ def __init__( self.prefix = kwargs.get("prefix", None) self.suffix = kwargs.get("suffix", None) + @classmethod + def _to_generated_list(cls, batch_document_inputs): + return [ + _BatchRequest( + source = _SourceInput( + source_url = batch_document_input.source_url, + filter = _DocumentFilter( + prefix = batch_document_input.prefix, + suffix = batch_document_input.suffix + ), + language = batch_document_input.source_language, + storage_source = batch_document_input.storage_source + ), + targets = StorageTarget._to_generated_list(batch_document_input.targets), + storage_type = batch_document_input.storage_type + ) + for batch_document_input in batch_document_inputs + ] + + class JobStatusDetail(object): """Job status response. @@ -146,18 +208,18 @@ def __init__( @classmethod def _from_generated(cls, batch_status_details): return cls( - id=batch_status_details.id, - created_on=batch_status_details.created_date_time_utc, - last_updated_on=batch_status_details.last_action_date_time_utc, - status=batch_status_details.status, - error=DocumentTranslationError._from_generated(batch_status_details.error), - documents_total_count=batch_status_details.summary.total, - documents_failed_count=batch_status_details.summary.failed, - documents_succeeded_count=batch_status_details.summary.success, - documents_in_progress_count=batch_status_details.summary.in_progress, - documents_not_yet_started_count=batch_status_details.summary.not_yet_started, - documents_cancelled_count=batch_status_details.summary.cancelled, - total_characters_charged=batch_status_details.summary.total_character_charged + id = batch_status_details.id, + created_on = batch_status_details.created_date_time_utc, + last_updated_on = batch_status_details.last_action_date_time_utc, + status = batch_status_details.status, + error = DocumentTranslationError._from_generated(batch_status_details.error), + documents_total_count = batch_status_details.summary.total, + documents_failed_count = batch_status_details.summary.failed, + documents_succeeded_count = batch_status_details.summary.success, + documents_in_progress_count = batch_status_details.summary.in_progress, + documents_not_yet_started_count = batch_status_details.summary.not_yet_started, + documents_cancelled_count = batch_status_details.summary.cancelled, + total_characters_charged = batch_status_details.summary.total_character_charged ) From 83e2b9d56c839d1c5b1e99a4755ac67e8be183c4 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 01:14:48 +0200 Subject: [PATCH 13/59] [models wrapping] added document status --- .../azure/ai/documenttranslation/_client.py | 4 +++- .../azure/ai/documenttranslation/_models.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 75645df41cf7..7f30435c77b1 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -142,7 +142,9 @@ def get_document_status(self, job_id, document_id, **kwargs): :type document_id: str :rtype: ~azure.ai.documenttranslation.DocumentStatusDetail """ - return self._client.document_translation.get_document_status(job_id, document_id, **kwargs) + + result = self._client.document_translation.get_document_status(job_id, document_id, **kwargs) + return DocumentStatusDetail._from_generated(result) @distributed_trace def get_supported_storage_sources(self, **kwargs): diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 6159a0badd71..6cf7ebfe7663 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -265,6 +265,21 @@ def __init__( self.characters_charged = kwargs.get('characters_charged', None) + @classmethod + def _from_generated(cls, doc_status): + return cls( + url = doc_status.path, + created_on = doc_status.created_date_time_utc, + last_updated_on = doc_status.last_action_date_time_utc, + status = doc_status.status, + translate_to = doc_status.to, + error = DocumentTranslationError._from_generated(doc_status.error), + translation_progress = doc_status.progress, + id = doc_status.id, + characters_charged = doc_status.character_charged + ) + + class DocumentTranslationError(object): """This contains an outer error with error code, message, details, target and an inner error with more descriptive details. From 4626d7f9fd6ec04f70c01ddafdc3ff53affb5e9a Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 01:53:33 +0200 Subject: [PATCH 14/59] [models wrapping] added support for list document status --- .../azure/ai/documenttranslation/_client.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 7f30435c77b1..d084606e5433 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -129,6 +129,22 @@ def list_documents_statuses(self, job_id, **kwargs): :rtype: ~azure.core.paging.ItemPaged[DocumentStatusDetail] """ + skip = kwargs.pop('skip', None) + top = kwargs.pop('top', None) + + def _convert_from_generated_model(generated_model): + return DocumentStatusDetail._from_generated(generated_model) + + model_conversion_function = kwargs.pop("cls", lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses]) + + return self._client.document_translation.get_operation_documents_status( + top = top, + skip = skip, + cls = model_conversion_function, + **kwargs + ) + + return self._client.document_translation.get_operation_documents_status(job_id, **kwargs) @distributed_trace From 3b66c59168a7bae8a13b7164a093bca368278288 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 01:54:39 +0200 Subject: [PATCH 15/59] [models wrapping] remove unwanted code --- .../azure/ai/documenttranslation/_client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index d084606e5433..f0e60e462cec 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -143,9 +143,7 @@ def _convert_from_generated_model(generated_model): cls = model_conversion_function, **kwargs ) - - - return self._client.document_translation.get_operation_documents_status(job_id, **kwargs) + @distributed_trace def get_document_status(self, job_id, document_id, **kwargs): From ee18f400e279b9c6dc825d0b1c39ea8d6be01099 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 02:37:36 +0200 Subject: [PATCH 16/59] [models wrapping] forgot to add job id --- .../azure/ai/documenttranslation/_client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index f0e60e462cec..95d12f621e50 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -138,12 +138,13 @@ def _convert_from_generated_model(generated_model): model_conversion_function = kwargs.pop("cls", lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses]) return self._client.document_translation.get_operation_documents_status( + id = job_id, top = top, skip = skip, cls = model_conversion_function, **kwargs ) - + @distributed_trace def get_document_status(self, job_id, document_id, **kwargs): From c55886ec178248b17fa7151b30393e7c299fe9dd Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 02:48:32 +0200 Subject: [PATCH 17/59] [model wrapping] list all jobs --- .../azure/ai/documenttranslation/_client.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 95d12f621e50..d7cbbbb64542 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -115,7 +115,21 @@ def list_submitted_jobs(self, **kwargs): :keyword int skip: :rtype: ~azure.core.polling.ItemPaged[JobStatusDetail] """ - return self._client.document_translation.get_operations(**kwargs) + + skip = kwargs.pop('skip', None) + top = kwargs.pop('top', None) + + def _convert_from_generated_model(generated_model): + return JobStatusDetail._from_generated(generated_model) + + model_conversion_function = kwargs.pop("cls", lambda job_statuses: [_convert_from_generated_model(job_status) for job_status in job_statuses]) + + return self._client.document_translation.get_operations( + top = top, + skip = skip, + cls = model_conversion_function, + **kwargs + ) @distributed_trace def list_documents_statuses(self, job_id, **kwargs): From 990cb650ed221b35f97b5938f0f1ae052061b370 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 03:18:28 +0200 Subject: [PATCH 18/59] [pr review] extract job id --- .../azure/ai/documenttranslation/_client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index d7cbbbb64542..d166be99e6c0 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -63,7 +63,8 @@ def create_translation_job(self, batch, **kwargs): ) # get job id from response header - job_id = response_headers['Operation-Location'] + operation_location_header = response_headers['Operation-Location'] + job_id = operation_location_header.split('/')[-1] # extract job id. ex: https://document-translator.cognitiveservices.azure.com/translator/text/batch/v1.0-preview.1/batches/cd0asdd0-2ce6-asd4-abd4-9asd7698c26a # get job status return self.get_job_status(job_id) From 373a6f5dc988f942727c5868baa5bca1a6e7139b Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 12:55:02 +0200 Subject: [PATCH 19/59] [refactor] some refactoring --- .../azure/ai/documenttranslation/_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index d166be99e6c0..59379edac7d6 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -62,9 +62,13 @@ def create_translation_job(self, batch, **kwargs): **kwargs ) + def get_job_id(operation_loc_header): + # extract job id. ex: https://document-translator.cognitiveservices.azure.com/translator/text/batch/v1.0-preview.1/batches/cd0asdd0-2ce6-asd4-abd4-9asd7698c26a + return operation_loc_header.split('/')[-1] + # get job id from response header operation_location_header = response_headers['Operation-Location'] - job_id = operation_location_header.split('/')[-1] # extract job id. ex: https://document-translator.cognitiveservices.azure.com/translator/text/batch/v1.0-preview.1/batches/cd0asdd0-2ce6-asd4-abd4-9asd7698c26a + job_id = get_job_id(operation_location_header) # get job status return self.get_job_status(job_id) From 691d703daa649d614c6c8056e0f894ed913b5e9d Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 12:58:45 +0200 Subject: [PATCH 20/59] [pr reviews] refactor --- .../azure/ai/documenttranslation/_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 6cf7ebfe7663..a1b551a64d15 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -42,7 +42,7 @@ def __init__( @classmethod def _to_generated_list(cls, glossaries): - result = list(_Glossary) + result = [] for glossary in glossaries: if isinstance(TranslationGlossary): result.append( From a68c1ab1b890ef4a3dcf1c8747c19b26ae5f18b8 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 13:26:12 +0200 Subject: [PATCH 21/59] [refactor] to_generated in glossary --- .../azure/ai/documenttranslation/_models.py | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index a1b551a64d15..56c86614b0ec 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -14,6 +14,7 @@ Glossary as _Glossary ) +import six class TranslationGlossary(object): """Glossary / translation memory for the request. @@ -40,26 +41,24 @@ def __init__( self.format_version = kwargs.get("format_version", None) self.storage_source = kwargs.get("storage_source", None) - @classmethod - def _to_generated_list(cls, glossaries): - result = [] - for glossary in glossaries: - if isinstance(TranslationGlossary): - result.append( - _Glossary( - glossary_url = glossary.glossary_url, - format = glossary.format, - version = glossary.version, - storage_source = glossary.storage_source - ) + + @staticmethod + def _to_generated(glossary): + if isinstance(glossary, TranslationGlossary): + return _Glossary( + glossary_url = glossary.glossary_url, + format = glossary.format, + version = glossary.version, + storage_source = glossary.storage_source ) - elif isinstance(str): - result.append( - _Glossary( - glossary_url = glossary, - ) + elif isinstance(glossary, six.string_types): + return _Glossary( + glossary_url = glossary, ) - return result + + @staticmethod + def _to_generated_list(glossaries): + return [TranslationGlossary._to_generated(glossary) for glossary in glossaries] class StorageTarget(object): From 432503a5cdff6d42e5d4f196707a30a93ec5d423 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 13:30:08 +0200 Subject: [PATCH 22/59] [refactor] to generated in StorageTargets --- .../azure/ai/documenttranslation/_models.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 56c86614b0ec..dc497d37542c 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -88,18 +88,18 @@ def __init__( self.glossaries = kwargs.get("glossaries", None) self.storage_source = kwargs.get("storage_source", None) - @classmethod - def _to_generated_list(cls, targets): - return [ - _TargetInput( - target_url = target.target_url, - category = target.category_id, - language = target.language, - storage_source = target.storage_source, - glossaries = TranslationGlossary._to_generated_list(target.glossaries) - ) - for target in targets - ] + def _to_generated(self): + return _TargetInput( + target_url = self.target_url, + category = self.category_id, + language = self.language, + storage_source = self.storage_source, + glossaries = TranslationGlossary._to_generated_list(self.glossaries) + ) + + @staticmethod + def _to_generated_list(targets): + return [ target._to_generated() for target in targets] class BatchDocumentInput(object): From 057319048c93887c9d3fad57b79d2b71f61eb817 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 13:33:20 +0200 Subject: [PATCH 23/59] [refactor] to_generated in BatchDocumentInput --- .../azure/ai/documenttranslation/_models.py | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index dc497d37542c..60e47a4cbac4 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -138,24 +138,24 @@ def __init__( self.prefix = kwargs.get("prefix", None) self.suffix = kwargs.get("suffix", None) - @classmethod - def _to_generated_list(cls, batch_document_inputs): - return [ - _BatchRequest( - source = _SourceInput( - source_url = batch_document_input.source_url, - filter = _DocumentFilter( - prefix = batch_document_input.prefix, - suffix = batch_document_input.suffix - ), - language = batch_document_input.source_language, - storage_source = batch_document_input.storage_source + def _to_generated(self): + return _BatchRequest( + source = _SourceInput( + source_url = self.source_url, + filter = _DocumentFilter( + prefix = self.prefix, + suffix = self.suffix ), - targets = StorageTarget._to_generated_list(batch_document_input.targets), - storage_type = batch_document_input.storage_type - ) - for batch_document_input in batch_document_inputs - ] + language = self.source_language, + storage_source = self.storage_source + ), + targets = StorageTarget._to_generated_list(self.targets), + storage_type = self.storage_type + ) + + @staticmethod + def _to_generated_list(batch_document_inputs): + return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] From daee063ecbed793c1ad17c4313a82420ea219617 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 13:40:16 +0200 Subject: [PATCH 24/59] [refactor] to_generated in Glossary more --- .../azure/ai/documenttranslation/_models.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 60e47a4cbac4..fe63da9a1d6b 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -41,16 +41,18 @@ def __init__( self.format_version = kwargs.get("format_version", None) self.storage_source = kwargs.get("storage_source", None) + def _to_generated(self): + return _Glossary( + glossary_url = self.glossary_url, + format = self.format, + version = self.format_version, + storage_source = self.storage_source + ) @staticmethod def _to_generated(glossary): if isinstance(glossary, TranslationGlossary): - return _Glossary( - glossary_url = glossary.glossary_url, - format = glossary.format, - version = glossary.version, - storage_source = glossary.storage_source - ) + return glossary._to_generated() elif isinstance(glossary, six.string_types): return _Glossary( glossary_url = glossary, From 9ec007bd48640b6ac1e5d5cd6c5082ef35a5b837 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 13:51:05 +0200 Subject: [PATCH 25/59] [refactor] make code readable -> storage formats --- .../azure/ai/documenttranslation/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 59379edac7d6..dcbbe3a06d5a 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -188,7 +188,7 @@ def get_supported_storage_sources(self, **kwargs): :rtype: List[str] """ result = self._client.document_translation.get_document_storage_source(**kwargs) - return result.value + return [storage_source for storage_source in result.value] @distributed_trace def get_supported_glossary_formats(self, **kwargs): From ec4c16cbb74769f689df09ecc9972d84cda6e1c1 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 18:41:57 +0200 Subject: [PATCH 26/59] [integration] added support for wait_until_done --- .../azure/ai/documenttranslation/_client.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index dcbbe3a06d5a..09e14e75b415 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -109,7 +109,15 @@ def wait_until_done(self, job_id, **kwargs): :return: JobStatusDetail :rtype: JobStatusDetail """ - pass + import time + + while True: + job_result = self.get_job_status(job_id) + if job_result.status in ["NotStarted", "Running"]: + time.sleep(0.1) + continue + else: + return job_result @distributed_trace def list_submitted_jobs(self, **kwargs): @@ -177,8 +185,8 @@ def get_document_status(self, job_id, document_id, **kwargs): :rtype: ~azure.ai.documenttranslation.DocumentStatusDetail """ - result = self._client.document_translation.get_document_status(job_id, document_id, **kwargs) - return DocumentStatusDetail._from_generated(result) + document_status = self._client.document_translation.get_document_status(job_id, document_id, **kwargs) + return DocumentStatusDetail._from_generated(document_status) @distributed_trace def get_supported_storage_sources(self, **kwargs): @@ -187,8 +195,8 @@ def get_supported_storage_sources(self, **kwargs): :rtype: List[str] """ - result = self._client.document_translation.get_document_storage_source(**kwargs) - return [storage_source for storage_source in result.value] + storage_sources_response = self._client.document_translation.get_document_storage_source(**kwargs) + return [storage_source for storage_source in storage_sources_response.value] @distributed_trace def get_supported_glossary_formats(self, **kwargs): From c18bef59c8d45192c4dfa8bf580aae7929b0ac99 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 8 Mar 2021 18:47:07 +0200 Subject: [PATCH 27/59] [refactor] spelling error --- .../azure/ai/documenttranslation/_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index fe63da9a1d6b..7672e2c4fc33 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -341,7 +341,7 @@ def __init__( def _from_generated(cls, file_format): return cls( format=file_format.format, - file_extentions=file_format.file_extentions, + file_extensions=file_format.file_extensions, content_types=file_format.content_types, versions=file_format.versions ) From 457398434204e35cc11fc63c2f50fa74c5b8d55c Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 10 Mar 2021 15:40:07 +0200 Subject: [PATCH 28/59] [integrate sdk] updated "wait_until_done" to use azure core poller instead of busy wait --- .../azure/ai/documenttranslation/_client.py | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 09e14e75b415..7234764522aa 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -6,7 +6,10 @@ from typing import Union, Any, TYPE_CHECKING, List from azure.core.tracing.decorator import distributed_trace +from azure.core.polling import LROPoller +from azure.core.polling.base_polling import LROBasePolling from ._generated import BatchDocumentTranslationClient as _BatchDocumentTranslationClient +from ._generated.models import BatchStatusDetail as _BatchStatusDetail from ._helpers import get_authentication_policy from ._user_agent import USER_AGENT if TYPE_CHECKING: @@ -58,7 +61,7 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( inputs = BatchDocumentInput._to_generated_list(batch), - cls = lambda x,y,z: z, + cls = lambda pipeline_response, _, response_headers: response_headers, **kwargs ) @@ -109,15 +112,27 @@ def wait_until_done(self, job_id, **kwargs): :return: JobStatusDetail :rtype: JobStatusDetail """ - import time - - while True: - job_result = self.get_job_status(job_id) - if job_result.status in ["NotStarted", "Running"]: - time.sleep(0.1) - continue - else: - return job_result + pipeline_response = self.get_job_status( + job_id, + cls=lambda pipeline_response, _, response_headers: pipeline_response + ) + + def callback(raw_response): + detail = self._client._deserialize(_BatchStatusDetail, raw_response) + # return JobStatusDetail._from_generated(detail) + return detail + + poller = LROPoller( + client=self._client._client, + initial_response=pipeline_response, + deserialization_callback=callback, + polling_method=LROBasePolling( + timeout=30, + lro_algorithms=None, # LROBasePolling already has 3 different algorithms (which our OperationLocationHeader is supported) + **kwargs + ), + ) + return poller.result() @distributed_trace def list_submitted_jobs(self, **kwargs): From 0bc61593e2388cf2b82481f20cba589d9b4d877a Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 18:56:15 +0200 Subject: [PATCH 29/59] [sync client][wait till done] initial poller --- .../azure/ai/documenttranslation/_client.py | 3 +- .../ai/documenttranslation/_poller_helpers.py | 73 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 7234764522aa..342a037ad67b 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -12,6 +12,7 @@ from ._generated.models import BatchStatusDetail as _BatchStatusDetail from ._helpers import get_authentication_policy from ._user_agent import USER_AGENT +from ._poller_helpers import StatusPollingGETRequest if TYPE_CHECKING: from azure.core.paging import ItemPaged from azure.core.credentials import AzureKeyCredential, TokenCredential @@ -128,7 +129,7 @@ def callback(raw_response): deserialization_callback=callback, polling_method=LROBasePolling( timeout=30, - lro_algorithms=None, # LROBasePolling already has 3 different algorithms (which our OperationLocationHeader is supported) + lro_algorithms=StatusPollingGETRequest(), **kwargs ), ) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py new file mode 100644 index 000000000000..d40edcd856aa --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py @@ -0,0 +1,73 @@ +#my_polling + +from azure.core.polling.base_polling import ( + LongRunningOperation, + _is_empty, + _as_json, + BadResponse, + OperationFailed +) + + +class StatusPollingGETRequest(LongRunningOperation): + """Implements a Location polling. + """ + + def __init__(self): + self._location_url = None + + def can_poll(self, pipeline_response): + # type: (PipelineResponseType) -> bool + """Answer if this polling method could be used. + """ + response = pipeline_response.http_response + if not _is_empty(response): + body = _as_json(response) + status = body.get("status") + return True if status else False + return False + + def get_polling_url(self): + # type: () -> str + """Return the polling URL. + """ + return self._async_url + + def get_final_get_url(self, pipeline_response): + # type: (PipelineResponseType) -> Optional[str] + """If a final GET is needed, returns the URL. + + :rtype: str + """ + return self._async_url + + def set_initial_status(self, pipeline_response): + # type: (PipelineResponseType) -> str + """Process first response after initiating long running operation. + + :param azure.core.pipeline.PipelineResponse response: initial REST call response. + """ + response = pipeline_response.http_response + if not _is_empty(response): + body = _as_json(response) + status = body.get("status") + if status: + return status + raise OperationFailed("Operation failed or canceled") + + def get_status(self, pipeline_response): + # type: (PipelineResponseType) -> str + """Process the latest status update retrieved from a 'location' header. + + :param azure.core.pipeline.PipelineResponse response: latest REST call response. + :raises: BadResponse if response has no body and not status 202. + """ + response = pipeline_response.http_response + if not _is_empty(response): + body = _as_json(response) + status = body.get("status") + if status: + return status + else: + raise BadResponse("No status found in body") + raise BadResponse("The response from long running operation does not contain a body.") From 7a27db5d8b57307db5aba2ca1ff3234d8ce3c5ab Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 19:08:28 +0200 Subject: [PATCH 30/59] [sync client][wait poller] poller algorithm completed --- .../azure/ai/documenttranslation/_client.py | 4 ++-- .../{_poller_helpers.py => _polling.py} | 21 ++++++------------- 2 files changed, 8 insertions(+), 17 deletions(-) rename sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/{_poller_helpers.py => _polling.py} (79%) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 342a037ad67b..4f63a841da58 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -12,7 +12,7 @@ from ._generated.models import BatchStatusDetail as _BatchStatusDetail from ._helpers import get_authentication_policy from ._user_agent import USER_AGENT -from ._poller_helpers import StatusPollingGETRequest +from ._polling import TranslationPolling if TYPE_CHECKING: from azure.core.paging import ItemPaged from azure.core.credentials import AzureKeyCredential, TokenCredential @@ -129,7 +129,7 @@ def callback(raw_response): deserialization_callback=callback, polling_method=LROBasePolling( timeout=30, - lro_algorithms=StatusPollingGETRequest(), + lro_algorithms=TranslationPolling(), **kwargs ), ) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py similarity index 79% rename from sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py rename to sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py index d40edcd856aa..ae81dad9759a 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller_helpers.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -9,12 +9,12 @@ ) -class StatusPollingGETRequest(LongRunningOperation): +class TranslationPolling(LongRunningOperation): """Implements a Location polling. """ def __init__(self): - self._location_url = None + self._async_url = None def can_poll(self, pipeline_response): # type: (PipelineResponseType) -> bool @@ -33,26 +33,17 @@ def get_polling_url(self): """ return self._async_url - def get_final_get_url(self, pipeline_response): - # type: (PipelineResponseType) -> Optional[str] - """If a final GET is needed, returns the URL. - - :rtype: str - """ - return self._async_url - def set_initial_status(self, pipeline_response): # type: (PipelineResponseType) -> str """Process first response after initiating long running operation. :param azure.core.pipeline.PipelineResponse response: initial REST call response. """ + self._async_url = pipeline_response.http_response.request.url + response = pipeline_response.http_response - if not _is_empty(response): - body = _as_json(response) - status = body.get("status") - if status: - return status + if response.status_code in {200, 201, 202, 204} and self._async_url: + return "InProgress" raise OperationFailed("Operation failed or canceled") def get_status(self, pipeline_response): From 866f766f3895279f2b20f57465bbe4e3bcfa78a6 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 19:28:58 +0200 Subject: [PATCH 31/59] [PR comments] wrap model before returning --- .../azure/ai/documenttranslation/_client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 4f63a841da58..bd7835dccf2e 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -120,8 +120,7 @@ def wait_until_done(self, job_id, **kwargs): def callback(raw_response): detail = self._client._deserialize(_BatchStatusDetail, raw_response) - # return JobStatusDetail._from_generated(detail) - return detail + return JobStatusDetail._from_generated(detail) poller = LROPoller( client=self._client._client, From a4fd853fa6f650919347931bca62b80e2bed30e5 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 19:39:08 +0200 Subject: [PATCH 32/59] [pr reviews] adjust parameter type to list --- .../azure/ai/documenttranslation/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index bd7835dccf2e..d5106e774ea3 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -128,7 +128,7 @@ def callback(raw_response): deserialization_callback=callback, polling_method=LROBasePolling( timeout=30, - lro_algorithms=TranslationPolling(), + lro_algorithms=[TranslationPolling()], **kwargs ), ) From 223a363fbca7dd61247e227ec9cb1bab3c3110d8 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 19:40:39 +0200 Subject: [PATCH 33/59] [pr reviews] some linter checks --- .../azure/ai/documenttranslation/_models.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 7672e2c4fc33..6d1574cd60f2 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -5,16 +5,14 @@ # ------------------------------------ from typing import Any, List - +import six from ._generated.models import ( BatchRequest as _BatchRequest, SourceInput as _SourceInput, DocumentFilter as _DocumentFilter, TargetInput as _TargetInput, Glossary as _Glossary -) - -import six +) class TranslationGlossary(object): """Glossary / translation memory for the request. From 61faadc39612b23f5a4ab5a8043cb62db1e013e7 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 19:53:55 +0200 Subject: [PATCH 34/59] [pr reviews] remove static methods, and fix private naming convention in model functions --- .../azure/ai/documenttranslation/_client.py | 6 +-- .../azure/ai/documenttranslation/_models.py | 47 +++++++++---------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index d5106e774ea3..b170440af01b 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -61,7 +61,7 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( - inputs = BatchDocumentInput._to_generated_list(batch), + inputs = BatchDocumentInput.to_generated_list(batch), cls = lambda pipeline_response, _, response_headers: response_headers, **kwargs ) @@ -222,7 +222,7 @@ def get_supported_glossary_formats(self, **kwargs): """ glossary_formats = self._client.document_translation.get_glossary_formats(**kwargs) - return FileFormat._from_generated_list(glossary_formats) + return FileFormat.from_generated_list(glossary_formats) @distributed_trace def get_supported_document_formats(self, **kwargs): @@ -233,4 +233,4 @@ def get_supported_document_formats(self, **kwargs): """ document_formats = self._client.document_translation.get_document_formats(**kwargs) - return FileFormat._from_generated_list(document_formats) + return FileFormat.from_generated_list(document_formats) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 6d1574cd60f2..cc00dc8f6646 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -39,7 +39,7 @@ def __init__( self.format_version = kwargs.get("format_version", None) self.storage_source = kwargs.get("storage_source", None) - def _to_generated(self): + def to_generated(self): return _Glossary( glossary_url = self.glossary_url, format = self.format, @@ -47,18 +47,16 @@ def _to_generated(self): storage_source = self.storage_source ) - @staticmethod - def _to_generated(glossary): + def to_generated_unknown_type(glossary): if isinstance(glossary, TranslationGlossary): - return glossary._to_generated() + return glossary.to_generated() elif isinstance(glossary, six.string_types): return _Glossary( glossary_url = glossary, ) - @staticmethod - def _to_generated_list(glossaries): - return [TranslationGlossary._to_generated(glossary) for glossary in glossaries] + def to_generated_list(glossaries): + return [TranslationGlossary.to_generated_unknown_type(glossary) for glossary in glossaries] class StorageTarget(object): @@ -88,18 +86,17 @@ def __init__( self.glossaries = kwargs.get("glossaries", None) self.storage_source = kwargs.get("storage_source", None) - def _to_generated(self): + def to_generated(self): return _TargetInput( target_url = self.target_url, category = self.category_id, language = self.language, storage_source = self.storage_source, - glossaries = TranslationGlossary._to_generated_list(self.glossaries) + glossaries = TranslationGlossary.to_generated_list(self.glossaries) ) - @staticmethod - def _to_generated_list(targets): - return [ target._to_generated() for target in targets] + def to_generated_list(targets): + return [ target.to_generated() for target in targets] class BatchDocumentInput(object): @@ -138,7 +135,7 @@ def __init__( self.prefix = kwargs.get("prefix", None) self.suffix = kwargs.get("suffix", None) - def _to_generated(self): + def to_generated(self): return _BatchRequest( source = _SourceInput( source_url = self.source_url, @@ -149,13 +146,12 @@ def _to_generated(self): language = self.source_language, storage_source = self.storage_source ), - targets = StorageTarget._to_generated_list(self.targets), + targets = StorageTarget.to_generated_list(self.targets), storage_type = self.storage_type ) - @staticmethod - def _to_generated_list(batch_document_inputs): - return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] + def to_generated_list(batch_document_inputs): + return [ batch_document_input.to_generated() for batch_document_input in batch_document_inputs ] @@ -205,13 +201,13 @@ def __init__( self.total_characters_charged = kwargs.get('total_characters_charged', None) @classmethod - def _from_generated(cls, batch_status_details): + def from_generated(cls, batch_status_details): return cls( id = batch_status_details.id, created_on = batch_status_details.created_date_time_utc, last_updated_on = batch_status_details.last_action_date_time_utc, status = batch_status_details.status, - error = DocumentTranslationError._from_generated(batch_status_details.error), + error = DocumentTranslationError.from_generated(batch_status_details.error), documents_total_count = batch_status_details.summary.total, documents_failed_count = batch_status_details.summary.failed, documents_succeeded_count = batch_status_details.summary.success, @@ -265,14 +261,14 @@ def __init__( @classmethod - def _from_generated(cls, doc_status): + def from_generated(cls, doc_status): return cls( url = doc_status.path, created_on = doc_status.created_date_time_utc, last_updated_on = doc_status.last_action_date_time_utc, status = doc_status.status, translate_to = doc_status.to, - error = DocumentTranslationError._from_generated(doc_status.error), + error = DocumentTranslationError.from_generated(doc_status.error), translation_progress = doc_status.progress, id = doc_status.id, characters_charged = doc_status.character_charged @@ -304,7 +300,7 @@ def __init__( self.target = None @classmethod - def _from_generated(cls, error): + def from_generated(cls, error): return cls( code=error.code, message=error.message, @@ -336,7 +332,7 @@ def __init__( self.versions = kwargs.get('versions', None) @classmethod - def _from_generated(cls, file_format): + def from_generated(cls, file_format): return cls( format=file_format.format, file_extensions=file_format.file_extensions, @@ -344,6 +340,5 @@ def _from_generated(cls, file_format): versions=file_format.versions ) - @classmethod - def _from_generated_list(cls, file_formats): - return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) + def from_generated_list(file_formats): + return list( [ FileFormat.from_generated(file_formats) for file_formats in file_formats ] ) From 23932aad457fdea6659c4aff7ad15165784352c6 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 20:13:45 +0200 Subject: [PATCH 35/59] [pr reviews] fix return type value --- .../azure/ai/documenttranslation/_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index b170440af01b..c200838bf515 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -222,7 +222,7 @@ def get_supported_glossary_formats(self, **kwargs): """ glossary_formats = self._client.document_translation.get_glossary_formats(**kwargs) - return FileFormat.from_generated_list(glossary_formats) + return FileFormat.from_generated_list(glossary_formats.value) @distributed_trace def get_supported_document_formats(self, **kwargs): @@ -233,4 +233,4 @@ def get_supported_document_formats(self, **kwargs): """ document_formats = self._client.document_translation.get_document_formats(**kwargs) - return FileFormat.from_generated_list(document_formats) + return FileFormat.from_generated_list(document_formats.value) From 4c01ceb86ca7eaa19c3ea3da337aff35c0b02d66 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 20:22:32 +0200 Subject: [PATCH 36/59] [PR reviews] implement unimplemented inherited abstract method --- .../azure/ai/documenttranslation/_polling.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py index ae81dad9759a..18694ecf6467 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -1,4 +1,8 @@ -#my_polling +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ from azure.core.polling.base_polling import ( LongRunningOperation, @@ -62,3 +66,11 @@ def get_status(self, pipeline_response): else: raise BadResponse("No status found in body") raise BadResponse("The response from long running operation does not contain a body.") + + def get_final_get_url(self, pipeline_response): + # type: (PipelineResponseType) -> Optional[str] + """If a final GET is needed, returns the URL. + + :rtype: str + """ + return None From e9cdb61c29d156e82eba50c75e8a0ee143f70a37 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 20:24:07 +0200 Subject: [PATCH 37/59] [pr reviews] fix python private method naming in client --- .../azure/ai/documenttranslation/_client.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index c200838bf515..055a6dfcd541 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -89,7 +89,7 @@ def get_job_status(self, job_id, **kwargs): """ job_status = self._client.document_translation.get_operation_status(job_id, **kwargs) - return JobStatusDetail._from_generated(job_status) + return JobStatusDetail.from_generated(job_status) @distributed_trace def cancel_job(self, job_id, **kwargs): @@ -120,7 +120,7 @@ def wait_until_done(self, job_id, **kwargs): def callback(raw_response): detail = self._client._deserialize(_BatchStatusDetail, raw_response) - return JobStatusDetail._from_generated(detail) + return JobStatusDetail.from_generated(detail) poller = LROPoller( client=self._client._client, @@ -148,7 +148,7 @@ def list_submitted_jobs(self, **kwargs): top = kwargs.pop('top', None) def _convert_from_generated_model(generated_model): - return JobStatusDetail._from_generated(generated_model) + return JobStatusDetail.from_generated(generated_model) model_conversion_function = kwargs.pop("cls", lambda job_statuses: [_convert_from_generated_model(job_status) for job_status in job_statuses]) @@ -175,7 +175,7 @@ def list_documents_statuses(self, job_id, **kwargs): top = kwargs.pop('top', None) def _convert_from_generated_model(generated_model): - return DocumentStatusDetail._from_generated(generated_model) + return DocumentStatusDetail.from_generated(generated_model) model_conversion_function = kwargs.pop("cls", lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses]) @@ -201,7 +201,7 @@ def get_document_status(self, job_id, document_id, **kwargs): """ document_status = self._client.document_translation.get_document_status(job_id, document_id, **kwargs) - return DocumentStatusDetail._from_generated(document_status) + return DocumentStatusDetail.from_generated(document_status) @distributed_trace def get_supported_storage_sources(self, **kwargs): From 53f92089909429d0f962e974eaf89dbbbba096c0 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 20:33:20 +0200 Subject: [PATCH 38/59] [pr review] handle case with no error happening --- .../azure/ai/documenttranslation/_models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index cc00dc8f6646..ee8bcb12c060 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -207,7 +207,7 @@ def from_generated(cls, batch_status_details): created_on = batch_status_details.created_date_time_utc, last_updated_on = batch_status_details.last_action_date_time_utc, status = batch_status_details.status, - error = DocumentTranslationError.from_generated(batch_status_details.error), + error = DocumentTranslationError.from_generated(batch_status_details.error) if batch_status_details.error else None, documents_total_count = batch_status_details.summary.total, documents_failed_count = batch_status_details.summary.failed, documents_succeeded_count = batch_status_details.summary.success, @@ -268,7 +268,7 @@ def from_generated(cls, doc_status): last_updated_on = doc_status.last_action_date_time_utc, status = doc_status.status, translate_to = doc_status.to, - error = DocumentTranslationError.from_generated(doc_status.error), + error = DocumentTranslationError.from_generated(doc_status.error) if doc_status.error else None, translation_progress = doc_status.progress, id = doc_status.id, characters_charged = doc_status.character_charged From 334cdff6f2032e18572f237a1b28c6895f38dcac Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 21:00:11 +0200 Subject: [PATCH 39/59] [pr reviews] renaming stuff :) --- .../azure/ai/documenttranslation/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 055a6dfcd541..3be8e06dd75b 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -145,7 +145,7 @@ def list_submitted_jobs(self, **kwargs): """ skip = kwargs.pop('skip', None) - top = kwargs.pop('top', None) + top = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): return JobStatusDetail.from_generated(generated_model) From 38e479b8316413a6717c65dc8b2c36d32674eb98 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Mon, 15 Mar 2021 21:59:19 +0200 Subject: [PATCH 40/59] [pr reviews] renaming more stuff :) --- .../azure/ai/documenttranslation/_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 3be8e06dd75b..1104f7dd708d 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -145,7 +145,7 @@ def list_submitted_jobs(self, **kwargs): """ skip = kwargs.pop('skip', None) - top = kwargs.pop('results_per_page', None) + results_per_page = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): return JobStatusDetail.from_generated(generated_model) @@ -153,7 +153,7 @@ def _convert_from_generated_model(generated_model): model_conversion_function = kwargs.pop("cls", lambda job_statuses: [_convert_from_generated_model(job_status) for job_status in job_statuses]) return self._client.document_translation.get_operations( - top = top, + top = results_per_page, skip = skip, cls = model_conversion_function, **kwargs @@ -172,7 +172,7 @@ def list_documents_statuses(self, job_id, **kwargs): """ skip = kwargs.pop('skip', None) - top = kwargs.pop('top', None) + results_per_page = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): return DocumentStatusDetail.from_generated(generated_model) @@ -181,7 +181,7 @@ def _convert_from_generated_model(generated_model): return self._client.document_translation.get_operation_documents_status( id = job_id, - top = top, + top = results_per_page, skip = skip, cls = model_conversion_function, **kwargs From edba47fa19ec765dfff68302c6bec7da749d0280 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Tue, 16 Mar 2021 01:01:58 +0200 Subject: [PATCH 41/59] [pr reviews] refactor private functions and linting options --- .../azure/ai/documenttranslation/_client.py | 22 ++++++--- .../azure/ai/documenttranslation/_models.py | 46 ++++++++++--------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 1104f7dd708d..f4989ab34bed 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -61,6 +61,7 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( + # pylint: disable=protected-access inputs = BatchDocumentInput.to_generated_list(batch), cls = lambda pipeline_response, _, response_headers: response_headers, **kwargs @@ -89,7 +90,8 @@ def get_job_status(self, job_id, **kwargs): """ job_status = self._client.document_translation.get_operation_status(job_id, **kwargs) - return JobStatusDetail.from_generated(job_status) + # pylint: disable=protected-access + return JobStatusDetail._from_generated(job_status) @distributed_trace def cancel_job(self, job_id, **kwargs): @@ -120,7 +122,8 @@ def wait_until_done(self, job_id, **kwargs): def callback(raw_response): detail = self._client._deserialize(_BatchStatusDetail, raw_response) - return JobStatusDetail.from_generated(detail) + # pylint: disable=protected-access + return JobStatusDetail._from_generated(detail) poller = LROPoller( client=self._client._client, @@ -148,7 +151,8 @@ def list_submitted_jobs(self, **kwargs): results_per_page = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): - return JobStatusDetail.from_generated(generated_model) + # pylint: disable=protected-access + return JobStatusDetail._from_generated(generated_model) model_conversion_function = kwargs.pop("cls", lambda job_statuses: [_convert_from_generated_model(job_status) for job_status in job_statuses]) @@ -175,7 +179,8 @@ def list_documents_statuses(self, job_id, **kwargs): results_per_page = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): - return DocumentStatusDetail.from_generated(generated_model) + # pylint: disable=protected-access + return DocumentStatusDetail._from_generated(generated_model) model_conversion_function = kwargs.pop("cls", lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses]) @@ -201,7 +206,8 @@ def get_document_status(self, job_id, document_id, **kwargs): """ document_status = self._client.document_translation.get_document_status(job_id, document_id, **kwargs) - return DocumentStatusDetail.from_generated(document_status) + # pylint: disable=protected-access + return DocumentStatusDetail._from_generated(document_status) @distributed_trace def get_supported_storage_sources(self, **kwargs): @@ -222,7 +228,8 @@ def get_supported_glossary_formats(self, **kwargs): """ glossary_formats = self._client.document_translation.get_glossary_formats(**kwargs) - return FileFormat.from_generated_list(glossary_formats.value) + # pylint: disable=protected-access + return FileFormat._from_generated_list(glossary_formats.value) @distributed_trace def get_supported_document_formats(self, **kwargs): @@ -233,4 +240,5 @@ def get_supported_document_formats(self, **kwargs): """ document_formats = self._client.document_translation.get_document_formats(**kwargs) - return FileFormat.from_generated_list(document_formats.value) + # pylint: disable=protected-access + return FileFormat._from_generated_list(document_formats.value) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index ee8bcb12c060..99f0e1a777cf 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -39,7 +39,7 @@ def __init__( self.format_version = kwargs.get("format_version", None) self.storage_source = kwargs.get("storage_source", None) - def to_generated(self): + def _to_generated(self): return _Glossary( glossary_url = self.glossary_url, format = self.format, @@ -47,16 +47,16 @@ def to_generated(self): storage_source = self.storage_source ) - def to_generated_unknown_type(glossary): + def _to_generated_unknown_type(glossary): if isinstance(glossary, TranslationGlossary): - return glossary.to_generated() + return glossary._to_generated() elif isinstance(glossary, six.string_types): return _Glossary( glossary_url = glossary, ) - def to_generated_list(glossaries): - return [TranslationGlossary.to_generated_unknown_type(glossary) for glossary in glossaries] + def _to_generated_list(glossaries): + return [TranslationGlossary._to_generated_unknown_type(glossary) for glossary in glossaries] class StorageTarget(object): @@ -86,17 +86,18 @@ def __init__( self.glossaries = kwargs.get("glossaries", None) self.storage_source = kwargs.get("storage_source", None) - def to_generated(self): + def _to_generated(self): return _TargetInput( target_url = self.target_url, category = self.category_id, language = self.language, storage_source = self.storage_source, - glossaries = TranslationGlossary.to_generated_list(self.glossaries) + # pylint: disable=protected-access + glossaries = TranslationGlossary._to_generated_list(self.glossaries) ) - def to_generated_list(targets): - return [ target.to_generated() for target in targets] + def _to_generated_list(targets): + return [ target._to_generated() for target in targets] class BatchDocumentInput(object): @@ -135,7 +136,7 @@ def __init__( self.prefix = kwargs.get("prefix", None) self.suffix = kwargs.get("suffix", None) - def to_generated(self): + def _to_generated(self): return _BatchRequest( source = _SourceInput( source_url = self.source_url, @@ -146,12 +147,13 @@ def to_generated(self): language = self.source_language, storage_source = self.storage_source ), - targets = StorageTarget.to_generated_list(self.targets), + # pylint: disable=protected-access + targets = StorageTarget._to_generated_list(self.targets), storage_type = self.storage_type ) - def to_generated_list(batch_document_inputs): - return [ batch_document_input.to_generated() for batch_document_input in batch_document_inputs ] + def _to_generated_list(batch_document_inputs): + return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] @@ -201,13 +203,14 @@ def __init__( self.total_characters_charged = kwargs.get('total_characters_charged', None) @classmethod - def from_generated(cls, batch_status_details): + def _from_generated(cls, batch_status_details): return cls( id = batch_status_details.id, created_on = batch_status_details.created_date_time_utc, last_updated_on = batch_status_details.last_action_date_time_utc, status = batch_status_details.status, - error = DocumentTranslationError.from_generated(batch_status_details.error) if batch_status_details.error else None, + # pylint: disable=protected-access + error = DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, documents_total_count = batch_status_details.summary.total, documents_failed_count = batch_status_details.summary.failed, documents_succeeded_count = batch_status_details.summary.success, @@ -261,14 +264,15 @@ def __init__( @classmethod - def from_generated(cls, doc_status): + def _from_generated(cls, doc_status): return cls( url = doc_status.path, created_on = doc_status.created_date_time_utc, last_updated_on = doc_status.last_action_date_time_utc, status = doc_status.status, translate_to = doc_status.to, - error = DocumentTranslationError.from_generated(doc_status.error) if doc_status.error else None, + # pylint: disable=protected-access + error = DocumentTranslationError._from_generated(doc_status.error) if doc_status.error else None, translation_progress = doc_status.progress, id = doc_status.id, characters_charged = doc_status.character_charged @@ -300,7 +304,7 @@ def __init__( self.target = None @classmethod - def from_generated(cls, error): + def _from_generated(cls, error): return cls( code=error.code, message=error.message, @@ -332,7 +336,7 @@ def __init__( self.versions = kwargs.get('versions', None) @classmethod - def from_generated(cls, file_format): + def _from_generated(cls, file_format): return cls( format=file_format.format, file_extensions=file_format.file_extensions, @@ -340,5 +344,5 @@ def from_generated(cls, file_format): versions=file_format.versions ) - def from_generated_list(file_formats): - return list( [ FileFormat.from_generated(file_formats) for file_formats in file_formats ] ) + def _from_generated_list(file_formats): + return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) From 0a569d49619ecf83ff5b76e90e1d9bb95acfb056 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Tue, 16 Mar 2021 16:36:40 +0200 Subject: [PATCH 42/59] [bug fix] poller -> handle non-standard status responses --- .../azure/ai/documenttranslation/_polling.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py index 18694ecf6467..1bafdceba8c6 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -62,7 +62,7 @@ def get_status(self, pipeline_response): body = _as_json(response) status = body.get("status") if status: - return status + return self._map_nonstandard_statuses(status) else: raise BadResponse("No status found in body") raise BadResponse("The response from long running operation does not contain a body.") @@ -74,3 +74,18 @@ def get_final_get_url(self, pipeline_response): :rtype: str """ return None + + def _map_nonstandard_statuses(status): + # type: (str) -> str + """Map non-standard statuses. + all statuses here: https://docs.microsoft.com/en-us/rest/api/cognitiveservices/translator/documenttranslation/getoperationstatus#status + + :param str status: lro process status. + """ + if status in ["ValidationFailed"]: + return "Failed" + elif status in ["Cancelled", "Cancelling"]: + return "Canceled" + return status + + From eb445bc7b12a1babb37563c9363978f65c67d6f7 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Tue, 16 Mar 2021 16:38:40 +0200 Subject: [PATCH 43/59] [#17289] remove supported storage sources method --- .../azure/ai/documenttranslation/_client.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index f4989ab34bed..8d5881c6f5c1 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -209,16 +209,6 @@ def get_document_status(self, job_id, document_id, **kwargs): # pylint: disable=protected-access return DocumentStatusDetail._from_generated(document_status) - @distributed_trace - def get_supported_storage_sources(self, **kwargs): - # type: (**Any) -> List[str] - """ - - :rtype: List[str] - """ - storage_sources_response = self._client.document_translation.get_document_storage_source(**kwargs) - return [storage_source for storage_source in storage_sources_response.value] - @distributed_trace def get_supported_glossary_formats(self, **kwargs): # type: (**Any) -> List[FileFormat] From 93d9ceabb04b939efbac84c4fa84a0e7ac938dc3 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Tue, 16 Mar 2021 17:55:25 +0200 Subject: [PATCH 44/59] [bug fix] private method name --- .../azure/ai/documenttranslation/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 8d5881c6f5c1..3a0cd641fb3e 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -62,7 +62,7 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( # pylint: disable=protected-access - inputs = BatchDocumentInput.to_generated_list(batch), + inputs = BatchDocumentInput._to_generated_list(batch), cls = lambda pipeline_response, _, response_headers: response_headers, **kwargs ) From e20284a764bcc6d8798fc7c20f7842ec288935be Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Tue, 16 Mar 2021 21:19:16 +0200 Subject: [PATCH 45/59] [linting] _models.py --- .../azure/ai/documenttranslation/_models.py | 168 +++++++++--------- 1 file changed, 84 insertions(+), 84 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index b7df7eb7e674..002d28a3308d 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -34,17 +34,17 @@ def __init__( **kwargs ): # type: (str, **Any) -> None - self.glossary_url = glossary_url - self.format = kwargs.get("format", None) - self.format_version = kwargs.get("format_version", None) - self.storage_source = kwargs.get("storage_source", None) + self.glossary_url=glossary_url + self.format=kwargs.get("format", None) + self.format_version=kwargs.get("format_version", None) + self.storage_source=kwargs.get("storage_source", None) def _to_generated(self): return _Glossary( - glossary_url = self.glossary_url, - format = self.format, - version = self.format_version, - storage_source = self.storage_source + glossary_url=self.glossary_url, + format=self.format, + version=self.format_version, + storage_source=self.storage_source ) def _to_generated_unknown_type(glossary): @@ -52,7 +52,7 @@ def _to_generated_unknown_type(glossary): return glossary._to_generated() elif isinstance(glossary, six.string_types): return _Glossary( - glossary_url = glossary, + glossary_url=glossary, ) def _to_generated_list(glossaries): @@ -80,20 +80,20 @@ def __init__( **kwargs ): # type: (str, str, **Any) -> None - self.target_url = target_url - self.language = language - self.category_id = kwargs.get("category_id", None) - self.glossaries = kwargs.get("glossaries", None) - self.storage_source = kwargs.get("storage_source", None) + self.target_url=target_url + self.language=language + self.category_id=kwargs.get("category_id", None) + self.glossaries=kwargs.get("glossaries", None) + self.storage_source=kwargs.get("storage_source", None) def _to_generated(self): return _TargetInput( - target_url = self.target_url, - category = self.category_id, - language = self.language, - storage_source = self.storage_source, + target_url=self.target_url, + category=self.category_id, + language=self.language, + storage_source=self.storage_source, # pylint: disable=protected-access - glossaries = TranslationGlossary._to_generated_list(self.glossaries) + glossaries=TranslationGlossary._to_generated_list(self.glossaries) ) def _to_generated_list(targets): @@ -128,28 +128,28 @@ def __init__( **kwargs ): # type: (str, List[StorageTarget], **Any) -> None - self.source_url = source_url - self.targets = targets - self.source_language = kwargs.get("source_language", None) - self.storage_type = kwargs.get("storage_type", None) - self.storage_source = kwargs.get("storage_source", None) - self.prefix = kwargs.get("prefix", None) - self.suffix = kwargs.get("suffix", None) + self.source_url=source_url + self.targets=targets + self.source_language=kwargs.get("source_language", None) + self.storage_type=kwargs.get("storage_type", None) + self.storage_source=kwargs.get("storage_source", None) + self.prefix=kwargs.get("prefix", None) + self.suffix=kwargs.get("suffix", None) def _to_generated(self): return _BatchRequest( - source = _SourceInput( - source_url = self.source_url, - filter = _DocumentFilter( - prefix = self.prefix, - suffix = self.suffix + source=_SourceInput( + source_url=self.source_url, + filter=_DocumentFilter( + prefix=self.prefix, + suffix=self.suffix ), - language = self.source_language, - storage_source = self.storage_source + language=self.source_language, + storage_source=self.storage_source ), # pylint: disable=protected-access - targets = StorageTarget._to_generated_list(self.targets), - storage_type = self.storage_type + targets=StorageTarget._to_generated_list(self.targets), + storage_type=self.storage_type ) def _to_generated_list(batch_document_inputs): @@ -189,35 +189,35 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.id = kwargs['id'] - self.created_on = kwargs['created_on'] - self.last_updated_on = kwargs['last_updated_on'] - self.status = kwargs.get('status', None) - self.error = kwargs.get("error", None) - self.documents_total_count = kwargs.get('documents_total_count', None) - self.documents_failed_count = kwargs.get('documents_failed_count', None) - self.documents_succeeded_count = kwargs.get('documents_succeeded_count', None) - self.documents_in_progress_count = kwargs.get('documents_in_progress_count', None) - self.documents_not_yet_started_count = kwargs.get('documents_not_yet_started_count', None) - self.documents_cancelled_count = kwargs.get('documents_cancelled_count', None) - self.total_characters_charged = kwargs.get('total_characters_charged', None) + self.id=kwargs['id'] + self.created_on=kwargs['created_on'] + self.last_updated_on=kwargs['last_updated_on'] + self.status=kwargs.get('status', None) + self.error=kwargs.get("error", None) + self.documents_total_count=kwargs.get('documents_total_count', None) + self.documents_failed_count=kwargs.get('documents_failed_count', None) + self.documents_succeeded_count=kwargs.get('documents_succeeded_count', None) + self.documents_in_progress_count=kwargs.get('documents_in_progress_count', None) + self.documents_not_yet_started_count=kwargs.get('documents_not_yet_started_count', None) + self.documents_cancelled_count=kwargs.get('documents_cancelled_count', None) + self.total_characters_charged=kwargs.get('total_characters_charged', None) @classmethod def _from_generated(cls, batch_status_details): return cls( - id = batch_status_details.id, - created_on = batch_status_details.created_date_time_utc, - last_updated_on = batch_status_details.last_action_date_time_utc, - status = batch_status_details.status, + id=batch_status_details.id, + created_on=batch_status_details.created_date_time_utc, + last_updated_on=batch_status_details.last_action_date_time_utc, + status=batch_status_details.status, # pylint: disable=protected-access - error = DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, - documents_total_count = batch_status_details.summary.total, - documents_failed_count = batch_status_details.summary.failed, - documents_succeeded_count = batch_status_details.summary.success, - documents_in_progress_count = batch_status_details.summary.in_progress, - documents_not_yet_started_count = batch_status_details.summary.not_yet_started, - documents_cancelled_count = batch_status_details.summary.cancelled, - total_characters_charged = batch_status_details.summary.total_character_charged + error=DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, + documents_total_count=batch_status_details.summary.total, + documents_failed_count=batch_status_details.summary.failed, + documents_succeeded_count=batch_status_details.summary.success, + documents_in_progress_count=batch_status_details.summary.in_progress, + documents_not_yet_started_count=batch_status_details.summary.not_yet_started, + documents_cancelled_count=batch_status_details.summary.cancelled, + total_characters_charged=batch_status_details.summary.total_character_charged ) @@ -252,30 +252,30 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.url = kwargs['url'] - self.created_on = kwargs['created_on'] - self.last_updated_on = kwargs['last_updated_on'] - self.status = kwargs['status'] - self.translate_to = kwargs['translate_to'] - self.error = kwargs.get('error', None) - self.translation_progress = kwargs.get('translation_progress', None) - self.id = kwargs.get('id', None) - self.characters_charged = kwargs.get('characters_charged', None) + self.url=kwargs['url'] + self.created_on=kwargs['created_on'] + self.last_updated_on=kwargs['last_updated_on'] + self.status=kwargs['status'] + self.translate_to=kwargs['translate_to'] + self.error=kwargs.get('error', None) + self.translation_progress=kwargs.get('translation_progress', None) + self.id=kwargs.get('id', None) + self.characters_charged=kwargs.get('characters_charged', None) @classmethod def _from_generated(cls, doc_status): return cls( - url = doc_status.path, - created_on = doc_status.created_date_time_utc, - last_updated_on = doc_status.last_action_date_time_utc, - status = doc_status.status, - translate_to = doc_status.to, + url=doc_status.path, + created_on=doc_status.created_date_time_utc, + last_updated_on=doc_status.last_action_date_time_utc, + status=doc_status.status, + translate_to=doc_status.to, # pylint: disable=protected-access - error = DocumentTranslationError._from_generated(doc_status.error) if doc_status.error else None, - translation_progress = doc_status.progress, - id = doc_status.id, - characters_charged = doc_status.character_charged + error=DocumentTranslationError._from_generated(doc_status.error) if doc_status.error else None, + translation_progress=doc_status.progress, + id=doc_status.id, + characters_charged=doc_status.character_charged ) @@ -299,9 +299,9 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.code = kwargs.get('code', None) - self.message = None - self.target = None + self.code=kwargs.get('code', None) + self.message=None + self.target=None @classmethod def _from_generated(cls, error): @@ -330,10 +330,10 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.format = kwargs.get('format', None) - self.file_extensions = kwargs.get('file_extensions', None) - self.content_types = kwargs.get('content_types', None) - self.versions = kwargs.get('versions', None) + self.format=kwargs.get('format', None) + self.file_extensions=kwargs.get('file_extensions', None) + self.content_types=kwargs.get('content_types', None) + self.versions=kwargs.get('versions', None) @classmethod def _from_generated(cls, file_format): From ebb407ad062bd976b41f6343b23f5fa0d698faac Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 14:36:23 +0200 Subject: [PATCH 46/59] [linting] client --- .../azure/ai/documenttranslation/_client.py | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index e320504292f7..57095930bd06 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -10,6 +10,13 @@ from azure.core.polling.base_polling import LROBasePolling from ._generated import BatchDocumentTranslationClient as _BatchDocumentTranslationClient from ._generated.models import BatchStatusDetail as _BatchStatusDetail +from ._models import ( + BatchDocumentInput, + JobStatusDetail, + DocumentStatusDetail, + BatchDocumentInput, + FileFormat +) from ._helpers import get_authentication_policy from ._user_agent import USER_AGENT from ._polling import TranslationPolling @@ -60,6 +67,7 @@ def create_translation_job(self, batch, **kwargs): """ # submit translation job + # pylint: disable=protected-access response_headers = self._client.document_translation._submit_batch_request_initial( # pylint: disable=protected-access inputs = BatchDocumentInput._to_generated_list(batch), @@ -67,13 +75,12 @@ def create_translation_job(self, batch, **kwargs): **kwargs ) - def get_job_id(operation_loc_header): - # extract job id. ex: https://document-translator.cognitiveservices.azure.com/translator/text/batch/v1.0-preview.1/batches/cd0asdd0-2ce6-asd4-abd4-9asd7698c26a + def get_job_id(response_headers): + operation_loc_header = response_headers['Operation-Location'] return operation_loc_header.split('/')[-1] # get job id from response header - operation_location_header = response_headers['Operation-Location'] - job_id = get_job_id(operation_location_header) + job_id = get_job_id(response_headers) # get job status return self.get_job_status(job_id) @@ -122,11 +129,13 @@ def wait_until_done(self, job_id, **kwargs): ) def callback(raw_response): + # pylint: disable=protected-access detail = self._client._deserialize(_BatchStatusDetail, raw_response) # pylint: disable=protected-access return JobStatusDetail._from_generated(detail) poller = LROPoller( + # pylint: disable=protected-access client=self._client._client, initial_response=pipeline_response, deserialization_callback=callback, @@ -155,7 +164,11 @@ def _convert_from_generated_model(generated_model): # pylint: disable=protected-access return JobStatusDetail._from_generated(generated_model) - model_conversion_function = kwargs.pop("cls", lambda job_statuses: [_convert_from_generated_model(job_status) for job_status in job_statuses]) + model_conversion_function = kwargs.pop( + "cls", + lambda job_statuses: [ + _convert_from_generated_model(job_status) for job_status in job_statuses + ]) return self._client.document_translation.get_operations( top = results_per_page, @@ -183,7 +196,11 @@ def _convert_from_generated_model(generated_model): # pylint: disable=protected-access return DocumentStatusDetail._from_generated(generated_model) - model_conversion_function = kwargs.pop("cls", lambda doc_statuses: [_convert_from_generated_model(doc_status) for doc_status in doc_statuses]) + model_conversion_function = kwargs.pop( + "cls", + lambda doc_statuses: [ + _convert_from_generated_model(doc_status) for doc_status in doc_statuses + ]) return self._client.document_translation.get_operation_documents_status( id = job_id, @@ -206,7 +223,10 @@ def get_document_status(self, job_id, document_id, **kwargs): :rtype: ~azure.ai.documenttranslation.DocumentStatusDetail """ - document_status = self._client.document_translation.get_document_status(job_id, document_id, **kwargs) + document_status = self._client.document_translation.get_document_status( + job_id, + document_id, + **kwargs) # pylint: disable=protected-access return DocumentStatusDetail._from_generated(document_status) From aab715f0a9f916f5f61570aadd4e9d819ea8431a Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 16:24:30 +0200 Subject: [PATCH 47/59] [linting] models --- .../azure/ai/documenttranslation/_models.py | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 002d28a3308d..22b1443266cc 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -4,6 +4,7 @@ # Licensed under the MIT License. # ------------------------------------ +# pylint: disable=unused-import from typing import Any, List import six from ._generated.models import ( @@ -12,9 +13,9 @@ DocumentFilter as _DocumentFilter, TargetInput as _TargetInput, Glossary as _Glossary -) +) -class TranslationGlossary(object): +class TranslationGlossary(object): # pylint: disable=useless-object-inheritance """Glossary / translation memory for the request. :param glossary_url: Required. Location of the glossary. @@ -47,19 +48,22 @@ def _to_generated(self): storage_source=self.storage_source ) + @staticmethod def _to_generated_unknown_type(glossary): if isinstance(glossary, TranslationGlossary): - return glossary._to_generated() - elif isinstance(glossary, six.string_types): + return glossary._to_generated() # pylint: disable=protected-access + if isinstance(glossary, six.string_types): return _Glossary( glossary_url=glossary, ) + return None + @staticmethod def _to_generated_list(glossaries): return [TranslationGlossary._to_generated_unknown_type(glossary) for glossary in glossaries] -class StorageTarget(object): +class StorageTarget(object): # pylint: disable=useless-object-inheritance """Destination for the finished translated documents. :param target_url: Required. Location of the folder / container with your documents. @@ -92,15 +96,15 @@ def _to_generated(self): category=self.category_id, language=self.language, storage_source=self.storage_source, - # pylint: disable=protected-access - glossaries=TranslationGlossary._to_generated_list(self.glossaries) + glossaries=TranslationGlossary._to_generated_list(self.glossaries) # pylint: disable=protected-access ) + @staticmethod def _to_generated_list(targets): return [ target._to_generated() for target in targets] -class BatchDocumentInput(object): +class BatchDocumentInput(object): # pylint: disable=useless-object-inheritance """Definition for the input batch translation request. :param source_url: Required. Location of the folder / container or single file with your @@ -147,17 +151,17 @@ def _to_generated(self): language=self.source_language, storage_source=self.storage_source ), - # pylint: disable=protected-access - targets=StorageTarget._to_generated_list(self.targets), + targets=StorageTarget._to_generated_list(self.targets), # pylint: disable=protected-access storage_type=self.storage_type ) + @staticmethod def _to_generated_list(batch_document_inputs): return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] -class JobStatusDetail(object): # pylint: disable=too-many-instance-attributes +class JobStatusDetail(object): # pylint: disable=useless-object-inheritance """Job status response. :ivar id: Required. Id of the job. @@ -209,8 +213,7 @@ def _from_generated(cls, batch_status_details): created_on=batch_status_details.created_date_time_utc, last_updated_on=batch_status_details.last_action_date_time_utc, status=batch_status_details.status, - # pylint: disable=protected-access - error=DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, + error=DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, # pylint: disable=protected-access documents_total_count=batch_status_details.summary.total, documents_failed_count=batch_status_details.summary.failed, documents_succeeded_count=batch_status_details.summary.success, @@ -221,7 +224,7 @@ def _from_generated(cls, batch_status_details): ) -class DocumentStatusDetail(object): +class DocumentStatusDetail(object): # pylint: disable=useless-object-inheritance """DocumentStatusDetail. :ivar url: Required. Location of the document or folder. @@ -271,15 +274,14 @@ def _from_generated(cls, doc_status): last_updated_on=doc_status.last_action_date_time_utc, status=doc_status.status, translate_to=doc_status.to, - # pylint: disable=protected-access - error=DocumentTranslationError._from_generated(doc_status.error) if doc_status.error else None, + error=DocumentTranslationError._from_generated(doc_status.error) if doc_status.error else None, # pylint: disable=protected-access translation_progress=doc_status.progress, id=doc_status.id, characters_charged=doc_status.character_charged ) -class DocumentTranslationError(object): +class DocumentTranslationError(object): # pylint: disable=useless-object-inheritance """This contains an outer error with error code, message, details, target and an inner error with more descriptive details. @@ -312,7 +314,7 @@ def _from_generated(cls, error): ) -class FileFormat(object): +class FileFormat(object): # pylint: disable=useless-object-inheritance """FileFormat. :ivar format: Name of the format. @@ -344,5 +346,6 @@ def _from_generated(cls, file_format): versions=file_format.versions ) + @staticmethod def _from_generated_list(file_formats): - return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) + return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) From 84e1448da1d97178d0e61d3a392ad2019cfd4a63 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 16:26:19 +0200 Subject: [PATCH 48/59] [bug fix] wait until done pipeline response --- .../azure/ai/documenttranslation/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 57095930bd06..84638578fb24 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -123,7 +123,7 @@ def wait_until_done(self, job_id, **kwargs): :rtype: JobStatusDetail """ - pipeline_response = self.get_job_status( + pipeline_response = self._client.document_translation.get_operation_status( job_id, cls=lambda pipeline_response, _, response_headers: pipeline_response ) From 4bea54e5eee7b0644342a69329504074b1715e34 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 17:59:38 +0200 Subject: [PATCH 49/59] [linting] more client linting --- .../azure/ai/documenttranslation/_client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 84638578fb24..acf17cd27552 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -11,7 +11,6 @@ from ._generated import BatchDocumentTranslationClient as _BatchDocumentTranslationClient from ._generated.models import BatchStatusDetail as _BatchStatusDetail from ._models import ( - BatchDocumentInput, JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, @@ -23,10 +22,10 @@ if TYPE_CHECKING: from azure.core.paging import ItemPaged from azure.core.credentials import AzureKeyCredential, TokenCredential - from ._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat + from ._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat # pylint: disable=w0404 -class DocumentTranslationClient(object): +class DocumentTranslationClient(object): # pylint: disable=r0205 """DocumentTranslationClient """ From ea9482cb089365ff79309d69cfa4141ab1cdc8e1 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 18:03:33 +0200 Subject: [PATCH 50/59] [linting] more client --- .../azure/ai/documenttranslation/_client.py | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index acf17cd27552..0556a9c14232 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -22,7 +22,7 @@ if TYPE_CHECKING: from azure.core.paging import ItemPaged from azure.core.credentials import AzureKeyCredential, TokenCredential - from ._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat # pylint: disable=w0404 + from ._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat class DocumentTranslationClient(object): # pylint: disable=r0205 @@ -66,10 +66,8 @@ def create_translation_job(self, batch, **kwargs): """ # submit translation job - # pylint: disable=protected-access - response_headers = self._client.document_translation._submit_batch_request_initial( - # pylint: disable=protected-access - inputs = BatchDocumentInput._to_generated_list(batch), + response_headers = self._client.document_translation._submit_batch_request_initial( # pylint: disable=protected-access + inputs = BatchDocumentInput._to_generated_list(batch), # pylint: disable=protected-access cls = lambda pipeline_response, _, response_headers: response_headers, **kwargs ) @@ -96,8 +94,7 @@ def get_job_status(self, job_id, **kwargs): """ job_status = self._client.document_translation.get_operation_status(job_id, **kwargs) - # pylint: disable=protected-access - return JobStatusDetail._from_generated(job_status) + return JobStatusDetail._from_generated(job_status) # pylint: disable=protected-access @distributed_trace def cancel_job(self, job_id, **kwargs): @@ -128,14 +125,11 @@ def wait_until_done(self, job_id, **kwargs): ) def callback(raw_response): - # pylint: disable=protected-access - detail = self._client._deserialize(_BatchStatusDetail, raw_response) - # pylint: disable=protected-access - return JobStatusDetail._from_generated(detail) + detail = self._client._deserialize(_BatchStatusDetail, raw_response) # pylint: disable=protected-access + return JobStatusDetail._from_generated(detail) # pylint: disable=protected-access poller = LROPoller( - # pylint: disable=protected-access - client=self._client._client, + client=self._client._client, # pylint: disable=protected-access initial_response=pipeline_response, deserialization_callback=callback, polling_method=LROBasePolling( @@ -159,9 +153,8 @@ def list_submitted_jobs(self, **kwargs): skip = kwargs.pop('skip', None) results_per_page = kwargs.pop('results_per_page', None) - def _convert_from_generated_model(generated_model): - # pylint: disable=protected-access - return JobStatusDetail._from_generated(generated_model) + def _convert_from_generated_model(generated_model): # pylint: disable=protected-access + return JobStatusDetail._from_generated(generated_model) # pylint: disable=protected-access model_conversion_function = kwargs.pop( "cls", @@ -192,8 +185,7 @@ def list_documents_statuses(self, job_id, **kwargs): results_per_page = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): - # pylint: disable=protected-access - return DocumentStatusDetail._from_generated(generated_model) + return DocumentStatusDetail._from_generated(generated_model) # pylint: disable=protected-access model_conversion_function = kwargs.pop( "cls", @@ -226,8 +218,7 @@ def get_document_status(self, job_id, document_id, **kwargs): job_id, document_id, **kwargs) - # pylint: disable=protected-access - return DocumentStatusDetail._from_generated(document_status) + return DocumentStatusDetail._from_generated(document_status) # pylint: disable=protected-access @distributed_trace def get_supported_glossary_formats(self, **kwargs): @@ -238,8 +229,7 @@ def get_supported_glossary_formats(self, **kwargs): """ glossary_formats = self._client.document_translation.get_glossary_formats(**kwargs) - # pylint: disable=protected-access - return FileFormat._from_generated_list(glossary_formats.value) + return FileFormat._from_generated_list(glossary_formats.value) # pylint: disable=protected-access @distributed_trace def get_supported_document_formats(self, **kwargs): @@ -250,5 +240,4 @@ def get_supported_document_formats(self, **kwargs): """ document_formats = self._client.document_translation.get_document_formats(**kwargs) - # pylint: disable=protected-access - return FileFormat._from_generated_list(document_formats.value) + return FileFormat._from_generated_list(document_formats.value) # pylint: disable=protected-access From b1615003d3e9280f61a4b4149b87777f96417357 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 18:33:12 +0200 Subject: [PATCH 51/59] [linting] more in models --- .../azure/ai/documenttranslation/_models.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 22b1443266cc..875baa61ae52 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -105,6 +105,7 @@ def _to_generated_list(targets): class BatchDocumentInput(object): # pylint: disable=useless-object-inheritance + # pylint: disable=C0301 """Definition for the input batch translation request. :param source_url: Required. Location of the folder / container or single file with your @@ -157,7 +158,7 @@ def _to_generated(self): @staticmethod def _to_generated_list(batch_document_inputs): - return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] + return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] # pylint: disable=C0301 @@ -224,7 +225,7 @@ def _from_generated(cls, batch_status_details): ) -class DocumentStatusDetail(object): # pylint: disable=useless-object-inheritance +class DocumentStatusDetail(object): # pylint: disable=useless-object-inheritance, R0903 """DocumentStatusDetail. :ivar url: Required. Location of the document or folder. @@ -281,7 +282,7 @@ def _from_generated(cls, doc_status): ) -class DocumentTranslationError(object): # pylint: disable=useless-object-inheritance +class DocumentTranslationError(object): # pylint: disable=useless-object-inheritance, R0903 """This contains an outer error with error code, message, details, target and an inner error with more descriptive details. @@ -314,7 +315,7 @@ def _from_generated(cls, error): ) -class FileFormat(object): # pylint: disable=useless-object-inheritance +class FileFormat(object): # pylint: disable=useless-object-inheritance, R0903 """FileFormat. :ivar format: Name of the format. @@ -348,4 +349,4 @@ def _from_generated(cls, file_format): @staticmethod def _from_generated_list(file_formats): - return list( [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] ) + return [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] From 8c27992bb3cab27cca97bd999a131c6342223b36 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Wed, 17 Mar 2021 19:34:19 +0200 Subject: [PATCH 52/59] more linting --- .../azure/ai/documenttranslation/_client.py | 1 - .../azure/ai/documenttranslation/_models.py | 88 +++++++++---------- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 0556a9c14232..cb55ce5dd6ca 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -22,7 +22,6 @@ if TYPE_CHECKING: from azure.core.paging import ItemPaged from azure.core.credentials import AzureKeyCredential, TokenCredential - from ._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat class DocumentTranslationClient(object): # pylint: disable=r0205 diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 875baa61ae52..2ca4c33c8180 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -35,10 +35,10 @@ def __init__( **kwargs ): # type: (str, **Any) -> None - self.glossary_url=glossary_url - self.format=kwargs.get("format", None) - self.format_version=kwargs.get("format_version", None) - self.storage_source=kwargs.get("storage_source", None) + self.glossary_url = glossary_url + self.format = kwargs.get("format", None) + self.format_version = kwargs.get("format_version", None) + self.storage_source = kwargs.get("storage_source", None) def _to_generated(self): return _Glossary( @@ -84,11 +84,11 @@ def __init__( **kwargs ): # type: (str, str, **Any) -> None - self.target_url=target_url - self.language=language - self.category_id=kwargs.get("category_id", None) - self.glossaries=kwargs.get("glossaries", None) - self.storage_source=kwargs.get("storage_source", None) + self.target_url = target_url + self.language = language + self.category_id = kwargs.get("category_id", None) + self.glossaries = kwargs.get("glossaries", None) + self.storage_source = kwargs.get("storage_source", None) def _to_generated(self): return _TargetInput( @@ -133,13 +133,13 @@ def __init__( **kwargs ): # type: (str, List[StorageTarget], **Any) -> None - self.source_url=source_url - self.targets=targets - self.source_language=kwargs.get("source_language", None) - self.storage_type=kwargs.get("storage_type", None) - self.storage_source=kwargs.get("storage_source", None) - self.prefix=kwargs.get("prefix", None) - self.suffix=kwargs.get("suffix", None) + self.source_url = source_url + self.targets = targets + self.source_language = kwargs.get("source_language", None) + self.storage_type = kwargs.get("storage_type", None) + self.storage_source = kwargs.get("storage_source", None) + self.prefix = kwargs.get("prefix", None) + self.suffix = kwargs.get("suffix", None) def _to_generated(self): return _BatchRequest( @@ -194,18 +194,18 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.id=kwargs['id'] - self.created_on=kwargs['created_on'] - self.last_updated_on=kwargs['last_updated_on'] - self.status=kwargs.get('status', None) - self.error=kwargs.get("error", None) - self.documents_total_count=kwargs.get('documents_total_count', None) - self.documents_failed_count=kwargs.get('documents_failed_count', None) - self.documents_succeeded_count=kwargs.get('documents_succeeded_count', None) - self.documents_in_progress_count=kwargs.get('documents_in_progress_count', None) - self.documents_not_yet_started_count=kwargs.get('documents_not_yet_started_count', None) - self.documents_cancelled_count=kwargs.get('documents_cancelled_count', None) - self.total_characters_charged=kwargs.get('total_characters_charged', None) + self.id = kwargs['id'] + self.created_on = kwargs['created_on'] + self.last_updated_on = kwargs['last_updated_on'] + self.status = kwargs.get('status', None) + self.error = kwargs.get("error", None) + self.documents_total_count = kwargs.get('documents_total_count', None) + self.documents_failed_count = kwargs.get('documents_failed_count', None) + self.documents_succeeded_count = kwargs.get('documents_succeeded_count', None) + self.documents_in_progress_count = kwargs.get('documents_in_progress_count', None) + self.documents_not_yet_started_count = kwargs.get('documents_not_yet_started_count', None) + self.documents_cancelled_count = kwargs.get('documents_cancelled_count', None) + self.total_characters_charged = kwargs.get('total_characters_charged', None) @classmethod def _from_generated(cls, batch_status_details): @@ -256,15 +256,15 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.url=kwargs['url'] - self.created_on=kwargs['created_on'] - self.last_updated_on=kwargs['last_updated_on'] - self.status=kwargs['status'] - self.translate_to=kwargs['translate_to'] - self.error=kwargs.get('error', None) - self.translation_progress=kwargs.get('translation_progress', None) - self.id=kwargs.get('id', None) - self.characters_charged=kwargs.get('characters_charged', None) + self.url = kwargs['url'] + self.created_on = kwargs['created_on'] + self.last_updated_on = kwargs['last_updated_on'] + self.status = kwargs['status'] + self.translate_to = kwargs['translate_to'] + self.error = kwargs.get('error', None) + self.translation_progress = kwargs.get('translation_progress', None) + self.id = kwargs.get('id', None) + self.characters_charged = kwargs.get('characters_charged', None) @classmethod @@ -302,9 +302,9 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.code=kwargs.get('code', None) - self.message=None - self.target=None + self.code = kwargs.get('code', None) + self.message = None + self.target = None @classmethod def _from_generated(cls, error): @@ -333,10 +333,10 @@ def __init__( **kwargs ): # type: (**Any) -> None - self.format=kwargs.get('format', None) - self.file_extensions=kwargs.get('file_extensions', None) - self.content_types=kwargs.get('content_types', None) - self.versions=kwargs.get('versions', None) + self.format = kwargs.get('format', None) + self.file_extensions = kwargs.get('file_extensions', None) + self.content_types = kwargs.get('content_types', None) + self.versions = kwargs.get('versions', None) @classmethod def _from_generated(cls, file_format): From ba16f86e5fbbd1ab6b79308cafbd249f836d4c58 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 12:39:40 +0200 Subject: [PATCH 53/59] [linting] more linting things --- .../azure/ai/documenttranslation/_client.py | 18 +++++++++--------- .../azure/ai/documenttranslation/_models.py | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index cb55ce5dd6ca..094b310fee80 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -66,8 +66,8 @@ def create_translation_job(self, batch, **kwargs): # submit translation job response_headers = self._client.document_translation._submit_batch_request_initial( # pylint: disable=protected-access - inputs = BatchDocumentInput._to_generated_list(batch), # pylint: disable=protected-access - cls = lambda pipeline_response, _, response_headers: response_headers, + inputs=BatchDocumentInput._to_generated_list(batch), # pylint: disable=protected-access + cls=lambda pipeline_response, _, response_headers: response_headers, **kwargs ) @@ -162,9 +162,9 @@ def _convert_from_generated_model(generated_model): # pylint: disable=protected ]) return self._client.document_translation.get_operations( - top = results_per_page, - skip = skip, - cls = model_conversion_function, + top=results_per_page, + skip=skip, + cls=model_conversion_function, **kwargs ) @@ -193,10 +193,10 @@ def _convert_from_generated_model(generated_model): ]) return self._client.document_translation.get_operation_documents_status( - id = job_id, - top = results_per_page, - skip = skip, - cls = model_conversion_function, + id=job_id, + top=results_per_page, + skip=skip, + cls=model_conversion_function, **kwargs ) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 2ca4c33c8180..0e4a7837e156 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -101,7 +101,7 @@ def _to_generated(self): @staticmethod def _to_generated_list(targets): - return [ target._to_generated() for target in targets] + return [target._to_generated() for target in targets] class BatchDocumentInput(object): # pylint: disable=useless-object-inheritance @@ -158,7 +158,7 @@ def _to_generated(self): @staticmethod def _to_generated_list(batch_document_inputs): - return [ batch_document_input._to_generated() for batch_document_input in batch_document_inputs ] # pylint: disable=C0301 + return [batch_document_input._to_generated() for batch_document_input in batch_document_inputs] # pylint: disable=C0301 @@ -349,4 +349,4 @@ def _from_generated(cls, file_format): @staticmethod def _from_generated_list(file_formats): - return [ FileFormat._from_generated(file_formats) for file_formats in file_formats ] + return [FileFormat._from_generated(file_formats) for file_formats in file_formats] From ea59b055d1ca7a7832cda28b939574426f1738e4 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 14:17:27 +0200 Subject: [PATCH 54/59] [linting] client --- .../azure/ai/documenttranslation/_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py index 094b310fee80..54179455add4 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_client.py @@ -150,7 +150,7 @@ def list_submitted_jobs(self, **kwargs): """ skip = kwargs.pop('skip', None) - results_per_page = kwargs.pop('results_per_page', None) + results_per_page = kwargs.pop('results_per_page', None) def _convert_from_generated_model(generated_model): # pylint: disable=protected-access return JobStatusDetail._from_generated(generated_model) # pylint: disable=protected-access From 1e56b140730019c3f252d3e4c4bc656c494a10dd Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 17:10:32 +0200 Subject: [PATCH 55/59] [linting] more --- .../azure/ai/documenttranslation/_models.py | 6 +++--- .../azure/ai/documenttranslation/_polling.py | 15 ++++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 0e4a7837e156..78aa62191cd8 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -101,7 +101,7 @@ def _to_generated(self): @staticmethod def _to_generated_list(targets): - return [target._to_generated() for target in targets] + return [target._to_generated() for target in targets] # pylint: disable=protected-access class BatchDocumentInput(object): # pylint: disable=useless-object-inheritance @@ -158,7 +158,7 @@ def _to_generated(self): @staticmethod def _to_generated_list(batch_document_inputs): - return [batch_document_input._to_generated() for batch_document_input in batch_document_inputs] # pylint: disable=C0301 + return [batch_document_input._to_generated() for batch_document_input in batch_document_inputs] # pylint: disable=C0301, protected-access @@ -214,7 +214,7 @@ def _from_generated(cls, batch_status_details): created_on=batch_status_details.created_date_time_utc, last_updated_on=batch_status_details.last_action_date_time_utc, status=batch_status_details.status, - error=DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, # pylint: disable=protected-access + error=DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, # pylint: disable=protected-access, line-too-long documents_total_count=batch_status_details.summary.total, documents_failed_count=batch_status_details.summary.failed, documents_succeeded_count=batch_status_details.summary.success, diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py index 1bafdceba8c6..5fb312882c63 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -28,7 +28,8 @@ def can_poll(self, pipeline_response): if not _is_empty(response): body = _as_json(response) status = body.get("status") - return True if status else False + if status: + return True return False def get_polling_url(self): @@ -63,8 +64,7 @@ def get_status(self, pipeline_response): status = body.get("status") if status: return self._map_nonstandard_statuses(status) - else: - raise BadResponse("No status found in body") + raise BadResponse("No status found in body") raise BadResponse("The response from long running operation does not contain a body.") def get_final_get_url(self, pipeline_response): @@ -75,17 +75,14 @@ def get_final_get_url(self, pipeline_response): """ return None - def _map_nonstandard_statuses(status): + def _map_nonstandard_statuses(self, status): # type: (str) -> str """Map non-standard statuses. - all statuses here: https://docs.microsoft.com/en-us/rest/api/cognitiveservices/translator/documenttranslation/getoperationstatus#status :param str status: lro process status. """ if status in ["ValidationFailed"]: return "Failed" - elif status in ["Cancelled", "Cancelling"]: + if status in ["Cancelled", "Cancelling"]: return "Canceled" - return status - - + return status \ No newline at end of file From 95f7f27cac6c11adfacf693f26b02df0c4c5080a Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 17:34:06 +0200 Subject: [PATCH 56/59] [linting] more --- .../azure/ai/documenttranslation/_models.py | 2 +- .../azure/ai/documenttranslation/_polling.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 78aa62191cd8..a9ee1f87f96f 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -162,7 +162,7 @@ def _to_generated_list(batch_document_inputs): -class JobStatusDetail(object): # pylint: disable=useless-object-inheritance +class JobStatusDetail(object): # pylint: disable=useless-object-inheritance, too-many-instance-attributes """Job status response. :ivar id: Required. Id of the job. diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py index 5fb312882c63..a04ca71c6143 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -75,7 +75,7 @@ def get_final_get_url(self, pipeline_response): """ return None - def _map_nonstandard_statuses(self, status): + def _map_nonstandard_statuses(self, status): #pylint disable=no-self-use # type: (str) -> str """Map non-standard statuses. @@ -85,4 +85,4 @@ def _map_nonstandard_statuses(self, status): return "Failed" if status in ["Cancelled", "Cancelling"]: return "Canceled" - return status \ No newline at end of file + return status From fe31acbe58edbbf7eebf87dc4e1896b45fc70af8 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 17:52:00 +0200 Subject: [PATCH 57/59] [linting] for goodness sakes! --- .../azure/ai/documenttranslation/_polling.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py index a04ca71c6143..c07d92cfc192 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_polling.py @@ -75,7 +75,8 @@ def get_final_get_url(self, pipeline_response): """ return None - def _map_nonstandard_statuses(self, status): #pylint disable=no-self-use + # pylint: disable=R0201 + def _map_nonstandard_statuses(self, status): # type: (str) -> str """Map non-standard statuses. From 194372bc4863d3c0ebb4b3972f7037a0e077e933 Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 18:27:44 +0200 Subject: [PATCH 58/59] [bug fix] models -> StorageTarget -> handle when user doesn't pass glossaries --- .../azure/ai/documenttranslation/_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index a9ee1f87f96f..02234e4bceb3 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -96,7 +96,7 @@ def _to_generated(self): category=self.category_id, language=self.language, storage_source=self.storage_source, - glossaries=TranslationGlossary._to_generated_list(self.glossaries) # pylint: disable=protected-access + glossaries=TranslationGlossary._to_generated_list(self.glossaries) if self.glossaries else None # pylint: disable=protected-access, line-too-long ) @staticmethod From 58cf644e0c7a0aa0e398d9116b0620795a79771c Mon Sep 17 00:00:00 2001 From: "MIDDLEEAST\\v-moshaban" Date: Thu, 18 Mar 2021 18:50:23 +0200 Subject: [PATCH 59/59] [linting] line-too-long --- .../azure/ai/documenttranslation/_models.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py index 02234e4bceb3..50f6fcaefc93 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_models.py @@ -96,7 +96,8 @@ def _to_generated(self): category=self.category_id, language=self.language, storage_source=self.storage_source, - glossaries=TranslationGlossary._to_generated_list(self.glossaries) if self.glossaries else None # pylint: disable=protected-access, line-too-long + glossaries=TranslationGlossary._to_generated_list(self.glossaries) # pylint: disable=protected-access + if self.glossaries else None ) @staticmethod @@ -158,8 +159,10 @@ def _to_generated(self): @staticmethod def _to_generated_list(batch_document_inputs): - return [batch_document_input._to_generated() for batch_document_input in batch_document_inputs] # pylint: disable=C0301, protected-access - + return [ + batch_document_input._to_generated() # pylint: disable=protected-access + for batch_document_input in batch_document_inputs + ] class JobStatusDetail(object): # pylint: disable=useless-object-inheritance, too-many-instance-attributes @@ -214,7 +217,8 @@ def _from_generated(cls, batch_status_details): created_on=batch_status_details.created_date_time_utc, last_updated_on=batch_status_details.last_action_date_time_utc, status=batch_status_details.status, - error=DocumentTranslationError._from_generated(batch_status_details.error) if batch_status_details.error else None, # pylint: disable=protected-access, line-too-long + error=DocumentTranslationError._from_generated(batch_status_details.error) # pylint: disable=protected-access + if batch_status_details.error else None, documents_total_count=batch_status_details.summary.total, documents_failed_count=batch_status_details.summary.failed, documents_succeeded_count=batch_status_details.summary.success,