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