Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/grpc/beta/implementations.py
7771 views
# Copyright 2015-2016 gRPC authors.1#2# Licensed under the Apache License, Version 2.0 (the "License");3# you may not use this file except in compliance with the License.4# You may obtain a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.13"""Entry points into the Beta API of gRPC Python."""1415# threading is referenced from specification in this module.16import threading # pylint: disable=unused-import1718# interfaces, cardinality, and face are referenced from specification in this19# module.20import grpc21from grpc import _auth22from grpc.beta import _client_adaptations23from grpc.beta import _metadata24from grpc.beta import _server_adaptations25from grpc.beta import interfaces # pylint: disable=unused-import26from grpc.framework.common import cardinality # pylint: disable=unused-import27from grpc.framework.interfaces.face import \28face # pylint: disable=unused-import2930# pylint: disable=too-many-arguments3132ChannelCredentials = grpc.ChannelCredentials33ssl_channel_credentials = grpc.ssl_channel_credentials34CallCredentials = grpc.CallCredentials353637def metadata_call_credentials(metadata_plugin, name=None):3839def plugin(context, callback):4041def wrapped_callback(beta_metadata, error):42callback(_metadata.unbeta(beta_metadata), error)4344metadata_plugin(context, wrapped_callback)4546return grpc.metadata_call_credentials(plugin, name=name)474849def google_call_credentials(credentials):50"""Construct CallCredentials from GoogleCredentials.5152Args:53credentials: A GoogleCredentials object from the oauth2client library.5455Returns:56A CallCredentials object for use in a GRPCCallOptions object.57"""58return metadata_call_credentials(_auth.GoogleCallCredentials(credentials))596061access_token_call_credentials = grpc.access_token_call_credentials62composite_call_credentials = grpc.composite_call_credentials63composite_channel_credentials = grpc.composite_channel_credentials646566class Channel(object):67"""A channel to a remote host through which RPCs may be conducted.6869Only the "subscribe" and "unsubscribe" methods are supported for application70use. This class' instance constructor and all other attributes are71unsupported.72"""7374def __init__(self, channel):75self._channel = channel7677def subscribe(self, callback, try_to_connect=None):78"""Subscribes to this Channel's connectivity.7980Args:81callback: A callable to be invoked and passed an82interfaces.ChannelConnectivity identifying this Channel's connectivity.83The callable will be invoked immediately upon subscription and again for84every change to this Channel's connectivity thereafter until it is85unsubscribed.86try_to_connect: A boolean indicating whether or not this Channel should87attempt to connect if it is not already connected and ready to conduct88RPCs.89"""90self._channel.subscribe(callback, try_to_connect=try_to_connect)9192def unsubscribe(self, callback):93"""Unsubscribes a callback from this Channel's connectivity.9495Args:96callback: A callable previously registered with this Channel from having97been passed to its "subscribe" method.98"""99self._channel.unsubscribe(callback)100101102def insecure_channel(host, port):103"""Creates an insecure Channel to a remote host.104105Args:106host: The name of the remote host to which to connect.107port: The port of the remote host to which to connect.108If None only the 'host' part will be used.109110Returns:111A Channel to the remote host through which RPCs may be conducted.112"""113channel = grpc.insecure_channel(host if port is None else '%s:%d' %114(host, port))115return Channel(channel)116117118def secure_channel(host, port, channel_credentials):119"""Creates a secure Channel to a remote host.120121Args:122host: The name of the remote host to which to connect.123port: The port of the remote host to which to connect.124If None only the 'host' part will be used.125channel_credentials: A ChannelCredentials.126127Returns:128A secure Channel to the remote host through which RPCs may be conducted.129"""130channel = grpc.secure_channel(131host if port is None else '%s:%d' % (host, port), channel_credentials)132return Channel(channel)133134135class StubOptions(object):136"""A value encapsulating the various options for creation of a Stub.137138This class and its instances have no supported interface - it exists to define139the type of its instances and its instances exist to be passed to other140functions.141"""142143def __init__(self, host, request_serializers, response_deserializers,144metadata_transformer, thread_pool, thread_pool_size):145self.host = host146self.request_serializers = request_serializers147self.response_deserializers = response_deserializers148self.metadata_transformer = metadata_transformer149self.thread_pool = thread_pool150self.thread_pool_size = thread_pool_size151152153_EMPTY_STUB_OPTIONS = StubOptions(None, None, None, None, None, None)154155156def stub_options(host=None,157request_serializers=None,158response_deserializers=None,159metadata_transformer=None,160thread_pool=None,161thread_pool_size=None):162"""Creates a StubOptions value to be passed at stub creation.163164All parameters are optional and should always be passed by keyword.165166Args:167host: A host string to set on RPC calls.168request_serializers: A dictionary from service name-method name pair to169request serialization behavior.170response_deserializers: A dictionary from service name-method name pair to171response deserialization behavior.172metadata_transformer: A callable that given a metadata object produces173another metadata object to be used in the underlying communication on the174wire.175thread_pool: A thread pool to use in stubs.176thread_pool_size: The size of thread pool to create for use in stubs;177ignored if thread_pool has been passed.178179Returns:180A StubOptions value created from the passed parameters.181"""182return StubOptions(host, request_serializers, response_deserializers,183metadata_transformer, thread_pool, thread_pool_size)184185186def generic_stub(channel, options=None):187"""Creates a face.GenericStub on which RPCs can be made.188189Args:190channel: A Channel for use by the created stub.191options: A StubOptions customizing the created stub.192193Returns:194A face.GenericStub on which RPCs can be made.195"""196effective_options = _EMPTY_STUB_OPTIONS if options is None else options197return _client_adaptations.generic_stub(198channel._channel, # pylint: disable=protected-access199effective_options.host,200effective_options.metadata_transformer,201effective_options.request_serializers,202effective_options.response_deserializers)203204205def dynamic_stub(channel, service, cardinalities, options=None):206"""Creates a face.DynamicStub with which RPCs can be invoked.207208Args:209channel: A Channel for the returned face.DynamicStub to use.210service: The package-qualified full name of the service.211cardinalities: A dictionary from RPC method name to cardinality.Cardinality212value identifying the cardinality of the RPC method.213options: An optional StubOptions value further customizing the functionality214of the returned face.DynamicStub.215216Returns:217A face.DynamicStub with which RPCs can be invoked.218"""219effective_options = _EMPTY_STUB_OPTIONS if options is None else options220return _client_adaptations.dynamic_stub(221channel._channel, # pylint: disable=protected-access222service,223cardinalities,224effective_options.host,225effective_options.metadata_transformer,226effective_options.request_serializers,227effective_options.response_deserializers)228229230ServerCredentials = grpc.ServerCredentials231ssl_server_credentials = grpc.ssl_server_credentials232233234class ServerOptions(object):235"""A value encapsulating the various options for creation of a Server.236237This class and its instances have no supported interface - it exists to define238the type of its instances and its instances exist to be passed to other239functions.240"""241242def __init__(self, multi_method_implementation, request_deserializers,243response_serializers, thread_pool, thread_pool_size,244default_timeout, maximum_timeout):245self.multi_method_implementation = multi_method_implementation246self.request_deserializers = request_deserializers247self.response_serializers = response_serializers248self.thread_pool = thread_pool249self.thread_pool_size = thread_pool_size250self.default_timeout = default_timeout251self.maximum_timeout = maximum_timeout252253254_EMPTY_SERVER_OPTIONS = ServerOptions(None, None, None, None, None, None, None)255256257def server_options(multi_method_implementation=None,258request_deserializers=None,259response_serializers=None,260thread_pool=None,261thread_pool_size=None,262default_timeout=None,263maximum_timeout=None):264"""Creates a ServerOptions value to be passed at server creation.265266All parameters are optional and should always be passed by keyword.267268Args:269multi_method_implementation: A face.MultiMethodImplementation to be called270to service an RPC if the server has no specific method implementation for271the name of the RPC for which service was requested.272request_deserializers: A dictionary from service name-method name pair to273request deserialization behavior.274response_serializers: A dictionary from service name-method name pair to275response serialization behavior.276thread_pool: A thread pool to use in stubs.277thread_pool_size: The size of thread pool to create for use in stubs;278ignored if thread_pool has been passed.279default_timeout: A duration in seconds to allow for RPC service when280servicing RPCs that did not include a timeout value when invoked.281maximum_timeout: A duration in seconds to allow for RPC service when282servicing RPCs no matter what timeout value was passed when the RPC was283invoked.284285Returns:286A StubOptions value created from the passed parameters.287"""288return ServerOptions(multi_method_implementation, request_deserializers,289response_serializers, thread_pool, thread_pool_size,290default_timeout, maximum_timeout)291292293def server(service_implementations, options=None):294"""Creates an interfaces.Server with which RPCs can be serviced.295296Args:297service_implementations: A dictionary from service name-method name pair to298face.MethodImplementation.299options: An optional ServerOptions value further customizing the300functionality of the returned Server.301302Returns:303An interfaces.Server with which RPCs can be serviced.304"""305effective_options = _EMPTY_SERVER_OPTIONS if options is None else options306return _server_adaptations.server(307service_implementations, effective_options.multi_method_implementation,308effective_options.request_deserializers,309effective_options.response_serializers, effective_options.thread_pool,310effective_options.thread_pool_size)311312313