_runtime_protos.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright 2020 The 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. import sys
  15. _REQUIRED_SYMBOLS = ("_protos", "_services", "_protos_and_services")
  16. _MINIMUM_VERSION = (3, 5, 0)
  17. _UNINSTALLED_TEMPLATE = "Install the grpcio-tools package (1.32.0+) to use the {} function."
  18. _VERSION_ERROR_TEMPLATE = "The {} function is only on available on Python 3.X interpreters."
  19. def _has_runtime_proto_symbols(mod):
  20. return all(hasattr(mod, sym) for sym in _REQUIRED_SYMBOLS)
  21. def _is_grpc_tools_importable():
  22. try:
  23. import grpc_tools # pylint: disable=unused-import
  24. return True
  25. except ImportError as e:
  26. # NOTE: It's possible that we're encountering a transitive ImportError, so
  27. # we check for that and re-raise if so.
  28. if "grpc_tools" not in e.args[0]:
  29. raise
  30. return False
  31. def _call_with_lazy_import(fn_name, protobuf_path):
  32. """Calls one of the three functions, lazily importing grpc_tools.
  33. Args:
  34. fn_name: The name of the function to import from grpc_tools.protoc.
  35. protobuf_path: The path to import.
  36. Returns:
  37. The appropriate module object.
  38. """
  39. if sys.version_info < _MINIMUM_VERSION:
  40. raise NotImplementedError(_VERSION_ERROR_TEMPLATE.format(fn_name))
  41. else:
  42. if not _is_grpc_tools_importable():
  43. raise NotImplementedError(_UNINSTALLED_TEMPLATE.format(fn_name))
  44. import grpc_tools.protoc
  45. if _has_runtime_proto_symbols(grpc_tools.protoc):
  46. fn = getattr(grpc_tools.protoc, '_' + fn_name)
  47. return fn(protobuf_path)
  48. else:
  49. raise NotImplementedError(_UNINSTALLED_TEMPLATE.format(fn_name))
  50. def protos(protobuf_path): # pylint: disable=unused-argument
  51. """Returns a module generated by the indicated .proto file.
  52. THIS IS AN EXPERIMENTAL API.
  53. Use this function to retrieve classes corresponding to message
  54. definitions in the .proto file.
  55. To inspect the contents of the returned module, use the dir function.
  56. For example:
  57. ```
  58. protos = grpc.protos("foo.proto")
  59. print(dir(protos))
  60. ```
  61. The returned module object corresponds to the _pb2.py file generated
  62. by protoc. The path is expected to be relative to an entry on sys.path
  63. and all transitive dependencies of the file should also be resolveable
  64. from an entry on sys.path.
  65. To completely disable the machinery behind this function, set the
  66. GRPC_PYTHON_DISABLE_DYNAMIC_STUBS environment variable to "true".
  67. Args:
  68. protobuf_path: The path to the .proto file on the filesystem. This path
  69. must be resolveable from an entry on sys.path and so must all of its
  70. transitive dependencies.
  71. Returns:
  72. A module object corresponding to the message code for the indicated
  73. .proto file. Equivalent to a generated _pb2.py file.
  74. """
  75. return _call_with_lazy_import("protos", protobuf_path)
  76. def services(protobuf_path): # pylint: disable=unused-argument
  77. """Returns a module generated by the indicated .proto file.
  78. THIS IS AN EXPERIMENTAL API.
  79. Use this function to retrieve classes and functions corresponding to
  80. service definitions in the .proto file, including both stub and servicer
  81. definitions.
  82. To inspect the contents of the returned module, use the dir function.
  83. For example:
  84. ```
  85. services = grpc.services("foo.proto")
  86. print(dir(services))
  87. ```
  88. The returned module object corresponds to the _pb2_grpc.py file generated
  89. by protoc. The path is expected to be relative to an entry on sys.path
  90. and all transitive dependencies of the file should also be resolveable
  91. from an entry on sys.path.
  92. To completely disable the machinery behind this function, set the
  93. GRPC_PYTHON_DISABLE_DYNAMIC_STUBS environment variable to "true".
  94. Args:
  95. protobuf_path: The path to the .proto file on the filesystem. This path
  96. must be resolveable from an entry on sys.path and so must all of its
  97. transitive dependencies.
  98. Returns:
  99. A module object corresponding to the stub/service code for the indicated
  100. .proto file. Equivalent to a generated _pb2_grpc.py file.
  101. """
  102. return _call_with_lazy_import("services", protobuf_path)
  103. def protos_and_services(protobuf_path): # pylint: disable=unused-argument
  104. """Returns a 2-tuple of modules corresponding to protos and services.
  105. THIS IS AN EXPERIMENTAL API.
  106. The return value of this function is equivalent to a call to protos and a
  107. call to services.
  108. To completely disable the machinery behind this function, set the
  109. GRPC_PYTHON_DISABLE_DYNAMIC_STUBS environment variable to "true".
  110. Args:
  111. protobuf_path: The path to the .proto file on the filesystem. This path
  112. must be resolveable from an entry on sys.path and so must all of its
  113. transitive dependencies.
  114. Returns:
  115. A 2-tuple of module objects corresponding to (protos(path), services(path)).
  116. """
  117. return _call_with_lazy_import("protos_and_services", protobuf_path)