schema

The schema module provides a normalized API representation.

In general, this module can be considered in three parts: wrappers, metadata, and a roll-up view of an API as a whole.

These three parts are divided into the three component modules.

api

This module contains the “roll-up” class, API. Everything else in the schema module is usually accessed through an API object.

class gapic.schema.api.API(naming: gapic.schema.naming.Naming, all_protos: Mapping[str, gapic.schema.api.Proto], service_yaml_config: google.api.service_pb2.Service, subpackage_view: Tuple[str, ...] = <factory>)[source]

A representation of a full API.

This represents a top-down view of a complete API, as loaded from a set of protocol buffer files. Once the descriptors are loaded (see load()), this object contains every message, method, service, and everything else needed to write a client library.

An instance of this object is made available to every template (as api).

classmethod build(file_descriptors: Sequence[google.protobuf.descriptor_pb2.FileDescriptorProto], package: str = '', opts: gapic.utils.options.Options = Options(name='', namespace=(), warehouse_package_name='', retry=None, sample_configs=(), autogen_snippets=False, templates=('DEFAULT',), lazy_import=False, old_naming=False, add_iam_methods=False, metadata=False, transport=[], service_yaml_config={}, PYTHON_GAPIC_PREFIX='python-gapic-', OPT_FLAGS=frozenset({'service-yaml', 'old-naming', 'samples', 'lazy-import', 'retry-config', 'autogen-snippets', 'add-iam-methods', 'warehouse-package-name', 'transport', 'metadata'})), prior_protos: Mapping[str, Proto] = None) → gapic.schema.api.API[source]

Build the internal API schema based on the request.

Parameters:
  • file_descriptors (Sequence[FileDescriptorProto]) – A list of FileDescriptorProto objects describing the API.
  • package (str) – A protocol buffer package, as a string, for which code should be explicitly generated (including subpackages). Protos with packages outside this list are considered imports rather than explicit targets.
  • opts (Options) – CLI options passed to the generator.
  • prior_protos (Proto) – Previous, already processed protos. These are needed to look up messages in imported protos. Primarily used for testing.
enums

Return a map of all enums available in the API.

http_options

Return a map of API-wide http rules.

messages

Return a map of all messages available in the API.

protos

Return a map of all protos specific to this API.

This property excludes imported protos that are dependencies of this API but not being directly generated.

services

Return a map of all services available in the API.

subpackages

Return a map of all subpackages, if any.

Each value in the mapping is another API object, but the protos property only shows protos belonging to the subpackage.

top_level_enums

Return a map of all messages that are NOT nested.

top_level_messages

Return a map of all messages that are NOT nested.

class gapic.schema.api.Proto(file_pb2: google.protobuf.descriptor_pb2.FileDescriptorProto, services: Mapping[str, gapic.schema.wrappers.Service], all_messages: Mapping[str, gapic.schema.wrappers.MessageType], all_enums: Mapping[str, gapic.schema.wrappers.EnumType], file_to_generate: bool, meta: gapic.schema.metadata.Metadata = <factory>)[source]

A representation of a particular proto file within an API.

classmethod build(file_descriptor: google.protobuf.descriptor_pb2.FileDescriptorProto, file_to_generate: bool, naming: gapic.schema.naming.Naming, opts: gapic.utils.options.Options = Options(name='', namespace=(), warehouse_package_name='', retry=None, sample_configs=(), autogen_snippets=False, templates=('DEFAULT', ), lazy_import=False, old_naming=False, add_iam_methods=False, metadata=False, transport=[], service_yaml_config={}, PYTHON_GAPIC_PREFIX='python-gapic-', OPT_FLAGS=frozenset({'service-yaml', 'old-naming', 'samples', 'lazy-import', 'retry-config', 'autogen-snippets', 'add-iam-methods', 'warehouse-package-name', 'transport', 'metadata'})), prior_protos: Mapping[str, Proto] = None, load_services: bool = True, all_resources: Optional[Mapping[str, gapic.schema.wrappers.MessageType]] = None) → gapic.schema.api.Proto[source]

Build and return a Proto instance.

Parameters:
  • file_descriptor (FileDescriptorProto) – The protocol buffer object describing the proto file.
  • file_to_generate (bool) – Whether this is a file which is to be directly generated, or a dependency.
  • naming (Naming) – The Naming instance associated with the API.
  • prior_protos (Proto) – Previous, already processed protos. These are needed to look up messages in imported protos.
  • load_services (bool) – Toggle whether the proto file should load its services. Not doing so enables a two-pass fix for LRO response and metadata types in certain situations.
