Base Adapter
mosaicolabs.bridges.mcap.adapter_base ¶
MCAPSchemaMetadata ¶
Bases: BaseSchemaMetadata
Encapsulates Mosaico's reserved _mcap_ topic-metadata namespace in a single place.
Every topic ingested by the MCAP bridge carries MCAP-specific bookkeeping (original
channel_name, channel_encoding, schema_name, schema_encoding, raw
schema_def) plus bridge-internal fields (e.g. the source mcap file) under one reserved
key, so that:
- The literal string
"_mcap_"exists in exactly one place (KEY), instead of being duplicated across adapters, loaders, and the injector. - Callers build up this namespace incrementally via
updatewithout ever touching the wrapping dict shape by hand.
Example
# Every key in REQUIRED_KEYS must be provided, otherwise a ValueError is raised
meta = MCAPSchemaMetadata(
channel_name="/imu",
channel_encoding="protobuf",
schema_name="sensor_msgs.Imu",
schema_encoding="protobuf",
schema_def="<stringified schema definition>",
).update(source_file="a.mcap")
topic_metadata = meta.merge_into(user_supplied_metadata)
# topic_metadata == {..., "_mcap_": {"channel_name": "/imu", ..., "source_file": "a.mcap"}}
KEY
class-attribute
¶
The reserved metadata key. Adapters/loaders/the injector should reference this constant rather than the literal string, so the namespace can be renamed in one place.
REQUIRED_KEYS
class-attribute
¶
REQUIRED_KEYS = {
"schema_name": str,
"schema_encoding": str,
"schema_def": str,
"channel_name": str,
"channel_encoding": str,
}
The keys that need to be present in order to create the MCAPSchemaMetadata.
get_channel_encoding ¶
Returns the channel encoding contained in the metadata.
update ¶
Merges additional fields into this block, in place. Returns self for chaining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Any
|
Additional fields to merge. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
BaseSchemaMetadata |
BaseSchemaMetadata
|
The updated metadata instance. |
to_dict ¶
Wraps the current fields under the reserved key.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary containing the |
merge_into ¶
Merges this block into an existing metadata dict's KEY namespace, creating it
if absent. Mutates and returns metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict
|
The existing metadata dict to merge into. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The updated metadata dict with the |
extract
classmethod
¶
Reads the KEY block out of a metadata dict or {} if absent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Optional[dict]
|
A metadata dict, typically |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The extracted |
from_dict
classmethod
¶
Creates a BaseSchemaMetadata from a plain metadata dict, e.g. the return value of
ROSAdapterBase.schema_metadata(). Any keys outside the KEY namespace are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Optional[dict]
|
A metadata dict, typically |
required |
Returns:
| Name | Type | Description |
|---|---|---|
BaseSchemaMetadata |
BaseSchemaMetadata
|
A new instance seeded with the extracted |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of |
TypeError
|
If any of |
McapReturnType
dataclass
¶
Container returned by MCAPAdapterBase.to_native.
Attributes:
| Name | Type | Description |
|---|---|---|
data_bytes |
bytes
|
The native MCAP message, serialized according to the adapter's |
publish_time_ns |
Optional[int]
|
The message publish time (nanoseconds) returned by |
MCAPAdapterBase ¶
Bases: BridgeAdapterBase[OntologyT, McapReturnType], Generic[OntologyT, McapT]
Abstract Base Class for converting MCAP messages to Mosaico Ontology types.
The Adaptation Layer is the semantic core of the MCAP Bridge. Rather than performing simple parsing, adapters actively translate MCAP data into standardized, strongly-typed Mosaico Ontology objects.
Attributes:
| Name | Type | Description |
|---|---|---|
schema_name |
str
|
The MCAP schema name this adapter handles (e.g. |
schema_encoding |
str
|
The MCAP schema encoding this adapter handles (e.g. |
_REQUIRED_KEYS |
Tuple[str, ...]
|
Internal validation list for mandatory MCAP message fields. |
translate
classmethod
¶
Translates a MCAP message instance into a Mosaico Message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
MCAPMessage
|
The source container yielded by the MCAPLoader. |
required |
**kwargs
|
Any
|
Contextual data such as calibration parameters or frame overrides. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Message |
Message
|
A Mosaico Message object containing the instantiated ontology data. |
Raises:
| Type | Description |
|---|---|
Exception
|
If translation fails due to missing fields, type mismatches, or other errors. |
from_dict
abstractmethod
classmethod
¶
Maps the raw MCAP dictionary to the Mosaico model.
This method performs field validation and reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mcap_data
|
dict
|
The decoded MCAP message payload. |
required |
publish_time_ns
|
int
|
The message publish time (nanoseconds). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OntologyT |
OntologyT
|
The constructed ontology instance. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the adapter does not override this method. |
to_native
classmethod
¶
Converts a Mosaico message or ontology object into a serialized native MCAP message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mosaico_data
|
Union[Message, OntologyT]
|
A |
required |
**kwargs
|
Any
|
Extra arguments forwarded to
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
McapReturnType |
McapReturnType
|
The native MCAP message serialized according to |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
NotImplementedError
|
If |
to_mcap
abstractmethod
classmethod
¶
Converts a Mosaico ontology object back into a native MCAP message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mosaico_data
|
OntologyT
|
The ontology instance to convert. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[McapT, Optional[int]]
|
Tuple[McapT, Optional[int]]: The native MCAP message (a protobuf |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the adapter does not override this method. |
is_mcap_type_valid
classmethod
¶
Checks whether a given MCAP message type (schema_name, schema_encoding) is handled by this adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding_to_validate
|
str
|
The full MCAP encoding string to check
(e.g., |
required |
schema_name_to_validate
|
str
|
The full MCAP schema name string to check
(e.g., |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
schema_metadata
classmethod
¶
Builds the MCAP-specific schema metadata for this adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name
|
str
|
The full name of the MCAP channel the adapter is associated to. |
required |
channel_encoding
|
str
|
The encoding of the MCAP channel the adapter is associated to. |
required |
schema_def
|
str
|
The string representation of the MCAP schema the adapter is
associated to, as produced by the resolved |
required |
Returns:
| Type | Description |
|---|---|
Optional[dict]
|
Optional[dict]: The schema metadata, wrapped under the
|
ontology_data_type
classmethod
¶
Returns the Ontology class type associated with this adapter.
unpack_mosaico_msg
classmethod
¶
Extracts the typed Mosaico payload and its Header (if present) from a wrapped or bare message.
Handles two input cases:
Messagewrapper: the typed data is extracted viaget_data().- Raw ontology instance: returned as-is.
In both cases the Header is extracted from the ontology (if supported), otherwise a
default Header (empty frame_id and zero Time) is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mosaico_msg
|
Union[Message, T]
|
Either a |
required |
Returns:
| Type | Description |
|---|---|
T
|
tuple[T, Header]: A |
Header
|
header is the corresponding |
tuple[T, Header]
|
zero |
Raises:
| Type | Description |
|---|---|
TypeError
|
If mosaico_msg is neither a |
compute_mcap_msg_type ¶
Returns the specific combination of the schema name + schema encoding, defining the MCAP message type handled by this adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_name
|
str
|
the schema name of the MCAP message. |
required |
schema_encoding
|
str
|
the schema encoding of the MCAP message. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The unique combination for passed schema_name and schema_encoding. |