generator¶
The generator
module contains the code generation logic.
The core of this work is around the Generator
class,
which divides up the processing of individual templates.
-
class
gapic.generator.generator.
Generator
(api_schema: gapic.schema.api.API, *, templates: str = None)[source]¶ A protoc code generator for client libraries.
This class receives a
API
, a representation of the API schema, and provides an interface for getting aCodeGeneratorResponse
(which it does through rendering templates).Parameters: - api_schema (API) – An API schema object, which is sent to every
template as the
api
variable. - templates (str) – Optional. Path to the templates to be rendered. If this is not provided, the templates included with this application are used.
-
get_response
() → google.protobuf.compiler.plugin_pb2.CodeGeneratorResponse[source]¶ Return a
CodeGeneratorResponse
for this library.This is a complete response to be written to (usually) stdout, and thus read by
protoc
.Returns: A response describing appropriate files and contents. See plugin.proto
.Return type: CodeGeneratorResponse
- api_schema (API) – An API schema object, which is sent to every
template as the
-
class
gapic.generator.loader.
TemplateLoader
(searchpath, encoding='utf-8', followlinks=False)[source]¶ A jinja2 template loader that tracks what is left to be loaded.
This class behaves identically to
jinja2.FileSystemLoader
but provides methods to return templates segmented by type.There are two types of templates: templates that describe the API as a whole (and for which the template is rendered once per API), and templates describing a service (which are rendered once per service in the API).
-
api_templates
¶ Return the (public) templates tied to the API as a whole.
All templates in the
templates/
directory are included except:- Templates corresponding to services (in a
$service/
subdirectory) are excluded. Seeservice_templates()
. - Templates beginning with
_
are excluded.
When these templates are rendered, they are expected to be sent one and only one variable: an
API
object spelledapi
.Returns: A set of templates. Return type: Set[str] - Templates corresponding to services (in a
-
proto_templates
¶ Return the templates specific to each proto.
This corresponds to all of the templates with
$proto
in the filename or path.When these templates are rendered, they are expected to be sent two variables: an
API
object spelledapi
, and theProto
object being iterated over, spelledproto
. These templates are rendered once per proto, with a distinctproto
variable each time.Returns: A list of proto templates. Return type: Set[str]
-
service_templates
¶ Return the templates specific to each service.
This corresponds to all of the templates with
$service
in the filename or path.When these templates are rendered, they are expected to be sent two variables: an
API
object spelledapi
, and theService
object being iterated over, spelledservice
. These templates are rendered once per service, with a distinctservice
variable each time.Returns: A list of service templates. Return type: Set[str]
-