disambiguate(string: str) → str[source]

Return a disambiguated string for the context of this proto.

This is used for avoiding naming collisions. Generally, this method returns the same string, but it returns a modified version if it will cause a naming collision with messages or fields in this proto.

enums

Return top-level enums on the proto.

messages

Return top-level messages on the proto.

module_name

Return the appropriate module name for this service.

Returns:
The module name for this service (which is the service
name in snake case).
Return type:str
names

Return a set of names used by this proto.

This is used for detecting naming collisions in the module names used for imports.

python_modules

Return a sequence of Python modules, for import.

The results of this method are in alphabetical order (by package, then module), and do not contain duplicates.

Returns:The package and module pair, intended for use in a from package import module type of statement.
Return type:Sequence[Tuple[str, str]]
resource_messages

Return the file level resources of the proto.

metadata

The metadata module defines schema for where data was parsed from. This library places every protocol buffer descriptor in a wrapper class (see wrappers) before loading it into the API object.

As we iterate over descriptors during the loading process, it is important to know where they came from, because sometimes protocol buffer types are referenced by fully-qualified string (e.g. method.input_type), and we want to resolve those references.

Additionally, protocol buffers stores data from the comments in the .proto in a separate structure, and this object model re-connects the comments with the things they describe for easy access in templates.

class gapic.schema.metadata.Address(name:str='', module:str='', module_path:Tuple[int, ...]=<factory>, package:Tuple[str, ...]=<factory>, parent:Tuple[str, ...]=<factory>, api_naming:gapic.schema.naming.Naming=<factory>, collisions:FrozenSet[str]=<factory>)[source]
child(child_name: str, path: Tuple[int, ...]) → gapic.schema.metadata.Address[source]

Return a new child of the current Address.

Parameters:child_name (str) – The name of the child node. This address’ name is appended to parent.
Returns:The new address object.
Return type:Address
module_alias

Return an appropriate module alias if necessary.

If the module name is not a collision, return empty string.

This method provides a mechanism for resolving naming conflicts, while still providing names that are fundamentally readable to users (albeit looking auto-generated).

proto

Return the proto selector for this type.

proto_package

Return the proto package for this type.

python_import

Return the Python import for this type.

rel(address: gapic.schema.metadata.Address) → str[source]

Return an identifier for this type, relative to the given address.

Similar to __str__(), but accepts an address (expected to be the module being written) and truncates the beginning module if the address matches the identifier’s address. Templates can use this in situations where otherwise they would refer to themselves.

Parameters:address (Address) – The address to compare against.
Returns:The appropriate identifier.
Return type:str
resolve(selector: str) → str[source]

Resolve a potentially-relative protobuf selector.

This takes a protobuf selector which may be fully-qualified (e.g. foo.bar.v1.Baz) or may be relative (Baz) and returns the fully-qualified version.

This method is naive and does not check to see if the message actually exists.

Parameters:selector (str) – A protobuf selector, either fully-qualified or relative.
Returns:An absolute selector.
Return type:str
sphinx

Return the Sphinx identifier for this type.

subpackage

Return the subpackage below the versioned module name, if any.

with_context(*, collisions: FrozenSet[str]) → gapic.schema.metadata.Address[source]

Return a derivative of this address with the provided context.

This method is used to address naming collisions. The returned Address object aliases module names to avoid naming collisions in the file being written.

class gapic.schema.metadata.BaseAddress(name:str='', module:str='', module_path:Tuple[int, ...]=<factory>, package:Tuple[str, ...]=<factory>, parent:Tuple[str, ...]=<factory>)[source]
class gapic.schema.metadata.FieldIdentifier(ident:gapic.schema.metadata.Address, repeated:bool)[source]
class gapic.schema.metadata.Metadata(address:gapic.schema.metadata.Address=<factory>, documentation:google.protobuf.descriptor_pb2.Location=<factory>)[source]
doc

Return the best comment.

This property prefers the leading comment if one is available, and falls back to a trailing comment or a detached comment otherwise.

If there are no comments, return empty string. (This means a template is always guaranteed to get a string.)

