-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy path__init__.py
More file actions
222 lines (207 loc) · 8.3 KB
/
__init__.py
File metadata and controls
222 lines (207 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
# Copyright 2025 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.
#
import sys
import google.api_core as api_core
from google.cloud.speech_v1 import gapic_version as package_version
__version__ = package_version.__version__
if sys.version_info >= (3, 8): # pragma: NO COVER
from importlib import metadata
else: # pragma: NO COVER
# TODO(https://github.com/googleapis/python-api-core/issues/835): Remove
# this code path once we drop support for Python 3.7
import importlib_metadata as metadata
from .services.adaptation import AdaptationAsyncClient, AdaptationClient
from .services.speech import SpeechAsyncClient, SpeechClient
from .types.cloud_speech import (
LongRunningRecognizeMetadata,
LongRunningRecognizeRequest,
LongRunningRecognizeResponse,
RecognitionAudio,
RecognitionConfig,
RecognitionMetadata,
RecognizeRequest,
RecognizeResponse,
SpeakerDiarizationConfig,
SpeechAdaptationInfo,
SpeechContext,
SpeechRecognitionAlternative,
SpeechRecognitionResult,
StreamingRecognitionConfig,
StreamingRecognitionResult,
StreamingRecognizeRequest,
StreamingRecognizeResponse,
TranscriptOutputConfig,
WordInfo,
)
from .types.cloud_speech_adaptation import (
CreateCustomClassRequest,
CreatePhraseSetRequest,
DeleteCustomClassRequest,
DeletePhraseSetRequest,
GetCustomClassRequest,
GetPhraseSetRequest,
ListCustomClassesRequest,
ListCustomClassesResponse,
ListPhraseSetRequest,
ListPhraseSetResponse,
UpdateCustomClassRequest,
UpdatePhraseSetRequest,
)
from .types.resource import (
CustomClass,
PhraseSet,
SpeechAdaptation,
TranscriptNormalization,
)
if hasattr(api_core, "check_python_version") and hasattr(
api_core, "check_dependency_versions"
): # pragma: NO COVER
api_core.check_python_version("google.cloud.speech_v1") # type: ignore
api_core.check_dependency_versions("google.cloud.speech_v1") # type: ignore
else: # pragma: NO COVER
# An older version of api_core is installed which does not define the
# functions above. We do equivalent checks manually.
try:
import sys
import warnings
_py_version_str = sys.version.split()[0]
_package_label = "google.cloud.speech_v1"
if sys.version_info < (3, 9):
warnings.warn(
"You are using a non-supported Python version "
+ f"({_py_version_str}). Google will not post any further "
+ f"updates to {_package_label} supporting this Python version. "
+ "Please upgrade to the latest Python version, or at "
+ f"least to Python 3.9, and then update {_package_label}.",
FutureWarning,
)
if sys.version_info[:2] == (3, 9):
warnings.warn(
f"You are using a Python version ({_py_version_str}) "
+ f"which Google will stop supporting in {_package_label} in "
+ "January 2026. Please "
+ "upgrade to the latest Python version, or at "
+ "least to Python 3.10, before then, and "
+ f"then update {_package_label}.",
FutureWarning,
)
def parse_version_to_tuple(version_string: str):
"""Safely converts a semantic version string to a comparable tuple of integers.
Example: "4.25.8" -> (4, 25, 8)
Ignores non-numeric parts and handles common version formats.
Args:
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
Returns:
Tuple of integers for the parsed version string.
"""
parts = []
for part in version_string.split("."):
try:
parts.append(int(part))
except ValueError:
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
# This is a simplification compared to 'packaging.parse_version', but sufficient
# for comparing strictly numeric semantic versions.
break
return tuple(parts)
def _get_version(dependency_name):
try:
version_string: str = metadata.version(dependency_name)
parsed_version = parse_version_to_tuple(version_string)
return (parsed_version, version_string)
except Exception:
# Catch exceptions from metadata.version() (e.g., PackageNotFoundError)
# or errors during parse_version_to_tuple
return (None, "--")
_dependency_package = "google.protobuf"
_next_supported_version = "4.25.8"
_next_supported_version_tuple = (4, 25, 8)
_recommendation = " (we recommend 6.x)"
(_version_used, _version_used_string) = _get_version(_dependency_package)
if _version_used and _version_used < _next_supported_version_tuple:
warnings.warn(
f"Package {_package_label} depends on "
+ f"{_dependency_package}, currently installed at version "
+ f"{_version_used_string}. Future updates to "
+ f"{_package_label} will require {_dependency_package} at "
+ f"version {_next_supported_version} or higher{_recommendation}."
+ " Please ensure "
+ "that either (a) your Python environment doesn't pin the "
+ f"version of {_dependency_package}, so that updates to "
+ f"{_package_label} can require the higher version, or "
+ "(b) you manually update your Python environment to use at "
+ f"least version {_next_supported_version} of "
+ f"{_dependency_package}.",
FutureWarning,
)
except Exception:
warnings.warn(
"Could not determine the version of Python "
+ "currently being used. To continue receiving "
+ "updates for {_package_label}, ensure you are "
+ "using a supported version of Python; see "
+ "https://devguide.python.org/versions/"
)
from google.cloud.speech_v1.helpers import SpeechHelpers
# This class merges the auto-generated GAPIC client with handwritten helper methods.
# We ignore [misc] because mypy is flagging that both parent classes have a method
# named `streaming_recognize`,
# but their type signatures don't match.
# We ignore [no-redef] because of the name shadow with SpeechClient. We don't want
# to expose the GAPIC client without the helpers.
class SpeechClient(SpeechHelpers, SpeechClient): # type: ignore[no-redef, misc]
__doc__ = SpeechClient.__doc__
__all__ = (
"AdaptationAsyncClient",
"SpeechAsyncClient",
"AdaptationClient",
"CreateCustomClassRequest",
"CreatePhraseSetRequest",
"CustomClass",
"DeleteCustomClassRequest",
"DeletePhraseSetRequest",
"GetCustomClassRequest",
"GetPhraseSetRequest",
"ListCustomClassesRequest",
"ListCustomClassesResponse",
"ListPhraseSetRequest",
"ListPhraseSetResponse",
"LongRunningRecognizeMetadata",
"LongRunningRecognizeRequest",
"LongRunningRecognizeResponse",
"PhraseSet",
"RecognitionAudio",
"RecognitionConfig",
"RecognitionMetadata",
"RecognizeRequest",
"RecognizeResponse",
"SpeakerDiarizationConfig",
"SpeechAdaptation",
"SpeechAdaptationInfo",
"SpeechClient",
"SpeechContext",
"SpeechRecognitionAlternative",
"SpeechRecognitionResult",
"StreamingRecognitionConfig",
"StreamingRecognitionResult",
"StreamingRecognizeRequest",
"StreamingRecognizeResponse",
"TranscriptNormalization",
"TranscriptOutputConfig",
"UpdateCustomClassRequest",
"UpdatePhraseSetRequest",
"WordInfo",
)