_protoc_plugin.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """A `protoc` plugin that generates chattolib's Connect client stubs.
  2. Speaks the standard plugin protocol: reads a ``CodeGeneratorRequest`` from
  3. stdin, writes a ``CodeGeneratorResponse`` to stdout. For each proto that
  4. declares a ``service``, it emits one ``<name>_connect.py`` defining an async
  5. ``*ServiceClient(ConnectClient)`` and a sync ``*ServiceClientSync
  6. (ConnectClientSync)`` — both delegating to the shared runtime in
  7. ``chattolib._connect``.
  8. Installed as the console script ``protoc-gen-chattolib`` so ``protoc`` finds
  9. it on PATH and it is invoked with ``--chattolib_out=<dir>``.
  10. """
  11. from __future__ import annotations
  12. import importlib.util
  13. import os
  14. import sys
  15. from typing import Any
  16. _CODEGEN_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_codegen")
  17. def _load_codegen_module(name: str) -> Any:
  18. """Load a vendored codegen descriptor module directly from its file path.
  19. The descriptors live under ``chattolib._codegen``, but importing them through
  20. the ``chattolib`` package would execute ``chattolib/__init__.py`` (which
  21. eagerly imports the client) — undesirable for a standalone ``protoc`` plugin
  22. process. Loading by file path avoids that.
  23. """
  24. path = os.path.join(_CODEGEN_DIR, f"{name}.py")
  25. spec = importlib.util.spec_from_file_location(f"_chattolib_codegen_{name}", path)
  26. assert spec is not None
  27. mod = importlib.util.module_from_spec(spec)
  28. sys.modules[spec.name] = mod
  29. assert spec.loader is not None
  30. spec.loader.exec_module(mod)
  31. return mod
  32. def _load_descriptors() -> tuple[Any, Any]:
  33. return (
  34. _load_codegen_module("protoc_gen_request_pb2"),
  35. _load_codegen_module("protoc_gen_response_pb2"),
  36. )
  37. _g, _r = _load_descriptors()
  38. g: Any = _g
  39. r: Any = _r
  40. _HEADER = "# Generated by chattolib's protoc plugin (protoc-gen-chattolib). DO NOT EDIT!\n"
  41. def _method_name(m: Any) -> str:
  42. """snake_case the RPC name, matching Python method naming.
  43. Acronym-aware: consecutive capitals (an acronym like ``DM`` in
  44. ``StartDM``) do NOT get split apart — ``StartDM`` -> ``start_dm``,
  45. not ``start_d_m``. An underscore is inserted before an uppercase run
  46. only at a word boundary: after a lowercase/digit, or before the final
  47. capital of a run that is followed by a lowercase letter.
  48. """
  49. name = m.name
  50. out = []
  51. for i, ch in enumerate(name):
  52. if ch.isupper() and i:
  53. prev = name[i - 1]
  54. nxt = name[i + 1] if i + 1 < len(name) else ""
  55. if not prev.isupper() or nxt.islower():
  56. out.append("_")
  57. out.append(ch.lower())
  58. return "".join(out)
  59. def _pb2_module(fd: Any) -> str:
  60. """The import path of the pb2 module protoc emits for ``fd``."""
  61. name: str = fd.name
  62. return name[: -len(".proto")].replace("/", ".") + "_pb2"
  63. def _alias_for(module: str) -> str:
  64. """A valid, unique import alias for a pb2 module (dots -> underscores)."""
  65. return module.replace(".", "_")
  66. def _build_module_map(request: Any) -> dict[str, str]:
  67. """Map a fully-qualified message name (leading dot) to the pb2 module of
  68. the file that defines it.
  69. A service method's input/output may be a message declared in a *different*
  70. proto file than the service itself (e.g. ``MyAccountService`` in
  71. ``account.proto`` using ``ListExternalIdentitiesRequest`` from
  72. ``external_identities.proto``). The generated stub must import the module
  73. that actually defines the referenced message, not the service's own module.
  74. """
  75. mapping: dict[str, str] = {}
  76. for fd in request.proto_file:
  77. module = _pb2_module(fd)
  78. def walk(msgs: list[Any], prefix: list[str]) -> None:
  79. for mt in msgs:
  80. full = "." + ".".join([p for p in (fd.package, *prefix, mt.name) if p])
  81. mapping[full] = module
  82. walk(list(mt.nested_type), prefix + [mt.name])
  83. walk(list(fd.message_type), [])
  84. return mapping
  85. def _service_preamble(fd: Any, modules: list[str]) -> str:
  86. """The shared header + imports for a generated module (emitted once).
  87. ``modules`` is the (deduplicated) set of pb2 modules the file's services
  88. reference; each is imported under a unique alias so cross-file messages
  89. resolve to the module that defines them.
  90. """
  91. lines = [_HEADER, f"# source: {fd.name}", ""]
  92. for module in modules:
  93. lines.append(f"import {module} as {_alias_for(module)}")
  94. lines.append("from chattolib._connect import ConnectClient, ConnectClientSync, MethodInfo")
  95. lines += ["", ""]
  96. return "\n".join(lines)
  97. def _service_body(fd: Any, svc: Any, mod_by_fullname: dict[str, str]) -> str:
  98. """Render one service's async + sync client classes (no preamble)."""
  99. pkg = fd.package
  100. base = f"{pkg}.{svc.name}" if pkg else svc.name
  101. lines: list[str] = [f"class {svc.name}Client(ConnectClient):"]
  102. for m in svc.method:
  103. mn = _method_name(m)
  104. in_module = mod_by_fullname[m.input_type]
  105. out_module = mod_by_fullname[m.output_type]
  106. in_name = m.input_type.lstrip(".").split(".")[-1]
  107. out_name = m.output_type.lstrip(".").split(".")[-1]
  108. in_alias = _alias_for(in_module)
  109. out_alias = _alias_for(out_module)
  110. lines += [
  111. f" async def {mn}(self, request: {in_alias}.{in_name}, *,"
  112. f" headers: dict[str, str] | None = None) -> {out_alias}.{out_name}:",
  113. " return await self.execute_unary(",
  114. " request=request,",
  115. " method=MethodInfo(",
  116. f' name="{m.name}",',
  117. f' service_name="{base}",',
  118. f" input={in_alias}.{in_name},",
  119. f" output={out_alias}.{out_name},",
  120. " ),",
  121. " headers=headers,",
  122. " )",
  123. "",
  124. ]
  125. lines += [f"class {svc.name}ClientSync(ConnectClientSync):", ""]
  126. for m in svc.method:
  127. mn = _method_name(m)
  128. in_module = mod_by_fullname[m.input_type]
  129. out_module = mod_by_fullname[m.output_type]
  130. in_name = m.input_type.lstrip(".").split(".")[-1]
  131. out_name = m.output_type.lstrip(".").split(".")[-1]
  132. in_alias = _alias_for(in_module)
  133. out_alias = _alias_for(out_module)
  134. lines += [
  135. f" def {mn}(self, request: {in_alias}.{in_name}, *,"
  136. f" headers: dict[str, str] | None = None) -> {out_alias}.{out_name}:",
  137. " return self.execute_unary(",
  138. " request=request,",
  139. " method=MethodInfo(",
  140. f' name="{m.name}",',
  141. f' service_name="{base}",',
  142. f" input={in_alias}.{in_name},",
  143. f" output={out_alias}.{out_name},",
  144. " ),",
  145. " headers=headers,",
  146. " )",
  147. "",
  148. ]
  149. return "\n".join(lines)
  150. def generate(request: g.CodeGeneratorRequest) -> r.CodeGeneratorResponse:
  151. resp = r.CodeGeneratorResponse()
  152. # Tell protoc (>= the version that checks this) we can handle proto3
  153. # `optional` fields. Without this bit, recent protoc refuses to run the
  154. # plugin on any proto3 file that declares an optional field.
  155. resp.supported_features = r.CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL
  156. # file_to_generate is a repeated *string* (proto file names), not a list of
  157. # message objects — match by string membership.
  158. to_generate = set(request.file_to_generate)
  159. mod_by_fullname = _build_module_map(request)
  160. for fd in request.proto_file:
  161. if fd.name not in to_generate:
  162. continue
  163. services = [svc for svc in fd.service if svc.method]
  164. if not services:
  165. continue
  166. # One file per proto, containing every service it declares (e.g.
  167. # notifications.proto -> NotificationServiceClient and
  168. # NotificationPolicyServiceClient in the same module), matching the
  169. # historical connectrpc layout.
  170. required: list[str] = []
  171. seen: set[str] = set()
  172. for svc in services:
  173. for m in svc.method:
  174. for ref in (m.input_type, m.output_type):
  175. module = mod_by_fullname[ref]
  176. if module not in seen:
  177. seen.add(module)
  178. required.append(module)
  179. out = resp.file.add()
  180. out.name = fd.name.replace(".proto", "_connect.py")
  181. out.content = _service_preamble(fd, required) + "\n".join(
  182. _service_body(fd, svc, mod_by_fullname) for svc in services
  183. )
  184. return resp
  185. def main() -> None:
  186. data = sys.stdin.buffer.read()
  187. request = g.CodeGeneratorRequest.FromString(data)
  188. resp = generate(request)
  189. sys.stdout.buffer.write(resp.SerializeToString())
  190. if __name__ == "__main__":
  191. main()