protobuf - Registry for Google Protocol Buffer messages and enums

Overview

This module provides central registry for Google Protocol Buffer messages and enums. The generated *_pb2.py protobuf files could be registered using register_decriptor or load_registered function. The registry could be then used to obtain information about protobuf messages or enum types, or to create message instances or enum values.

Functions

register_decriptor

firebird.base.protobuf.register_decriptor(file_descriptor)

Registers enums and messages defined by protobuf file DESCRIPTOR.

Return type

None

load_registered

firebird.base.protobuf.load_registered(group)

Load registered protobuf packages.

Protobuf packages must register the pb2-file DESCRIPTOR in entry_points section of setup.cfg file.

Parameters

group (str) – Entry-point group name.

Example

# setup.cfg:

[options.entry_points]
firebird.base.protobuf =
    firebird.base.lib_a = firebird.base.lib_a_pb2:DESCRIPTOR
    firebird.base.lib_b = firebird.base.lib_b_pb2:DESCRIPTOR
    firebird.base.lib_c = firebird.base.lib_c_pb2:DESCRIPTOR

# will be loaded with:

load_registered('firebird.base.protobuf')
Return type

None

is_msg_registered

firebird.base.protobuf.is_msg_registered(name)

Returns True if specified name refers to registered protobuf message type.

Return type

bool

is_enum_registered

firebird.base.protobuf.is_enum_registered(name)

Returns True if specified name refers to registered protobuf enum type.

Return type

bool

get_enum_type

firebird.base.protobuf.get_enum_type(name)

Returns wrapper instance for protobuf enum type with specified name.

Raises

KeyError – When enum type is not registered.

Return type

ProtoEnumType

get_enum_value_name

firebird.base.protobuf.get_enum_value_name(enum_type_name, value)

Returns name for the enum value.

Return type

str

create_message

firebird.base.protobuf.create_message(name, serialized=None)

Returns new protobuf message instance.

Parameters
  • name (str) – Fully qualified protobuf message name.

  • serialized (Optional[bytes]) – Serialized message.

Raises
  • KeyError – When message type is not registered.

  • google.protobuf.message.DecodeError – When deserializations fails.

Return type

Message

get_enum_field_type

firebird.base.protobuf.get_enum_field_type(msg, field_name)

Returns name of enum type for message enum field.

Raises

KeyError – When message does not have specified field.

Return type

str

struct2dict

firebird.base.protobuf.struct2dict(struct)

Unpacks google.protobuf.Struct message to Python dict value.

Return type

Dict

dict2struct

firebird.base.protobuf.dict2struct(value)

Returns dict packed into google.protobuf.Struct message.

Return type

Struct

Data classes

ProtoMessageType

class firebird.base.protobuf.ProtoMessageType(name, constructor)

Bases: firebird.base.types.Distinct

Google protobuf message type.

get_key()

Returns name.

Return type

Any

constructor: Callable
name: str

ProtoEnumType

class firebird.base.protobuf.ProtoEnumType(descriptor)

Bases: firebird.base.types.Distinct

Google protobuf enum type

__getattr__(name)

Returns the value corresponding to the given enum name.

get_key()

Returns name.

Return type

Any

get_value_name(number)

Returns a string containing the name of an enum value.

Raises

KeyError – If there is no value for specified name.

Return type

str

items()

Return a list of the (name, value) pairs of the enum.

These are returned in the order they were defined in the .proto file.

keys()

Return a list of the string names in the enum.

These are returned in the order they were defined in the .proto file.

values()

Return a list of the integer values in the enum.

These are returned in the order they were defined in the .proto file.

descriptor: google.protobuf.descriptor.EnumDescriptor
property name

Full enum type name.

Return type

str