Add support for rosbag2 version 5 metadata

This commit is contained in:
Marko Durkovic 2022-04-21 15:58:09 +02:00
parent 34ffe96692
commit d196e8b74e
2 changed files with 27 additions and 2 deletions

View File

@ -44,6 +44,14 @@ if TYPE_CHECKING:
message_count: int message_count: int
topic_metadata: TopicMetadata topic_metadata: TopicMetadata
class FileInformation(TypedDict):
"""Per file metadata."""
path: str
starting_time: StartingTime
duration: Duration
message_count: int
class Metadata(TypedDict): class Metadata(TypedDict):
"""Rosbag2 metadata file.""" """Rosbag2 metadata file."""
@ -56,6 +64,7 @@ if TYPE_CHECKING:
compression_format: str compression_format: str
compression_mode: str compression_mode: str
topics_with_message_count: list[TopicWithMessageCount] topics_with_message_count: list[TopicWithMessageCount]
files: list[FileInformation]
class ReaderError(Exception): class ReaderError(Exception):
@ -99,6 +108,8 @@ class Reader:
- Version 2: Changed field sizes in C++ implementation. - Version 2: Changed field sizes in C++ implementation.
- Version 3: Added compression. - Version 3: Added compression.
- Version 4: Added QoS metadata to topics, changed relative file paths - Version 4: Added QoS metadata to topics, changed relative file paths
- Version 5: Added per file metadata
""" """
def __init__(self, path: Union[Path, str]): def __init__(self, path: Union[Path, str]):
@ -125,7 +136,7 @@ class Reader:
try: try:
self.metadata: Metadata = dct['rosbag2_bagfile_information'] self.metadata: Metadata = dct['rosbag2_bagfile_information']
if (ver := self.metadata['version']) > 4: if (ver := self.metadata['version']) > 5:
raise ReaderError(f'Rosbag2 version {ver} not supported; please report issue.') raise ReaderError(f'Rosbag2 version {ver} not supported; please report issue.')
if storageid := self.metadata['storage_identifier'] != 'sqlite3': if storageid := self.metadata['storage_identifier'] != 'sqlite3':
raise ReaderError( raise ReaderError(
@ -160,6 +171,8 @@ class Reader:
if self.compression_mode and (cfmt := self.compression_format) != 'zstd': if self.compression_mode and (cfmt := self.compression_format) != 'zstd':
raise ReaderError(f'Compression format {cfmt!r} is not supported.') raise ReaderError(f'Compression format {cfmt!r} is not supported.')
self.files: list[FileInformation] = self.metadata.get('files', [])[:]
except KeyError as exc: except KeyError as exc:
raise ReaderError(f'A metadata key is missing {exc!r}.') from None raise ReaderError(f'A metadata key is missing {exc!r}.') from None

View File

@ -233,7 +233,7 @@ class Writer: # pylint: disable=too-many-instance-attributes
metadata: dict[str, Metadata] = { metadata: dict[str, Metadata] = {
'rosbag2_bagfile_information': { 'rosbag2_bagfile_information': {
'version': 4, 'version': 5,
'storage_identifier': 'sqlite3', 'storage_identifier': 'sqlite3',
'relative_file_paths': [self.dbpath.name], 'relative_file_paths': [self.dbpath.name],
'duration': { 'duration': {
@ -256,6 +256,18 @@ class Writer: # pylint: disable=too-many-instance-attributes
], ],
'compression_format': self.compression_format, 'compression_format': self.compression_format,
'compression_mode': self.compression_mode, 'compression_mode': self.compression_mode,
'files': [
{
'path': self.dbpath.name,
'starting_time': {
'nanoseconds_since_epoch': start,
},
'duration': {
'nanoseconds': duration,
},
'message_count': count,
},
],
}, },
} }
with self.metapath.open('w') as metafile: with self.metapath.open('w') as metafile: