MCAPTraceFileWriter

class osi_utilities.tracefile.mcap_writer.MCAPTraceFileWriter[source]

Bases: TraceFileWriter

Writer for MCAP-format OSI trace files (.mcap).

Supports multi-channel writing with schema registration, OSI-compliant file/channel metadata, and FileDescriptorSet-based schemas.

open(path, metadata=None, *, compression=None, chunk_size=None)[source]

Open an MCAP file for writing.

Parameters:
  • path (Path) – Path to the output file. Must have .mcap extension.

  • metadata (dict[str, str] | None) – Optional net.asam.osi.trace file metadata. If None, default metadata is used.

  • compression (str | None) – Compression algorithm — "none", "lz4", or "zstd". If None, the mcap library default is used.

  • chunk_size (int | None) – Chunk size in bytes. Must be between MIN_CHUNK_SIZE and MAX_CHUNK_SIZE. If None, DEFAULT_CHUNK_SIZE is used.

Returns:

True on success, False on failure.

Return type:

bool

add_channel(topic, message_class, metadata=None)[source]

Register an OSI channel with schema.

Parameters:
  • topic (str) – Channel topic name.

  • message_class (type[Message]) – The protobuf message class for this channel.

  • metadata (dict[str, str] | None) – Optional channel metadata dict.

Returns:

The channel ID.

Raises:

RuntimeError – If writer is not open or topic already exists.

Return type:

int

write_message(message, topic='')[source]

Write a protobuf message to the specified topic channel.

If no channels are registered, auto-creates one using the message type.

Parameters:
  • message (Message) – The protobuf message to write.

  • topic (str) – The channel topic. If empty and only one channel exists, uses that channel.

Returns:

True on success, False on failure.

Return type:

bool

add_file_metadata(name, data)[source]

Add additional file-level metadata.

Parameters:
  • name (str) – Metadata entry name.

  • data (dict[str, str]) – Key-value metadata pairs.

Returns:

True on success.

Return type:

bool

close()[source]

Finalize and close the MCAP file.

Return type:

None

property written_count: int