with_context(*, collisions: FrozenSet[str]) → gapic.schema.metadata.Metadata[source]

Return a derivative of this metadata with the provided context.

This method is used to address naming collisions. The returned Address object aliases module names to avoid naming collisions in the file being written.

naming

class gapic.schema.naming.Naming(name: str = '', namespace: Tuple[str, ...] = <factory>, version: str = '', product_name: str = '', proto_package: str = '', _warehouse_package_name: str = '')[source]

Naming data for an API.

This class contains the naming nomenclature used for this API within templates.

An concrete child of this object is made available to every template (as api.naming).

static build(*file_descriptors, opts: gapic.utils.options.Options = Options(name='', namespace=(), warehouse_package_name='', retry=None, sample_configs=(), autogen_snippets=False, templates=('DEFAULT', ), lazy_import=False, old_naming=False, add_iam_methods=False, metadata=False, transport=[], service_yaml_config={}, PYTHON_GAPIC_PREFIX='python-gapic-', OPT_FLAGS=frozenset({'service-yaml', 'old-naming', 'samples', 'lazy-import', 'retry-config', 'autogen-snippets', 'add-iam-methods', 'warehouse-package-name', 'transport', 'metadata'}))) → gapic.schema.naming.Naming[source]

Return a full Naming instance based on these file descriptors.

This is pieced together from the proto package names as well as the google.api.metadata file annotation. This information may be present in one or many files; this method is tolerant as long as the data does not conflict.

Parameters:file_descriptors (Iterable[FileDescriptorProto]) – A list of file descriptor protos. This list should only include the files actually targeted for output (not their imports).
Returns:
A Naming instance which is provided to
templates as part of the API.
Return type:Naming
Raises:ValueError – If the provided file descriptors contain contradictory information.
long_name

Return an appropriate title-cased long name.

module_name

Return the appropriate Python module name.

module_namespace

Return the appropriate Python module namespace as a tuple.

namespace_packages

Return the appropriate Python namespace packages.

versioned_module_name

Return the versiond module name (e.g. apiname_v1).

If there is no version, this is the same as module_name.

warehouse_package_name

Return the appropriate Python package name for Warehouse.

class gapic.schema.naming.NewNaming(name: str = '', namespace: Tuple[str, ...] = <factory>, version: str = '', product_name: str = '', proto_package: str = '', _warehouse_package_name: str = '')[source]
versioned_module_name

Return the versiond module name (e.g. apiname_v1).

If there is no version, this is the same as module_name.

class gapic.schema.naming.OldNaming(name: str = '', namespace: Tuple[str, ...] = <factory>, version: str = '', product_name: str = '', proto_package: str = '', _warehouse_package_name: str = '')[source]
versioned_module_name

Return the versiond module name (e.g. apiname_v1).

If there is no version, this is the same as module_name.

wrappers

Module containing wrapper classes around meta-descriptors.

This module contains dataclasses which wrap the descriptor protos defined in google/protobuf/descriptor.proto (which are descriptors that describe descriptors).

These wrappers exist in order to provide useful helper methods and generally ease access to things in templates (in particular, documentation, certain aggregate views of things, etc.)

Reading of underlying descriptor properties in templates is okay, a __getattr__ method which consistently routes in this way is provided. Documentation is consistently at {thing}.meta.doc.

class gapic.schema.wrappers.CommonResource(type_name:str, pattern:str)[source]
class gapic.schema.wrappers.EnumType(enum_pb: google.protobuf.descriptor_pb2.EnumDescriptorProto, values: List[gapic.schema.wrappers.EnumValueType], meta: gapic.schema.metadata.Metadata = <factory>)[source]

Description of an enum (defined with the enum keyword.)

ident

Return the identifier data to be used in templates.

options_dict

Return the EnumOptions (if present) as a dict.

This is a hack to support a pythonic structure representation for the generator templates.

with_context(*, collisions: FrozenSet[str]) → gapic.schema.wrappers.EnumType[source]

Return a derivative of this enum with the provided context.

This method is used to address naming collisions. The returned EnumType object aliases module names to avoid naming collisions in the file being written.

class gapic.schema.wrappers.EnumValueType(enum_value_pb: google.protobuf.descriptor_pb2.EnumValueDescriptorProto, meta: gapic.schema.metadata.Metadata = <factory>)[source]

Description of an enum value.

