• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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"""Shared implementation."""
15
16import logging
17
18import six
19
20import grpc
21from grpc._cython import cygrpc
22
23logging.basicConfig()
24_LOGGER = logging.getLogger(__name__)
25
26CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY = {
27    cygrpc.ConnectivityState.idle:
28    grpc.ChannelConnectivity.IDLE,
29    cygrpc.ConnectivityState.connecting:
30    grpc.ChannelConnectivity.CONNECTING,
31    cygrpc.ConnectivityState.ready:
32    grpc.ChannelConnectivity.READY,
33    cygrpc.ConnectivityState.transient_failure:
34    grpc.ChannelConnectivity.TRANSIENT_FAILURE,
35    cygrpc.ConnectivityState.shutdown:
36    grpc.ChannelConnectivity.SHUTDOWN,
37}
38
39CYGRPC_STATUS_CODE_TO_STATUS_CODE = {
40    cygrpc.StatusCode.ok: grpc.StatusCode.OK,
41    cygrpc.StatusCode.cancelled: grpc.StatusCode.CANCELLED,
42    cygrpc.StatusCode.unknown: grpc.StatusCode.UNKNOWN,
43    cygrpc.StatusCode.invalid_argument: grpc.StatusCode.INVALID_ARGUMENT,
44    cygrpc.StatusCode.deadline_exceeded: grpc.StatusCode.DEADLINE_EXCEEDED,
45    cygrpc.StatusCode.not_found: grpc.StatusCode.NOT_FOUND,
46    cygrpc.StatusCode.already_exists: grpc.StatusCode.ALREADY_EXISTS,
47    cygrpc.StatusCode.permission_denied: grpc.StatusCode.PERMISSION_DENIED,
48    cygrpc.StatusCode.unauthenticated: grpc.StatusCode.UNAUTHENTICATED,
49    cygrpc.StatusCode.resource_exhausted: grpc.StatusCode.RESOURCE_EXHAUSTED,
50    cygrpc.StatusCode.failed_precondition: grpc.StatusCode.FAILED_PRECONDITION,
51    cygrpc.StatusCode.aborted: grpc.StatusCode.ABORTED,
52    cygrpc.StatusCode.out_of_range: grpc.StatusCode.OUT_OF_RANGE,
53    cygrpc.StatusCode.unimplemented: grpc.StatusCode.UNIMPLEMENTED,
54    cygrpc.StatusCode.internal: grpc.StatusCode.INTERNAL,
55    cygrpc.StatusCode.unavailable: grpc.StatusCode.UNAVAILABLE,
56    cygrpc.StatusCode.data_loss: grpc.StatusCode.DATA_LOSS,
57}
58STATUS_CODE_TO_CYGRPC_STATUS_CODE = {
59    grpc_code: cygrpc_code
60    for cygrpc_code, grpc_code in six.iteritems(
61        CYGRPC_STATUS_CODE_TO_STATUS_CODE)
62}
63
64
65def encode(s):
66    if isinstance(s, bytes):
67        return s
68    else:
69        return s.encode('ascii')
70
71
72def decode(b):
73    if isinstance(b, str):
74        return b
75    else:
76        try:
77            return b.decode('utf8')
78        except UnicodeDecodeError:
79            _LOGGER.exception('Invalid encoding on %s', b)
80            return b.decode('latin1')
81
82
83def _transform(message, transformer, exception_message):
84    if transformer is None:
85        return message
86    else:
87        try:
88            return transformer(message)
89        except Exception:  # pylint: disable=broad-except
90            _LOGGER.exception(exception_message)
91            return None
92
93
94def serialize(message, serializer):
95    return _transform(message, serializer, 'Exception serializing message!')
96
97
98def deserialize(serialized_message, deserializer):
99    return _transform(serialized_message, deserializer,
100                      'Exception deserializing message!')
101
102
103def fully_qualified_method(group, method):
104    return '/{}/{}'.format(group, method)
105