grpcio-fips 1.70.0__4-cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- grpc/__init__.py +2348 -0
- grpc/_auth.py +80 -0
- grpc/_channel.py +2267 -0
- grpc/_common.py +183 -0
- grpc/_compression.py +71 -0
- grpc/_cython/__init__.py +13 -0
- grpc/_cython/_credentials/roots.pem +4337 -0
- grpc/_cython/_cygrpc/__init__.py +13 -0
- grpc/_cython/cygrpc.cp313-win_amd64.pyd +0 -0
- grpc/_grpcio_metadata.py +1 -0
- grpc/_interceptor.py +813 -0
- grpc/_observability.py +299 -0
- grpc/_plugin_wrapping.py +136 -0
- grpc/_runtime_protos.py +165 -0
- grpc/_server.py +1528 -0
- grpc/_simple_stubs.py +588 -0
- grpc/_typing.py +95 -0
- grpc/_utilities.py +222 -0
- grpc/aio/__init__.py +95 -0
- grpc/aio/_base_call.py +257 -0
- grpc/aio/_base_channel.py +364 -0
- grpc/aio/_base_server.py +385 -0
- grpc/aio/_call.py +764 -0
- grpc/aio/_channel.py +627 -0
- grpc/aio/_interceptor.py +1178 -0
- grpc/aio/_metadata.py +137 -0
- grpc/aio/_server.py +239 -0
- grpc/aio/_typing.py +43 -0
- grpc/aio/_utils.py +22 -0
- grpc/beta/__init__.py +13 -0
- grpc/beta/_client_adaptations.py +1015 -0
- grpc/beta/_metadata.py +56 -0
- grpc/beta/_server_adaptations.py +465 -0
- grpc/beta/implementations.py +345 -0
- grpc/beta/interfaces.py +163 -0
- grpc/beta/utilities.py +153 -0
- grpc/experimental/__init__.py +134 -0
- grpc/experimental/aio/__init__.py +16 -0
- grpc/experimental/gevent.py +27 -0
- grpc/experimental/session_cache.py +45 -0
- grpc/framework/__init__.py +13 -0
- grpc/framework/common/__init__.py +13 -0
- grpc/framework/common/cardinality.py +26 -0
- grpc/framework/common/style.py +24 -0
- grpc/framework/foundation/__init__.py +13 -0
- grpc/framework/foundation/abandonment.py +22 -0
- grpc/framework/foundation/callable_util.py +98 -0
- grpc/framework/foundation/future.py +219 -0
- grpc/framework/foundation/logging_pool.py +72 -0
- grpc/framework/foundation/stream.py +43 -0
- grpc/framework/foundation/stream_util.py +148 -0
- grpc/framework/interfaces/__init__.py +13 -0
- grpc/framework/interfaces/base/__init__.py +13 -0
- grpc/framework/interfaces/base/base.py +328 -0
- grpc/framework/interfaces/base/utilities.py +83 -0
- grpc/framework/interfaces/face/__init__.py +13 -0
- grpc/framework/interfaces/face/face.py +1084 -0
- grpc/framework/interfaces/face/utilities.py +245 -0
- grpcio_fips-1.70.0.dist-info/METADATA +55 -0
- grpcio_fips-1.70.0.dist-info/RECORD +63 -0
- grpcio_fips-1.70.0.dist-info/WHEEL +5 -0
- grpcio_fips-1.70.0.dist-info/licenses/LICENSE +610 -0
- grpcio_fips-1.70.0.dist-info/top_level.txt +1 -0
grpc/_observability.py
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
# Copyright 2023 The gRPC authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import abc
|
|
18
|
+
import contextlib
|
|
19
|
+
import logging
|
|
20
|
+
import threading
|
|
21
|
+
from typing import Any, Generator, Generic, List, Optional, TypeVar
|
|
22
|
+
|
|
23
|
+
from grpc._cython import cygrpc as _cygrpc
|
|
24
|
+
from grpc._typing import ChannelArgumentType
|
|
25
|
+
|
|
26
|
+
_LOGGER = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
_channel = Any # _channel.py imports this module.
|
|
29
|
+
ClientCallTracerCapsule = TypeVar("ClientCallTracerCapsule")
|
|
30
|
+
ServerCallTracerFactoryCapsule = TypeVar("ServerCallTracerFactoryCapsule")
|
|
31
|
+
|
|
32
|
+
_plugin_lock: threading.RLock = threading.RLock()
|
|
33
|
+
_OBSERVABILITY_PLUGIN: Optional["ObservabilityPlugin"] = None
|
|
34
|
+
_SERVICES_TO_EXCLUDE: List[bytes] = [
|
|
35
|
+
b"google.monitoring.v3.MetricService",
|
|
36
|
+
b"google.devtools.cloudtrace.v2.TraceService",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ServerCallTracerFactory:
|
|
41
|
+
"""An encapsulation of a ServerCallTracerFactory.
|
|
42
|
+
|
|
43
|
+
Instances of this class can be passed to a Channel as values for the
|
|
44
|
+
grpc.experimental.server_call_tracer_factory option
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def __init__(self, address):
|
|
48
|
+
self._address = address
|
|
49
|
+
|
|
50
|
+
def __int__(self):
|
|
51
|
+
return self._address
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ObservabilityPlugin(
|
|
55
|
+
Generic[ClientCallTracerCapsule, ServerCallTracerFactoryCapsule],
|
|
56
|
+
metaclass=abc.ABCMeta,
|
|
57
|
+
):
|
|
58
|
+
"""Abstract base class for observability plugin.
|
|
59
|
+
|
|
60
|
+
*This is a semi-private class that was intended for the exclusive use of
|
|
61
|
+
the gRPC team.*
|
|
62
|
+
|
|
63
|
+
The ClientCallTracerCapsule and ClientCallTracerCapsule created by this
|
|
64
|
+
plugin should be injected to gRPC core using observability_init at the
|
|
65
|
+
start of a program, before any channels/servers are built.
|
|
66
|
+
|
|
67
|
+
Any future methods added to this interface cannot have the
|
|
68
|
+
@abc.abstractmethod annotation.
|
|
69
|
+
|
|
70
|
+
Attributes:
|
|
71
|
+
_stats_enabled: A bool indicates whether tracing is enabled.
|
|
72
|
+
_tracing_enabled: A bool indicates whether stats(metrics) is enabled.
|
|
73
|
+
_registered_methods: A set which stores the registered method names in
|
|
74
|
+
bytes.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
_tracing_enabled: bool = False
|
|
78
|
+
_stats_enabled: bool = False
|
|
79
|
+
|
|
80
|
+
@abc.abstractmethod
|
|
81
|
+
def create_client_call_tracer(
|
|
82
|
+
self, method_name: bytes, target: bytes
|
|
83
|
+
) -> ClientCallTracerCapsule:
|
|
84
|
+
"""Creates a ClientCallTracerCapsule.
|
|
85
|
+
|
|
86
|
+
After register the plugin, if tracing or stats is enabled, this method
|
|
87
|
+
will be called after a call was created, the ClientCallTracer created
|
|
88
|
+
by this method will be saved to call context.
|
|
89
|
+
|
|
90
|
+
The ClientCallTracer is an object which implements `grpc_core::ClientCallTracer`
|
|
91
|
+
interface and wrapped in a PyCapsule using `client_call_tracer` as name.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
method_name: The method name of the call in byte format.
|
|
95
|
+
target: The channel target of the call in byte format.
|
|
96
|
+
registered_method: Whether this method is pre-registered.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
A PyCapsule which stores a ClientCallTracer object.
|
|
100
|
+
"""
|
|
101
|
+
raise NotImplementedError()
|
|
102
|
+
|
|
103
|
+
@abc.abstractmethod
|
|
104
|
+
def save_trace_context(
|
|
105
|
+
self, trace_id: str, span_id: str, is_sampled: bool
|
|
106
|
+
) -> None:
|
|
107
|
+
"""Saves the trace_id and span_id related to the current span.
|
|
108
|
+
|
|
109
|
+
After register the plugin, if tracing is enabled, this method will be
|
|
110
|
+
called after the server finished sending response.
|
|
111
|
+
|
|
112
|
+
This method can be used to propagate census context.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
trace_id: The identifier for the trace associated with the span as a
|
|
116
|
+
32-character hexadecimal encoded string,
|
|
117
|
+
e.g. 26ed0036f2eff2b7317bccce3e28d01f
|
|
118
|
+
span_id: The identifier for the span as a 16-character hexadecimal encoded
|
|
119
|
+
string. e.g. 113ec879e62583bc
|
|
120
|
+
is_sampled: A bool indicates whether the span is sampled.
|
|
121
|
+
"""
|
|
122
|
+
raise NotImplementedError()
|
|
123
|
+
|
|
124
|
+
@abc.abstractmethod
|
|
125
|
+
def create_server_call_tracer_factory(
|
|
126
|
+
self,
|
|
127
|
+
*,
|
|
128
|
+
xds: bool = False,
|
|
129
|
+
) -> Optional[ServerCallTracerFactoryCapsule]:
|
|
130
|
+
"""Creates a ServerCallTracerFactoryCapsule.
|
|
131
|
+
|
|
132
|
+
This method will be called at server initialization time to create a
|
|
133
|
+
ServerCallTracerFactory, which will be registered to gRPC core.
|
|
134
|
+
|
|
135
|
+
The ServerCallTracerFactory is an object which implements
|
|
136
|
+
`grpc_core::ServerCallTracerFactory` interface and wrapped in a PyCapsule
|
|
137
|
+
using `server_call_tracer_factory` as name.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
xds: Whether the server is xds server.
|
|
141
|
+
Returns:
|
|
142
|
+
A PyCapsule which stores a ServerCallTracerFactory object. Or None if
|
|
143
|
+
plugin decides not to create ServerCallTracerFactory.
|
|
144
|
+
"""
|
|
145
|
+
raise NotImplementedError()
|
|
146
|
+
|
|
147
|
+
@abc.abstractmethod
|
|
148
|
+
def record_rpc_latency(
|
|
149
|
+
self, method: str, target: str, rpc_latency: float, status_code: Any
|
|
150
|
+
) -> None:
|
|
151
|
+
"""Record the latency of the RPC.
|
|
152
|
+
|
|
153
|
+
After register the plugin, if stats is enabled, this method will be
|
|
154
|
+
called at the end of each RPC.
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
method: The fully-qualified name of the RPC method being invoked.
|
|
158
|
+
target: The target name of the RPC method being invoked.
|
|
159
|
+
rpc_latency: The latency for the RPC in seconds, equals to the time between
|
|
160
|
+
when the client invokes the RPC and when the client receives the status.
|
|
161
|
+
status_code: An element of grpc.StatusCode in string format representing the
|
|
162
|
+
final status for the RPC.
|
|
163
|
+
"""
|
|
164
|
+
raise NotImplementedError()
|
|
165
|
+
|
|
166
|
+
def set_tracing(self, enable: bool) -> None:
|
|
167
|
+
"""Enable or disable tracing.
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
enable: A bool indicates whether tracing should be enabled.
|
|
171
|
+
"""
|
|
172
|
+
self._tracing_enabled = enable
|
|
173
|
+
|
|
174
|
+
def set_stats(self, enable: bool) -> None:
|
|
175
|
+
"""Enable or disable stats(metrics).
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
enable: A bool indicates whether stats should be enabled.
|
|
179
|
+
"""
|
|
180
|
+
self._stats_enabled = enable
|
|
181
|
+
|
|
182
|
+
def save_registered_method(self, method_name: bytes) -> None:
|
|
183
|
+
"""Saves the method name to registered_method list.
|
|
184
|
+
|
|
185
|
+
When exporting metrics, method name for unregistered methods will be replaced
|
|
186
|
+
with 'other' by default.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
method_name: The method name in bytes.
|
|
190
|
+
"""
|
|
191
|
+
raise NotImplementedError()
|
|
192
|
+
|
|
193
|
+
@property
|
|
194
|
+
def tracing_enabled(self) -> bool:
|
|
195
|
+
return self._tracing_enabled
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def stats_enabled(self) -> bool:
|
|
199
|
+
return self._stats_enabled
|
|
200
|
+
|
|
201
|
+
@property
|
|
202
|
+
def observability_enabled(self) -> bool:
|
|
203
|
+
return self.tracing_enabled or self.stats_enabled
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@contextlib.contextmanager
|
|
207
|
+
def get_plugin() -> Generator[Optional[ObservabilityPlugin], None, None]:
|
|
208
|
+
"""Get the ObservabilityPlugin in _observability module.
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
The ObservabilityPlugin currently registered with the _observability
|
|
212
|
+
module. Or None if no plugin exists at the time of calling this method.
|
|
213
|
+
"""
|
|
214
|
+
with _plugin_lock:
|
|
215
|
+
yield _OBSERVABILITY_PLUGIN
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def set_plugin(observability_plugin: Optional[ObservabilityPlugin]) -> None:
|
|
219
|
+
"""Save ObservabilityPlugin to _observability module.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
observability_plugin: The ObservabilityPlugin to save.
|
|
223
|
+
|
|
224
|
+
Raises:
|
|
225
|
+
ValueError: If an ObservabilityPlugin was already registered at the
|
|
226
|
+
time of calling this method.
|
|
227
|
+
"""
|
|
228
|
+
global _OBSERVABILITY_PLUGIN # pylint: disable=global-statement
|
|
229
|
+
with _plugin_lock:
|
|
230
|
+
if observability_plugin and _OBSERVABILITY_PLUGIN:
|
|
231
|
+
raise ValueError("observability_plugin was already set!")
|
|
232
|
+
_OBSERVABILITY_PLUGIN = observability_plugin
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def observability_init(observability_plugin: ObservabilityPlugin) -> None:
|
|
236
|
+
"""Initialize observability with provided ObservabilityPlugin.
|
|
237
|
+
|
|
238
|
+
This method have to be called at the start of a program, before any
|
|
239
|
+
channels/servers are built.
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
observability_plugin: The ObservabilityPlugin to use.
|
|
243
|
+
|
|
244
|
+
Raises:
|
|
245
|
+
ValueError: If an ObservabilityPlugin was already registered at the
|
|
246
|
+
time of calling this method.
|
|
247
|
+
"""
|
|
248
|
+
set_plugin(observability_plugin)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def observability_deinit() -> None:
|
|
252
|
+
"""Clear the observability context, including ObservabilityPlugin and
|
|
253
|
+
ServerCallTracerFactory
|
|
254
|
+
|
|
255
|
+
This method have to be called after exit observability context so that
|
|
256
|
+
it's possible to re-initialize again.
|
|
257
|
+
"""
|
|
258
|
+
set_plugin(None)
|
|
259
|
+
_cygrpc.clear_server_call_tracer_factory()
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def maybe_record_rpc_latency(state: "_channel._RPCState") -> None:
|
|
263
|
+
"""Record the latency of the RPC, if the plugin is registered and stats is enabled.
|
|
264
|
+
|
|
265
|
+
This method will be called at the end of each RPC.
|
|
266
|
+
|
|
267
|
+
Args:
|
|
268
|
+
state: a grpc._channel._RPCState object which contains the stats related to the
|
|
269
|
+
RPC.
|
|
270
|
+
"""
|
|
271
|
+
# TODO(xuanwn): use channel args to exclude those metrics.
|
|
272
|
+
for exclude_prefix in _SERVICES_TO_EXCLUDE:
|
|
273
|
+
if exclude_prefix in state.method.encode("utf8"):
|
|
274
|
+
return
|
|
275
|
+
with get_plugin() as plugin:
|
|
276
|
+
if plugin and plugin.stats_enabled:
|
|
277
|
+
rpc_latency_s = state.rpc_end_time - state.rpc_start_time
|
|
278
|
+
rpc_latency_ms = rpc_latency_s * 1000
|
|
279
|
+
plugin.record_rpc_latency(
|
|
280
|
+
state.method, state.target, rpc_latency_ms, state.code
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def create_server_call_tracer_factory_option(xds: bool) -> ChannelArgumentType:
|
|
285
|
+
with get_plugin() as plugin:
|
|
286
|
+
if plugin and plugin.stats_enabled:
|
|
287
|
+
server_call_tracer_factory_address = (
|
|
288
|
+
_cygrpc.get_server_call_tracer_factory_address(plugin, xds)
|
|
289
|
+
)
|
|
290
|
+
if server_call_tracer_factory_address:
|
|
291
|
+
return (
|
|
292
|
+
(
|
|
293
|
+
"grpc.experimental.server_call_tracer_factory",
|
|
294
|
+
ServerCallTracerFactory(
|
|
295
|
+
server_call_tracer_factory_address
|
|
296
|
+
),
|
|
297
|
+
),
|
|
298
|
+
)
|
|
299
|
+
return ()
|
grpc/_plugin_wrapping.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Copyright 2015 gRPC authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import collections
|
|
16
|
+
import logging
|
|
17
|
+
import threading
|
|
18
|
+
from typing import Callable, Optional, Type
|
|
19
|
+
|
|
20
|
+
import grpc
|
|
21
|
+
from grpc import _common
|
|
22
|
+
from grpc._cython import cygrpc
|
|
23
|
+
from grpc._typing import MetadataType
|
|
24
|
+
|
|
25
|
+
_LOGGER = logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _AuthMetadataContext(
|
|
29
|
+
collections.namedtuple(
|
|
30
|
+
"AuthMetadataContext",
|
|
31
|
+
(
|
|
32
|
+
"service_url",
|
|
33
|
+
"method_name",
|
|
34
|
+
),
|
|
35
|
+
),
|
|
36
|
+
grpc.AuthMetadataContext,
|
|
37
|
+
):
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class _CallbackState(object):
|
|
42
|
+
def __init__(self):
|
|
43
|
+
self.lock = threading.Lock()
|
|
44
|
+
self.called = False
|
|
45
|
+
self.exception = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class _AuthMetadataPluginCallback(grpc.AuthMetadataPluginCallback):
|
|
49
|
+
_state: _CallbackState
|
|
50
|
+
_callback: Callable
|
|
51
|
+
|
|
52
|
+
def __init__(self, state: _CallbackState, callback: Callable):
|
|
53
|
+
self._state = state
|
|
54
|
+
self._callback = callback
|
|
55
|
+
|
|
56
|
+
def __call__(
|
|
57
|
+
self, metadata: MetadataType, error: Optional[Type[BaseException]]
|
|
58
|
+
):
|
|
59
|
+
with self._state.lock:
|
|
60
|
+
if self._state.exception is None:
|
|
61
|
+
if self._state.called:
|
|
62
|
+
raise RuntimeError(
|
|
63
|
+
"AuthMetadataPluginCallback invoked more than once!"
|
|
64
|
+
)
|
|
65
|
+
else:
|
|
66
|
+
self._state.called = True
|
|
67
|
+
else:
|
|
68
|
+
raise RuntimeError(
|
|
69
|
+
'AuthMetadataPluginCallback raised exception "{}"!'.format(
|
|
70
|
+
self._state.exception
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
if error is None:
|
|
74
|
+
self._callback(metadata, cygrpc.StatusCode.ok, None)
|
|
75
|
+
else:
|
|
76
|
+
self._callback(
|
|
77
|
+
None, cygrpc.StatusCode.internal, _common.encode(str(error))
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class _Plugin(object):
|
|
82
|
+
_metadata_plugin: grpc.AuthMetadataPlugin
|
|
83
|
+
|
|
84
|
+
def __init__(self, metadata_plugin: grpc.AuthMetadataPlugin):
|
|
85
|
+
self._metadata_plugin = metadata_plugin
|
|
86
|
+
self._stored_ctx = None
|
|
87
|
+
|
|
88
|
+
try:
|
|
89
|
+
import contextvars # pylint: disable=wrong-import-position
|
|
90
|
+
|
|
91
|
+
# The plugin may be invoked on a thread created by Core, which will not
|
|
92
|
+
# have the context propagated. This context is stored and installed in
|
|
93
|
+
# the thread invoking the plugin.
|
|
94
|
+
self._stored_ctx = contextvars.copy_context()
|
|
95
|
+
except ImportError:
|
|
96
|
+
# Support versions predating contextvars.
|
|
97
|
+
pass
|
|
98
|
+
|
|
99
|
+
def __call__(self, service_url: str, method_name: str, callback: Callable):
|
|
100
|
+
context = _AuthMetadataContext(
|
|
101
|
+
_common.decode(service_url), _common.decode(method_name)
|
|
102
|
+
)
|
|
103
|
+
callback_state = _CallbackState()
|
|
104
|
+
try:
|
|
105
|
+
self._metadata_plugin(
|
|
106
|
+
context, _AuthMetadataPluginCallback(callback_state, callback)
|
|
107
|
+
)
|
|
108
|
+
except Exception as exception: # pylint: disable=broad-except
|
|
109
|
+
_LOGGER.exception(
|
|
110
|
+
'AuthMetadataPluginCallback "%s" raised exception!',
|
|
111
|
+
self._metadata_plugin,
|
|
112
|
+
)
|
|
113
|
+
with callback_state.lock:
|
|
114
|
+
callback_state.exception = exception
|
|
115
|
+
if callback_state.called:
|
|
116
|
+
return
|
|
117
|
+
callback(
|
|
118
|
+
None, cygrpc.StatusCode.internal, _common.encode(str(exception))
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def metadata_plugin_call_credentials(
|
|
123
|
+
metadata_plugin: grpc.AuthMetadataPlugin, name: Optional[str]
|
|
124
|
+
) -> grpc.CallCredentials:
|
|
125
|
+
if name is None:
|
|
126
|
+
try:
|
|
127
|
+
effective_name = metadata_plugin.__name__
|
|
128
|
+
except AttributeError:
|
|
129
|
+
effective_name = metadata_plugin.__class__.__name__
|
|
130
|
+
else:
|
|
131
|
+
effective_name = name
|
|
132
|
+
return grpc.CallCredentials(
|
|
133
|
+
cygrpc.MetadataPluginCallCredentials(
|
|
134
|
+
_Plugin(metadata_plugin), _common.encode(effective_name)
|
|
135
|
+
)
|
|
136
|
+
)
|
grpc/_runtime_protos.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Copyright 2020 The gRPC authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import sys
|
|
16
|
+
import types
|
|
17
|
+
from typing import Tuple, Union
|
|
18
|
+
|
|
19
|
+
_REQUIRED_SYMBOLS = ("_protos", "_services", "_protos_and_services")
|
|
20
|
+
_MINIMUM_VERSION = (3, 5, 0)
|
|
21
|
+
|
|
22
|
+
_UNINSTALLED_TEMPLATE = (
|
|
23
|
+
"Install the grpcio-tools package (1.32.0+) to use the {} function."
|
|
24
|
+
)
|
|
25
|
+
_VERSION_ERROR_TEMPLATE = (
|
|
26
|
+
"The {} function is only on available on Python 3.X interpreters."
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _has_runtime_proto_symbols(mod: types.ModuleType) -> bool:
|
|
31
|
+
return all(hasattr(mod, sym) for sym in _REQUIRED_SYMBOLS)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _is_grpc_tools_importable() -> bool:
|
|
35
|
+
try:
|
|
36
|
+
import grpc_tools # pylint: disable=unused-import # pytype: disable=import-error
|
|
37
|
+
|
|
38
|
+
return True
|
|
39
|
+
except ImportError as e:
|
|
40
|
+
# NOTE: It's possible that we're encountering a transitive ImportError, so
|
|
41
|
+
# we check for that and re-raise if so.
|
|
42
|
+
if "grpc_tools" not in e.args[0]:
|
|
43
|
+
raise
|
|
44
|
+
return False
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _call_with_lazy_import(
|
|
48
|
+
fn_name: str, protobuf_path: str
|
|
49
|
+
) -> Union[types.ModuleType, Tuple[types.ModuleType, types.ModuleType]]:
|
|
50
|
+
"""Calls one of the three functions, lazily importing grpc_tools.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
fn_name: The name of the function to import from grpc_tools.protoc.
|
|
54
|
+
protobuf_path: The path to import.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
The appropriate module object.
|
|
58
|
+
"""
|
|
59
|
+
if sys.version_info < _MINIMUM_VERSION:
|
|
60
|
+
raise NotImplementedError(_VERSION_ERROR_TEMPLATE.format(fn_name))
|
|
61
|
+
else:
|
|
62
|
+
if not _is_grpc_tools_importable():
|
|
63
|
+
raise NotImplementedError(_UNINSTALLED_TEMPLATE.format(fn_name))
|
|
64
|
+
import grpc_tools.protoc # pytype: disable=import-error
|
|
65
|
+
|
|
66
|
+
if _has_runtime_proto_symbols(grpc_tools.protoc):
|
|
67
|
+
fn = getattr(grpc_tools.protoc, "_" + fn_name)
|
|
68
|
+
return fn(protobuf_path)
|
|
69
|
+
else:
|
|
70
|
+
raise NotImplementedError(_UNINSTALLED_TEMPLATE.format(fn_name))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def protos(protobuf_path): # pylint: disable=unused-argument
|
|
74
|
+
"""Returns a module generated by the indicated .proto file.
|
|
75
|
+
|
|
76
|
+
THIS IS AN EXPERIMENTAL API.
|
|
77
|
+
|
|
78
|
+
Use this function to retrieve classes corresponding to message
|
|
79
|
+
definitions in the .proto file.
|
|
80
|
+
|
|
81
|
+
To inspect the contents of the returned module, use the dir function.
|
|
82
|
+
For example:
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
protos = grpc.protos("foo.proto")
|
|
86
|
+
print(dir(protos))
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The returned module object corresponds to the _pb2.py file generated
|
|
90
|
+
by protoc. The path is expected to be relative to an entry on sys.path
|
|
91
|
+
and all transitive dependencies of the file should also be resolvable
|
|
92
|
+
from an entry on sys.path.
|
|
93
|
+
|
|
94
|
+
To completely disable the machinery behind this function, set the
|
|
95
|
+
GRPC_PYTHON_DISABLE_DYNAMIC_STUBS environment variable to "true".
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
protobuf_path: The path to the .proto file on the filesystem. This path
|
|
99
|
+
must be resolvable from an entry on sys.path and so must all of its
|
|
100
|
+
transitive dependencies.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
A module object corresponding to the message code for the indicated
|
|
104
|
+
.proto file. Equivalent to a generated _pb2.py file.
|
|
105
|
+
"""
|
|
106
|
+
return _call_with_lazy_import("protos", protobuf_path)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def services(protobuf_path): # pylint: disable=unused-argument
|
|
110
|
+
"""Returns a module generated by the indicated .proto file.
|
|
111
|
+
|
|
112
|
+
THIS IS AN EXPERIMENTAL API.
|
|
113
|
+
|
|
114
|
+
Use this function to retrieve classes and functions corresponding to
|
|
115
|
+
service definitions in the .proto file, including both stub and servicer
|
|
116
|
+
definitions.
|
|
117
|
+
|
|
118
|
+
To inspect the contents of the returned module, use the dir function.
|
|
119
|
+
For example:
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
services = grpc.services("foo.proto")
|
|
123
|
+
print(dir(services))
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The returned module object corresponds to the _pb2_grpc.py file generated
|
|
127
|
+
by protoc. The path is expected to be relative to an entry on sys.path
|
|
128
|
+
and all transitive dependencies of the file should also be resolvable
|
|
129
|
+
from an entry on sys.path.
|
|
130
|
+
|
|
131
|
+
To completely disable the machinery behind this function, set the
|
|
132
|
+
GRPC_PYTHON_DISABLE_DYNAMIC_STUBS environment variable to "true".
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
protobuf_path: The path to the .proto file on the filesystem. This path
|
|
136
|
+
must be resolvable from an entry on sys.path and so must all of its
|
|
137
|
+
transitive dependencies.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
A module object corresponding to the stub/service code for the indicated
|
|
141
|
+
.proto file. Equivalent to a generated _pb2_grpc.py file.
|
|
142
|
+
"""
|
|
143
|
+
return _call_with_lazy_import("services", protobuf_path)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def protos_and_services(protobuf_path): # pylint: disable=unused-argument
|
|
147
|
+
"""Returns a 2-tuple of modules corresponding to protos and services.
|
|
148
|
+
|
|
149
|
+
THIS IS AN EXPERIMENTAL API.
|
|
150
|
+
|
|
151
|
+
The return value of this function is equivalent to a call to protos and a
|
|
152
|
+
call to services.
|
|
153
|
+
|
|
154
|
+
To completely disable the machinery behind this function, set the
|
|
155
|
+
GRPC_PYTHON_DISABLE_DYNAMIC_STUBS environment variable to "true".
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
protobuf_path: The path to the .proto file on the filesystem. This path
|
|
159
|
+
must be resolvable from an entry on sys.path and so must all of its
|
|
160
|
+
transitive dependencies.
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
A 2-tuple of module objects corresponding to (protos(path), services(path)).
|
|
164
|
+
"""
|
|
165
|
+
return _call_with_lazy_import("protos_and_services", protobuf_path)
|