Skip to content

Commit

Permalink
[#17]: Fix gRPC contract generation
Browse files Browse the repository at this point in the history
  • Loading branch information
NymanRobin committed Dec 3, 2021
1 parent 76e6ce6 commit cf9e06f
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions fellowship/utilis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class GrpcGenerationError(Exception):
pass


def load_custom_schema(schema_file: str) -> dict:
"""Loads custom meta schema from the schemas directory
Expand Down Expand Up @@ -107,19 +111,21 @@ def get_grpc_service_descriptor(proto_file: str) -> 'descriptor':
path_for_proto_import = os.path.dirname(os.path.abspath(proto_file))
sys.path.append(path_for_proto_import)
grpc_services = services(os.path.basename(proto_file))
descriptor_name = get_descriptor_name(proto_file)
descriptor_name = get_service_descriptor_name(grpc_services)
return getattr(grpc_services, descriptor_name)


def get_descriptor_name(proto_file: str) -> str:
"""Returns the name of the grpc _descriptor based on protocol buffer file
def get_service_descriptor_name(grpc_services: 'descriptor') -> str:
"""Returns the name of the grpc service descriptor based on grpc descriptor
Args:
proto_file (str): path to proto_file
grpc_services ('descrptor'): grpc descriptor object
Returns:
str: Name of the _descriptor service
str: Name of the descriptor service
"""
descriptor_name = os.path.basename(proto_file).split(".")[0]
descriptor_name += "__pb2"
descriptor_name = next(prop for prop in dir(grpc_services) if "__pb2" in prop)
if descriptor_name is None:
raise GrpcGenerationError(f"Could not find DescriptorProto from the generated "
f"code. Searched for {descriptor_name}")
return descriptor_name

0 comments on commit cf9e06f

Please sign in to comment.