Protobuf support for the Python MCAP library
Project description
Python MCAP protobuf support
This package provides protobuf support for the Python MCAP file format reader.
Installation
Install via Pipenv by adding mcap-protobuf-support
to your Pipfile
or via the command line:
pipenv install mcap-protobuf-support
MCAP protobuf writing example
First, compile each of your message definitions:
protoc complex_message.proto --python_out . -o complex_message.fds
protoc simple_message.proto --python_out . -o simple_message.fds
Next, use those message definitions to register schema in the mcap file. Note that the names
of the messages in the proto files must correspond to the schema names in the mcap file. For example,
the message type SimpleMessage
in the .proto file must be registered as an mcap schema with the name
SimpleMessage
.
from pathlib import Path
from typing import IO, Any
from mcap.mcap0.writer import Writer as McapWriter
from .complex_message_pb2 import ComplexMessage
from .simple_message_pb2 import SimpleMessage
output = open("example.mcap", "wb")
mcap_writer = McapWriter(output)
mcap_writer.start(profile="protobuf", library="test")
simple_schema_id = mcap_writer.register_schema(
name="SimpleMessage",
encoding="protobuf",
data=(Path(__file__).parent / "simple_message.fds").read_bytes(),
)
complex_schema_id = mcap_writer.register_schema(
name="ComplexMessage",
encoding="protobuf",
data=(Path(__file__).parent / "complex_message.fds").read_bytes(),
)
simple_channel_id = mcap_writer.register_channel(
topic="/simple_message",
message_encoding="protobuf",
schema_id=simple_schema_id,
)
complex_channel_id = mcap_writer.register_channel(
topic="/complex_message",
message_encoding="protobuf",
schema_id=complex_schema_id,
)
for i in range(1, 11):
simple_message = SimpleMessage(data=f"Hello MCAP protobuf world #{i}!")
mcap_writer.add_message(
channel_id=simple_channel_id,
log_time=i * 1000,
data=simple_message.SerializeToString(),
publish_time=i * 1000,
)
complex_message = ComplexMessage(fieldA=f"Field A {i}", fieldB="Field B {i}")
mcap_writer.add_message(
channel_id=complex_channel_id,
log_time=i * 1000,
data=complex_message.SerializeToString(),
publish_time=i * 1000,
)
mcap_writer.finish()
output.close()
MCAP protobuf decoding example
from mcap.mcap0.stream_reader import StreamReader
from mcap_protobuf.decoder import Decoder
reader = StreamReader("my_data.mcap")
decoder = Decoder(reader)
for m in decoder.messages:
print(m)
Stay in touch
Join our Slack channel to ask questions, share feedback, and stay up to date on what our team is working on.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mcap-protobuf-support-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47a2719bdd242f9d97562df69bf0c542596bd9c670867e607726c6fc50c28ec4 |
|
MD5 | 3537157221c3ed35b2dac1ff44f19571 |
|
BLAKE2b-256 | 76a3d779ef69e808e6a21353711a75ed18ea692862eec5d48af2d3d6e5edc14f |
Hashes for mcap_protobuf_support-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbe05accf3d9a5ff88fdd483a3dd4aa36a244d110705bbb517e284f5c90aa7a2 |
|
MD5 | e7ca8e8ff89e04edf84b65f4f529d922 |
|
BLAKE2b-256 | fd2c538f494f25c1c759cb294bb11852164d9bb272b5ab40438852ad7a9aa1c1 |