utilities.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. """Utilities for use with the base interface of RPC Framework."""
  15. import collections
  16. from grpc.framework.interfaces.base import base
  17. class _Completion(base.Completion,
  18. collections.namedtuple('_Completion', (
  19. 'terminal_metadata',
  20. 'code',
  21. 'message',
  22. ))):
  23. """A trivial implementation of base.Completion."""
  24. class _Subscription(base.Subscription,
  25. collections.namedtuple('_Subscription', (
  26. 'kind',
  27. 'termination_callback',
  28. 'allowance',
  29. 'operator',
  30. 'protocol_receiver',
  31. ))):
  32. """A trivial implementation of base.Subscription."""
  33. _NONE_SUBSCRIPTION = _Subscription(base.Subscription.Kind.NONE, None, None,
  34. None, None)
  35. def completion(terminal_metadata, code, message):
  36. """Creates a base.Completion aggregating the given operation values.
  37. Args:
  38. terminal_metadata: A terminal metadata value for an operaton.
  39. code: A code value for an operation.
  40. message: A message value for an operation.
  41. Returns:
  42. A base.Completion aggregating the given operation values.
  43. """
  44. return _Completion(terminal_metadata, code, message)
  45. def full_subscription(operator, protocol_receiver):
  46. """Creates a "full" base.Subscription for the given base.Operator.
  47. Args:
  48. operator: A base.Operator to be used in an operation.
  49. protocol_receiver: A base.ProtocolReceiver to be used in an operation.
  50. Returns:
  51. A base.Subscription of kind base.Subscription.Kind.FULL wrapping the given
  52. base.Operator and base.ProtocolReceiver.
  53. """
  54. return _Subscription(base.Subscription.Kind.FULL, None, None, operator,
  55. protocol_receiver)