| 1 |
# Copyright 2020 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 |
"""Base implementation of reflection servicer.""" |
| 15 |
|
| 16 |
from google.protobuf import descriptor_pb2 |
| 17 |
from google.protobuf import descriptor_pool |
| 18 |
import grpc |
| 19 |
from grpc_reflection.v1alpha import reflection_pb2 as _reflection_pb2 |
| 20 |
from grpc_reflection.v1alpha import reflection_pb2_grpc as _reflection_pb2_grpc |
| 21 |
|
| 22 |
_POOL = descriptor_pool.Default() |
| 23 |
|
| 24 |
|
| 25 |
def _not_found_error(): |
| 26 |
return _reflection_pb2.ServerReflectionResponse( |
| 27 |
error_response=_reflection_pb2.ErrorResponse( |
| 28 |
error_code=grpc.StatusCode.NOT_FOUND.value[0], |
| 29 |
error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(), |
| 30 |
)) |
| 31 |
|
| 32 |
|
| 33 |
def _file_descriptor_response(descriptor): |
| 34 |
proto = descriptor_pb2.FileDescriptorProto() |
| 35 |
descriptor.CopyToProto(proto) |
| 36 |
serialized_proto = proto.SerializeToString() |
| 37 |
return _reflection_pb2.ServerReflectionResponse( |
| 38 |
file_descriptor_response=_reflection_pb2.FileDescriptorResponse( |
| 39 |
file_descriptor_proto=(serialized_proto,)),) |
| 40 |
|
| 41 |
|
| 42 |
class BaseReflectionServicer(_reflection_pb2_grpc.ServerReflectionServicer): |
| 43 |
"""Base class for reflection servicer.""" |
| 44 |
|
| 45 |
def __init__(self, service_names, pool=None): |
| 46 |
"""Constructor. |
| 47 |
|
| 48 |
Args: |
| 49 |
service_names: Iterable of fully-qualified service names available. |
| 50 |
pool: An optional DescriptorPool instance. |
| 51 |
""" |
| 52 |
self._service_names = tuple(sorted(service_names)) |
| 53 |
self._pool = _POOL if pool is None else pool |
| 54 |
|
| 55 |
def _file_by_filename(self, filename): |
| 56 |
try: |
| 57 |
descriptor = self._pool.FindFileByName(filename) |
| 58 |
except KeyError: |
| 59 |
return _not_found_error() |
| 60 |
else: |
| 61 |
return _file_descriptor_response(descriptor) |
| 62 |
|
| 63 |
def _file_containing_symbol(self, fully_qualified_name): |
| 64 |
try: |
| 65 |
descriptor = self._pool.FindFileContainingSymbol( |
| 66 |
fully_qualified_name) |
| 67 |
except KeyError: |
| 68 |
return _not_found_error() |
| 69 |
else: |
| 70 |
return _file_descriptor_response(descriptor) |
| 71 |
|
| 72 |
def _file_containing_extension(self, containing_type, extension_number): |
| 73 |
try: |
| 74 |
message_descriptor = self._pool.FindMessageTypeByName( |
| 75 |
containing_type) |
| 76 |
extension_descriptor = self._pool.FindExtensionByNumber( |
| 77 |
message_descriptor, extension_number) |
| 78 |
descriptor = self._pool.FindFileContainingSymbol( |
| 79 |
extension_descriptor.full_name) |
| 80 |
except KeyError: |
| 81 |
return _not_found_error() |
| 82 |
else: |
| 83 |
return _file_descriptor_response(descriptor) |
| 84 |
|
| 85 |
def _all_extension_numbers_of_type(self, containing_type): |
| 86 |
try: |
| 87 |
message_descriptor = self._pool.FindMessageTypeByName( |
| 88 |
containing_type) |
| 89 |
extension_numbers = tuple( |
| 90 |
sorted(extension.number for extension in |
| 91 |
self._pool.FindAllExtensions(message_descriptor))) |
| 92 |
except KeyError: |
| 93 |
return _not_found_error() |
| 94 |
else: |
| 95 |
return _reflection_pb2.ServerReflectionResponse( |
| 96 |
all_extension_numbers_response=_reflection_pb2. |
| 97 |
ExtensionNumberResponse( |
| 98 |
base_type_name=message_descriptor.full_name, |
| 99 |
extension_number=extension_numbers)) |
| 100 |
|
| 101 |
def _list_services(self): |
| 102 |
return _reflection_pb2.ServerReflectionResponse( |
| 103 |
list_services_response=_reflection_pb2.ListServiceResponse(service=[ |
| 104 |
_reflection_pb2.ServiceResponse(name=service_name) |
| 105 |
for service_name in self._service_names |
| 106 |
])) |
| 107 |
|
| 108 |
|
| 109 |
__all__ = ['BaseReflectionServicer'] |