class gapic.schema.wrappers.Field(field_pb: google.protobuf.descriptor_pb2.FieldDescriptorProto, message: Optional[MessageType] = None, enum: Optional[EnumType] = None, meta: gapic.schema.metadata.Metadata = <factory>, oneof: Optional[str] = None)[source]

Description of a field.

ident

Return the identifier to be used in templates.

inner_mock(stack, visited_fields) → str[source]

Return a repr of a valid, usually truthy mock value.

is_primitive

Return True if the field is a primitive, False otherwise.

map

Return True if this field is a map, False otherwise.

name

Used to prevent collisions with python keywords

primitive_mock(suffix: int = 0) → Union[str, bytes, int, float, List[Any], None][source]

Generate a valid mock for a primitive type. This function returns the original (Python) type.

If a suffix is provided, generate a slightly different mock using the provided integer.

primitive_mock_as_str() → str[source]

Like primitive mock, but return the mock as a string.

proto_type

Return the proto type constant to be used in templates.

repeated

Return True if this is a repeated field, False otherwise.

Returns:Whether this field is repeated.
Return type:bool
required

Return True if this is a required field, False otherwise.

Returns:Whether this field is required.
Return type:bool
resource_reference

Return a resource reference type if it exists.

This is only applicable for string fields. Example: “translate.googleapis.com/Glossary”

type

Return the type of this field.

with_context(*, collisions: FrozenSet[str], visited_messages: FrozenSet[MessageType]) → gapic.schema.wrappers.Field[source]

Return a derivative of this field with the provided context.

This method is used to address naming collisions. The returned Field object aliases module names to avoid naming collisions in the file being written.

class gapic.schema.wrappers.HttpRule(method: str, uri: str, body: Optional[str])[source]

Representation of the method’s http bindings.

path_fields(method: ~.Method) → List[Tuple[gapic.schema.wrappers.Field, str, str]][source]

return list of (name, template) tuples extracted from uri.

sample_request(method: ~.Method) → str[source]

return json dict for sample request matching the uri template.

class gapic.schema.wrappers.MessageType(message_pb: google.protobuf.descriptor_pb2.DescriptorProto, fields: Mapping[str, gapic.schema.wrappers.Field], nested_enums: Mapping[str, EnumType], nested_messages: Mapping[str, MessageType], meta: gapic.schema.metadata.Metadata = <factory>, oneofs: Optional[Mapping[str, Oneof]] = None)[source]

Description of a message (defined with the message keyword).

get_field(*field_path, collisions: FrozenSet[str] = frozenset()) → gapic.schema.wrappers.Field[source]

Return a field arbitrarily deep in this message’s structure.

This method recursively traverses the message tree to return the requested inner-field.

Traversing through repeated fields is not supported; a repeated field may be specified if and only if it is the last field in the path.

Parameters:field_path (Sequence[str]) – The field path.
Returns:A field object.
Return type:Field
Raises:KeyError – If a repeated field is used in the non-terminal position in the path.
ident

Return the identifier data to be used in templates.

map

Return True if the given message is a map, False otherwise.

recursive_field_types

Return all composite fields used in this proto’s messages.

resource_path

If this message describes a resource, return the path to the resource. If there are multiple paths, returns the first one.

with_context(*, collisions: FrozenSet[str], skip_fields: bool = False, visited_messages: FrozenSet[MessageType] = frozenset()) → gapic.schema.wrappers.MessageType[source]

Return a derivative of this message with the provided context.

This method is used to address naming collisions. The returned MessageType object aliases module names to avoid naming collisions in the file being written.

The skip_fields argument will omit applying the context to the underlying fields. This provides for an “exit” in the case of circular references.

class gapic.schema.wrappers.Method(method_pb: google.protobuf.descriptor_pb2.MethodDescriptorProto, input: gapic.schema.wrappers.MessageType, output: gapic.schema.wrappers.MessageType, lro: Optional[gapic.schema.wrappers.OperationInfo] = None, retry: Optional[gapic.schema.wrappers.RetryInfo] = None, timeout: Optional[float] = None, meta: gapic.schema.metadata.Metadata = <factory>)[source]

Description of a method (defined with the rpc keyword).

field_headers

Return the field headers defined for this method.

grpc_stub_type

Return the type of gRPC stub to use.

http_opt

Return the (main) http option for this method.

