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