__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2018 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. """gRPC's experimental APIs.
  15. These APIs are subject to be removed during any minor version release.
  16. """
  17. import copy
  18. import functools
  19. import sys
  20. import warnings
  21. import grpc
  22. from grpc._cython import cygrpc as _cygrpc
  23. _EXPERIMENTAL_APIS_USED = set()
  24. class ChannelOptions(object):
  25. """Indicates a channel option unique to gRPC Python.
  26. This enumeration is part of an EXPERIMENTAL API.
  27. Attributes:
  28. SingleThreadedUnaryStream: Perform unary-stream RPCs on a single thread.
  29. """
  30. SingleThreadedUnaryStream = "SingleThreadedUnaryStream"
  31. class UsageError(Exception):
  32. """Raised by the gRPC library to indicate usage not allowed by the API."""
  33. # It's important that there be a single insecure credentials object so that its
  34. # hash is deterministic and can be used for indexing in the simple stubs cache.
  35. _insecure_channel_credentials = grpc.ChannelCredentials(
  36. _cygrpc.channel_credentials_insecure())
  37. def insecure_channel_credentials():
  38. """Creates a ChannelCredentials for use with an insecure channel.
  39. THIS IS AN EXPERIMENTAL API.
  40. """
  41. return _insecure_channel_credentials
  42. class ExperimentalApiWarning(Warning):
  43. """A warning that an API is experimental."""
  44. def _warn_experimental(api_name, stack_offset):
  45. if api_name not in _EXPERIMENTAL_APIS_USED:
  46. _EXPERIMENTAL_APIS_USED.add(api_name)
  47. msg = ("'{}' is an experimental API. It is subject to change or ".
  48. format(api_name) +
  49. "removal between minor releases. Proceed with caution.")
  50. warnings.warn(msg, ExperimentalApiWarning, stacklevel=2 + stack_offset)
  51. def experimental_api(f):
  52. @functools.wraps(f)
  53. def _wrapper(*args, **kwargs):
  54. _warn_experimental(f.__name__, 1)
  55. return f(*args, **kwargs)
  56. return _wrapper
  57. def wrap_server_method_handler(wrapper, handler):
  58. """Wraps the server method handler function.
  59. The server implementation requires all server handlers being wrapped as
  60. RpcMethodHandler objects. This helper function ease the pain of writing
  61. server handler wrappers.
  62. Args:
  63. wrapper: A wrapper function that takes in a method handler behavior
  64. (the actual function) and returns a wrapped function.
  65. handler: A RpcMethodHandler object to be wrapped.
  66. Returns:
  67. A newly created RpcMethodHandler.
  68. """
  69. if not handler:
  70. return None
  71. if not handler.request_streaming:
  72. if not handler.response_streaming:
  73. # NOTE(lidiz) _replace is a public API:
  74. # https://docs.python.org/dev/library/collections.html
  75. return handler._replace(unary_unary=wrapper(handler.unary_unary))
  76. else:
  77. return handler._replace(unary_stream=wrapper(handler.unary_stream))
  78. else:
  79. if not handler.response_streaming:
  80. return handler._replace(stream_unary=wrapper(handler.stream_unary))
  81. else:
  82. return handler._replace(
  83. stream_stream=wrapper(handler.stream_stream))
  84. __all__ = (
  85. 'ChannelOptions',
  86. 'ExperimentalApiWarning',
  87. 'UsageError',
  88. 'insecure_channel_credentials',
  89. 'wrap_server_method_handler',
  90. )
  91. if sys.version_info > (3, 6):
  92. from grpc._simple_stubs import stream_stream
  93. from grpc._simple_stubs import stream_unary
  94. from grpc._simple_stubs import unary_stream
  95. from grpc._simple_stubs import unary_unary
  96. __all__ = __all__ + (unary_unary, unary_stream, stream_unary, stream_stream)