Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viridianforge/improve descriptor methods #32

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/grpc_requests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import Enum
from functools import partial
from typing import Any, Dict, Iterable, List, NamedTuple, Optional, Tuple, TypeVar, Union
import warnings

import grpc
from google.protobuf import descriptor_pb2, descriptor_pool as _descriptor_pool, message_factory
Expand All @@ -11,7 +12,7 @@
from google.protobuf.json_format import MessageToDict, ParseDict
from grpc_reflection.v1alpha import reflection_pb2, reflection_pb2_grpc

from .utils import describe_request, load_data
from .utils import describe_descriptor, describe_request, load_data

if sys.version_info >= (3, 8):
from typing import TypedDict # pylint: disable=no-name-in-module
Expand Down Expand Up @@ -307,8 +308,18 @@ def get_service_descriptor(self, service):
return self._desc_pool.FindServiceByName(service)

def describe_method_request(self, service, method):
warnings.warn(
"This function is deprecated, and will be removed in a future release. Use describe_request() instead.",
DeprecationWarning
)
return describe_request(self.get_method_descriptor(service, method))

def describe_request(self, service, method):
return describe_descriptor(self.get_method_descriptor(service, method).input_type)

def describe_response(self, service, method):
return describe_descriptor(self.get_method_descriptor(service, method).output_type)

def get_method_descriptor(self, service, method):
svc_desc = self.get_service_descriptor(service)
return svc_desc.FindMethodByName(method)
Expand Down
58 changes: 57 additions & 1 deletion src/grpc_requests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path
from google.protobuf.descriptor import MethodDescriptor
from google.protobuf.descriptor import Descriptor, EnumDescriptor, MethodDescriptor, OneofDescriptor

import warnings

# String descriptions of protobuf field types
FIELD_TYPES = [
Expand Down Expand Up @@ -35,7 +37,61 @@ def describe_request(method_descriptor: MethodDescriptor) -> dict:
:param method_descriptor: MethodDescriptor
:return: dict - a mapping of field names to their types
"""
warnings.warn(
"This function is deprecated, and will be removed in a future release. Use describe_descriptor() instead.",
DeprecationWarning
)
description = {}
for field in method_descriptor.input_type.fields:
description[field.name] = FIELD_TYPES[field.type-1]
return description

def describe_descriptor(descriptor: Descriptor, indent: int = 0) -> str:
"""
Prints a human readable description of a protobuf descriptor.
:param descriptor: Descriptor - a protobuf descriptor
:return: str - a human readable description of the descriptor
"""
description = descriptor.name
padding = "\t" * indent

if descriptor.enum_types:
description += f"\n{padding}Enums:"
for enum in descriptor.enum_types:
description += describe_enum_descriptor(enum, indent+1)

if descriptor.fields:
description += f"\n{padding}Fields:"
for field in descriptor.fields:
description += f"\n\t{padding}{field.name}: {FIELD_TYPES[field.type-1]}"

if descriptor.oneofs:
description += f"\n{padding}Oneofs:"
for oneof in descriptor.oneofs:
description += describe_oneof_descriptor(oneof, indent+1)

return description

def describe_enum_descriptor(enum_descriptor: EnumDescriptor, indent: int = 0) -> str:
"""
Prints a human readable description of a protobuf enum descriptor.
:param enum_descriptor: EnumDescriptor - a protobuf enum descriptor
:return: str - a human readable description of the enum descriptor
"""
padding = "\t" * indent
description = f"\n{padding}{enum_descriptor.name}:"
for value in enum_descriptor.values:
description += f"\n{padding}{value.name} = {value.number}"
return description

def describe_oneof_descriptor(oneof_descriptor: OneofDescriptor, indent: int = 0) -> str:
"""
Prints a human readable description of a protobuf oneof descriptor.
:param oneof_descriptor: OneofDescriptor - a protobuf oneof descriptor
:return: str - a human readable description of the oneof descriptor
"""
padding = "\t" * indent
description = f"\n{padding}{oneof_descriptor.name}:"
for field in oneof_descriptor.fields:
description += f"\n{padding}{field.name}: {FIELD_TYPES[field.type-1]}"
return description
22 changes: 22 additions & 0 deletions src/tests/reflection_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ def test_describe_method_request(client_tester_reflection_client):
request_description == expected_request_description
), f"Expected: {expected_request_description}, Actual: {request_description}"

def test_describe_request(client_tester_reflection_client):
request_description = \
client_tester_reflection_client.describe_request('client_tester.ClientTester', 'TestUnaryUnary')
expected_request_description = """TestRequest
Fields:
\tfactor: INT32
\treadings: FLOAT
\tuuid: UINT64
\tsample_flag: BOOL
\trequest_name: STRING
\textra_data: BYTES"""
assert request_description == expected_request_description

def test_describe_response(client_tester_reflection_client):
request_description = \
client_tester_reflection_client.describe_response('client_tester.ClientTester', 'TestUnaryUnary')
expected_response_description = """TestResponse
Fields:
\taverage: DOUBLE
\tfeedback: STRING"""
assert request_description == expected_response_description

def test_empty_body_request(helloworld_reflection_client):
response = helloworld_reflection_client.request('helloworld.Greeter', 'SayHello', {})
assert isinstance(response, dict)
Expand Down