Skip to content

Commit b75d158

Browse files
author
Takashi Matsuo
authored
feat: incorporate upstream changes (#73)
* feat: Support MULAW audio encoding * feat: Support MP3_64_KBPS audio encoding * feat: Support timepointing via SSML tag * feat: Support quota_project_id * docs: generate and upload docfx yaml files * testing: add docs-presubmit build
1 parent 57ae7e1 commit b75d158

File tree

32 files changed

+1511
-389
lines changed

32 files changed

+1511
-389
lines changed

packages/google-cloud-texttospeech/.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# Generated by synthtool. DO NOT EDIT!
1818
[flake8]
19-
ignore = E203, E266, E501, W503, F401, F841
19+
ignore = E203, E266, E501, W503
2020
exclude =
2121
# Exclude generated code.
2222
**/proto/**

packages/google-cloud-texttospeech/.kokoro/docker/docs/fetch_gpg_keys.sh

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515

1616
# A script to fetch gpg keys with retry.
17+
# Avoid jinja parsing the file.
18+
#
1719

1820
function retry {
1921
if [[ "${#}" -le 1 ]]; then
@@ -39,3 +41,5 @@ retry 3 gpg --keyserver ha.pool.sks-keyservers.net --recv-keys \
3941
# 3.8.0 (Łukasz Langa)
4042
retry 3 gpg --keyserver ha.pool.sks-keyservers.net --recv-keys \
4143
E3FF2839C048B25C084DEBE9B26995E310250568
44+
45+
#

packages/google-cloud-texttospeech/.kokoro/docs/common.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ env_vars: {
1818
key: "TRAMPOLINE_IMAGE"
1919
value: "gcr.io/cloud-devrel-kokoro-resources/python-lib-docs"
2020
}
21-
2221
env_vars: {
2322
key: "TRAMPOLINE_BUILD_FILE"
2423
value: "github/python-texttospeech/.kokoro/publish-docs.sh"

packages/google-cloud-texttospeech/.kokoro/publish-docs.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ python3 -m docuploader create-metadata \
5656
--distribution-name=$(python3 setup.py --name) \
5757
--product-page=$(jq --raw-output '.product_documentation // empty' .repo-metadata.json) \
5858
--github-repository=$(jq --raw-output '.repo // empty' .repo-metadata.json) \
59-
--issue-tracker=$(jq --raw-output '.issue_tracker // empty' .repo-metadata.json) \
60-
--serving-path="/python/docs/reference/$(jq --raw-output '.name // empty' .repo-metadata.json)/$(jq --raw-output '.distribution_name // empty' .repo-metadata.json)/latest"
59+
--issue-tracker=$(jq --raw-output '.issue_tracker // empty' .repo-metadata.json)
6160

6261
cat docs.metadata
6362

packages/google-cloud-texttospeech/docs/_templates/layout.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
<div class="body" role="main">
2323
<div class="admonition" id="python2-eol">
24-
On January 1, 2020 this library will no longer support Python 2 on the latest released version.
25-
Previously released library versions will continue to be available. For more information please
24+
As of January 1, 2020 this library no longer supports Python 2 on the latest released version.
25+
Library versions released prior to that date will continue to be available. For more information please
2626
visit <a href="https://cloud.google.com/python/docs/python2-sunset/">Python 2 support on Google Cloud</a>.
2727
</div>
2828
{% block body %} {% endblock %}

packages/google-cloud-texttospeech/docs/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# For plugins that can not read conf.py.
2424
sys.path.insert(0, os.path.abspath("."))
2525

26-
2726
__version__ = ""
2827

2928
# -- General configuration ------------------------------------------------
@@ -341,7 +340,7 @@
341340
intersphinx_mapping = {
342341
"python": ("http://python.readthedocs.org/en/latest/", None),
343342
"google-auth": ("https://google-auth.readthedocs.io/en/stable", None),
344-
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None),
343+
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,),
345344
"grpc": ("https://grpc.io/grpc/python/", None),
346345
}
347346

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
from .client import TextToSpeechClient
1919
from .async_client import TextToSpeechAsyncClient
2020

21-
__all__ = ("TextToSpeechClient", "TextToSpeechAsyncClient")
21+
__all__ = (
22+
"TextToSpeechClient",
23+
"TextToSpeechAsyncClient",
24+
)

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/async_client.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(
8888
"""
8989

9090
self._client = TextToSpeechClient(
91-
credentials=credentials, transport=transport, client_options=client_options
91+
credentials=credentials, transport=transport, client_options=client_options,
9292
)
9393

9494
async def list_voices(
@@ -113,9 +113,9 @@ async def list_voices(
113113
only return voices that can be used to synthesize this
114114
language_code. E.g. when specifying "en-NZ", you will
115115
get supported "en-\*" voices; when specifying "no", you
116-
will get supported "no-\*" (Norwegian) and "nb-*"
116+
will get supported "no-\*" (Norwegian) and "nb-\*"
117117
(Norwegian Bokmal) voices; specifying "zh" will also get
118-
supported "cmn-*" voices; specifying "zh-hk" will also
118+
supported "cmn-\*" voices; specifying "zh-hk" will also
119119
get supported "yue-\*" voices.
120120
This corresponds to the ``language_code`` field
121121
on the ``request`` instance; if ``request`` is provided, this
@@ -159,7 +159,7 @@ async def list_voices(
159159
)
160160

161161
# Send the request.
162-
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata)
162+
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
163163

164164
# Done; return the response.
165165
return response
@@ -243,7 +243,7 @@ async def synthesize_speech(
243243
)
244244

245245
# Send the request.
246-
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata)
246+
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
247247

248248
# Done; return the response.
249249
return response
@@ -252,8 +252,8 @@ async def synthesize_speech(
252252
try:
253253
_client_info = gapic_v1.client_info.ClientInfo(
254254
gapic_version=pkg_resources.get_distribution(
255-
"google-cloud-texttospeech"
256-
).version
255+
"google-cloud-texttospeech",
256+
).version,
257257
)
258258
except pkg_resources.DistributionNotFound:
259259
_client_info = gapic_v1.client_info.ClientInfo()

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/client.py

+46-32
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TextToSpeechClientMeta(type):
4949
_transport_registry["grpc"] = TextToSpeechGrpcTransport
5050
_transport_registry["grpc_asyncio"] = TextToSpeechGrpcAsyncIOTransport
5151

52-
def get_transport_class(cls, label: str = None) -> Type[TextToSpeechTransport]:
52+
def get_transport_class(cls, label: str = None,) -> Type[TextToSpeechTransport]:
5353
"""Return an appropriate transport class.
5454
5555
Args:
@@ -192,19 +192,27 @@ def __init__(
192192
# instance provides an extensibility point for unusual situations.
193193
if isinstance(transport, TextToSpeechTransport):
194194
# transport is a TextToSpeechTransport instance.
195-
if credentials:
195+
if credentials or client_options.credentials_file:
196196
raise ValueError(
197197
"When providing a transport instance, "
198198
"provide its credentials directly."
199199
)
200+
if client_options.scopes:
201+
raise ValueError(
202+
"When providing a transport instance, "
203+
"provide its scopes directly."
204+
)
200205
self._transport = transport
201206
else:
202207
Transport = type(self).get_transport_class(transport)
203208
self._transport = Transport(
204209
credentials=credentials,
210+
credentials_file=client_options.credentials_file,
205211
host=client_options.api_endpoint,
212+
scopes=client_options.scopes,
206213
api_mtls_endpoint=client_options.api_endpoint,
207214
client_cert_source=client_options.client_cert_source,
215+
quota_project_id=client_options.quota_project_id,
208216
)
209217

210218
def list_voices(
@@ -229,9 +237,9 @@ def list_voices(
229237
only return voices that can be used to synthesize this
230238
language_code. E.g. when specifying "en-NZ", you will
231239
get supported "en-\*" voices; when specifying "no", you
232-
will get supported "no-\*" (Norwegian) and "nb-*"
240+
will get supported "no-\*" (Norwegian) and "nb-\*"
233241
(Norwegian Bokmal) voices; specifying "zh" will also get
234-
supported "cmn-*" voices; specifying "zh-hk" will also
242+
supported "cmn-\*" voices; specifying "zh-hk" will also
235243
get supported "yue-\*" voices.
236244
This corresponds to the ``language_code`` field
237245
on the ``request`` instance; if ``request`` is provided, this
@@ -252,28 +260,32 @@ def list_voices(
252260
# Create or coerce a protobuf request object.
253261
# Sanity check: If we got a request object, we should *not* have
254262
# gotten any keyword arguments that map to the request.
255-
if request is not None and any([language_code]):
263+
has_flattened_params = any([language_code])
264+
if request is not None and has_flattened_params:
256265
raise ValueError(
257266
"If the `request` argument is set, then none of "
258267
"the individual field arguments should be set."
259268
)
260269

261-
request = cloud_tts.ListVoicesRequest(request)
270+
# Minor optimization to avoid making a copy if the user passes
271+
# in a cloud_tts.ListVoicesRequest.
272+
# There's no risk of modifying the input as we've already verified
273+
# there are no flattened fields.
274+
if not isinstance(request, cloud_tts.ListVoicesRequest):
275+
request = cloud_tts.ListVoicesRequest(request)
262276

263-
# If we have keyword arguments corresponding to fields on the
264-
# request, apply these.
277+
# If we have keyword arguments corresponding to fields on the
278+
# request, apply these.
265279

266-
if language_code is not None:
267-
request.language_code = language_code
280+
if language_code is not None:
281+
request.language_code = language_code
268282

269283
# Wrap the RPC method; this adds retry and timeout information,
270284
# and friendly error handling.
271-
rpc = gapic_v1.method.wrap_method(
272-
self._transport.list_voices, default_timeout=None, client_info=_client_info
273-
)
285+
rpc = self._transport._wrapped_methods[self._transport.list_voices]
274286

275287
# Send the request.
276-
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata)
288+
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
277289

278290
# Done; return the response.
279291
return response
@@ -330,34 +342,36 @@ def synthesize_speech(
330342
# Create or coerce a protobuf request object.
331343
# Sanity check: If we got a request object, we should *not* have
332344
# gotten any keyword arguments that map to the request.
333-
if request is not None and any([input, voice, audio_config]):
345+
has_flattened_params = any([input, voice, audio_config])
346+
if request is not None and has_flattened_params:
334347
raise ValueError(
335348
"If the `request` argument is set, then none of "
336349
"the individual field arguments should be set."
337350
)
338351

339-
request = cloud_tts.SynthesizeSpeechRequest(request)
352+
# Minor optimization to avoid making a copy if the user passes
353+
# in a cloud_tts.SynthesizeSpeechRequest.
354+
# There's no risk of modifying the input as we've already verified
355+
# there are no flattened fields.
356+
if not isinstance(request, cloud_tts.SynthesizeSpeechRequest):
357+
request = cloud_tts.SynthesizeSpeechRequest(request)
340358

341-
# If we have keyword arguments corresponding to fields on the
342-
# request, apply these.
359+
# If we have keyword arguments corresponding to fields on the
360+
# request, apply these.
343361

344-
if input is not None:
345-
request.input = input
346-
if voice is not None:
347-
request.voice = voice
348-
if audio_config is not None:
349-
request.audio_config = audio_config
362+
if input is not None:
363+
request.input = input
364+
if voice is not None:
365+
request.voice = voice
366+
if audio_config is not None:
367+
request.audio_config = audio_config
350368

351369
# Wrap the RPC method; this adds retry and timeout information,
352370
# and friendly error handling.
353-
rpc = gapic_v1.method.wrap_method(
354-
self._transport.synthesize_speech,
355-
default_timeout=None,
356-
client_info=_client_info,
357-
)
371+
rpc = self._transport._wrapped_methods[self._transport.synthesize_speech]
358372

359373
# Send the request.
360-
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata)
374+
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
361375

362376
# Done; return the response.
363377
return response
@@ -366,8 +380,8 @@ def synthesize_speech(
366380
try:
367381
_client_info = gapic_v1.client_info.ClientInfo(
368382
gapic_version=pkg_resources.get_distribution(
369-
"google-cloud-texttospeech"
370-
).version
383+
"google-cloud-texttospeech",
384+
).version,
371385
)
372386
except pkg_resources.DistributionNotFound:
373387
_client_info = gapic_v1.client_info.ClientInfo()

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py

+53-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@
1717

1818
import abc
1919
import typing
20+
import pkg_resources
2021

2122
from google import auth
23+
from google.api_core import exceptions # type: ignore
24+
from google.api_core import gapic_v1 # type: ignore
25+
from google.api_core import retry as retries # type: ignore
2226
from google.auth import credentials # type: ignore
2327

2428
from google.cloud.texttospeech_v1.types import cloud_tts
2529

2630

31+
try:
32+
_client_info = gapic_v1.client_info.ClientInfo(
33+
gapic_version=pkg_resources.get_distribution(
34+
"google-cloud-texttospeech",
35+
).version,
36+
)
37+
except pkg_resources.DistributionNotFound:
38+
_client_info = gapic_v1.client_info.ClientInfo()
39+
40+
2741
class TextToSpeechTransport(abc.ABC):
2842
"""Abstract transport class for TextToSpeech."""
2943

@@ -34,6 +48,9 @@ def __init__(
3448
*,
3549
host: str = "texttospeech.googleapis.com",
3650
credentials: credentials.Credentials = None,
51+
credentials_file: typing.Optional[str] = None,
52+
scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES,
53+
quota_project_id: typing.Optional[str] = None,
3754
**kwargs,
3855
) -> None:
3956
"""Instantiate the transport.
@@ -45,6 +62,12 @@ def __init__(
4562
credentials identify the application to the service; if none
4663
are specified, the client will attempt to ascertain the
4764
credentials from the environment.
65+
credentials_file (Optional[str]): A file with credentials that can
66+
be loaded with :func:`google.auth.load_credentials_from_file`.
67+
This argument is mutually exclusive with credentials.
68+
scope (Optional[Sequence[str]]): A list of scopes.
69+
quota_project_id (Optional[str]): An optional project to use for billing
70+
and quota.
4871
"""
4972
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
5073
if ":" not in host:
@@ -53,15 +76,41 @@ def __init__(
5376

5477
# If no credentials are provided, then determine the appropriate
5578
# defaults.
56-
if credentials is None:
57-
credentials, _ = auth.default(scopes=self.AUTH_SCOPES)
79+
if credentials and credentials_file:
80+
raise exceptions.DuplicateCredentialArgs(
81+
"'credentials_file' and 'credentials' are mutually exclusive"
82+
)
83+
84+
if credentials_file is not None:
85+
credentials, _ = auth.load_credentials_from_file(
86+
credentials_file, scopes=scopes, quota_project_id=quota_project_id
87+
)
88+
89+
elif credentials is None:
90+
credentials, _ = auth.default(
91+
scopes=scopes, quota_project_id=quota_project_id
92+
)
5893

5994
# Save the credentials.
6095
self._credentials = credentials
6196

97+
# Lifted into its own function so it can be stubbed out during tests.
98+
self._prep_wrapped_messages()
99+
100+
def _prep_wrapped_messages(self):
101+
# Precompute the wrapped methods.
102+
self._wrapped_methods = {
103+
self.list_voices: gapic_v1.method.wrap_method(
104+
self.list_voices, default_timeout=None, client_info=_client_info,
105+
),
106+
self.synthesize_speech: gapic_v1.method.wrap_method(
107+
self.synthesize_speech, default_timeout=None, client_info=_client_info,
108+
),
109+
}
110+
62111
@property
63112
def list_voices(
64-
self
113+
self,
65114
) -> typing.Callable[
66115
[cloud_tts.ListVoicesRequest],
67116
typing.Union[
@@ -72,7 +121,7 @@ def list_voices(
72121

73122
@property
74123
def synthesize_speech(
75-
self
124+
self,
76125
) -> typing.Callable[
77126
[cloud_tts.SynthesizeSpeechRequest],
78127
typing.Union[

0 commit comments

Comments
 (0)