From 267ee84c02105c97d1f877a08009190e5d456740 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:42:33 -0800 Subject: [PATCH 01/26] changes without context autosynth cannot find the source of changes triggered by earlier changes in this repository, or by version upgrades to tools such as linters. --- docs/conf.py | 2 +- docs/texttospeech_v1/services.rst | 6 +- docs/texttospeech_v1/text_to_speech.rst | 6 + docs/texttospeech_v1/types.rst | 2 + docs/texttospeech_v1beta1/services.rst | 6 +- docs/texttospeech_v1beta1/text_to_speech.rst | 6 + docs/texttospeech_v1beta1/types.rst | 2 + .../services/text_to_speech/async_client.py | 90 ++- .../services/text_to_speech/client.py | 218 ++++-- .../text_to_speech/transports/__init__.py | 1 - .../text_to_speech/transports/base.py | 2 +- .../text_to_speech/transports/grpc.py | 74 +- .../text_to_speech/transports/grpc_asyncio.py | 63 +- .../cloud/texttospeech_v1/types/__init__.py | 5 +- .../cloud/texttospeech_v1/types/cloud_tts.py | 16 +- .../services/text_to_speech/async_client.py | 90 ++- .../services/text_to_speech/client.py | 218 ++++-- .../text_to_speech/transports/__init__.py | 1 - .../text_to_speech/transports/base.py | 2 +- .../text_to_speech/transports/grpc.py | 74 +- .../text_to_speech/transports/grpc_asyncio.py | 63 +- .../texttospeech_v1beta1/types/__init__.py | 5 +- .../texttospeech_v1beta1/types/cloud_tts.py | 24 +- noxfile.py | 4 +- scripts/fixup_texttospeech_v1_keywords.py | 3 +- .../fixup_texttospeech_v1beta1_keywords.py | 3 +- synth.metadata | 108 ++- .../texttospeech_v1/test_text_to_speech.py | 678 +++++++++++------- .../test_text_to_speech.py | 678 +++++++++++------- 29 files changed, 1667 insertions(+), 783 deletions(-) create mode 100644 docs/texttospeech_v1/text_to_speech.rst create mode 100644 docs/texttospeech_v1beta1/text_to_speech.rst diff --git a/docs/conf.py b/docs/conf.py index b30742a4..284c96c0 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -29,7 +29,7 @@ # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = "1.5.5" +needs_sphinx = "1.6.3" # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom diff --git a/docs/texttospeech_v1/services.rst b/docs/texttospeech_v1/services.rst index 23799bc0..a77f8167 100644 --- a/docs/texttospeech_v1/services.rst +++ b/docs/texttospeech_v1/services.rst @@ -1,6 +1,6 @@ Services for Google Cloud Texttospeech v1 API ============================================= +.. toctree:: + :maxdepth: 2 -.. automodule:: google.cloud.texttospeech_v1.services.text_to_speech - :members: - :inherited-members: + text_to_speech diff --git a/docs/texttospeech_v1/text_to_speech.rst b/docs/texttospeech_v1/text_to_speech.rst new file mode 100644 index 00000000..2ad3a8e9 --- /dev/null +++ b/docs/texttospeech_v1/text_to_speech.rst @@ -0,0 +1,6 @@ +TextToSpeech +------------------------------ + +.. automodule:: google.cloud.texttospeech_v1.services.text_to_speech + :members: + :inherited-members: diff --git a/docs/texttospeech_v1/types.rst b/docs/texttospeech_v1/types.rst index 626ff0d3..646ebdc9 100644 --- a/docs/texttospeech_v1/types.rst +++ b/docs/texttospeech_v1/types.rst @@ -3,3 +3,5 @@ Types for Google Cloud Texttospeech v1 API .. automodule:: google.cloud.texttospeech_v1.types :members: + :undoc-members: + :show-inheritance: diff --git a/docs/texttospeech_v1beta1/services.rst b/docs/texttospeech_v1beta1/services.rst index ddf8876a..67852d05 100644 --- a/docs/texttospeech_v1beta1/services.rst +++ b/docs/texttospeech_v1beta1/services.rst @@ -1,6 +1,6 @@ Services for Google Cloud Texttospeech v1beta1 API ================================================== +.. toctree:: + :maxdepth: 2 -.. automodule:: google.cloud.texttospeech_v1beta1.services.text_to_speech - :members: - :inherited-members: + text_to_speech diff --git a/docs/texttospeech_v1beta1/text_to_speech.rst b/docs/texttospeech_v1beta1/text_to_speech.rst new file mode 100644 index 00000000..8521daf8 --- /dev/null +++ b/docs/texttospeech_v1beta1/text_to_speech.rst @@ -0,0 +1,6 @@ +TextToSpeech +------------------------------ + +.. automodule:: google.cloud.texttospeech_v1beta1.services.text_to_speech + :members: + :inherited-members: diff --git a/docs/texttospeech_v1beta1/types.rst b/docs/texttospeech_v1beta1/types.rst index 84c63fc8..0306c6cd 100644 --- a/docs/texttospeech_v1beta1/types.rst +++ b/docs/texttospeech_v1beta1/types.rst @@ -3,3 +3,5 @@ Types for Google Cloud Texttospeech v1beta1 API .. automodule:: google.cloud.texttospeech_v1beta1.types :members: + :undoc-members: + :show-inheritance: diff --git a/google/cloud/texttospeech_v1/services/text_to_speech/async_client.py b/google/cloud/texttospeech_v1/services/text_to_speech/async_client.py index 3d94944e..f1f9ace1 100644 --- a/google/cloud/texttospeech_v1/services/text_to_speech/async_client.py +++ b/google/cloud/texttospeech_v1/services/text_to_speech/async_client.py @@ -43,9 +43,44 @@ class TextToSpeechAsyncClient: DEFAULT_ENDPOINT = TextToSpeechClient.DEFAULT_ENDPOINT DEFAULT_MTLS_ENDPOINT = TextToSpeechClient.DEFAULT_MTLS_ENDPOINT + common_billing_account_path = staticmethod( + TextToSpeechClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + TextToSpeechClient.parse_common_billing_account_path + ) + + common_folder_path = staticmethod(TextToSpeechClient.common_folder_path) + parse_common_folder_path = staticmethod(TextToSpeechClient.parse_common_folder_path) + + common_organization_path = staticmethod(TextToSpeechClient.common_organization_path) + parse_common_organization_path = staticmethod( + TextToSpeechClient.parse_common_organization_path + ) + + common_project_path = staticmethod(TextToSpeechClient.common_project_path) + parse_common_project_path = staticmethod( + TextToSpeechClient.parse_common_project_path + ) + + common_location_path = staticmethod(TextToSpeechClient.common_location_path) + parse_common_location_path = staticmethod( + TextToSpeechClient.parse_common_location_path + ) + + from_service_account_info = TextToSpeechClient.from_service_account_info from_service_account_file = TextToSpeechClient.from_service_account_file from_service_account_json = from_service_account_file + @property + def transport(self) -> TextToSpeechTransport: + """Return the transport used by the client instance. + + Returns: + TextToSpeechTransport: The transport used by the client instance. + """ + return self._client.transport + get_transport_class = functools.partial( type(TextToSpeechClient).get_transport_class, type(TextToSpeechClient) ) @@ -72,16 +107,19 @@ def __init__( client_options (ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -107,7 +145,7 @@ async def list_voices( r"""Returns a list of Voice supported for synthesis. Args: - request (:class:`~.cloud_tts.ListVoicesRequest`): + request (:class:`google.cloud.texttospeech_v1.types.ListVoicesRequest`): The request object. The top-level message sent by the client for the `ListVoices` method. language_code (:class:`str`): @@ -121,6 +159,7 @@ async def list_voices( (Norwegian Bokmal) voices; specifying "zh" will also get 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 should not be set. @@ -132,15 +171,16 @@ async def list_voices( sent along with the request as metadata. Returns: - ~.cloud_tts.ListVoicesResponse: - The message returned to the client by the ``ListVoices`` + google.cloud.texttospeech_v1.types.ListVoicesResponse: + The message returned to the client by the ListVoices method. """ # 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." @@ -172,7 +212,7 @@ async def synthesize_speech( self, request: cloud_tts.SynthesizeSpeechRequest = None, *, - input: cloud_tts.SynthesisInput = None, + input_: cloud_tts.SynthesisInput = None, voice: cloud_tts.VoiceSelectionParams = None, audio_config: cloud_tts.AudioConfig = None, retry: retries.Retry = gapic_v1.method.DEFAULT, @@ -183,24 +223,27 @@ async def synthesize_speech( after all text input has been processed. Args: - request (:class:`~.cloud_tts.SynthesizeSpeechRequest`): + request (:class:`google.cloud.texttospeech_v1.types.SynthesizeSpeechRequest`): The request object. The top-level message sent by the client for the `SynthesizeSpeech` method. - input (:class:`~.cloud_tts.SynthesisInput`): + input_ (:class:`google.cloud.texttospeech_v1.types.SynthesisInput`): Required. The Synthesizer requires either plain text or SSML as input. - This corresponds to the ``input`` field + + This corresponds to the ``input_`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - voice (:class:`~.cloud_tts.VoiceSelectionParams`): + voice (:class:`google.cloud.texttospeech_v1.types.VoiceSelectionParams`): Required. The desired voice of the synthesized audio. + This corresponds to the ``voice`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - audio_config (:class:`~.cloud_tts.AudioConfig`): + audio_config (:class:`google.cloud.texttospeech_v1.types.AudioConfig`): Required. The configuration of the synthesized audio. + This corresponds to the ``audio_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -212,15 +255,16 @@ async def synthesize_speech( sent along with the request as metadata. Returns: - ~.cloud_tts.SynthesizeSpeechResponse: + google.cloud.texttospeech_v1.types.SynthesizeSpeechResponse: The message returned to the client by the - ``SynthesizeSpeech`` method. + SynthesizeSpeech method. """ # 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." @@ -231,8 +275,8 @@ async def synthesize_speech( # If we have keyword arguments corresponding to fields on the # request, apply these. - if input is not None: - request.input = input + if input_ is not None: + request.input_ = input_ if voice is not None: request.voice = voice if audio_config is not None: diff --git a/google/cloud/texttospeech_v1/services/text_to_speech/client.py b/google/cloud/texttospeech_v1/services/text_to_speech/client.py index c365b0fa..8e73aeec 100644 --- a/google/cloud/texttospeech_v1/services/text_to_speech/client.py +++ b/google/cloud/texttospeech_v1/services/text_to_speech/client.py @@ -16,17 +16,19 @@ # from collections import OrderedDict +from distutils import util import os import re -from typing import Callable, Dict, Sequence, Tuple, Type, Union +from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union import pkg_resources -import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import client_options as client_options_lib # type: ignore 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.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore @@ -105,6 +107,22 @@ def _get_default_mtls_endpoint(api_endpoint): DEFAULT_ENDPOINT ) + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + TextToSpeechClient: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_info(info) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + @classmethod def from_service_account_file(cls, filename: str, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -117,7 +135,7 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): kwargs: Additional arguments to pass to the constructor. Returns: - {@api.name}: The constructed client. + TextToSpeechClient: The constructed client. """ credentials = service_account.Credentials.from_service_account_file(filename) kwargs["credentials"] = credentials @@ -125,12 +143,80 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): from_service_account_json = from_service_account_file + @property + def transport(self) -> TextToSpeechTransport: + """Return the transport used by the client instance. + + Returns: + TextToSpeechTransport: The transport used by the client instance. + """ + return self._transport + + @staticmethod + def common_billing_account_path(billing_account: str,) -> str: + """Return a fully-qualified billing_account string.""" + return "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + + @staticmethod + def parse_common_billing_account_path(path: str) -> Dict[str, str]: + """Parse a billing_account path into its component segments.""" + m = re.match(r"^billingAccounts/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_folder_path(folder: str,) -> str: + """Return a fully-qualified folder string.""" + return "folders/{folder}".format(folder=folder,) + + @staticmethod + def parse_common_folder_path(path: str) -> Dict[str, str]: + """Parse a folder path into its component segments.""" + m = re.match(r"^folders/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_organization_path(organization: str,) -> str: + """Return a fully-qualified organization string.""" + return "organizations/{organization}".format(organization=organization,) + + @staticmethod + def parse_common_organization_path(path: str) -> Dict[str, str]: + """Parse a organization path into its component segments.""" + m = re.match(r"^organizations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_project_path(project: str,) -> str: + """Return a fully-qualified project string.""" + return "projects/{project}".format(project=project,) + + @staticmethod + def parse_common_project_path(path: str) -> Dict[str, str]: + """Parse a project path into its component segments.""" + m = re.match(r"^projects/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_location_path(project: str, location: str,) -> str: + """Return a fully-qualified location string.""" + return "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + + @staticmethod + def parse_common_location_path(path: str) -> Dict[str, str]: + """Parse a location path into its component segments.""" + m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) + return m.groupdict() if m else {} + def __init__( self, *, - credentials: credentials.Credentials = None, - transport: Union[str, TextToSpeechTransport] = None, - client_options: ClientOptions = None, + credentials: Optional[credentials.Credentials] = None, + transport: Union[str, TextToSpeechTransport, None] = None, + client_options: Optional[client_options_lib.ClientOptions] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the text to speech client. @@ -141,26 +227,29 @@ def __init__( credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. - transport (Union[str, ~.TextToSpeechTransport]): The + transport (Union[str, TextToSpeechTransport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (ClientOptions): Custom options for the client. It - won't take effect if a ``transport`` instance is provided. + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -168,29 +257,47 @@ def __init__( creation failed for any reason. """ if isinstance(client_options, dict): - client_options = ClientOptions.from_dict(client_options) + client_options = client_options_lib.from_dict(client_options) if client_options is None: - client_options = ClientOptions.ClientOptions() + client_options = client_options_lib.ClientOptions() - if client_options.api_endpoint is None: - use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never") + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) + + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_env == "never": - client_options.api_endpoint = self.DEFAULT_ENDPOINT + api_endpoint = self.DEFAULT_ENDPOINT elif use_mtls_env == "always": - client_options.api_endpoint = self.DEFAULT_MTLS_ENDPOINT + api_endpoint = self.DEFAULT_MTLS_ENDPOINT elif use_mtls_env == "auto": - has_client_cert_source = ( - client_options.client_cert_source is not None - or mtls.has_default_client_cert_source() - ) - client_options.api_endpoint = ( - self.DEFAULT_MTLS_ENDPOINT - if has_client_cert_source - else self.DEFAULT_ENDPOINT + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT ) else: raise MutualTLSChannelError( - "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always" + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" ) # Save or instantiate the transport. @@ -214,10 +321,9 @@ def __init__( self._transport = Transport( credentials=credentials, credentials_file=client_options.credentials_file, - host=client_options.api_endpoint, + host=api_endpoint, scopes=client_options.scopes, - api_mtls_endpoint=client_options.api_endpoint, - client_cert_source=client_options.client_cert_source, + ssl_channel_credentials=ssl_credentials, quota_project_id=client_options.quota_project_id, client_info=client_info, ) @@ -234,10 +340,10 @@ def list_voices( r"""Returns a list of Voice supported for synthesis. Args: - request (:class:`~.cloud_tts.ListVoicesRequest`): + request (google.cloud.texttospeech_v1.types.ListVoicesRequest): The request object. The top-level message sent by the client for the `ListVoices` method. - language_code (:class:`str`): + language_code (str): Optional. Recommended. `BCP-47 `__ language tag. If specified, the ListVoices call will @@ -248,6 +354,7 @@ def list_voices( (Norwegian Bokmal) voices; specifying "zh" will also get 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 should not be set. @@ -259,8 +366,8 @@ def list_voices( sent along with the request as metadata. Returns: - ~.cloud_tts.ListVoicesResponse: - The message returned to the client by the ``ListVoices`` + google.cloud.texttospeech_v1.types.ListVoicesResponse: + The message returned to the client by the ListVoices method. """ @@ -301,7 +408,7 @@ def synthesize_speech( self, request: cloud_tts.SynthesizeSpeechRequest = None, *, - input: cloud_tts.SynthesisInput = None, + input_: cloud_tts.SynthesisInput = None, voice: cloud_tts.VoiceSelectionParams = None, audio_config: cloud_tts.AudioConfig = None, retry: retries.Retry = gapic_v1.method.DEFAULT, @@ -312,24 +419,27 @@ def synthesize_speech( after all text input has been processed. Args: - request (:class:`~.cloud_tts.SynthesizeSpeechRequest`): + request (google.cloud.texttospeech_v1.types.SynthesizeSpeechRequest): The request object. The top-level message sent by the client for the `SynthesizeSpeech` method. - input (:class:`~.cloud_tts.SynthesisInput`): + input_ (google.cloud.texttospeech_v1.types.SynthesisInput): Required. The Synthesizer requires either plain text or SSML as input. - This corresponds to the ``input`` field + + This corresponds to the ``input_`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - voice (:class:`~.cloud_tts.VoiceSelectionParams`): + voice (google.cloud.texttospeech_v1.types.VoiceSelectionParams): Required. The desired voice of the synthesized audio. + This corresponds to the ``voice`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - audio_config (:class:`~.cloud_tts.AudioConfig`): + audio_config (google.cloud.texttospeech_v1.types.AudioConfig): Required. The configuration of the synthesized audio. + This corresponds to the ``audio_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -341,15 +451,15 @@ def synthesize_speech( sent along with the request as metadata. Returns: - ~.cloud_tts.SynthesizeSpeechResponse: + google.cloud.texttospeech_v1.types.SynthesizeSpeechResponse: The message returned to the client by the - ``SynthesizeSpeech`` method. + SynthesizeSpeech method. """ # 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. - has_flattened_params = 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 " @@ -366,8 +476,8 @@ def synthesize_speech( # If we have keyword arguments corresponding to fields on the # request, apply these. - if input is not None: - request.input = input + if input_ is not None: + request.input_ = input_ if voice is not None: request.voice = voice if audio_config is not None: diff --git a/google/cloud/texttospeech_v1/services/text_to_speech/transports/__init__.py b/google/cloud/texttospeech_v1/services/text_to_speech/transports/__init__.py index 3a92efc9..ce3b91f1 100644 --- a/google/cloud/texttospeech_v1/services/text_to_speech/transports/__init__.py +++ b/google/cloud/texttospeech_v1/services/text_to_speech/transports/__init__.py @@ -28,7 +28,6 @@ _transport_registry["grpc"] = TextToSpeechGrpcTransport _transport_registry["grpc_asyncio"] = TextToSpeechGrpcAsyncIOTransport - __all__ = ( "TextToSpeechTransport", "TextToSpeechGrpcTransport", diff --git a/google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py b/google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py index d891cc63..8643ea1a 100644 --- a/google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py +++ b/google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py @@ -19,7 +19,7 @@ import typing import pkg_resources -from google import auth +from google import auth # type: ignore 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 diff --git a/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py b/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py index c44e247a..d9cbe474 100644 --- a/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py +++ b/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py @@ -15,6 +15,7 @@ # limitations under the License. # +import warnings from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import grpc_helpers # type: ignore @@ -23,7 +24,6 @@ from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore - import grpc # type: ignore from google.cloud.texttospeech_v1.types import cloud_tts @@ -56,6 +56,7 @@ def __init__( channel: grpc.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -76,20 +77,22 @@ def __init__( ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -98,6 +101,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -105,7 +110,13 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint @@ -135,6 +146,32 @@ def __init__( ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) self._stubs = {} # type: Dict[str, Callable] @@ -161,7 +198,7 @@ def create_channel( ) -> grpc.Channel: """Create and return a gRPC channel object. Args: - address (Optionsl[str]): The host for the channel to use. + address (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If @@ -196,19 +233,8 @@ def create_channel( @property def grpc_channel(self) -> grpc.Channel: - """Create the channel designed to connect to this service. - - This property caches on the instance; repeated calls return - the same channel. + """Return the channel designed to connect to this service. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - - # Return the channel from cache. return self._grpc_channel @property diff --git a/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py b/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py index 4105e365..0d1009af 100644 --- a/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py +++ b/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py @@ -15,10 +15,12 @@ # limitations under the License. # +import warnings from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple from google.api_core import gapic_v1 # type: ignore from google.api_core import grpc_helpers_async # type: ignore +from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -98,6 +100,7 @@ def __init__( channel: aio.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, quota_project_id=None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -119,14 +122,16 @@ def __init__( are passed to :func:`google.auth.default`. channel (Optional[aio.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): @@ -141,6 +146,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -148,13 +155,24 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443" ) + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: @@ -173,6 +191,32 @@ def __init__( ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) # Run the base constructor. @@ -194,13 +238,6 @@ def grpc_channel(self) -> aio.Channel: This property caches on the instance; repeated calls return the same channel. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - # Return the channel from cache. return self._grpc_channel diff --git a/google/cloud/texttospeech_v1/types/__init__.py b/google/cloud/texttospeech_v1/types/__init__.py index 47b20ad9..dcc97182 100644 --- a/google/cloud/texttospeech_v1/types/__init__.py +++ b/google/cloud/texttospeech_v1/types/__init__.py @@ -24,9 +24,10 @@ VoiceSelectionParams, AudioConfig, SynthesizeSpeechResponse, + SsmlVoiceGender, + AudioEncoding, ) - __all__ = ( "ListVoicesRequest", "ListVoicesResponse", @@ -36,4 +37,6 @@ "VoiceSelectionParams", "AudioConfig", "SynthesizeSpeechResponse", + "SsmlVoiceGender", + "AudioEncoding", ) diff --git a/google/cloud/texttospeech_v1/types/cloud_tts.py b/google/cloud/texttospeech_v1/types/cloud_tts.py index 602ffa03..78222ab9 100644 --- a/google/cloud/texttospeech_v1/types/cloud_tts.py +++ b/google/cloud/texttospeech_v1/types/cloud_tts.py @@ -80,7 +80,7 @@ class ListVoicesResponse(proto.Message): r"""The message returned to the client by the ``ListVoices`` method. Attributes: - voices (Sequence[~.cloud_tts.Voice]): + voices (Sequence[google.cloud.texttospeech_v1.types.Voice]): The list of voices. """ @@ -98,7 +98,7 @@ class Voice(proto.Message): name (str): The name of this voice. Each distinct voice has a unique name. - ssml_gender (~.cloud_tts.SsmlVoiceGender): + ssml_gender (google.cloud.texttospeech_v1.types.SsmlVoiceGender): The gender of this voice. natural_sample_rate_hertz (int): The natural sample rate (in hertz) for this @@ -119,18 +119,18 @@ class SynthesizeSpeechRequest(proto.Message): ``SynthesizeSpeech`` method. Attributes: - input (~.cloud_tts.SynthesisInput): + input_ (google.cloud.texttospeech_v1.types.SynthesisInput): Required. The Synthesizer requires either plain text or SSML as input. - voice (~.cloud_tts.VoiceSelectionParams): + voice (google.cloud.texttospeech_v1.types.VoiceSelectionParams): Required. The desired voice of the synthesized audio. - audio_config (~.cloud_tts.AudioConfig): + audio_config (google.cloud.texttospeech_v1.types.AudioConfig): Required. The configuration of the synthesized audio. """ - input = proto.Field(proto.MESSAGE, number=1, message="SynthesisInput",) + input_ = proto.Field(proto.MESSAGE, number=1, message="SynthesisInput",) voice = proto.Field(proto.MESSAGE, number=2, message="VoiceSelectionParams",) @@ -182,7 +182,7 @@ class VoiceSelectionParams(proto.Message): The name of the voice. If not set, the service will choose a voice based on the other parameters such as language_code and gender. - ssml_gender (~.cloud_tts.SsmlVoiceGender): + ssml_gender (google.cloud.texttospeech_v1.types.SsmlVoiceGender): The preferred gender of the voice. If not set, the service will choose a voice based on the other parameters such as language_code and name. Note that this is only a preference, @@ -202,7 +202,7 @@ class AudioConfig(proto.Message): r"""Description of audio data to be synthesized. Attributes: - audio_encoding (~.cloud_tts.AudioEncoding): + audio_encoding (google.cloud.texttospeech_v1.types.AudioEncoding): Required. The format of the audio byte stream. speaking_rate (float): diff --git a/google/cloud/texttospeech_v1beta1/services/text_to_speech/async_client.py b/google/cloud/texttospeech_v1beta1/services/text_to_speech/async_client.py index 4957ceda..bed03297 100644 --- a/google/cloud/texttospeech_v1beta1/services/text_to_speech/async_client.py +++ b/google/cloud/texttospeech_v1beta1/services/text_to_speech/async_client.py @@ -43,9 +43,44 @@ class TextToSpeechAsyncClient: DEFAULT_ENDPOINT = TextToSpeechClient.DEFAULT_ENDPOINT DEFAULT_MTLS_ENDPOINT = TextToSpeechClient.DEFAULT_MTLS_ENDPOINT + common_billing_account_path = staticmethod( + TextToSpeechClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + TextToSpeechClient.parse_common_billing_account_path + ) + + common_folder_path = staticmethod(TextToSpeechClient.common_folder_path) + parse_common_folder_path = staticmethod(TextToSpeechClient.parse_common_folder_path) + + common_organization_path = staticmethod(TextToSpeechClient.common_organization_path) + parse_common_organization_path = staticmethod( + TextToSpeechClient.parse_common_organization_path + ) + + common_project_path = staticmethod(TextToSpeechClient.common_project_path) + parse_common_project_path = staticmethod( + TextToSpeechClient.parse_common_project_path + ) + + common_location_path = staticmethod(TextToSpeechClient.common_location_path) + parse_common_location_path = staticmethod( + TextToSpeechClient.parse_common_location_path + ) + + from_service_account_info = TextToSpeechClient.from_service_account_info from_service_account_file = TextToSpeechClient.from_service_account_file from_service_account_json = from_service_account_file + @property + def transport(self) -> TextToSpeechTransport: + """Return the transport used by the client instance. + + Returns: + TextToSpeechTransport: The transport used by the client instance. + """ + return self._client.transport + get_transport_class = functools.partial( type(TextToSpeechClient).get_transport_class, type(TextToSpeechClient) ) @@ -72,16 +107,19 @@ def __init__( client_options (ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -107,7 +145,7 @@ async def list_voices( r"""Returns a list of Voice supported for synthesis. Args: - request (:class:`~.cloud_tts.ListVoicesRequest`): + request (:class:`google.cloud.texttospeech_v1beta1.types.ListVoicesRequest`): The request object. The top-level message sent by the client for the `ListVoices` method. language_code (:class:`str`): @@ -121,6 +159,7 @@ async def list_voices( (Norwegian Bokmal) voices; specifying "zh" will also get 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 should not be set. @@ -132,15 +171,16 @@ async def list_voices( sent along with the request as metadata. Returns: - ~.cloud_tts.ListVoicesResponse: - The message returned to the client by the ``ListVoices`` + google.cloud.texttospeech_v1beta1.types.ListVoicesResponse: + The message returned to the client by the ListVoices method. """ # 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." @@ -172,7 +212,7 @@ async def synthesize_speech( self, request: cloud_tts.SynthesizeSpeechRequest = None, *, - input: cloud_tts.SynthesisInput = None, + input_: cloud_tts.SynthesisInput = None, voice: cloud_tts.VoiceSelectionParams = None, audio_config: cloud_tts.AudioConfig = None, retry: retries.Retry = gapic_v1.method.DEFAULT, @@ -183,24 +223,27 @@ async def synthesize_speech( after all text input has been processed. Args: - request (:class:`~.cloud_tts.SynthesizeSpeechRequest`): + request (:class:`google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechRequest`): The request object. The top-level message sent by the client for the `SynthesizeSpeech` method. - input (:class:`~.cloud_tts.SynthesisInput`): + input_ (:class:`google.cloud.texttospeech_v1beta1.types.SynthesisInput`): Required. The Synthesizer requires either plain text or SSML as input. - This corresponds to the ``input`` field + + This corresponds to the ``input_`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - voice (:class:`~.cloud_tts.VoiceSelectionParams`): + voice (:class:`google.cloud.texttospeech_v1beta1.types.VoiceSelectionParams`): Required. The desired voice of the synthesized audio. + This corresponds to the ``voice`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - audio_config (:class:`~.cloud_tts.AudioConfig`): + audio_config (:class:`google.cloud.texttospeech_v1beta1.types.AudioConfig`): Required. The configuration of the synthesized audio. + This corresponds to the ``audio_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -212,15 +255,16 @@ async def synthesize_speech( sent along with the request as metadata. Returns: - ~.cloud_tts.SynthesizeSpeechResponse: + google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechResponse: The message returned to the client by the - ``SynthesizeSpeech`` method. + SynthesizeSpeech method. """ # 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." @@ -231,8 +275,8 @@ async def synthesize_speech( # If we have keyword arguments corresponding to fields on the # request, apply these. - if input is not None: - request.input = input + if input_ is not None: + request.input_ = input_ if voice is not None: request.voice = voice if audio_config is not None: diff --git a/google/cloud/texttospeech_v1beta1/services/text_to_speech/client.py b/google/cloud/texttospeech_v1beta1/services/text_to_speech/client.py index 2fa7e4ae..3e75ccc5 100644 --- a/google/cloud/texttospeech_v1beta1/services/text_to_speech/client.py +++ b/google/cloud/texttospeech_v1beta1/services/text_to_speech/client.py @@ -16,17 +16,19 @@ # from collections import OrderedDict +from distutils import util import os import re -from typing import Callable, Dict, Sequence, Tuple, Type, Union +from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union import pkg_resources -import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import client_options as client_options_lib # type: ignore 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.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore @@ -105,6 +107,22 @@ def _get_default_mtls_endpoint(api_endpoint): DEFAULT_ENDPOINT ) + @classmethod + def from_service_account_info(cls, info: dict, *args, **kwargs): + """Creates an instance of this client using the provided credentials info. + + Args: + info (dict): The service account private key info. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + TextToSpeechClient: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_info(info) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + @classmethod def from_service_account_file(cls, filename: str, *args, **kwargs): """Creates an instance of this client using the provided credentials @@ -117,7 +135,7 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): kwargs: Additional arguments to pass to the constructor. Returns: - {@api.name}: The constructed client. + TextToSpeechClient: The constructed client. """ credentials = service_account.Credentials.from_service_account_file(filename) kwargs["credentials"] = credentials @@ -125,12 +143,80 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): from_service_account_json = from_service_account_file + @property + def transport(self) -> TextToSpeechTransport: + """Return the transport used by the client instance. + + Returns: + TextToSpeechTransport: The transport used by the client instance. + """ + return self._transport + + @staticmethod + def common_billing_account_path(billing_account: str,) -> str: + """Return a fully-qualified billing_account string.""" + return "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + + @staticmethod + def parse_common_billing_account_path(path: str) -> Dict[str, str]: + """Parse a billing_account path into its component segments.""" + m = re.match(r"^billingAccounts/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_folder_path(folder: str,) -> str: + """Return a fully-qualified folder string.""" + return "folders/{folder}".format(folder=folder,) + + @staticmethod + def parse_common_folder_path(path: str) -> Dict[str, str]: + """Parse a folder path into its component segments.""" + m = re.match(r"^folders/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_organization_path(organization: str,) -> str: + """Return a fully-qualified organization string.""" + return "organizations/{organization}".format(organization=organization,) + + @staticmethod + def parse_common_organization_path(path: str) -> Dict[str, str]: + """Parse a organization path into its component segments.""" + m = re.match(r"^organizations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_project_path(project: str,) -> str: + """Return a fully-qualified project string.""" + return "projects/{project}".format(project=project,) + + @staticmethod + def parse_common_project_path(path: str) -> Dict[str, str]: + """Parse a project path into its component segments.""" + m = re.match(r"^projects/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_location_path(project: str, location: str,) -> str: + """Return a fully-qualified location string.""" + return "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + + @staticmethod + def parse_common_location_path(path: str) -> Dict[str, str]: + """Parse a location path into its component segments.""" + m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) + return m.groupdict() if m else {} + def __init__( self, *, - credentials: credentials.Credentials = None, - transport: Union[str, TextToSpeechTransport] = None, - client_options: ClientOptions = None, + credentials: Optional[credentials.Credentials] = None, + transport: Union[str, TextToSpeechTransport, None] = None, + client_options: Optional[client_options_lib.ClientOptions] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the text to speech client. @@ -141,26 +227,29 @@ def __init__( credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. - transport (Union[str, ~.TextToSpeechTransport]): The + transport (Union[str, TextToSpeechTransport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (ClientOptions): Custom options for the client. It - won't take effect if a ``transport`` instance is provided. + client_options (google.api_core.client_options.ClientOptions): Custom options for the + client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -168,29 +257,47 @@ def __init__( creation failed for any reason. """ if isinstance(client_options, dict): - client_options = ClientOptions.from_dict(client_options) + client_options = client_options_lib.from_dict(client_options) if client_options is None: - client_options = ClientOptions.ClientOptions() + client_options = client_options_lib.ClientOptions() - if client_options.api_endpoint is None: - use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never") + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) + + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_env == "never": - client_options.api_endpoint = self.DEFAULT_ENDPOINT + api_endpoint = self.DEFAULT_ENDPOINT elif use_mtls_env == "always": - client_options.api_endpoint = self.DEFAULT_MTLS_ENDPOINT + api_endpoint = self.DEFAULT_MTLS_ENDPOINT elif use_mtls_env == "auto": - has_client_cert_source = ( - client_options.client_cert_source is not None - or mtls.has_default_client_cert_source() - ) - client_options.api_endpoint = ( - self.DEFAULT_MTLS_ENDPOINT - if has_client_cert_source - else self.DEFAULT_ENDPOINT + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT ) else: raise MutualTLSChannelError( - "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always" + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" ) # Save or instantiate the transport. @@ -214,10 +321,9 @@ def __init__( self._transport = Transport( credentials=credentials, credentials_file=client_options.credentials_file, - host=client_options.api_endpoint, + host=api_endpoint, scopes=client_options.scopes, - api_mtls_endpoint=client_options.api_endpoint, - client_cert_source=client_options.client_cert_source, + ssl_channel_credentials=ssl_credentials, quota_project_id=client_options.quota_project_id, client_info=client_info, ) @@ -234,10 +340,10 @@ def list_voices( r"""Returns a list of Voice supported for synthesis. Args: - request (:class:`~.cloud_tts.ListVoicesRequest`): + request (google.cloud.texttospeech_v1beta1.types.ListVoicesRequest): The request object. The top-level message sent by the client for the `ListVoices` method. - language_code (:class:`str`): + language_code (str): Optional. Recommended. `BCP-47 `__ language tag. If specified, the ListVoices call will @@ -248,6 +354,7 @@ def list_voices( (Norwegian Bokmal) voices; specifying "zh" will also get 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 should not be set. @@ -259,8 +366,8 @@ def list_voices( sent along with the request as metadata. Returns: - ~.cloud_tts.ListVoicesResponse: - The message returned to the client by the ``ListVoices`` + google.cloud.texttospeech_v1beta1.types.ListVoicesResponse: + The message returned to the client by the ListVoices method. """ @@ -301,7 +408,7 @@ def synthesize_speech( self, request: cloud_tts.SynthesizeSpeechRequest = None, *, - input: cloud_tts.SynthesisInput = None, + input_: cloud_tts.SynthesisInput = None, voice: cloud_tts.VoiceSelectionParams = None, audio_config: cloud_tts.AudioConfig = None, retry: retries.Retry = gapic_v1.method.DEFAULT, @@ -312,24 +419,27 @@ def synthesize_speech( after all text input has been processed. Args: - request (:class:`~.cloud_tts.SynthesizeSpeechRequest`): + request (google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechRequest): The request object. The top-level message sent by the client for the `SynthesizeSpeech` method. - input (:class:`~.cloud_tts.SynthesisInput`): + input_ (google.cloud.texttospeech_v1beta1.types.SynthesisInput): Required. The Synthesizer requires either plain text or SSML as input. - This corresponds to the ``input`` field + + This corresponds to the ``input_`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - voice (:class:`~.cloud_tts.VoiceSelectionParams`): + voice (google.cloud.texttospeech_v1beta1.types.VoiceSelectionParams): Required. The desired voice of the synthesized audio. + This corresponds to the ``voice`` field on the ``request`` instance; if ``request`` is provided, this should not be set. - audio_config (:class:`~.cloud_tts.AudioConfig`): + audio_config (google.cloud.texttospeech_v1beta1.types.AudioConfig): Required. The configuration of the synthesized audio. + This corresponds to the ``audio_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. @@ -341,15 +451,15 @@ def synthesize_speech( sent along with the request as metadata. Returns: - ~.cloud_tts.SynthesizeSpeechResponse: + google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechResponse: The message returned to the client by the - ``SynthesizeSpeech`` method. + SynthesizeSpeech method. """ # 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. - has_flattened_params = 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 " @@ -366,8 +476,8 @@ def synthesize_speech( # If we have keyword arguments corresponding to fields on the # request, apply these. - if input is not None: - request.input = input + if input_ is not None: + request.input_ = input_ if voice is not None: request.voice = voice if audio_config is not None: diff --git a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/__init__.py b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/__init__.py index 3a92efc9..ce3b91f1 100644 --- a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/__init__.py +++ b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/__init__.py @@ -28,7 +28,6 @@ _transport_registry["grpc"] = TextToSpeechGrpcTransport _transport_registry["grpc_asyncio"] = TextToSpeechGrpcAsyncIOTransport - __all__ = ( "TextToSpeechTransport", "TextToSpeechGrpcTransport", diff --git a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/base.py b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/base.py index dfd1be01..cdce4625 100644 --- a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/base.py +++ b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/base.py @@ -19,7 +19,7 @@ import typing import pkg_resources -from google import auth +from google import auth # type: ignore 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 diff --git a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py index 95f453f6..ed36ff1f 100644 --- a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py +++ b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py @@ -15,6 +15,7 @@ # limitations under the License. # +import warnings from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import grpc_helpers # type: ignore @@ -23,7 +24,6 @@ from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore - import grpc # type: ignore from google.cloud.texttospeech_v1beta1.types import cloud_tts @@ -56,6 +56,7 @@ def __init__( channel: grpc.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -76,20 +77,22 @@ def __init__( ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing your own client library. Raises: @@ -98,6 +101,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -105,7 +110,13 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint @@ -135,6 +146,32 @@ def __init__( ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) self._stubs = {} # type: Dict[str, Callable] @@ -161,7 +198,7 @@ def create_channel( ) -> grpc.Channel: """Create and return a gRPC channel object. Args: - address (Optionsl[str]): The host for the channel to use. + address (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If @@ -196,19 +233,8 @@ def create_channel( @property def grpc_channel(self) -> grpc.Channel: - """Create the channel designed to connect to this service. - - This property caches on the instance; repeated calls return - the same channel. + """Return the channel designed to connect to this service. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - - # Return the channel from cache. return self._grpc_channel @property diff --git a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc_asyncio.py b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc_asyncio.py index d2d77873..5334a394 100644 --- a/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc_asyncio.py +++ b/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc_asyncio.py @@ -15,10 +15,12 @@ # limitations under the License. # +import warnings from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple from google.api_core import gapic_v1 # type: ignore from google.api_core import grpc_helpers_async # type: ignore +from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -98,6 +100,7 @@ def __init__( channel: aio.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, quota_project_id=None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: @@ -119,14 +122,16 @@ def __init__( are passed to :func:`google.auth.default`. channel (Optional[aio.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. quota_project_id (Optional[str]): An optional project to use for billing and quota. client_info (google.api_core.gapic_v1.client_info.ClientInfo): @@ -141,6 +146,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -148,13 +155,24 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443" ) + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: @@ -173,6 +191,32 @@ def __init__( ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) # Run the base constructor. @@ -194,13 +238,6 @@ def grpc_channel(self) -> aio.Channel: This property caches on the instance; repeated calls return the same channel. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - # Return the channel from cache. return self._grpc_channel diff --git a/google/cloud/texttospeech_v1beta1/types/__init__.py b/google/cloud/texttospeech_v1beta1/types/__init__.py index cb459aa3..81ca7177 100644 --- a/google/cloud/texttospeech_v1beta1/types/__init__.py +++ b/google/cloud/texttospeech_v1beta1/types/__init__.py @@ -25,9 +25,10 @@ AudioConfig, SynthesizeSpeechResponse, Timepoint, + SsmlVoiceGender, + AudioEncoding, ) - __all__ = ( "ListVoicesRequest", "ListVoicesResponse", @@ -38,4 +39,6 @@ "AudioConfig", "SynthesizeSpeechResponse", "Timepoint", + "SsmlVoiceGender", + "AudioEncoding", ) diff --git a/google/cloud/texttospeech_v1beta1/types/cloud_tts.py b/google/cloud/texttospeech_v1beta1/types/cloud_tts.py index 90855911..a160fdbf 100644 --- a/google/cloud/texttospeech_v1beta1/types/cloud_tts.py +++ b/google/cloud/texttospeech_v1beta1/types/cloud_tts.py @@ -83,7 +83,7 @@ class ListVoicesResponse(proto.Message): r"""The message returned to the client by the ``ListVoices`` method. Attributes: - voices (Sequence[~.cloud_tts.Voice]): + voices (Sequence[google.cloud.texttospeech_v1beta1.types.Voice]): The list of voices. """ @@ -101,7 +101,7 @@ class Voice(proto.Message): name (str): The name of this voice. Each distinct voice has a unique name. - ssml_gender (~.cloud_tts.SsmlVoiceGender): + ssml_gender (google.cloud.texttospeech_v1beta1.types.SsmlVoiceGender): The gender of this voice. natural_sample_rate_hertz (int): The natural sample rate (in hertz) for this @@ -122,16 +122,16 @@ class SynthesizeSpeechRequest(proto.Message): ``SynthesizeSpeech`` method. Attributes: - input (~.cloud_tts.SynthesisInput): + input_ (google.cloud.texttospeech_v1beta1.types.SynthesisInput): Required. The Synthesizer requires either plain text or SSML as input. - voice (~.cloud_tts.VoiceSelectionParams): + voice (google.cloud.texttospeech_v1beta1.types.VoiceSelectionParams): Required. The desired voice of the synthesized audio. - audio_config (~.cloud_tts.AudioConfig): + audio_config (google.cloud.texttospeech_v1beta1.types.AudioConfig): Required. The configuration of the synthesized audio. - enable_time_pointing (Sequence[~.cloud_tts.SynthesizeSpeechRequest.TimepointType]): + enable_time_pointing (Sequence[google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechRequest.TimepointType]): Whether and what timepoints should be returned in the response. """ @@ -143,7 +143,7 @@ class TimepointType(proto.Enum): TIMEPOINT_TYPE_UNSPECIFIED = 0 SSML_MARK = 1 - input = proto.Field(proto.MESSAGE, number=1, message="SynthesisInput",) + input_ = proto.Field(proto.MESSAGE, number=1, message="SynthesisInput",) voice = proto.Field(proto.MESSAGE, number=2, message="VoiceSelectionParams",) @@ -199,7 +199,7 @@ class VoiceSelectionParams(proto.Message): The name of the voice. If not set, the service will choose a voice based on the other parameters such as language_code and gender. - ssml_gender (~.cloud_tts.SsmlVoiceGender): + ssml_gender (google.cloud.texttospeech_v1beta1.types.SsmlVoiceGender): The preferred gender of the voice. If not set, the service will choose a voice based on the other parameters such as language_code and name. Note that this is only a preference, @@ -219,7 +219,7 @@ class AudioConfig(proto.Message): r"""Description of audio data to be synthesized. Attributes: - audio_encoding (~.cloud_tts.AudioEncoding): + audio_encoding (google.cloud.texttospeech_v1beta1.types.AudioEncoding): Required. The format of the audio byte stream. speaking_rate (float): @@ -287,11 +287,11 @@ class SynthesizeSpeechResponse(proto.Message): include the WAV header. Note: as with all bytes fields, protobuffers use a pure binary representation, whereas JSON representations use base64. - timepoints (Sequence[~.cloud_tts.Timepoint]): + timepoints (Sequence[google.cloud.texttospeech_v1beta1.types.Timepoint]): A link between a position in the original request input and a corresponding time in the output audio. It's only supported via ```` of SSML input. - audio_config (~.cloud_tts.AudioConfig): + audio_config (google.cloud.texttospeech_v1beta1.types.AudioConfig): The audio metadata of ``audio_content``. """ @@ -299,7 +299,7 @@ class SynthesizeSpeechResponse(proto.Message): timepoints = proto.RepeatedField(proto.MESSAGE, number=2, message="Timepoint",) - audio_config = proto.Field(proto.MESSAGE, number=4, message=AudioConfig,) + audio_config = proto.Field(proto.MESSAGE, number=4, message="AudioConfig",) class Timepoint(proto.Message): diff --git a/noxfile.py b/noxfile.py index 6ae48790..b48c0eb9 100644 --- a/noxfile.py +++ b/noxfile.py @@ -173,9 +173,7 @@ def docfx(session): """Build the docfx yaml files for this library.""" session.install("-e", ".") - # sphinx-docfx-yaml supports up to sphinx version 1.5.5. - # https://github.com/docascode/sphinx-docfx-yaml/issues/97 - session.install("sphinx==1.5.5", "alabaster", "recommonmark", "sphinx-docfx-yaml") + session.install("sphinx", "alabaster", "recommonmark", "sphinx-docfx-yaml") shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) session.run( diff --git a/scripts/fixup_texttospeech_v1_keywords.py b/scripts/fixup_texttospeech_v1_keywords.py index 7c548aef..96456f3f 100644 --- a/scripts/fixup_texttospeech_v1_keywords.py +++ b/scripts/fixup_texttospeech_v1_keywords.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2020 Google LLC @@ -41,7 +42,7 @@ class texttospeechCallTransformer(cst.CSTTransformer): CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { 'list_voices': ('language_code', ), - 'synthesize_speech': ('input', 'voice', 'audio_config', ), + 'synthesize_speech': ('input_', 'voice', 'audio_config', ), } diff --git a/scripts/fixup_texttospeech_v1beta1_keywords.py b/scripts/fixup_texttospeech_v1beta1_keywords.py index b11f7d87..8ee0e6cf 100644 --- a/scripts/fixup_texttospeech_v1beta1_keywords.py +++ b/scripts/fixup_texttospeech_v1beta1_keywords.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2020 Google LLC @@ -41,7 +42,7 @@ class texttospeechCallTransformer(cst.CSTTransformer): CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { 'list_voices': ('language_code', ), - 'synthesize_speech': ('input', 'voice', 'audio_config', 'enable_time_pointing', ), + 'synthesize_speech': ('input_', 'voice', 'audio_config', 'enable_time_pointing', ), } diff --git a/synth.metadata b/synth.metadata index 3af3ad5e..7e5530b4 100644 --- a/synth.metadata +++ b/synth.metadata @@ -4,7 +4,7 @@ "git": { "name": ".", "remote": "https://github.com/googleapis/python-texttospeech.git", - "sha": "97b044d0ed59483d34a86fbd99faf85a32507c4c" + "sha": "420eb87aa22756376a14f46d579db1b518f615da" } }, { @@ -49,5 +49,111 @@ "generator": "gapic-generator-python" } } + ], + "generatedFiles": [ + ".coveragerc", + ".flake8", + ".github/CONTRIBUTING.md", + ".github/ISSUE_TEMPLATE/bug_report.md", + ".github/ISSUE_TEMPLATE/feature_request.md", + ".github/ISSUE_TEMPLATE/support_request.md", + ".github/PULL_REQUEST_TEMPLATE.md", + ".github/release-please.yml", + ".gitignore", + ".kokoro/build.sh", + ".kokoro/continuous/common.cfg", + ".kokoro/continuous/continuous.cfg", + ".kokoro/docker/docs/Dockerfile", + ".kokoro/docker/docs/fetch_gpg_keys.sh", + ".kokoro/docs/common.cfg", + ".kokoro/docs/docs-presubmit.cfg", + ".kokoro/docs/docs.cfg", + ".kokoro/presubmit/common.cfg", + ".kokoro/presubmit/presubmit.cfg", + ".kokoro/publish-docs.sh", + ".kokoro/release.sh", + ".kokoro/release/common.cfg", + ".kokoro/release/release.cfg", + ".kokoro/samples/lint/common.cfg", + ".kokoro/samples/lint/continuous.cfg", + ".kokoro/samples/lint/periodic.cfg", + ".kokoro/samples/lint/presubmit.cfg", + ".kokoro/samples/python3.6/common.cfg", + ".kokoro/samples/python3.6/continuous.cfg", + ".kokoro/samples/python3.6/periodic.cfg", + ".kokoro/samples/python3.6/presubmit.cfg", + ".kokoro/samples/python3.7/common.cfg", + ".kokoro/samples/python3.7/continuous.cfg", + ".kokoro/samples/python3.7/periodic.cfg", + ".kokoro/samples/python3.7/presubmit.cfg", + ".kokoro/samples/python3.8/common.cfg", + ".kokoro/samples/python3.8/continuous.cfg", + ".kokoro/samples/python3.8/periodic.cfg", + ".kokoro/samples/python3.8/presubmit.cfg", + ".kokoro/test-samples.sh", + ".kokoro/trampoline.sh", + ".kokoro/trampoline_v2.sh", + ".trampolinerc", + "CODE_OF_CONDUCT.md", + "CONTRIBUTING.rst", + "LICENSE", + "MANIFEST.in", + "docs/_static/custom.css", + "docs/_templates/layout.html", + "docs/conf.py", + "docs/multiprocessing.rst", + "docs/texttospeech_v1/services.rst", + "docs/texttospeech_v1/text_to_speech.rst", + "docs/texttospeech_v1/types.rst", + "docs/texttospeech_v1beta1/services.rst", + "docs/texttospeech_v1beta1/text_to_speech.rst", + "docs/texttospeech_v1beta1/types.rst", + "google/cloud/texttospeech/__init__.py", + "google/cloud/texttospeech/py.typed", + "google/cloud/texttospeech_v1/__init__.py", + "google/cloud/texttospeech_v1/py.typed", + "google/cloud/texttospeech_v1/services/__init__.py", + "google/cloud/texttospeech_v1/services/text_to_speech/__init__.py", + "google/cloud/texttospeech_v1/services/text_to_speech/async_client.py", + "google/cloud/texttospeech_v1/services/text_to_speech/client.py", + "google/cloud/texttospeech_v1/services/text_to_speech/transports/__init__.py", + "google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py", + "google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py", + "google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py", + "google/cloud/texttospeech_v1/types/__init__.py", + "google/cloud/texttospeech_v1/types/cloud_tts.py", + "google/cloud/texttospeech_v1beta1/__init__.py", + "google/cloud/texttospeech_v1beta1/py.typed", + "google/cloud/texttospeech_v1beta1/services/__init__.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/__init__.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/async_client.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/client.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/__init__.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/base.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py", + "google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc_asyncio.py", + "google/cloud/texttospeech_v1beta1/types/__init__.py", + "google/cloud/texttospeech_v1beta1/types/cloud_tts.py", + "mypy.ini", + "noxfile.py", + "renovate.json", + "samples/AUTHORING_GUIDE.md", + "samples/CONTRIBUTING.md", + "samples/snippets/noxfile.py", + "scripts/decrypt-secrets.sh", + "scripts/fixup_texttospeech_v1_keywords.py", + "scripts/fixup_texttospeech_v1beta1_keywords.py", + "scripts/readme-gen/readme_gen.py", + "scripts/readme-gen/templates/README.tmpl.rst", + "scripts/readme-gen/templates/auth.tmpl.rst", + "scripts/readme-gen/templates/auth_api_key.tmpl.rst", + "scripts/readme-gen/templates/install_deps.tmpl.rst", + "scripts/readme-gen/templates/install_portaudio.tmpl.rst", + "setup.cfg", + "testing/.gitignore", + "tests/unit/gapic/texttospeech_v1/__init__.py", + "tests/unit/gapic/texttospeech_v1/test_text_to_speech.py", + "tests/unit/gapic/texttospeech_v1beta1/__init__.py", + "tests/unit/gapic/texttospeech_v1beta1/test_text_to_speech.py" ] } \ No newline at end of file diff --git a/tests/unit/gapic/texttospeech_v1/test_text_to_speech.py b/tests/unit/gapic/texttospeech_v1/test_text_to_speech.py index 5cfd08e9..1da1524f 100644 --- a/tests/unit/gapic/texttospeech_v1/test_text_to_speech.py +++ b/tests/unit/gapic/texttospeech_v1/test_text_to_speech.py @@ -80,7 +80,20 @@ def test__get_default_mtls_endpoint(): assert TextToSpeechClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi -@pytest.mark.parametrize("client_class", [TextToSpeechClient, TextToSpeechAsyncClient]) +def test_text_to_speech_client_from_service_account_info(): + creds = credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_info" + ) as factory: + factory.return_value = creds + info = {"valid": True} + client = TextToSpeechClient.from_service_account_info(info) + assert client.transport._credentials == creds + + assert client.transport._host == "texttospeech.googleapis.com:443" + + +@pytest.mark.parametrize("client_class", [TextToSpeechClient, TextToSpeechAsyncClient,]) def test_text_to_speech_client_from_service_account_file(client_class): creds = credentials.AnonymousCredentials() with mock.patch.object( @@ -88,17 +101,20 @@ def test_text_to_speech_client_from_service_account_file(client_class): ) as factory: factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds client = client_class.from_service_account_json("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds - assert client._transport._host == "texttospeech.googleapis.com:443" + assert client.transport._host == "texttospeech.googleapis.com:443" def test_text_to_speech_client_get_transport_class(): transport = TextToSpeechClient.get_transport_class() - assert transport == transports.TextToSpeechGrpcTransport + available_transports = [ + transports.TextToSpeechGrpcTransport, + ] + assert transport in available_transports transport = TextToSpeechClient.get_transport_class("grpc") assert transport == transports.TextToSpeechGrpcTransport @@ -147,15 +163,14 @@ def test_text_to_speech_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "never". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "never"}): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() @@ -164,15 +179,14 @@ def test_text_to_speech_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "always". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() @@ -181,95 +195,173 @@ def test_text_to_speech_client_client_options( credentials_file=None, host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and client_cert_source is provided. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + (TextToSpeechClient, transports.TextToSpeechGrpcTransport, "grpc", "true"), + ( + TextToSpeechAsyncClient, + transports.TextToSpeechGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (TextToSpeechClient, transports.TextToSpeechGrpcTransport, "grpc", "false"), + ( + TextToSpeechAsyncClient, + transports.TextToSpeechGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + TextToSpeechClient, "DEFAULT_ENDPOINT", modify_default_endpoint(TextToSpeechClient) +) +@mock.patch.object( + TextToSpeechAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(TextToSpeechAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_text_to_speech_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. + + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): options = client_options.ClientOptions( client_cert_source=client_cert_source_callback ) with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=client_cert_source_callback, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and default_client_cert_source is provided. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): - with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds ): patched.return_value = None - client = client_class() + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, + host=expected_host, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=expected_ssl_channel_creds, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", but client_cert_source and default_client_cert_source are None. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): with mock.patch.object(transport_class, "__init__") as patched: with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None ): - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has - # unsupported value. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}): - with pytest.raises(MutualTLSChannelError): - client = client_class() - - # Check the case quota_project_id is provided - options = client_options.ClientOptions(quota_project_id="octopus") - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - quota_project_id="octopus", - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) @pytest.mark.parametrize( @@ -296,8 +388,7 @@ def test_text_to_speech_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -327,8 +418,7 @@ def test_text_to_speech_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -345,8 +435,7 @@ def test_text_to_speech_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -362,7 +451,7 @@ def test_list_voices(transport: str = "grpc", request_type=cloud_tts.ListVoicesR request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.list_voices), "__call__") as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.ListVoicesResponse() @@ -375,6 +464,7 @@ def test_list_voices(transport: str = "grpc", request_type=cloud_tts.ListVoicesR assert args[0] == cloud_tts.ListVoicesRequest() # Establish that the response is the type that we expect. + assert isinstance(response, cloud_tts.ListVoicesResponse) @@ -383,19 +473,19 @@ def test_list_voices_from_dict(): @pytest.mark.asyncio -async def test_list_voices_async(transport: str = "grpc_asyncio"): +async def test_list_voices_async( + transport: str = "grpc_asyncio", request_type=cloud_tts.ListVoicesRequest +): client = TextToSpeechAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = cloud_tts.ListVoicesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.list_voices), "__call__" - ) as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( cloud_tts.ListVoicesResponse() @@ -407,17 +497,22 @@ async def test_list_voices_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == cloud_tts.ListVoicesRequest() # Establish that the response is the type that we expect. assert isinstance(response, cloud_tts.ListVoicesResponse) +@pytest.mark.asyncio +async def test_list_voices_async_from_dict(): + await test_list_voices_async(request_type=dict) + + def test_list_voices_flattened(): client = TextToSpeechClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.list_voices), "__call__") as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.ListVoicesResponse() @@ -449,9 +544,7 @@ async def test_list_voices_flattened_async(): client = TextToSpeechAsyncClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.list_voices), "__call__" - ) as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.ListVoicesResponse() @@ -495,7 +588,7 @@ def test_synthesize_speech( # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.SynthesizeSpeechResponse( @@ -511,6 +604,7 @@ def test_synthesize_speech( assert args[0] == cloud_tts.SynthesizeSpeechRequest() # Establish that the response is the type that we expect. + assert isinstance(response, cloud_tts.SynthesizeSpeechResponse) assert response.audio_content == b"audio_content_blob" @@ -521,18 +615,20 @@ def test_synthesize_speech_from_dict(): @pytest.mark.asyncio -async def test_synthesize_speech_async(transport: str = "grpc_asyncio"): +async def test_synthesize_speech_async( + transport: str = "grpc_asyncio", request_type=cloud_tts.SynthesizeSpeechRequest +): client = TextToSpeechAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = cloud_tts.SynthesizeSpeechRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -545,7 +641,7 @@ async def test_synthesize_speech_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == cloud_tts.SynthesizeSpeechRequest() # Establish that the response is the type that we expect. assert isinstance(response, cloud_tts.SynthesizeSpeechResponse) @@ -553,12 +649,17 @@ async def test_synthesize_speech_async(transport: str = "grpc_asyncio"): assert response.audio_content == b"audio_content_blob" +@pytest.mark.asyncio +async def test_synthesize_speech_async_from_dict(): + await test_synthesize_speech_async(request_type=dict) + + def test_synthesize_speech_flattened(): client = TextToSpeechClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.SynthesizeSpeechResponse() @@ -566,7 +667,7 @@ def test_synthesize_speech_flattened(): # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. client.synthesize_speech( - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -578,7 +679,7 @@ def test_synthesize_speech_flattened(): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0].input == cloud_tts.SynthesisInput(text="text_value") + assert args[0].input_ == cloud_tts.SynthesisInput(text="text_value") assert args[0].voice == cloud_tts.VoiceSelectionParams( language_code="language_code_value" @@ -597,7 +698,7 @@ def test_synthesize_speech_flattened_error(): with pytest.raises(ValueError): client.synthesize_speech( cloud_tts.SynthesizeSpeechRequest(), - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -611,7 +712,7 @@ async def test_synthesize_speech_flattened_async(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.SynthesizeSpeechResponse() @@ -622,7 +723,7 @@ async def test_synthesize_speech_flattened_async(): # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. response = await client.synthesize_speech( - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -634,7 +735,7 @@ async def test_synthesize_speech_flattened_async(): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0].input == cloud_tts.SynthesisInput(text="text_value") + assert args[0].input_ == cloud_tts.SynthesisInput(text="text_value") assert args[0].voice == cloud_tts.VoiceSelectionParams( language_code="language_code_value" @@ -654,7 +755,7 @@ async def test_synthesize_speech_flattened_error_async(): with pytest.raises(ValueError): await client.synthesize_speech( cloud_tts.SynthesizeSpeechRequest(), - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -698,7 +799,7 @@ def test_transport_instance(): credentials=credentials.AnonymousCredentials(), ) client = TextToSpeechClient(transport=transport) - assert client._transport is transport + assert client.transport is transport def test_transport_get_channel(): @@ -716,10 +817,25 @@ def test_transport_get_channel(): assert channel +@pytest.mark.parametrize( + "transport_class", + [ + transports.TextToSpeechGrpcTransport, + transports.TextToSpeechGrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = TextToSpeechClient(credentials=credentials.AnonymousCredentials(),) - assert isinstance(client._transport, transports.TextToSpeechGrpcTransport,) + assert isinstance(client.transport, transports.TextToSpeechGrpcTransport,) def test_text_to_speech_base_transport_error(): @@ -771,6 +887,17 @@ def test_text_to_speech_base_transport_with_credentials_file(): ) +def test_text_to_speech_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.cloud.texttospeech_v1.services.text_to_speech.transports.TextToSpeechTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.TextToSpeechTransport() + adc.assert_called_once() + + def test_text_to_speech_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(auth, "default") as adc: @@ -803,7 +930,7 @@ def test_text_to_speech_host_no_port(): api_endpoint="texttospeech.googleapis.com" ), ) - assert client._transport._host == "texttospeech.googleapis.com:443" + assert client.transport._host == "texttospeech.googleapis.com:443" def test_text_to_speech_host_with_port(): @@ -813,185 +940,220 @@ def test_text_to_speech_host_with_port(): api_endpoint="texttospeech.googleapis.com:8000" ), ) - assert client._transport._host == "texttospeech.googleapis.com:8000" + assert client.transport._host == "texttospeech.googleapis.com:8000" def test_text_to_speech_grpc_transport_channel(): - channel = grpc.insecure_channel("http://localhost/") + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.TextToSpeechGrpcTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None def test_text_to_speech_grpc_asyncio_transport_channel(): - channel = aio.insecure_channel("http://localhost/") + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.TextToSpeechGrpcAsyncIOTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_text_to_speech_grpc_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() +@pytest.mark.parametrize( + "transport_class", + [transports.TextToSpeechGrpcTransport, transports.TextToSpeechGrpcAsyncIOTransport], +) +def test_text_to_speech_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + +@pytest.mark.parametrize( + "transport_class", + [transports.TextToSpeechGrpcTransport, transports.TextToSpeechGrpcAsyncIOTransport], +) +def test_text_to_speech_transport_channel_mtls_with_adc(transport_class): mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel" + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel - transport = transports.TextToSpeechGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, + +def test_common_billing_account_path(): + billing_account = "squid" + + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) - assert transport.grpc_channel == mock_grpc_channel + actual = TextToSpeechClient.common_billing_account_path(billing_account) + assert expected == actual -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_text_to_speech_grpc_asyncio_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "clam", + } + path = TextToSpeechClient.common_billing_account_path(**expected) - mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_billing_account_path(path) + assert expected == actual - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - transport = transports.TextToSpeechGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_common_folder_path(): + folder = "whelk" + expected = "folders/{folder}".format(folder=folder,) + actual = TextToSpeechClient.common_folder_path(folder) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_text_to_speech_grpc_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.TextToSpeechGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_parse_common_folder_path(): + expected = { + "folder": "octopus", + } + path = TextToSpeechClient.common_folder_path(**expected) + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_folder_path(path) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_text_to_speech_grpc_asyncio_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.TextToSpeechGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_common_organization_path(): + organization = "oyster" + + expected = "organizations/{organization}".format(organization=organization,) + actual = TextToSpeechClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "nudibranch", + } + path = TextToSpeechClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "cuttlefish" + + expected = "projects/{project}".format(project=project,) + actual = TextToSpeechClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "mussel", + } + path = TextToSpeechClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "winkle" + location = "nautilus" + + expected = "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + actual = TextToSpeechClient.common_location_path(project, location) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "scallop", + "location": "abalone", + } + path = TextToSpeechClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_location_path(path) + assert expected == actual def test_client_withDEFAULT_CLIENT_INFO(): diff --git a/tests/unit/gapic/texttospeech_v1beta1/test_text_to_speech.py b/tests/unit/gapic/texttospeech_v1beta1/test_text_to_speech.py index 73e6e676..06f99dc2 100644 --- a/tests/unit/gapic/texttospeech_v1beta1/test_text_to_speech.py +++ b/tests/unit/gapic/texttospeech_v1beta1/test_text_to_speech.py @@ -82,7 +82,20 @@ def test__get_default_mtls_endpoint(): assert TextToSpeechClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi -@pytest.mark.parametrize("client_class", [TextToSpeechClient, TextToSpeechAsyncClient]) +def test_text_to_speech_client_from_service_account_info(): + creds = credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_info" + ) as factory: + factory.return_value = creds + info = {"valid": True} + client = TextToSpeechClient.from_service_account_info(info) + assert client.transport._credentials == creds + + assert client.transport._host == "texttospeech.googleapis.com:443" + + +@pytest.mark.parametrize("client_class", [TextToSpeechClient, TextToSpeechAsyncClient,]) def test_text_to_speech_client_from_service_account_file(client_class): creds = credentials.AnonymousCredentials() with mock.patch.object( @@ -90,17 +103,20 @@ def test_text_to_speech_client_from_service_account_file(client_class): ) as factory: factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds client = client_class.from_service_account_json("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds - assert client._transport._host == "texttospeech.googleapis.com:443" + assert client.transport._host == "texttospeech.googleapis.com:443" def test_text_to_speech_client_get_transport_class(): transport = TextToSpeechClient.get_transport_class() - assert transport == transports.TextToSpeechGrpcTransport + available_transports = [ + transports.TextToSpeechGrpcTransport, + ] + assert transport in available_transports transport = TextToSpeechClient.get_transport_class("grpc") assert transport == transports.TextToSpeechGrpcTransport @@ -149,15 +165,14 @@ def test_text_to_speech_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "never". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "never"}): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() @@ -166,15 +181,14 @@ def test_text_to_speech_client_client_options( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "always". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() @@ -183,95 +197,173 @@ def test_text_to_speech_client_client_options( credentials_file=None, host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and client_cert_source is provided. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + (TextToSpeechClient, transports.TextToSpeechGrpcTransport, "grpc", "true"), + ( + TextToSpeechAsyncClient, + transports.TextToSpeechGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (TextToSpeechClient, transports.TextToSpeechGrpcTransport, "grpc", "false"), + ( + TextToSpeechAsyncClient, + transports.TextToSpeechGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + TextToSpeechClient, "DEFAULT_ENDPOINT", modify_default_endpoint(TextToSpeechClient) +) +@mock.patch.object( + TextToSpeechAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(TextToSpeechAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_text_to_speech_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. + + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): options = client_options.ClientOptions( client_cert_source=client_cert_source_callback ) with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=client_cert_source_callback, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and default_client_cert_source is provided. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): - with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds ): patched.return_value = None - client = client_class() + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, + host=expected_host, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=expected_ssl_channel_creds, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", but client_cert_source and default_client_cert_source are None. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): with mock.patch.object(transport_class, "__init__") as patched: with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None ): - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has - # unsupported value. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}): - with pytest.raises(MutualTLSChannelError): - client = client_class() - - # Check the case quota_project_id is provided - options = client_options.ClientOptions(quota_project_id="octopus") - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - quota_project_id="octopus", - client_info=transports.base.DEFAULT_CLIENT_INFO, - ) + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) @pytest.mark.parametrize( @@ -298,8 +390,7 @@ def test_text_to_speech_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -329,8 +420,7 @@ def test_text_to_speech_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -347,8 +437,7 @@ def test_text_to_speech_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -364,7 +453,7 @@ def test_list_voices(transport: str = "grpc", request_type=cloud_tts.ListVoicesR request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.list_voices), "__call__") as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.ListVoicesResponse() @@ -377,6 +466,7 @@ def test_list_voices(transport: str = "grpc", request_type=cloud_tts.ListVoicesR assert args[0] == cloud_tts.ListVoicesRequest() # Establish that the response is the type that we expect. + assert isinstance(response, cloud_tts.ListVoicesResponse) @@ -385,19 +475,19 @@ def test_list_voices_from_dict(): @pytest.mark.asyncio -async def test_list_voices_async(transport: str = "grpc_asyncio"): +async def test_list_voices_async( + transport: str = "grpc_asyncio", request_type=cloud_tts.ListVoicesRequest +): client = TextToSpeechAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = cloud_tts.ListVoicesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.list_voices), "__call__" - ) as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( cloud_tts.ListVoicesResponse() @@ -409,17 +499,22 @@ async def test_list_voices_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == cloud_tts.ListVoicesRequest() # Establish that the response is the type that we expect. assert isinstance(response, cloud_tts.ListVoicesResponse) +@pytest.mark.asyncio +async def test_list_voices_async_from_dict(): + await test_list_voices_async(request_type=dict) + + def test_list_voices_flattened(): client = TextToSpeechClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.list_voices), "__call__") as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.ListVoicesResponse() @@ -451,9 +546,7 @@ async def test_list_voices_flattened_async(): client = TextToSpeechAsyncClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.list_voices), "__call__" - ) as call: + with mock.patch.object(type(client.transport.list_voices), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.ListVoicesResponse() @@ -497,7 +590,7 @@ def test_synthesize_speech( # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.SynthesizeSpeechResponse( @@ -513,6 +606,7 @@ def test_synthesize_speech( assert args[0] == cloud_tts.SynthesizeSpeechRequest() # Establish that the response is the type that we expect. + assert isinstance(response, cloud_tts.SynthesizeSpeechResponse) assert response.audio_content == b"audio_content_blob" @@ -523,18 +617,20 @@ def test_synthesize_speech_from_dict(): @pytest.mark.asyncio -async def test_synthesize_speech_async(transport: str = "grpc_asyncio"): +async def test_synthesize_speech_async( + transport: str = "grpc_asyncio", request_type=cloud_tts.SynthesizeSpeechRequest +): client = TextToSpeechAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = cloud_tts.SynthesizeSpeechRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -547,7 +643,7 @@ async def test_synthesize_speech_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == cloud_tts.SynthesizeSpeechRequest() # Establish that the response is the type that we expect. assert isinstance(response, cloud_tts.SynthesizeSpeechResponse) @@ -555,12 +651,17 @@ async def test_synthesize_speech_async(transport: str = "grpc_asyncio"): assert response.audio_content == b"audio_content_blob" +@pytest.mark.asyncio +async def test_synthesize_speech_async_from_dict(): + await test_synthesize_speech_async(request_type=dict) + + def test_synthesize_speech_flattened(): client = TextToSpeechClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.SynthesizeSpeechResponse() @@ -568,7 +669,7 @@ def test_synthesize_speech_flattened(): # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. client.synthesize_speech( - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -580,7 +681,7 @@ def test_synthesize_speech_flattened(): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0].input == cloud_tts.SynthesisInput(text="text_value") + assert args[0].input_ == cloud_tts.SynthesisInput(text="text_value") assert args[0].voice == cloud_tts.VoiceSelectionParams( language_code="language_code_value" @@ -599,7 +700,7 @@ def test_synthesize_speech_flattened_error(): with pytest.raises(ValueError): client.synthesize_speech( cloud_tts.SynthesizeSpeechRequest(), - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -613,7 +714,7 @@ async def test_synthesize_speech_flattened_async(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.synthesize_speech), "__call__" + type(client.transport.synthesize_speech), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = cloud_tts.SynthesizeSpeechResponse() @@ -624,7 +725,7 @@ async def test_synthesize_speech_flattened_async(): # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. response = await client.synthesize_speech( - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -636,7 +737,7 @@ async def test_synthesize_speech_flattened_async(): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0].input == cloud_tts.SynthesisInput(text="text_value") + assert args[0].input_ == cloud_tts.SynthesisInput(text="text_value") assert args[0].voice == cloud_tts.VoiceSelectionParams( language_code="language_code_value" @@ -656,7 +757,7 @@ async def test_synthesize_speech_flattened_error_async(): with pytest.raises(ValueError): await client.synthesize_speech( cloud_tts.SynthesizeSpeechRequest(), - input=cloud_tts.SynthesisInput(text="text_value"), + input_=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams(language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16 @@ -700,7 +801,7 @@ def test_transport_instance(): credentials=credentials.AnonymousCredentials(), ) client = TextToSpeechClient(transport=transport) - assert client._transport is transport + assert client.transport is transport def test_transport_get_channel(): @@ -718,10 +819,25 @@ def test_transport_get_channel(): assert channel +@pytest.mark.parametrize( + "transport_class", + [ + transports.TextToSpeechGrpcTransport, + transports.TextToSpeechGrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = TextToSpeechClient(credentials=credentials.AnonymousCredentials(),) - assert isinstance(client._transport, transports.TextToSpeechGrpcTransport,) + assert isinstance(client.transport, transports.TextToSpeechGrpcTransport,) def test_text_to_speech_base_transport_error(): @@ -773,6 +889,17 @@ def test_text_to_speech_base_transport_with_credentials_file(): ) +def test_text_to_speech_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.cloud.texttospeech_v1beta1.services.text_to_speech.transports.TextToSpeechTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.TextToSpeechTransport() + adc.assert_called_once() + + def test_text_to_speech_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(auth, "default") as adc: @@ -805,7 +932,7 @@ def test_text_to_speech_host_no_port(): api_endpoint="texttospeech.googleapis.com" ), ) - assert client._transport._host == "texttospeech.googleapis.com:443" + assert client.transport._host == "texttospeech.googleapis.com:443" def test_text_to_speech_host_with_port(): @@ -815,185 +942,220 @@ def test_text_to_speech_host_with_port(): api_endpoint="texttospeech.googleapis.com:8000" ), ) - assert client._transport._host == "texttospeech.googleapis.com:8000" + assert client.transport._host == "texttospeech.googleapis.com:8000" def test_text_to_speech_grpc_transport_channel(): - channel = grpc.insecure_channel("http://localhost/") + channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.TextToSpeechGrpcTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None def test_text_to_speech_grpc_asyncio_transport_channel(): - channel = aio.insecure_channel("http://localhost/") + channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.TextToSpeechGrpcAsyncIOTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_text_to_speech_grpc_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() +@pytest.mark.parametrize( + "transport_class", + [transports.TextToSpeechGrpcTransport, transports.TextToSpeechGrpcAsyncIOTransport], +) +def test_text_to_speech_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel" + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + +@pytest.mark.parametrize( + "transport_class", + [transports.TextToSpeechGrpcTransport, transports.TextToSpeechGrpcAsyncIOTransport], +) +def test_text_to_speech_transport_channel_mtls_with_adc(transport_class): mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel" + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel - transport = transports.TextToSpeechGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, + +def test_common_billing_account_path(): + billing_account = "squid" + + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) - assert transport.grpc_channel == mock_grpc_channel + actual = TextToSpeechClient.common_billing_account_path(billing_account) + assert expected == actual -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_text_to_speech_grpc_asyncio_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "clam", + } + path = TextToSpeechClient.common_billing_account_path(**expected) - mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_billing_account_path(path) + assert expected == actual - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - transport = transports.TextToSpeechGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_common_folder_path(): + folder = "whelk" + expected = "folders/{folder}".format(folder=folder,) + actual = TextToSpeechClient.common_folder_path(folder) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_text_to_speech_grpc_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.TextToSpeechGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_parse_common_folder_path(): + expected = { + "folder": "octopus", + } + path = TextToSpeechClient.common_folder_path(**expected) + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_folder_path(path) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_text_to_speech_grpc_asyncio_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.TextToSpeechGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - ) - assert transport.grpc_channel == mock_grpc_channel +def test_common_organization_path(): + organization = "oyster" + + expected = "organizations/{organization}".format(organization=organization,) + actual = TextToSpeechClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "nudibranch", + } + path = TextToSpeechClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "cuttlefish" + + expected = "projects/{project}".format(project=project,) + actual = TextToSpeechClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "mussel", + } + path = TextToSpeechClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "winkle" + location = "nautilus" + + expected = "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + actual = TextToSpeechClient.common_location_path(project, location) + assert expected == actual + + +def test_parse_common_location_path(): + expected = { + "project": "scallop", + "location": "abalone", + } + path = TextToSpeechClient.common_location_path(**expected) + + # Check that the path construction is reversible. + actual = TextToSpeechClient.parse_common_location_path(path) + assert expected == actual def test_client_withDEFAULT_CLIENT_INFO(): From 1ffc4d20ab104bc5bce8a15cc572421652471352 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:44:37 -0800 Subject: [PATCH 02/26] chore(py-library): update decrypt secrets file * chore(py-library): update decrypt secrets file From https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/scripts/decrypt-secrets.sh * docs: explain conditional Co-authored-by: Jeffrey Rennie Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Tue Sep 8 11:35:59 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: d302f93d7f47e2852e585ac35ab2d15585717ec0 Source-Link: https://github.com/googleapis/synthtool/commit/d302f93d7f47e2852e585ac35ab2d15585717ec0 --- scripts/decrypt-secrets.sh | 15 ++++++++++++++- synth.metadata | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/scripts/decrypt-secrets.sh b/scripts/decrypt-secrets.sh index ff599eb2..21f6d2a2 100755 --- a/scripts/decrypt-secrets.sh +++ b/scripts/decrypt-secrets.sh @@ -20,14 +20,27 @@ ROOT=$( dirname "$DIR" ) # Work from the project root. cd $ROOT +# Prevent it from overriding files. +# We recommend that sample authors use their own service account files and cloud project. +# In that case, they are supposed to prepare these files by themselves. +if [[ -f "testing/test-env.sh" ]] || \ + [[ -f "testing/service-account.json" ]] || \ + [[ -f "testing/client-secrets.json" ]]; then + echo "One or more target files exist, aborting." + exit 1 +fi + # Use SECRET_MANAGER_PROJECT if set, fallback to cloud-devrel-kokoro-resources. PROJECT_ID="${SECRET_MANAGER_PROJECT:-cloud-devrel-kokoro-resources}" gcloud secrets versions access latest --secret="python-docs-samples-test-env" \ + --project="${PROJECT_ID}" \ > testing/test-env.sh gcloud secrets versions access latest \ --secret="python-docs-samples-service-account" \ + --project="${PROJECT_ID}" \ > testing/service-account.json gcloud secrets versions access latest \ --secret="python-docs-samples-client-secrets" \ - > testing/client-secrets.json \ No newline at end of file + --project="${PROJECT_ID}" \ + > testing/client-secrets.json diff --git a/synth.metadata b/synth.metadata index 7e5530b4..117b7f38 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "4f8f5dc24af79694887385015294e4dbb214c352" + "sha": "d302f93d7f47e2852e585ac35ab2d15585717ec0" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "4f8f5dc24af79694887385015294e4dbb214c352" + "sha": "d302f93d7f47e2852e585ac35ab2d15585717ec0" } } ], @@ -59,6 +59,7 @@ ".github/ISSUE_TEMPLATE/support_request.md", ".github/PULL_REQUEST_TEMPLATE.md", ".github/release-please.yml", + ".github/snippet-bot.yml", ".gitignore", ".kokoro/build.sh", ".kokoro/continuous/common.cfg", From 7fbd8da1afbc6880d29a67c198179ec5914a7e90 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:45:13 -0800 Subject: [PATCH 03/26] chore(python-library): use sphinx 1.5.5 for the docfx job Originally tested at: https://github.com/googleapis/python-texttospeech/pull/89 This change will fix the missing docstring in the yaml files. Source-Author: Takashi Matsuo Source-Date: Thu Sep 10 04:12:14 2020 +0000 Source-Repo: googleapis/synthtool Source-Sha: ffcee7952b74f647cbb3ef021d95422f10816fca Source-Link: https://github.com/googleapis/synthtool/commit/ffcee7952b74f647cbb3ef021d95422f10816fca --- docs/conf.py | 2 +- noxfile.py | 4 +++- synth.metadata | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 284c96c0..b30742a4 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -29,7 +29,7 @@ # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = "1.6.3" +needs_sphinx = "1.5.5" # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom diff --git a/noxfile.py b/noxfile.py index b48c0eb9..6ae48790 100644 --- a/noxfile.py +++ b/noxfile.py @@ -173,7 +173,9 @@ def docfx(session): """Build the docfx yaml files for this library.""" session.install("-e", ".") - session.install("sphinx", "alabaster", "recommonmark", "sphinx-docfx-yaml") + # sphinx-docfx-yaml supports up to sphinx version 1.5.5. + # https://github.com/docascode/sphinx-docfx-yaml/issues/97 + session.install("sphinx==1.5.5", "alabaster", "recommonmark", "sphinx-docfx-yaml") shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) session.run( diff --git a/synth.metadata b/synth.metadata index 117b7f38..6f99bb83 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "d302f93d7f47e2852e585ac35ab2d15585717ec0" + "sha": "ffcee7952b74f647cbb3ef021d95422f10816fca" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "d302f93d7f47e2852e585ac35ab2d15585717ec0" + "sha": "ffcee7952b74f647cbb3ef021d95422f10816fca" } } ], From ce94d49f6aa07c8fbb1727f7e5a124aa05beaf3a Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:45:31 -0800 Subject: [PATCH 04/26] =?UTF-8?q?build(python):=20use=20release-publish=20?= =?UTF-8?q?app=20for=20notifying=20GitHub=20of=20releas=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * build(python): use release-publish app for notifying GitHub of release status * fix: re-add pypi password Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Sep 16 08:46:42 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 257fda18168bedb76985024bd198ed1725485488 Source-Link: https://github.com/googleapis/synthtool/commit/257fda18168bedb76985024bd198ed1725485488 --- .kokoro/release/common.cfg | 50 ++++++++++---------------------------- synth.metadata | 4 +-- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/.kokoro/release/common.cfg b/.kokoro/release/common.cfg index 25ed0e04..09630f49 100644 --- a/.kokoro/release/common.cfg +++ b/.kokoro/release/common.cfg @@ -23,42 +23,18 @@ env_vars: { value: "github/python-texttospeech/.kokoro/release.sh" } -# Fetch the token needed for reporting release status to GitHub -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "yoshi-automation-github-key" - } - } -} - -# Fetch PyPI password -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "google_cloud_pypi_password" - } - } -} - -# Fetch magictoken to use with Magic Github Proxy -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "releasetool-magictoken" - } - } +# Fetch PyPI password +before_action { + fetch_keystore { + keystore_resource { + keystore_config_id: 73713 + keyname: "google_cloud_pypi_password" + } + } } -# Fetch api key to use with Magic Github Proxy -before_action { - fetch_keystore { - keystore_resource { - keystore_config_id: 73713 - keyname: "magic-github-proxy-api-key" - } - } -} +# Tokens needed to report release status back to GitHub +env_vars: { + key: "SECRET_MANAGER_KEYS" + value: "releasetool-publish-reporter-app,releasetool-publish-reporter-googleapis-installation,releasetool-publish-reporter-pem" +} \ No newline at end of file diff --git a/synth.metadata b/synth.metadata index 6f99bb83..c1b0c8bd 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "ffcee7952b74f647cbb3ef021d95422f10816fca" + "sha": "257fda18168bedb76985024bd198ed1725485488" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "ffcee7952b74f647cbb3ef021d95422f10816fca" + "sha": "257fda18168bedb76985024bd198ed1725485488" } } ], From 84943f5cca6ab62475ca571103c2d23a048bfd3d Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:46:24 -0800 Subject: [PATCH 05/26] build(python): add secret manager in kokoro Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Sep 16 10:24:40 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: dba48bb9bc6959c232bec9150ac6313b608fe7bd Source-Link: https://github.com/googleapis/synthtool/commit/dba48bb9bc6959c232bec9150ac6313b608fe7bd --- .kokoro/populate-secrets.sh | 43 +++++++++++++++++++++++++++++++++++++ .kokoro/trampoline.sh | 15 ++++++++----- synth.metadata | 5 +++-- 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100755 .kokoro/populate-secrets.sh diff --git a/.kokoro/populate-secrets.sh b/.kokoro/populate-secrets.sh new file mode 100755 index 00000000..f5251425 --- /dev/null +++ b/.kokoro/populate-secrets.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright 2020 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eo pipefail + +function now { date +"%Y-%m-%d %H:%M:%S" | tr -d '\n' ;} +function msg { println "$*" >&2 ;} +function println { printf '%s\n' "$(now) $*" ;} + + +# Populates requested secrets set in SECRET_MANAGER_KEYS from service account: +# kokoro-trampoline@cloud-devrel-kokoro-resources.iam.gserviceaccount.com +SECRET_LOCATION="${KOKORO_GFILE_DIR}/secret_manager" +msg "Creating folder on disk for secrets: ${SECRET_LOCATION}" +mkdir -p ${SECRET_LOCATION} +for key in $(echo ${SECRET_MANAGER_KEYS} | sed "s/,/ /g") +do + msg "Retrieving secret ${key}" + docker run --entrypoint=gcloud \ + --volume=${KOKORO_GFILE_DIR}:${KOKORO_GFILE_DIR} \ + gcr.io/google.com/cloudsdktool/cloud-sdk \ + secrets versions access latest \ + --project cloud-devrel-kokoro-resources \ + --secret ${key} > \ + "${SECRET_LOCATION}/${key}" + if [[ $? == 0 ]]; then + msg "Secret written to ${SECRET_LOCATION}/${key}" + else + msg "Error retrieving secret ${key}" + fi +done diff --git a/.kokoro/trampoline.sh b/.kokoro/trampoline.sh index e8c4251f..f39236e9 100755 --- a/.kokoro/trampoline.sh +++ b/.kokoro/trampoline.sh @@ -15,9 +15,14 @@ set -eo pipefail -python3 "${KOKORO_GFILE_DIR}/trampoline_v1.py" || ret_code=$? +# Always run the cleanup script, regardless of the success of bouncing into +# the container. +function cleanup() { + chmod +x ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh + ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh + echo "cleanup"; +} +trap cleanup EXIT -chmod +x ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh -${KOKORO_GFILE_DIR}/trampoline_cleanup.sh || true - -exit ${ret_code} +$(dirname $0)/populate-secrets.sh # Secret Manager secrets. +python3 "${KOKORO_GFILE_DIR}/trampoline_v1.py" \ No newline at end of file diff --git a/synth.metadata b/synth.metadata index c1b0c8bd..53fbc325 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "257fda18168bedb76985024bd198ed1725485488" + "sha": "dba48bb9bc6959c232bec9150ac6313b608fe7bd" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "257fda18168bedb76985024bd198ed1725485488" + "sha": "dba48bb9bc6959c232bec9150ac6313b608fe7bd" } } ], @@ -69,6 +69,7 @@ ".kokoro/docs/common.cfg", ".kokoro/docs/docs-presubmit.cfg", ".kokoro/docs/docs.cfg", + ".kokoro/populate-secrets.sh", ".kokoro/presubmit/common.cfg", ".kokoro/presubmit/presubmit.cfg", ".kokoro/publish-docs.sh", From 43e6635f1f3ee606353be999b1b588a832257827 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:46:42 -0800 Subject: [PATCH 06/26] chore(python): add sphinx doctest extension Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Mon Sep 21 13:09:57 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 27f4406999b1eee29e04b09b2423a8e4646c7e24 Source-Link: https://github.com/googleapis/synthtool/commit/27f4406999b1eee29e04b09b2423a8e4646c7e24 --- docs/conf.py | 1 + synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index b30742a4..63d4fb20 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -39,6 +39,7 @@ "sphinx.ext.autosummary", "sphinx.ext.intersphinx", "sphinx.ext.coverage", + "sphinx.ext.doctest", "sphinx.ext.napoleon", "sphinx.ext.todo", "sphinx.ext.viewcode", diff --git a/synth.metadata b/synth.metadata index 53fbc325..3d5bb10b 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "dba48bb9bc6959c232bec9150ac6313b608fe7bd" + "sha": "27f4406999b1eee29e04b09b2423a8e4646c7e24" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "dba48bb9bc6959c232bec9150ac6313b608fe7bd" + "sha": "27f4406999b1eee29e04b09b2423a8e4646c7e24" } } ], From b9e8452963497c8184e19f82253a550e9821c826 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:47:18 -0800 Subject: [PATCH 07/26] chore(python): remove note about editable installs `pip install -e .` is supported and is how we install the library for tests. Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Tue Sep 22 12:06:12 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d Source-Link: https://github.com/googleapis/synthtool/commit/a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d --- CONTRIBUTING.rst | 19 ------------------- synth.metadata | 4 ++-- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 98f6a6be..214639e2 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -80,25 +80,6 @@ We use `nox `__ to instrument our tests. .. nox: https://pypi.org/project/nox/ -Note on Editable Installs / Develop Mode -======================================== - -- As mentioned previously, using ``setuptools`` in `develop mode`_ - or a ``pip`` `editable install`_ is not possible with this - library. This is because this library uses `namespace packages`_. - For context see `Issue #2316`_ and the relevant `PyPA issue`_. - - Since ``editable`` / ``develop`` mode can't be used, packages - need to be installed directly. Hence your changes to the source - tree don't get incorporated into the **already installed** - package. - -.. _namespace packages: https://www.python.org/dev/peps/pep-0420/ -.. _Issue #2316: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2316 -.. _PyPA issue: https://github.com/pypa/packaging-problems/issues/12 -.. _develop mode: https://setuptools.readthedocs.io/en/latest/setuptools.html#development-mode -.. _editable install: https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs - ***************************************** I'm getting weird errors... Can you help? ***************************************** diff --git a/synth.metadata b/synth.metadata index 3d5bb10b..42931bfe 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "27f4406999b1eee29e04b09b2423a8e4646c7e24" + "sha": "a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "27f4406999b1eee29e04b09b2423a8e4646c7e24" + "sha": "a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d" } } ], From 05bd615f30d8bd01581c7acd14e0700f62954151 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:48:29 -0800 Subject: [PATCH 08/26] chore(python): use BUILD_SPECIFIC_GCLOUD_PROJECT for samples https://github.com/googleapis/python-talent/blob/ef045e8eb348db36d7a2a611e6f26b11530d273b/samples/snippets/noxfile_config.py#L27-L32 `BUILD_SPECIFIC_GCLOUD_PROJECT` is an alternate project used for sample tests that do poorly with concurrent runs on the same project. Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Sep 30 13:06:03 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4 Source-Link: https://github.com/googleapis/synthtool/commit/9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4 --- .kokoro/samples/python3.6/common.cfg | 6 ++++++ .kokoro/samples/python3.7/common.cfg | 6 ++++++ .kokoro/samples/python3.8/common.cfg | 6 ++++++ synth.metadata | 4 ++-- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.kokoro/samples/python3.6/common.cfg b/.kokoro/samples/python3.6/common.cfg index b508c66b..ef2b0243 100644 --- a/.kokoro/samples/python3.6/common.cfg +++ b/.kokoro/samples/python3.6/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.6" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py36" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-texttospeech/.kokoro/test-samples.sh" diff --git a/.kokoro/samples/python3.7/common.cfg b/.kokoro/samples/python3.7/common.cfg index 48f4103b..83138745 100644 --- a/.kokoro/samples/python3.7/common.cfg +++ b/.kokoro/samples/python3.7/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.7" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py37" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-texttospeech/.kokoro/test-samples.sh" diff --git a/.kokoro/samples/python3.8/common.cfg b/.kokoro/samples/python3.8/common.cfg index 4d1900a8..b01fc11d 100644 --- a/.kokoro/samples/python3.8/common.cfg +++ b/.kokoro/samples/python3.8/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.8" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py38" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-texttospeech/.kokoro/test-samples.sh" diff --git a/synth.metadata b/synth.metadata index 42931bfe..012741b5 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d" + "sha": "9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "a651c5fb763c69a921aecdd3e1d8dc51dbf20f8d" + "sha": "9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4" } } ], From f90a13de2c20bd3b7d9b9d6bb1dea2b53fbc46e0 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:49:23 -0800 Subject: [PATCH 09/26] chore(python): use 'setup.py' to detect repo root Closes #792 Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Fri Oct 9 15:06:33 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: e0ae456852bf22f38796deb79cff30b516fde244 Source-Link: https://github.com/googleapis/synthtool/commit/e0ae456852bf22f38796deb79cff30b516fde244 --- samples/snippets/noxfile.py | 5 +++++ synth.metadata | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index ba55d7ce..01686e4a 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -201,6 +201,11 @@ def _get_repo_root(): break if Path(p / ".git").exists(): return str(p) + # .git is not available in repos cloned via Cloud Build + # setup.py is always in the library's root, so use that instead + # https://github.com/googleapis/synthtool/issues/792 + if Path(p / "setup.py").exists(): + return str(p) p = p.parent raise Exception("Unable to detect repository root.") diff --git a/synth.metadata b/synth.metadata index 012741b5..b6b6dee4 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4" + "sha": "e0ae456852bf22f38796deb79cff30b516fde244" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "9b0da5204ab90bcc36f8cd4e5689eff1a54cc3e4" + "sha": "e0ae456852bf22f38796deb79cff30b516fde244" } } ], From e79fb0b9c8693914df43caf7d750852db8dd2738 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:51:09 -0800 Subject: [PATCH 10/26] build(python): samples tests should pass if no samples exist Source-Author: Daniel Sanche Source-Date: Wed Oct 14 08:00:06 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 477764cc4ee6db346d3febef2bb1ea0abf27de52 Source-Link: https://github.com/googleapis/synthtool/commit/477764cc4ee6db346d3febef2bb1ea0abf27de52 --- .kokoro/test-samples.sh | 8 +++++++- synth.metadata | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.kokoro/test-samples.sh b/.kokoro/test-samples.sh index 51a72c73..86180393 100755 --- a/.kokoro/test-samples.sh +++ b/.kokoro/test-samples.sh @@ -28,6 +28,12 @@ if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then git checkout $LATEST_RELEASE fi +# Exit early if samples directory doesn't exist +if [ ! -d "./samples" ]; then + echo "No tests run. `./samples` not found" + exit 0 +fi + # Disable buffering, so that the logs stream through. export PYTHONUNBUFFERED=1 @@ -101,4 +107,4 @@ cd "$ROOT" # Workaround for Kokoro permissions issue: delete secrets rm testing/{test-env.sh,client-secrets.json,service-account.json} -exit "$RTN" \ No newline at end of file +exit "$RTN" diff --git a/synth.metadata b/synth.metadata index b6b6dee4..71c5705b 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "e0ae456852bf22f38796deb79cff30b516fde244" + "sha": "477764cc4ee6db346d3febef2bb1ea0abf27de52" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "e0ae456852bf22f38796deb79cff30b516fde244" + "sha": "477764cc4ee6db346d3febef2bb1ea0abf27de52" } } ], From d28c66268ee622cbcdf1afe468d6c33d49db830b Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:51:45 -0800 Subject: [PATCH 11/26] chore(python_library): change the docs bucket name Source-Author: Takashi Matsuo Source-Date: Fri Oct 16 09:58:05 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: da5c6050d13b4950c82666a81d8acd25157664ae Source-Link: https://github.com/googleapis/synthtool/commit/da5c6050d13b4950c82666a81d8acd25157664ae --- .kokoro/docs/common.cfg | 2 +- synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.kokoro/docs/common.cfg b/.kokoro/docs/common.cfg index 35fcc764..2d11b517 100644 --- a/.kokoro/docs/common.cfg +++ b/.kokoro/docs/common.cfg @@ -30,7 +30,7 @@ env_vars: { env_vars: { key: "V2_STAGING_BUCKET" - value: "docs-staging-v2-staging" + value: "docs-staging-v2" } # It will upload the docker image after successful builds. diff --git a/synth.metadata b/synth.metadata index 71c5705b..6ba63ebb 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "477764cc4ee6db346d3febef2bb1ea0abf27de52" + "sha": "da5c6050d13b4950c82666a81d8acd25157664ae" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "477764cc4ee6db346d3febef2bb1ea0abf27de52" + "sha": "da5c6050d13b4950c82666a81d8acd25157664ae" } } ], From 64b58855fceb258b70f6eda9769224d00cf1fcfd Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:52:57 -0800 Subject: [PATCH 12/26] chore(docs): update code of conduct of synthtool and templates Source-Author: Christopher Wilcox Source-Date: Thu Oct 22 14:22:01 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 5f6ef0ec5501d33c4667885b37a7685a30d41a76 Source-Link: https://github.com/googleapis/synthtool/commit/5f6ef0ec5501d33c4667885b37a7685a30d41a76 --- CODE_OF_CONDUCT.md | 123 ++++++++++++++++++++++++++++++++------------- synth.metadata | 4 +- 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index b3d1f602..039f4368 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,44 +1,95 @@ -# Contributor Code of Conduct +# Code of Conduct -As contributors and maintainers of this project, -and in the interest of fostering an open and welcoming community, -we pledge to respect all people who contribute through reporting issues, -posting feature requests, updating documentation, -submitting pull requests or patches, and other activities. +## Our Pledge -We are committed to making participation in this project -a harassment-free experience for everyone, -regardless of level of experience, gender, gender identity and expression, -sexual orientation, disability, personal appearance, -body size, race, ethnicity, age, religion, or nationality. +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of +experience, education, socio-economic status, nationality, personal appearance, +race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery -* Personal attacks -* Trolling or insulting/derogatory comments -* Public or private harassment -* Publishing other's private information, -such as physical or electronic -addresses, without explicit permission -* Other unethical or unprofessional conduct. +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject -comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct. -By adopting this Code of Conduct, -project maintainers commit themselves to fairly and consistently -applying these principles to every aspect of managing this project. -Project maintainers who do not follow or enforce the Code of Conduct -may be permanently removed from the project team. - -This code of conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. - -Instances of abusive, harassing, or otherwise unacceptable behavior -may be reported by opening an issue -or contacting one or more of the project maintainers. - -This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, -available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, or to ban temporarily or permanently any +contributor for other behaviors that they deem inappropriate, threatening, +offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +This Code of Conduct also applies outside the project spaces when the Project +Steward has a reasonable belief that an individual's behavior may have a +negative impact on the project or its community. + +## Conflict Resolution + +We do not believe that all conflict is bad; healthy debate and disagreement +often yield positive results. However, it is never okay to be disrespectful or +to engage in behavior that violates the project’s code of conduct. + +If you see someone violating the code of conduct, you are encouraged to address +the behavior directly with those involved. Many issues can be resolved quickly +and easily, and this gives people more control over the outcome of their +dispute. If you are unable to resolve the matter for any reason, or if the +behavior is threatening or harassing, report it. We are dedicated to providing +an environment where participants feel welcome and safe. + + +Reports should be directed to *googleapis-stewards@google.com*, the +Project Steward(s) for *Google Cloud Client Libraries*. It is the Project Steward’s duty to +receive and address reported violations of the code of conduct. They will then +work with a committee consisting of representatives from the Open Source +Programs Office and the Google Open Source Strategy team. If for any reason you +are uncomfortable reaching out to the Project Steward, please email +opensource@google.com. + +We will investigate every complaint, but you may not receive a direct response. +We will use our discretion in determining when and how to follow up on reported +incidents, which may range from not taking action to permanent expulsion from +the project and project-sponsored spaces. We will notify the accused of the +report and provide them an opportunity to discuss it before any action is taken. +The identity of the reporter will be omitted from the details of the report +supplied to the accused. In potentially harmful situations, such as ongoing +harassment or threats to anyone's safety, we may take action without notice. + +## Attribution + +This Code of Conduct is adapted from the Contributor Covenant, version 1.4, +available at +https://www.contributor-covenant.org/version/1/4/code-of-conduct.html \ No newline at end of file diff --git a/synth.metadata b/synth.metadata index 6ba63ebb..ef96bed2 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "da5c6050d13b4950c82666a81d8acd25157664ae" + "sha": "5f6ef0ec5501d33c4667885b37a7685a30d41a76" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "da5c6050d13b4950c82666a81d8acd25157664ae" + "sha": "5f6ef0ec5501d33c4667885b37a7685a30d41a76" } } ], From dba1c9830221b1ba96b4de5ad3fe275b5acdfb8a Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:54:08 -0800 Subject: [PATCH 13/26] docs: add proto-plus to intersphinx mapping Source-Author: Tim Swast Source-Date: Tue Oct 27 12:01:14 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: ea52b8a0bd560f72f376efcf45197fb7c8869120 Source-Link: https://github.com/googleapis/synthtool/commit/ea52b8a0bd560f72f376efcf45197fb7c8869120 --- docs/conf.py | 1 + synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 63d4fb20..37521b5b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -349,6 +349,7 @@ "google-auth": ("https://google-auth.readthedocs.io/en/stable", None), "google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,), "grpc": ("https://grpc.io/grpc/python/", None), + "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), } diff --git a/synth.metadata b/synth.metadata index ef96bed2..54740e51 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "5f6ef0ec5501d33c4667885b37a7685a30d41a76" + "sha": "ea52b8a0bd560f72f376efcf45197fb7c8869120" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "5f6ef0ec5501d33c4667885b37a7685a30d41a76" + "sha": "ea52b8a0bd560f72f376efcf45197fb7c8869120" } } ], From 5a797b0420e51156e8cfb1c4e616b486c7ab2f4d Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:55:02 -0800 Subject: [PATCH 14/26] fix(python_library): fix external unit test dependencies I recently submitted https://github.com/googleapis/synthtool/pull/811/files, allowing external dependencies for unit tests. This fixes a small missing comma bug Source-Author: Daniel Sanche Source-Date: Thu Oct 29 16:58:01 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 6542bd723403513626f61642fc02ddca528409aa Source-Link: https://github.com/googleapis/synthtool/commit/6542bd723403513626f61642fc02ddca528409aa --- noxfile.py | 4 +++- synth.metadata | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/noxfile.py b/noxfile.py index 6ae48790..db2bdc4e 100644 --- a/noxfile.py +++ b/noxfile.py @@ -72,7 +72,9 @@ def default(session): # Install all test dependencies, then install this package in-place. session.install("asyncmock", "pytest-asyncio") - session.install("mock", "pytest", "pytest-cov") + session.install( + "mock", "pytest", "pytest-cov", + ) session.install("-e", ".") # Run py.test against the unit tests. diff --git a/synth.metadata b/synth.metadata index 54740e51..6b9716d7 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "ea52b8a0bd560f72f376efcf45197fb7c8869120" + "sha": "6542bd723403513626f61642fc02ddca528409aa" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "ea52b8a0bd560f72f376efcf45197fb7c8869120" + "sha": "6542bd723403513626f61642fc02ddca528409aa" } } ], From 96b3968455da02bc5eae7a0284e857d119c6ab1e Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:55:37 -0800 Subject: [PATCH 15/26] chore: add type hint check Source-Author: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Source-Date: Wed Nov 4 17:36:32 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 3d3e94c4e02370f307a9a200b0c743c3d8d19f29 Source-Link: https://github.com/googleapis/synthtool/commit/3d3e94c4e02370f307a9a200b0c743c3d8d19f29 --- samples/snippets/noxfile.py | 9 ++++++++- synth.metadata | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index 01686e4a..4621115e 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -39,6 +39,10 @@ # You can opt out from the test for specific Python versions. 'ignored_versions': ["2.7"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + 'enforce_type_hints': False, + # An envvar key for determining the project id to use. Change it # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a # build specific Cloud project. You can also use your own string @@ -132,7 +136,10 @@ def _determine_local_import_names(start_dir): @nox.session def lint(session): - session.install("flake8", "flake8-import-order") + if not TEST_CONFIG['enforce_type_hints']: + session.install("flake8", "flake8-import-order") + else: + session.install("flake8", "flake8-import-order", "flake8-annotations") local_names = _determine_local_import_names(".") args = FLAKE8_COMMON_ARGS + [ diff --git a/synth.metadata b/synth.metadata index 6b9716d7..a28ebd30 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "6542bd723403513626f61642fc02ddca528409aa" + "sha": "3d3e94c4e02370f307a9a200b0c743c3d8d19f29" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "6542bd723403513626f61642fc02ddca528409aa" + "sha": "3d3e94c4e02370f307a9a200b0c743c3d8d19f29" } } ], From 2b46d0416801f5a0e065f83ffb4b3affd8ec41be Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:55:55 -0800 Subject: [PATCH 16/26] chore: add blacken to template Source-Author: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Source-Date: Thu Nov 5 15:22:03 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b Source-Link: https://github.com/googleapis/synthtool/commit/1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b --- samples/snippets/noxfile.py | 9 +++++++++ synth.metadata | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index 4621115e..17300976 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -148,7 +148,16 @@ def lint(session): "." ] session.run("flake8", *args) +# +# Black +# + +@nox.session +def blacken(session): + session.install("black") + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + session.run("black", *python_files) # # Sample Tests diff --git a/synth.metadata b/synth.metadata index a28ebd30..76624886 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "3d3e94c4e02370f307a9a200b0c743c3d8d19f29" + "sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "3d3e94c4e02370f307a9a200b0c743c3d8d19f29" + "sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b" } } ], From b360e101385d130cf23c966d99b5069a0f3012ca Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:57:25 -0800 Subject: [PATCH 17/26] fix: address lint issues Source-Author: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Source-Date: Thu Nov 12 11:30:49 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: e89175cf074dccc4babb4eca66ae913696e47a71 Source-Link: https://github.com/googleapis/synthtool/commit/e89175cf074dccc4babb4eca66ae913696e47a71 --- samples/snippets/noxfile.py | 1 + synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index 17300976..b90eef00 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -152,6 +152,7 @@ def lint(session): # Black # + @nox.session def blacken(session): session.install("black") diff --git a/synth.metadata b/synth.metadata index 76624886..7adf78a6 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b" + "sha": "e89175cf074dccc4babb4eca66ae913696e47a71" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b" + "sha": "e89175cf074dccc4babb4eca66ae913696e47a71" } } ], From 62fe3cfafd950f623e1c72bb22a9c1aa5c6bcfdb Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:58:01 -0800 Subject: [PATCH 18/26] docs(python): update intersphinx for grpc and auth * docs(python): update intersphinx for grpc and auth * use https for python intersphinx Co-authored-by: Tim Swast Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Nov 18 14:37:25 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: 9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9 Source-Link: https://github.com/googleapis/synthtool/commit/9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9 --- docs/conf.py | 9 ++++++--- synth.metadata | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 37521b5b..598c7b12 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -345,10 +345,13 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { - "python": ("http://python.readthedocs.org/en/latest/", None), - "google-auth": ("https://google-auth.readthedocs.io/en/stable", None), + "python": ("https://python.readthedocs.org/en/latest/", None), + "google-auth": ( + "https://googleapis.dev/python/google-auth/latest/index.html", + None, + ), "google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,), - "grpc": ("https://grpc.io/grpc/python/", None), + "grpc": ("https://grpc.github.io/grpc/python/", None), "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), } diff --git a/synth.metadata b/synth.metadata index 7adf78a6..dfa3dbb3 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "e89175cf074dccc4babb4eca66ae913696e47a71" + "sha": "9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "e89175cf074dccc4babb4eca66ae913696e47a71" + "sha": "9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9" } } ], From d04de9cc36fd0fb57c66a41f3b8a1ccdd43d9224 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:58:18 -0800 Subject: [PATCH 19/26] docs(python): fix intersphinx link for google-auth Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Thu Nov 19 10:16:05 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: a073c873f3928c561bdf87fdfbf1d081d1998984 Source-Link: https://github.com/googleapis/synthtool/commit/a073c873f3928c561bdf87fdfbf1d081d1998984 --- docs/conf.py | 5 +---- synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 598c7b12..acbea801 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -346,10 +346,7 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { "python": ("https://python.readthedocs.org/en/latest/", None), - "google-auth": ( - "https://googleapis.dev/python/google-auth/latest/index.html", - None, - ), + "google-auth": ("https://googleapis.dev/python/google-auth/latest/", None), "google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,), "grpc": ("https://grpc.github.io/grpc/python/", None), "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), diff --git a/synth.metadata b/synth.metadata index dfa3dbb3..195e671a 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9" + "sha": "a073c873f3928c561bdf87fdfbf1d081d1998984" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "9a7d9fbb7045c34c9d3d22c1ff766eeae51f04c9" + "sha": "a073c873f3928c561bdf87fdfbf1d081d1998984" } } ], From 51f97d5991001249a1efea747a4c634b71c8e0c0 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:59:12 -0800 Subject: [PATCH 20/26] chore: add config / docs for 'pre-commit' support Source-Author: Tres Seaver Source-Date: Tue Dec 1 16:01:20 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: 32af6da519a6b042e3da62008e2a75e991efb6b4 Source-Link: https://github.com/googleapis/synthtool/commit/32af6da519a6b042e3da62008e2a75e991efb6b4 --- .pre-commit-config.yaml | 17 +++++++++++++++++ CONTRIBUTING.rst | 10 ++++++++++ synth.metadata | 5 +++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..fb80a95e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml +- repo: https://github.com/psf/black + rev: 19.10b0 + hooks: + - id: black +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.8.4 + hooks: + - id: flake8 diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 214639e2..277528a3 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -111,6 +111,16 @@ Coding Style should point to the official ``googleapis`` checkout and the the branch should be the main branch on that remote (``master``). +- This repository contains configuration for the + `pre-commit `__ tool, which automates checking + our linters during a commit. If you have it installed on your ``$PATH``, + you can enable enforcing those checks via: + +.. code-block:: bash + + $ pre-commit install + pre-commit installed at .git/hooks/pre-commit + Exceptions to PEP8: - Many unit tests use a helper method, ``_call_fut`` ("FUT" is short for diff --git a/synth.metadata b/synth.metadata index 195e671a..02fa465c 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "a073c873f3928c561bdf87fdfbf1d081d1998984" + "sha": "32af6da519a6b042e3da62008e2a75e991efb6b4" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "a073c873f3928c561bdf87fdfbf1d081d1998984" + "sha": "32af6da519a6b042e3da62008e2a75e991efb6b4" } } ], @@ -95,6 +95,7 @@ ".kokoro/test-samples.sh", ".kokoro/trampoline.sh", ".kokoro/trampoline_v2.sh", + ".pre-commit-config.yaml", ".trampolinerc", "CODE_OF_CONDUCT.md", "CONTRIBUTING.rst", From be553d58534687798ce1450872a9b9e7e1d95861 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:59:12 -0800 Subject: [PATCH 21/26] chore(deps): update precommit hook pre-commit/pre-commit-hooks to v3.3.0 Source-Author: WhiteSource Renovate Source-Date: Wed Dec 2 17:18:24 2020 +0100 Source-Repo: googleapis/synthtool Source-Sha: 69629b64b83c6421d616be2b8e11795738ec8a6c Source-Link: https://github.com/googleapis/synthtool/commit/69629b64b83c6421d616be2b8e11795738ec8a6c --- .pre-commit-config.yaml | 2 +- synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fb80a95e..6ad83346 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 + rev: v3.3.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer diff --git a/synth.metadata b/synth.metadata index 02fa465c..e6894982 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "32af6da519a6b042e3da62008e2a75e991efb6b4" + "sha": "69629b64b83c6421d616be2b8e11795738ec8a6c" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "32af6da519a6b042e3da62008e2a75e991efb6b4" + "sha": "69629b64b83c6421d616be2b8e11795738ec8a6c" } } ], From 62af003806b5c54d6eb108367658b1ec1888d7f4 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:59:30 -0800 Subject: [PATCH 22/26] test(python): give filesystem paths to pytest-cov https://pytest-cov.readthedocs.io/en/latest/config.html The pytest-cov docs seem to suggest a filesystem path is expected. Source-Author: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Source-Date: Wed Dec 2 09:28:04 2020 -0700 Source-Repo: googleapis/synthtool Source-Sha: f94318521f63085b9ccb43d42af89f153fb39f15 Source-Link: https://github.com/googleapis/synthtool/commit/f94318521f63085b9ccb43d42af89f153fb39f15 --- noxfile.py | 5 ++--- synth.metadata | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/noxfile.py b/noxfile.py index db2bdc4e..5dcb1d54 100644 --- a/noxfile.py +++ b/noxfile.py @@ -81,9 +81,8 @@ def default(session): session.run( "py.test", "--quiet", - "--cov=google.cloud.texttospeech", - "--cov=google.cloud", - "--cov=tests.unit", + "--cov=google/cloud", + "--cov=tests/unit", "--cov-append", "--cov-config=.coveragerc", "--cov-report=", diff --git a/synth.metadata b/synth.metadata index e6894982..dd257d0c 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "69629b64b83c6421d616be2b8e11795738ec8a6c" + "sha": "f94318521f63085b9ccb43d42af89f153fb39f15" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "69629b64b83c6421d616be2b8e11795738ec8a6c" + "sha": "f94318521f63085b9ccb43d42af89f153fb39f15" } } ], From 81d81dc389f2e6f349f2422d6a3f060ba8a7b98a Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 08:59:48 -0800 Subject: [PATCH 23/26] chore: update noxfile.py.j2 * Update noxfile.py.j2 add changes from @glasnt to the template template to ensure that enforcing type hinting doesn't fail for repos with the sample noxfile (aka all samples repos) See https://github.com/GoogleCloudPlatform/python-docs-samples/pull/4869/files for context * fix typo Source-Author: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Source-Date: Thu Dec 3 13:44:30 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1 Source-Link: https://github.com/googleapis/synthtool/commit/18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1 --- samples/snippets/noxfile.py | 17 +++++++++-------- synth.metadata | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index b90eef00..bca0522e 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -17,6 +17,7 @@ import os from pathlib import Path import sys +from typing import Callable, Dict, List, Optional import nox @@ -68,7 +69,7 @@ TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) -def get_pytest_env_vars(): +def get_pytest_env_vars() -> Dict[str, str]: """Returns a dict for pytest invocation.""" ret = {} @@ -97,7 +98,7 @@ def get_pytest_env_vars(): # -def _determine_local_import_names(start_dir): +def _determine_local_import_names(start_dir: str) -> List[str]: """Determines all import names that should be considered "local". This is used when running the linter to insure that import order is @@ -135,7 +136,7 @@ def _determine_local_import_names(start_dir): @nox.session -def lint(session): +def lint(session: nox.sessions.Session) -> None: if not TEST_CONFIG['enforce_type_hints']: session.install("flake8", "flake8-import-order") else: @@ -154,7 +155,7 @@ def lint(session): @nox.session -def blacken(session): +def blacken(session: nox.sessions.Session) -> None: session.install("black") python_files = [path for path in os.listdir(".") if path.endswith(".py")] @@ -168,7 +169,7 @@ def blacken(session): PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] -def _session_tests(session, post_install=None): +def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None: """Runs py.test for a particular project.""" if os.path.exists("requirements.txt"): session.install("-r", "requirements.txt") @@ -194,7 +195,7 @@ def _session_tests(session, post_install=None): @nox.session(python=ALL_VERSIONS) -def py(session): +def py(session: nox.sessions.Session) -> None: """Runs py.test for a sample using the specified version of Python.""" if session.python in TESTED_VERSIONS: _session_tests(session) @@ -209,7 +210,7 @@ def py(session): # -def _get_repo_root(): +def _get_repo_root() -> Optional[str]: """ Returns the root folder of the project. """ # Get root of this repository. Assume we don't have directories nested deeper than 10 items. p = Path(os.getcwd()) @@ -232,7 +233,7 @@ def _get_repo_root(): @nox.session @nox.parametrize("path", GENERATED_READMES) -def readmegen(session, path): +def readmegen(session: nox.sessions.Session, path: str) -> None: """(Re-)generates the readme for a sample.""" session.install("jinja2", "pyyaml") dir_ = os.path.dirname(path) diff --git a/synth.metadata b/synth.metadata index dd257d0c..4f155135 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "f94318521f63085b9ccb43d42af89f153fb39f15" + "sha": "18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "f94318521f63085b9ccb43d42af89f153fb39f15" + "sha": "18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1" } } ], From a948fbf0eb424d8b6d864e166f02089792e8f4ad Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 09:01:00 -0800 Subject: [PATCH 24/26] chore(deps): update precommit hook pre-commit/pre-commit-hooks to v3.4.0 Co-authored-by: Tres Seaver Source-Author: WhiteSource Renovate Source-Date: Wed Dec 16 18:13:24 2020 +0100 Source-Repo: googleapis/synthtool Source-Sha: aa255b15d52b6d8950cca48cfdf58f7d27a60c8a Source-Link: https://github.com/googleapis/synthtool/commit/aa255b15d52b6d8950cca48cfdf58f7d27a60c8a --- .pre-commit-config.yaml | 2 +- synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6ad83346..a9024b15 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.3.0 + rev: v3.4.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer diff --git a/synth.metadata b/synth.metadata index 4f155135..1b3cc80a 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1" + "sha": "aa255b15d52b6d8950cca48cfdf58f7d27a60c8a" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "18c5dbdb4ac8cf75d4d8174e7b4558f48e76f8a1" + "sha": "aa255b15d52b6d8950cca48cfdf58f7d27a60c8a" } } ], From b78551be5fcabc4de48cbdb32f73cf5288df8dbc Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 09:01:36 -0800 Subject: [PATCH 25/26] docs(python): document adding Python 3.9 support, dropping 3.5 support Closes #787 Source-Author: Tres Seaver Source-Date: Thu Dec 17 16:08:02 2020 -0500 Source-Repo: googleapis/synthtool Source-Sha: b670a77a454f415d247907908e8ee7943e06d718 Source-Link: https://github.com/googleapis/synthtool/commit/b670a77a454f415d247907908e8ee7943e06d718 --- CONTRIBUTING.rst | 11 +++++------ synth.metadata | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 277528a3..d04e01cd 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -21,8 +21,8 @@ In order to add a feature: - The feature must be documented in both the API and narrative documentation. -- The feature must work fully on the following CPython versions: 2.7, - 3.5, 3.6, 3.7 and 3.8 on both UNIX and Windows. +- The feature must work fully on the following CPython versions: + 3.6, 3.7, 3.8 and 3.9 on both UNIX and Windows. - The feature must not add unnecessary dependencies (where "unnecessary" is of course subjective, but new dependencies should @@ -202,25 +202,24 @@ Supported Python Versions We support: -- `Python 3.5`_ - `Python 3.6`_ - `Python 3.7`_ - `Python 3.8`_ +- `Python 3.9`_ -.. _Python 3.5: https://docs.python.org/3.5/ .. _Python 3.6: https://docs.python.org/3.6/ .. _Python 3.7: https://docs.python.org/3.7/ .. _Python 3.8: https://docs.python.org/3.8/ +.. _Python 3.9: https://docs.python.org/3.9/ Supported versions can be found in our ``noxfile.py`` `config`_. .. _config: https://github.com/googleapis/python-texttospeech/blob/master/noxfile.py -Python 2.7 support is deprecated. All code changes should maintain Python 2.7 compatibility until January 1, 2020. We also explicitly decided to support Python 3 beginning with version -3.5. Reasons for this include: +3.6. Reasons for this include: - Encouraging use of newest versions of Python 3 - Taking the lead of `prominent`_ open-source `projects`_ diff --git a/synth.metadata b/synth.metadata index 1b3cc80a..7f481281 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "aa255b15d52b6d8950cca48cfdf58f7d27a60c8a" + "sha": "b670a77a454f415d247907908e8ee7943e06d718" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "aa255b15d52b6d8950cca48cfdf58f7d27a60c8a" + "sha": "b670a77a454f415d247907908e8ee7943e06d718" } } ], From d44468af72941b78c0a5c1090cc4566b6a5b2d61 Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Wed, 6 Jan 2021 09:02:30 -0800 Subject: [PATCH 26/26] chore: exclude `.nox` directories from linting The samples tests create `.nox` directories with all dependencies installed. These directories should be excluded from linting. I've tested this change locally, and it significantly speeds up linting on my machine. Source-Author: Tim Swast Source-Date: Tue Dec 22 13:04:04 2020 -0600 Source-Repo: googleapis/synthtool Source-Sha: 373861061648b5fe5e0ac4f8a38b32d639ee93e4 Source-Link: https://github.com/googleapis/synthtool/commit/373861061648b5fe5e0ac4f8a38b32d639ee93e4 --- .flake8 | 1 + synth.metadata | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.flake8 b/.flake8 index ed931638..29227d4c 100644 --- a/.flake8 +++ b/.flake8 @@ -26,6 +26,7 @@ exclude = *_pb2.py # Standard linting exemptions. + **/.nox/** __pycache__, .git, *.pyc, diff --git a/synth.metadata b/synth.metadata index 7f481281..be5f92f9 100644 --- a/synth.metadata +++ b/synth.metadata @@ -19,14 +19,14 @@ "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "b670a77a454f415d247907908e8ee7943e06d718" + "sha": "373861061648b5fe5e0ac4f8a38b32d639ee93e4" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "b670a77a454f415d247907908e8ee7943e06d718" + "sha": "373861061648b5fe5e0ac4f8a38b32d639ee93e4" } } ],