protobuf - Registry for Google Protocol Buffer messages and enums¶
Overview¶
firebird.base.protobuf
¶
This module provides a central registry for Google Protocol Buffer message types
and enum types generated from .proto files. It allows creating message instances
and accessing enum information using their fully qualified names (e.g.,
"my.package.MyMessage", "my.package.MyEnum") without needing to directly import
the corresponding generated _pb2.py modules throughout the codebase.
Benefits:
- Decouples code using protobuf messages from the specific generated modules.
- Provides a single point for managing and discovering available message/enum types.
- Facilitates dynamic loading of protobuf definitions via entry points.
Core Features:
- Register message/enum types using their file DESCRIPTOR object.
- Create new message instances by name using
create_message(). - Access enum descriptors and values by name using
get_enum_type(). - Load protobuf definitions registered by other installed packages via entry points
using
load_registered(). - Helpers for common types like
google.protobuf.Struct.
Example
# Assume you have my_proto_pb2.py generated from my_proto.proto
# containing:
# message Sample { required string name = 1; }
# enum Status { UNKNOWN = 0; OK = 1; ERROR = 2; }
from firebird.base.protobuf import (
register_descriptor, create_message, get_enum_type, is_msg_registered
)
# Import the generated descriptor (only needed once, e.g., at startup)
try:
from . import my_proto_pb2 # Replace with actual import path
HAS_MY_PROTO = True
except ImportError:
HAS_MY_PROTO = False
# 1. Register the types from the descriptor
if HAS_MY_PROTO:
register_descriptor(my_proto_pb2.DESCRIPTOR)
print(f"Is 'my_proto.Sample' registered? {is_msg_registered('my_proto.Sample')}")
# 2. Create a message instance by name
if HAS_MY_PROTO:
try:
msg = create_message('my_proto.Sample')
msg.name = "Example"
print(f"Created message: {msg}")
# 3. Access enum type and values by name
status_enum = get_enum_type('my_proto.Status')
print(f"Status enum name: {status_enum.name}")
print(f"OK value: {status_enum.OK}") # Access like attribute
print(f"Name for value 2: {status_enum.get_value_name(2)}") # Access via method
print(f"Available status keys: {status_enum.keys()}")
except KeyError as e:
print(f"Error accessing registered proto type: {e}")
Constants¶
firebird.base.protobuf.PROTO_EMPTY: str
module-attribute
¶
Name of well-known EMPTY protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_ANY: str
module-attribute
¶
Name of well-known ANY protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_DURATION: str
module-attribute
¶
Name of well-known DURATION protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_TIMESTAMP: str
module-attribute
¶
Name of well-known TIMESTAMP protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_STRUCT: str
module-attribute
¶
Name of well-known STRUCT protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_VALUE: str
module-attribute
¶
Name of well-known VALUE protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_LISTVALUE: str
module-attribute
¶
Name of well-known LISTVALUE protobuf message (for use with create_message())
firebird.base.protobuf.PROTO_FIELDMASK: str
module-attribute
¶
Name of well-known FIELDMASK protobuf message (for use with create_message())
Functions¶
firebird.base.protobuf.register_descriptor(file_descriptor: 'google.protobuf.descriptor.FileDescriptor') -> None
¶
Register all message and enum types defined within a protobuf file descriptor.
This is the primary mechanism for adding types to the registry. The descriptor
object is typically accessed as DESCRIPTOR from a generated _pb2.py module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_descriptor
|
'google.protobuf.descriptor.FileDescriptor'
|
The |
required |
Source code in firebird/base/protobuf.py
The former name register_decriptor() remains available as a deprecated alias until version 3.0.
firebird.base.protobuf.load_registered(group: str) -> None
¶
Load and register protobuf types defined via package entry points.
Searches for installed packages that register entry points under the specified
group. Each entry point should load a FileDescriptor object. This allows
packages to automatically make their protobuf types available to the registry
upon installation.
This function is typically called once during application initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str
|
The name of the entry-point group to scan (e.g., 'firebird.base.protobuf'). |
required |
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
# pyproject.toml
[project.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"
Usage
Source code in firebird/base/protobuf.py
firebird.base.protobuf.is_msg_registered(name: str) -> bool
¶
firebird.base.protobuf.is_enum_registered(name: str) -> bool
¶
firebird.base.protobuf.get_enum_type(name: str) -> ProtoEnumType
¶
Return the ProtoEnumType wrapper for a registered enum type by name.
Provides access to enum members and values via the wrapper object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified name of the registered protobuf enum type. |
required |
Returns:
| Type | Description |
|---|---|
ProtoEnumType
|
The |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.get_enum_value_name(enum_type_name: str, value: int) -> str
¶
Return the string name corresponding to a value within a registered enum type.
Convenience function equivalent to get_enum_type(enum_type_name).get_value_name(value).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enum_type_name
|
str
|
Fully qualified name of the registered enum type. |
required |
value
|
int
|
The integer value of the enum member. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The string name of the enum member. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.create_message(name: str, serialized: bytes | None = None) -> ProtoMessage
¶
Create a new instance of a registered protobuf message type by name.
Optionally initializes the message by parsing serialized data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified name of the registered protobuf message type. |
required |
serialized
|
bytes | None
|
Optional bytes containing the serialized message data. |
None
|
Returns:
| Type | Description |
|---|---|
Message
|
An instance of the requested protobuf message class. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
DecodeError
|
If |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.get_message_factory(name: str) -> Callable
¶
Return the constructor (factory callable) for a registered message type.
Allows creating multiple instances without repeated registry lookups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified name of the registered protobuf message type. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
The callable (message class) used to construct instances. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.get_enum_field_type(msg, field_name: str) -> str
¶
Return the fully qualified name of the enum type for a message field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
An instance of a protobuf message. |
required | |
field_name
|
str
|
The string name of the field within the message. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The fully qualified name of the enum type used by the field. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
TypeError
|
If the specified field is not an enum field. |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.struct2dict(struct: StructProto) -> dict[str, Any]
¶
Unpack a google.protobuf.Struct message into a Python dictionary.
Uses google.protobuf.json_format.MessageToDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Struct
|
The |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A Python dictionary representing the struct's content. |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.dict2struct(value: dict[str, Any]) -> StructProto
¶
Data classes¶
firebird.base.protobuf.ProtoMessageType
dataclass
¶
Bases: Distinct
Registry entry representing a registered Protocol Buffer message type.
Stores the fully qualified name and the constructor (the generated class) for a message type, allowing instantiation via the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified message type name (e.g., "package.Message"). |
required |
constructor
|
Callable
|
The callable (generated message class) used to create instances. |
required |
Source code in firebird/base/protobuf.py
firebird.base.protobuf.ProtoEnumType
dataclass
¶
Bases: Distinct
Registry entry providing access to a registered Protocol Buffer enum type.
Wraps the EnumDescriptor and provides an API similar to generated enum
types, allowing access to names and values without direct import of the
generated _pb2 module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descriptor
|
EnumDescriptor
|
The |
required |
Example
# Assuming 'my_proto.Status' enum (UNKNOWN=0, OK=1) is registered
status_enum = get_enum_type('my_proto.Status')
print(status_enum.OK) # Output: 1 (Access value by name)
print(status_enum.get_value_name(1)) # Output: 'OK' (Get name by value)
print(status_enum.keys()) # Output: ['UNKNOWN', 'OK']
print(status_enum.values()) # Output: [0, 1]
print(status_enum.items()) # Output: [('UNKNOWN', 0), ('OK', 1)]
try:
print(status_enum.NONEXISTENT)
except AttributeError as e:
print(e) # Output: Enum my_proto.Status has no value with name 'NONEXISTENT'
try:
print(status_enum.get_value_name(99))
except KeyError as e:
print(e) # Output: "Enum my_proto.Status has no name defined for value 99"
Source code in firebird/base/protobuf.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
descriptor: EnumDescriptor
instance-attribute
¶
The google.protobuf.descriptor.EnumDescriptor for the enum type.
name: str
property
¶
The fully qualified name of the enum type (e.g., "package.MyEnum").
__getattr__(name: str)
¶
Return the integer value corresponding to the enum member name name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string name of the enum member. |
required |
Returns:
| Type | Description |
|---|---|
|
The integer value of the enum member. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Source code in firebird/base/protobuf.py
get_key() -> str
¶
get_value_name(number: int) -> str
¶
Return the string name corresponding to the enum member value number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
int
|
The integer value of the enum member. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The string name of the enum member. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If there is no value for specified name. |
Source code in firebird/base/protobuf.py
items() -> list[tuple[str, int]]
¶
Return a list of the (name, value) pairs of the enum.
These are returned in the order they were defined in the .proto file.
Source code in firebird/base/protobuf.py
keys() -> list[str]
¶
Return a list of the string names in the enum.
These are returned in the order they were defined in the .proto file.
values() -> list[int]
¶
Return a list of the integer values in the enum.
These are returned in the order they were defined in the .proto file.