|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import io |
16 | 15 | import os |
17 | 16 | from typing import Any, Iterable, Mapping, Sequence |
18 | 17 |
|
19 | 18 | import jinja2 |
20 | 19 |
|
21 | | -from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest |
22 | 20 | from google.protobuf.compiler.plugin_pb2 import CodeGeneratorResponse |
23 | 21 |
|
24 | 22 | from gapic import utils |
|
30 | 28 | class Generator: |
31 | 29 | """A protoc code generator for client libraries. |
32 | 30 |
|
33 | | - This class receives a :class:`~.plugin_pb2.CodeGeneratorRequest` (as per |
34 | | - the protoc plugin contract), and provides an interface for getting |
35 | | - a :class:`~.plugin_pb2.CodeGeneratorResponse`. |
36 | | -
|
37 | | - That request with one or more protocol buffers which collectively |
38 | | - describe an API. |
| 31 | + This class receives a :class:`~.api.API`, a representation of the API |
| 32 | + schema, and provides an interface for getting a |
| 33 | + :class:`~.plugin_pb2.CodeGeneratorResponse` (which it does through |
| 34 | + rendering templates). |
39 | 35 |
|
40 | 36 | Args: |
41 | | - request (CodeGeneratorRequest): A request protocol buffer as provided |
42 | | - by protoc. See ``plugin.proto``. |
| 37 | + api_schema (~.API): An API schema object, which is sent to every |
| 38 | + template as the ``api`` variable. |
| 39 | + templates (str): Optional. Path to the templates to be |
| 40 | + rendered. If this is not provided, the templates included with |
| 41 | + this application are used. |
43 | 42 | """ |
44 | | - def __init__(self, api_schema: api.API) -> None: |
| 43 | + def __init__(self, api_schema: api.API, *, |
| 44 | + templates: str = None) -> None: |
45 | 45 | self._api = api_schema |
46 | 46 |
|
| 47 | + # If explicit templates were not provided, use our default. |
| 48 | + if not templates: |
| 49 | + templates = os.path.join( |
| 50 | + os.path.realpath(os.path.dirname(__file__)), |
| 51 | + '..', 'templates', |
| 52 | + ) |
| 53 | + |
47 | 54 | # Create the jinja environment with which to render templates. |
48 | 55 | self._env = jinja2.Environment( |
49 | | - loader=loader.TemplateLoader( |
50 | | - searchpath=os.path.join(_dirname, '..', 'templates'), |
51 | | - ), |
| 56 | + loader=loader.TemplateLoader(searchpath=templates), |
52 | 57 | undefined=jinja2.StrictUndefined, |
53 | 58 | ) |
54 | 59 |
|
@@ -173,9 +178,6 @@ def _get_output_filename( |
173 | 178 | return filename |
174 | 179 |
|
175 | 180 |
|
176 | | -_dirname = os.path.realpath(os.path.dirname(__file__)) |
177 | | - |
178 | | - |
179 | 181 | __all__ = ( |
180 | 182 | 'Generator', |
181 | 183 | ) |
0 commit comments