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 a CodeGeneratorResponse (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
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. See service_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 spelled api.

Returns:A set of templates.
Return type:Set[str]
is_private(path)[source]

Return True if path is a private template, False otherwise.

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 spelled api, and the Proto object being iterated over, spelled proto. These templates are rendered once per proto, with a distinct proto 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 spelled api, and the Service object being iterated over, spelled service. These templates are rendered once per service, with a distinct service variable each time.

Returns:A list of service templates.
Return type:Set[str]