Skip to content

Commit 2b72fd0

Browse files
kristapraticoMohamed Shaban
and
Mohamed Shaban
authored
[translation] initial library (#16837)
* initial commit * update samples * updates from feedback * johans feedback * renaming to use job terminology * update samples - optional src language * 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 * remove 3.5 support Co-authored-by: Mohamed Shaban <[email protected]>
1 parent 4f18a58 commit 2b72fd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+5278
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Release History
2+
3+
## 1.0.0b1 (Unreleased)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
recursive-include tests *.py
2+
recursive-include samples *.py *.md
3+
include *.md
4+
include azure/__init__.py
5+
include azure/ai/__init__.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[![Build Status](https://dev.azure.com/azure-sdk/public/_apis/build/status/azure-sdk-for-python.client?branchName=master)](https://dev.azure.com/azure-sdk/public/_build/latest?definitionId=46?branchName=master)
2+
3+
# Azure Template Package client library for Python
4+
5+
This template package matches necessary patterns that the development team has established to create a unified sdk functional from Python 2.7 onwards. The packages contained herein can be installed singly or as part of the `azure` namespace. Any other introductory text should go here.
6+
7+
This package has been tested with Python 2.7, 3.5, 3.6, 3.7 and 3.8
8+
9+
For a more complete set of Azure libraries, see https://aka.ms/azsdk/python/all
10+
11+
# Getting started
12+
13+
For a rich example of a well formatted readme, please check [here.](https://github.com/Azure/azure-sdk/blob/master/docs/policies/README-TEMPLATE.md) In addition, this is an [example readme](https://github.com/Azure/azure-sdk/blob/master/docs/policies/README-EXAMPLE.md) that should be emulated. Note that the top-level sections in this template align with that of the [template.](https://github.com/Azure/azure-sdk/blob/master/docs/policies/README-TEMPLATE.md)
14+
15+
# Key concepts
16+
17+
Bullet point list of your library's main concepts.
18+
19+
# Examples
20+
21+
Examples of some of the key concepts for your library.
22+
23+
# Troubleshooting
24+
25+
Running into issues? This section should contain details as to what to do there.
26+
27+
# Next steps
28+
29+
More sample code should go here, along with links out to the appropriate example tests.
30+
31+
# Contributing
32+
33+
If you encounter any bugs or have suggestions, please file an issue in the [Issues](<https://github.com/Azure/azure-sdk-for-python/issues>) section of the project.
34+
35+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-python%2Fsdk%2Ftemplate%2Fazure-template%2FREADME.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# coding=utf-8
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
from ._version import VERSION
8+
from ._client import DocumentTranslationClient
9+
from ._generated.models import (
10+
StorageInputType,
11+
)
12+
from ._api_version import DocumentTranslationVersion
13+
from ._models import (
14+
StorageTarget,
15+
JobStatusDetail,
16+
DocumentStatusDetail,
17+
DocumentTranslationError,
18+
TranslationGlossary,
19+
BatchDocumentInput,
20+
FileFormat
21+
)
22+
23+
__VERSION__ = VERSION
24+
25+
26+
__all__ = [
27+
"DocumentTranslationClient",
28+
"DocumentTranslationVersion",
29+
"BatchDocumentInput",
30+
"TranslationGlossary",
31+
"StorageInputType",
32+
"FileFormat",
33+
"StorageTarget",
34+
"JobStatusDetail",
35+
"DocumentStatusDetail",
36+
"DocumentTranslationError",
37+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
from enum import Enum
7+
8+
9+
class DocumentTranslationVersion(str, Enum):
10+
"""Document Translation API versions supported by this package"""
11+
12+
#: This is the default version
13+
V1_0_PREVIEW = "1.0-preview.1"
14+
15+
16+
def validate_api_version(api_version):
17+
# type: (str) -> None
18+
"""Raise ValueError if api_version is invalid """
19+
if not api_version:
20+
return
21+
22+
try:
23+
api_version = DocumentTranslationVersion(api_version)
24+
except ValueError:
25+
raise ValueError(
26+
"Unsupported API version '{}'. Please select from:\n{}".format(
27+
api_version, ", ".join(v.value for v in DocumentTranslationVersion))
28+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# coding=utf-8
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
from typing import Union, Any, TYPE_CHECKING, List
8+
from azure.core.tracing.decorator import distributed_trace
9+
from ._generated import BatchDocumentTranslationClient as _BatchDocumentTranslationClient
10+
from ._helpers import get_authentication_policy
11+
from ._user_agent import USER_AGENT
12+
if TYPE_CHECKING:
13+
from azure.core.paging import ItemPaged
14+
from azure.core.credentials import AzureKeyCredential, TokenCredential
15+
from ._models import JobStatusDetail, DocumentStatusDetail, BatchDocumentInput, FileFormat
16+
17+
18+
class DocumentTranslationClient(object):
19+
"""DocumentTranslationClient
20+
21+
"""
22+
23+
def __init__(self, endpoint, credential, **kwargs):
24+
# type: (str, Union[AzureKeyCredential, TokenCredential], **Any) -> None
25+
"""
26+
27+
:param str endpoint:
28+
:param credential:
29+
:type credential: Union[AzureKeyCredential, TokenCredential]
30+
:keyword str api_version:
31+
"""
32+
self._endpoint = endpoint
33+
self._credential = credential
34+
self._api_version = kwargs.pop('api_version', None)
35+
36+
authentication_policy = get_authentication_policy(credential)
37+
self._client = _BatchDocumentTranslationClient(
38+
endpoint=endpoint,
39+
credential=credential, # type: ignore
40+
api_version=self._api_version,
41+
sdk_moniker=USER_AGENT,
42+
authentication_policy=authentication_policy,
43+
polling_interval=5, # TODO what is appropriate polling interval
44+
**kwargs
45+
)
46+
47+
@distributed_trace
48+
def create_translation_job(self, batch, **kwargs):
49+
# type: (List[BatchDocumentInput], **Any) -> JobStatusDetail
50+
"""
51+
52+
:param batch:
53+
:type batch: List[~azure.ai.documenttranslation.BatchDocumentInput]
54+
:return: JobStatusDetail
55+
:rtype: JobStatusDetail
56+
"""
57+
58+
return self._client.document_translation.begin_submit_batch_request(
59+
inputs=batch,
60+
polling=True,
61+
**kwargs
62+
)
63+
64+
@distributed_trace
65+
def get_job_status(self, job_id, **kwargs):
66+
# type: (str, **Any) -> JobStatusDetail
67+
"""
68+
69+
:param job_id: guid id for job
70+
:type job_id: str
71+
:rtype: ~azure.ai.documenttranslation.JobStatusDetail
72+
"""
73+
74+
return self._client.document_translation.get_operation_status(job_id, **kwargs)
75+
76+
@distributed_trace
77+
def cancel_job(self, job_id, **kwargs):
78+
# type: (str, **Any) -> None
79+
"""
80+
81+
:param job_id: guid id for job
82+
:type job_id: str
83+
:rtype: None
84+
"""
85+
86+
self._client.document_translation.cancel_operation(job_id, **kwargs)
87+
88+
@distributed_trace
89+
def wait_until_done(self, job_id, **kwargs):
90+
# type: (str, **Any) -> JobStatusDetail
91+
"""
92+
93+
:param job_id: guid id for job
94+
:type job_id: str
95+
:return: JobStatusDetail
96+
:rtype: JobStatusDetail
97+
"""
98+
pass
99+
100+
@distributed_trace
101+
def list_submitted_jobs(self, **kwargs):
102+
# type: (**Any) -> ItemPaged[JobStatusDetail]
103+
"""
104+
105+
:keyword int results_per_page:
106+
:keyword int skip:
107+
:rtype: ~azure.core.polling.ItemPaged[JobStatusDetail]
108+
"""
109+
return self._client.document_translation.get_operations(**kwargs)
110+
111+
@distributed_trace
112+
def list_documents_statuses(self, job_id, **kwargs):
113+
# type: (str, **Any) -> ItemPaged[DocumentStatusDetail]
114+
"""
115+
116+
:param job_id: guid id for job
117+
:type job_id: str
118+
:keyword int results_per_page:
119+
:keyword int skip:
120+
:rtype: ~azure.core.paging.ItemPaged[DocumentStatusDetail]
121+
"""
122+
123+
return self._client.document_translation.get_operation_documents_status(job_id, **kwargs)
124+
125+
@distributed_trace
126+
def get_document_status(self, job_id, document_id, **kwargs):
127+
# type: (str, str, **Any) -> DocumentStatusDetail
128+
"""
129+
130+
:param job_id: guid id for job
131+
:type job_id: str
132+
:param document_id: guid id for document
133+
:type document_id: str
134+
:rtype: ~azure.ai.documenttranslation.DocumentStatusDetail
135+
"""
136+
return self._client.document_translation.get_document_status(job_id, document_id, **kwargs)
137+
138+
@distributed_trace
139+
def get_supported_storage_sources(self, **kwargs):
140+
# type: (**Any) -> List[str]
141+
"""
142+
143+
:rtype: List[str]
144+
"""
145+
return self._client.document_translation.get_document_storage_source(**kwargs)
146+
147+
@distributed_trace
148+
def get_supported_glossary_formats(self, **kwargs):
149+
# type: (**Any) -> List[FileFormat]
150+
"""
151+
152+
:rtype: List[FileFormat]
153+
"""
154+
155+
return self._client.document_translation.get_glossary_formats(**kwargs)
156+
157+
@distributed_trace
158+
def get_supported_document_formats(self, **kwargs):
159+
# type: (**Any) -> List[FileFormat]
160+
"""
161+
162+
:rtype: List[FileFormat]
163+
"""
164+
165+
return self._client.document_translation.get_document_formats(**kwargs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# coding=utf-8
2+
# --------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for license information.
5+
# Code generated by Microsoft (R) AutoRest Code Generator.
6+
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
7+
# --------------------------------------------------------------------------
8+
9+
from ._batch_document_translation_client import BatchDocumentTranslationClient
10+
__all__ = ['BatchDocumentTranslationClient']
11+
12+
try:
13+
from ._patch import patch_sdk # type: ignore
14+
patch_sdk()
15+
except ImportError:
16+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# coding=utf-8
2+
# --------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for license information.
5+
# Code generated by Microsoft (R) AutoRest Code Generator.
6+
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
7+
# --------------------------------------------------------------------------
8+
9+
from typing import TYPE_CHECKING
10+
11+
from azure.core import PipelineClient
12+
from msrest import Deserializer, Serializer
13+
14+
if TYPE_CHECKING:
15+
# pylint: disable=unused-import,ungrouped-imports
16+
from typing import Any
17+
18+
from azure.core.credentials import TokenCredential
19+
20+
from ._configuration import BatchDocumentTranslationClientConfiguration
21+
from .operations import DocumentTranslationOperations
22+
from . import models
23+
24+
25+
class BatchDocumentTranslationClient(object):
26+
"""BatchDocumentTranslationClient.
27+
28+
:ivar document_translation: DocumentTranslationOperations operations
29+
:vartype document_translation: azure.ai.documenttranslation.operations.DocumentTranslationOperations
30+
:param credential: Credential needed for the client to connect to Azure.
31+
:type credential: ~azure.core.credentials.TokenCredential
32+
:param endpoint: Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com).
33+
:type endpoint: str
34+
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no Retry-After header is present.
35+
"""
36+
37+
def __init__(
38+
self,
39+
credential, # type: "TokenCredential"
40+
endpoint, # type: str
41+
**kwargs # type: Any
42+
):
43+
# type: (...) -> None
44+
base_url = '{endpoint}/translator/text/batch/v1.0-preview.1'
45+
self._config = BatchDocumentTranslationClientConfiguration(credential, endpoint, **kwargs)
46+
self._client = PipelineClient(base_url=base_url, config=self._config, **kwargs)
47+
48+
client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
49+
self._serialize = Serializer(client_models)
50+
self._serialize.client_side_validation = False
51+
self._deserialize = Deserializer(client_models)
52+
53+
self.document_translation = DocumentTranslationOperations(
54+
self._client, self._config, self._serialize, self._deserialize)
55+
56+
def close(self):
57+
# type: () -> None
58+
self._client.close()
59+
60+
def __enter__(self):
61+
# type: () -> BatchDocumentTranslationClient
62+
self._client.__enter__()
63+
return self
64+
65+
def __exit__(self, *exc_details):
66+
# type: (Any) -> None
67+
self._client.__exit__(*exc_details)

0 commit comments

Comments
 (0)