e.g. {‘verb’: ‘post’
‘url’: ‘/some/path’ ‘body’: ‘*’}
http_options

Return a list of the http bindings for this method.

idempotent

Return True if we know this method is idempotent, False otherwise.

Note: We are intentionally conservative here. It is far less bad to falsely believe an idempotent method is non-idempotent than the converse.

ident

Return the identifier data to be used in templates.

is_deprecated

Returns true if the method is deprecated, false otherwise.

legacy_flattened_fields

top level fields only, required fields first

Type:Return the legacy flattening interface
paged_result_field

Return the response pagination field if the method is paginated.

path_params

Return the path parameters found in the http annotation path template

query_params

Return query parameters for API call as determined by http annotation and grpc transcoding

void

Return True if this method has no return value, False otherwise.

with_context(*, collisions: FrozenSet[str]) → gapic.schema.wrappers.Method[source]

Return a derivative of this method with the provided context.

This method is used to address naming collisions. The returned Method object aliases module names to avoid naming collisions in the file being written.

class gapic.schema.wrappers.Oneof(oneof_pb: google.protobuf.descriptor_pb2.OneofDescriptorProto)[source]

Description of a field.

class gapic.schema.wrappers.OperationInfo(response_type: gapic.schema.wrappers.MessageType, metadata_type: gapic.schema.wrappers.MessageType)[source]

Representation of long-running operation info.

with_context(*, collisions: FrozenSet[str]) → gapic.schema.wrappers.OperationInfo[source]

Return a derivative of this OperationInfo with the provided context.

This method is used to address naming collisions. The returned OperationInfo object aliases module names to avoid naming collisions in the file being written.

class gapic.schema.wrappers.PrimitiveType(meta: gapic.schema.metadata.Metadata, python_type: Optional[type])[source]

A representation of a Python primitive type.

classmethod build(primitive_type: Optional[type])[source]

Return a PrimitiveType object for the given Python primitive type.

Parameters:primitive_type (cls) – A Python primitive type, such as int or str. Despite not being a type, None is also accepted here.
Returns:The instantiated PrimitiveType object.
Return type:PrimitiveType
class gapic.schema.wrappers.PythonType(meta: gapic.schema.metadata.Metadata)[source]

Wrapper class for Python types.

This exists for interface consistency, so that methods like Field.type() can return an object and the caller can be confident that a name property will be present.

ident

Return the identifier to be used in templates.

class gapic.schema.wrappers.RetryInfo(max_attempts: int, initial_backoff: float, max_backoff: float, backoff_multiplier: float, retryable_exceptions: FrozenSet[google.api_core.exceptions.GoogleAPICallError])[source]

Representation of the method’s retry behavior.

class gapic.schema.wrappers.Service(service_pb: google.protobuf.descriptor_pb2.ServiceDescriptorProto, methods: Mapping[str, gapic.schema.wrappers.Method], visible_resources: Mapping[str, gapic.schema.wrappers.MessageType], meta: gapic.schema.metadata.Metadata = <factory>)[source]

Description of a service (defined with the service keyword).

async_client_name

Returns the name of the generated AsyncIO client class

client_name

Returns the name of the generated client class

has_lro

Return whether the service has a long-running method.

has_pagers

Return whether the service has paged methods.

host

Return the hostname for this service, if specified.

Returns:The hostname, with no protocol and no trailing /.
Return type:str
module_name

Return the appropriate module name for this service.

Returns:The service name, in snake case.
Return type:str
names

Return a set of names used in this service.

This is used for detecting naming collisions in the module names used for imports.

oauth_scopes

Return a sequence of oauth scopes, if applicable.

Returns:A sequence of OAuth scopes.
Return type:Sequence[str]
resource_messages

Returns all the resource message types used in all request and response fields in the service.

resource_messages_dict

Returns a dict from resource reference to the message type. This includes the common resource messages.

Returns:
A mapping from resource path
string to the corresponding MessageType. {“locations.googleapis.com/Location”: MessageType(…)}
Return type:Dict[str, MessageType]
shortname

Return the API short name. DRIFT uses this to identify APIs.

Returns:The api shortname.
Return type:str
with_context(*, collisions: FrozenSet[str]) → gapic.schema.wrappers.Service[source]

Return a derivative of this service with the provided context.

This method is used to address naming collisions. The returned Service object aliases module names to avoid naming collisions in the file being written.