Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

feat: incorporate upstream changes #73

Merged
merged 4 commits into from Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Expand Up @@ -16,7 +16,7 @@

# Generated by synthtool. DO NOT EDIT!
[flake8]
ignore = E203, E266, E501, W503, F401, F841
ignore = E203, E266, E501, W503
exclude =
# Exclude generated code.
**/proto/**
Expand Down
4 changes: 4 additions & 0 deletions .kokoro/docker/docs/fetch_gpg_keys.sh
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.

# A script to fetch gpg keys with retry.
# Avoid jinja parsing the file.
#

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

#
1 change: 0 additions & 1 deletion .kokoro/docs/common.cfg
Expand Up @@ -18,7 +18,6 @@ env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-kokoro-resources/python-lib-docs"
}

env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/python-texttospeech/.kokoro/publish-docs.sh"
Expand Down
3 changes: 1 addition & 2 deletions .kokoro/publish-docs.sh
Expand Up @@ -56,8 +56,7 @@ python3 -m docuploader create-metadata \
--distribution-name=$(python3 setup.py --name) \
--product-page=$(jq --raw-output '.product_documentation // empty' .repo-metadata.json) \
--github-repository=$(jq --raw-output '.repo // empty' .repo-metadata.json) \
--issue-tracker=$(jq --raw-output '.issue_tracker // empty' .repo-metadata.json) \
--serving-path="/python/docs/reference/$(jq --raw-output '.name // empty' .repo-metadata.json)/$(jq --raw-output '.distribution_name // empty' .repo-metadata.json)/latest"
--issue-tracker=$(jq --raw-output '.issue_tracker // empty' .repo-metadata.json)

cat docs.metadata

Expand Down
4 changes: 2 additions & 2 deletions docs/_templates/layout.html
Expand Up @@ -21,8 +21,8 @@

<div class="body" role="main">
<div class="admonition" id="python2-eol">
On January 1, 2020 this library will no longer support Python 2 on the latest released version.
Previously released library versions will continue to be available. For more information please
As of January 1, 2020 this library no longer supports Python 2 on the latest released version.
Library versions released prior to that date will continue to be available. For more information please
visit <a href="https://cloud.google.com/python/docs/python2-sunset/">Python 2 support on Google Cloud</a>.
</div>
{% block body %} {% endblock %}
Expand Down
3 changes: 1 addition & 2 deletions docs/conf.py
Expand Up @@ -23,7 +23,6 @@
# For plugins that can not read conf.py.
sys.path.insert(0, os.path.abspath("."))


__version__ = ""

# -- General configuration ------------------------------------------------
Expand Down Expand Up @@ -341,7 +340,7 @@
intersphinx_mapping = {
"python": ("http://python.readthedocs.org/en/latest/", None),
"google-auth": ("https://google-auth.readthedocs.io/en/stable", None),
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None),
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,),
"grpc": ("https://grpc.io/grpc/python/", None),
}

Expand Down
Expand Up @@ -18,4 +18,7 @@
from .client import TextToSpeechClient
from .async_client import TextToSpeechAsyncClient

__all__ = ("TextToSpeechClient", "TextToSpeechAsyncClient")
__all__ = (
"TextToSpeechClient",
"TextToSpeechAsyncClient",
)
Expand Up @@ -88,7 +88,7 @@ def __init__(
"""

self._client = TextToSpeechClient(
credentials=credentials, transport=transport, client_options=client_options
credentials=credentials, transport=transport, client_options=client_options,
)

async def list_voices(
Expand All @@ -113,9 +113,9 @@ async def list_voices(
only return voices that can be used to synthesize this
language_code. E.g. when specifying "en-NZ", you will
get supported "en-\*" voices; when specifying "no", you
will get supported "no-\*" (Norwegian) and "nb-*"
will get supported "no-\*" (Norwegian) and "nb-\*"
(Norwegian Bokmal) voices; specifying "zh" will also get
supported "cmn-*" voices; specifying "zh-hk" will also
supported "cmn-\*" voices; specifying "zh-hk" will also
get supported "yue-\*" voices.
This corresponds to the ``language_code`` field
on the ``request`` instance; if ``request`` is provided, this
Expand Down Expand Up @@ -159,7 +159,7 @@ async def list_voices(
)

# Send the request.
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata)
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
Expand Down Expand Up @@ -243,7 +243,7 @@ async def synthesize_speech(
)

# Send the request.
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata)
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
Expand All @@ -252,8 +252,8 @@ async def synthesize_speech(
try:
_client_info = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech"
).version
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
Expand Down
78 changes: 46 additions & 32 deletions google/cloud/texttospeech_v1/services/text_to_speech/client.py
Expand Up @@ -49,7 +49,7 @@ class TextToSpeechClientMeta(type):
_transport_registry["grpc"] = TextToSpeechGrpcTransport
_transport_registry["grpc_asyncio"] = TextToSpeechGrpcAsyncIOTransport

def get_transport_class(cls, label: str = None) -> Type[TextToSpeechTransport]:
def get_transport_class(cls, label: str = None,) -> Type[TextToSpeechTransport]:
"""Return an appropriate transport class.

Args:
Expand Down Expand Up @@ -192,19 +192,27 @@ def __init__(
# instance provides an extensibility point for unusual situations.
if isinstance(transport, TextToSpeechTransport):
# transport is a TextToSpeechTransport instance.
if credentials:
if credentials or client_options.credentials_file:
raise ValueError(
"When providing a transport instance, "
"provide its credentials directly."
)
if client_options.scopes:
raise ValueError(
"When providing a transport instance, "
"provide its scopes directly."
)
self._transport = transport
else:
Transport = type(self).get_transport_class(transport)
self._transport = Transport(
credentials=credentials,
credentials_file=client_options.credentials_file,
host=client_options.api_endpoint,
scopes=client_options.scopes,
api_mtls_endpoint=client_options.api_endpoint,
client_cert_source=client_options.client_cert_source,
quota_project_id=client_options.quota_project_id,
)

def list_voices(
Expand All @@ -229,9 +237,9 @@ def list_voices(
only return voices that can be used to synthesize this
language_code. E.g. when specifying "en-NZ", you will
get supported "en-\*" voices; when specifying "no", you
will get supported "no-\*" (Norwegian) and "nb-*"
will get supported "no-\*" (Norwegian) and "nb-\*"
(Norwegian Bokmal) voices; specifying "zh" will also get
supported "cmn-*" voices; specifying "zh-hk" will also
supported "cmn-\*" voices; specifying "zh-hk" will also
get supported "yue-\*" voices.
This corresponds to the ``language_code`` field
on the ``request`` instance; if ``request`` is provided, this
Expand All @@ -252,28 +260,32 @@ def list_voices(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([language_code]):
has_flattened_params = any([language_code])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)

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

# If we have keyword arguments corresponding to fields on the
# request, apply these.
# If we have keyword arguments corresponding to fields on the
# request, apply these.

if language_code is not None:
request.language_code = language_code
if language_code is not None:
request.language_code = language_code

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method.wrap_method(
self._transport.list_voices, default_timeout=None, client_info=_client_info
)
rpc = self._transport._wrapped_methods[self._transport.list_voices]

# Send the request.
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata)
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

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

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

# If we have keyword arguments corresponding to fields on the
# request, apply these.
# If we have keyword arguments corresponding to fields on the
# request, apply these.

if input is not None:
request.input = input
if voice is not None:
request.voice = voice
if audio_config is not None:
request.audio_config = audio_config
if input is not None:
request.input = input
if voice is not None:
request.voice = voice
if audio_config is not None:
request.audio_config = audio_config

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method.wrap_method(
self._transport.synthesize_speech,
default_timeout=None,
client_info=_client_info,
)
rpc = self._transport._wrapped_methods[self._transport.synthesize_speech]

# Send the request.
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata)
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response
Expand All @@ -366,8 +380,8 @@ def synthesize_speech(
try:
_client_info = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech"
).version
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()
Expand Down
Expand Up @@ -17,13 +17,27 @@

import abc
import typing
import pkg_resources

from google import auth
from google.api_core import exceptions # type: ignore
from google.api_core import gapic_v1 # type: ignore
from google.api_core import retry as retries # type: ignore
from google.auth import credentials # type: ignore

from google.cloud.texttospeech_v1.types import cloud_tts


try:
_client_info = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"google-cloud-texttospeech",
).version,
)
except pkg_resources.DistributionNotFound:
_client_info = gapic_v1.client_info.ClientInfo()


class TextToSpeechTransport(abc.ABC):
"""Abstract transport class for TextToSpeech."""

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

# If no credentials are provided, then determine the appropriate
# defaults.
if credentials is None:
credentials, _ = auth.default(scopes=self.AUTH_SCOPES)
if credentials and credentials_file:
raise exceptions.DuplicateCredentialArgs(
"'credentials_file' and 'credentials' are mutually exclusive"
)

if credentials_file is not None:
credentials, _ = auth.load_credentials_from_file(
credentials_file, scopes=scopes, quota_project_id=quota_project_id
)

elif credentials is None:
credentials, _ = auth.default(
scopes=scopes, quota_project_id=quota_project_id
)

# Save the credentials.
self._credentials = credentials

# Lifted into its own function so it can be stubbed out during tests.
self._prep_wrapped_messages()

def _prep_wrapped_messages(self):
# Precompute the wrapped methods.
self._wrapped_methods = {
self.list_voices: gapic_v1.method.wrap_method(
self.list_voices, default_timeout=None, client_info=_client_info,
),
self.synthesize_speech: gapic_v1.method.wrap_method(
self.synthesize_speech, default_timeout=None, client_info=_client_info,
),
}

@property
def list_voices(
self
self,
) -> typing.Callable[
[cloud_tts.ListVoicesRequest],
typing.Union[
Expand All @@ -72,7 +121,7 @@ def list_voices(

@property
def synthesize_speech(
self
self,
) -> typing.Callable[
[cloud_tts.SynthesizeSpeechRequest],
typing.Union[
Expand Down