|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Optional |
| 5 | +import protogen |
| 6 | + |
| 7 | +# Import the protobuf definitions that have been generated with the official |
| 8 | +# protobuf plugin for python. |
| 9 | +import acme.longrunning.operations_pb2 |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class OperationInfo: |
| 14 | + response_type: protogen.Message |
| 15 | + metadata_type: protogen.Message |
| 16 | + |
| 17 | + |
| 18 | +def operation_info( |
| 19 | + registry: protogen.Registry, method: protogen.Method |
| 20 | +) -> Optional[OperationInfo]: |
| 21 | + # Any options set for the extension can be found in the `options` field of |
| 22 | + # the protobuf message they have been set on, here on the `Method`. |
| 23 | + pb = None |
| 24 | + for fd, msg in method.proto.options.ListFields(): |
| 25 | + if fd.number == acme.longrunning.operations_pb2.OPERATION_INFO_FIELD_NUMBER: |
| 26 | + pb = msg |
| 27 | + break |
| 28 | + |
| 29 | + if pb is None: |
| 30 | + # The extension has not been set on the method. |
| 31 | + return None |
| 32 | + |
| 33 | + # pb is at this point a protobuf message as generated by the offical |
| 34 | + # protobuf plugin for python. In the example the extensions message type is |
| 35 | + # `operations_pb2.OperationInfo`. |
| 36 | + # |
| 37 | + # `operations_pb2.OperationInfo` has two fields: `response_type` and |
| 38 | + # `metadata_type` which themselfes reference protobuf messages by their |
| 39 | + # name. These names might be fully qualified like "google.protobuf.Empty" or |
| 40 | + # not fully qualified like `WriteBookRequest` which would resolve to |
| 41 | + # `acme.library.v1.WritebookRequest` given the current scope is the |
| 42 | + # `acme.library-v1.Library.WriteBook` service method. To resolve non |
| 43 | + # fully-qualified message names given a current scope as reference, use |
| 44 | + # `protogen.Registry.resolve_message_type`. |
| 45 | + |
| 46 | + operation_info = OperationInfo( |
| 47 | + registry.resolve_message_type(method.full_name, pb.response_type), |
| 48 | + registry.resolve_message_type(method.full_name, pb.metadata_type), |
| 49 | + ) |
| 50 | + |
| 51 | + if operation_info.response_type is None: |
| 52 | + raise Exception( |
| 53 | + f"message {pb.response_type} could not be resolved from base {method.full_name}" |
| 54 | + ) |
| 55 | + |
| 56 | + if operation_info.metadata_type is None: |
| 57 | + raise Exception( |
| 58 | + f"message {pb.metadata_type} could not be resolved from base {method.full_name}" |
| 59 | + ) |
| 60 | + |
| 61 | + return operation_info |
| 62 | + |
| 63 | + |
| 64 | +def generate(gen: protogen.Plugin): |
| 65 | + for f in gen.files_to_generate: |
| 66 | + g = gen.new_generated_file( |
| 67 | + f.proto.name.replace(".proto", "_ext.py"), |
| 68 | + f.py_import_path, |
| 69 | + ) |
| 70 | + |
| 71 | + g.P("# Autogenerated code. DO NOT EDIT.") |
| 72 | + g.P(f'"""This is an module docstring."""') |
| 73 | + g.P() |
| 74 | + g.print_import() |
| 75 | + g.P() |
| 76 | + |
| 77 | + for service in f.services: |
| 78 | + g.P(f"class {service.py_ident.py_name}Client:") |
| 79 | + g.P(f" def __init__(self, host: str):") |
| 80 | + g.P(f" self.host = host") |
| 81 | + g.P() |
| 82 | + |
| 83 | + for method in service.methods: |
| 84 | + # Create a new class `<MethodName>Operation` for each response type |
| 85 | + # for each method that returns a acme.longrunning.Operation. |
| 86 | + if method.output.full_name == "acme.longrunning.Operation": |
| 87 | + op = operation_info(gen.registry, method) |
| 88 | + g.P(f"class {method.proto.name}Operation:") |
| 89 | + g.P(" def wait() -> ", method.output.py_ident, ":") |
| 90 | + g.P(" pass") |
| 91 | + g.P() |
| 92 | + g.P(" response_type:", op.response_type.py_ident) |
| 93 | + g.P(" metadata_type:", op.metadata_type.py_ident) |
| 94 | + |
| 95 | + # fmt: off |
| 96 | + g.P(f" def {method.py_name}(req: ", method.input.py_ident, ") -> ", method.output.py_ident, ":") |
| 97 | + g.P(f" pass") |
| 98 | + g.P() |
| 99 | + # fmt: on |
| 100 | + |
| 101 | + |
| 102 | +def options_py_import_func(proto_filename: str, proto_package: str): |
| 103 | + return protogen.PyImportPath( |
| 104 | + proto_filename.replace(".proto", "_ext").replace("/", ".") |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +opts = protogen.Options(py_import_func=options_py_import_func) |
| 109 | +opts.run(generate) |
0 commit comments