From d196e8b74e4608cd07d0c6db23f723d948dd9eef Mon Sep 17 00:00:00 2001 From: Marko Durkovic Date: Thu, 21 Apr 2022 15:58:09 +0200 Subject: [PATCH] Add support for rosbag2 version 5 metadata --- src/rosbags/rosbag2/reader.py | 15 ++++++++++++++- src/rosbags/rosbag2/writer.py | 14 +++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/rosbags/rosbag2/reader.py b/src/rosbags/rosbag2/reader.py index 0f261a99..dfc19a9b 100644 --- a/src/rosbags/rosbag2/reader.py +++ b/src/rosbags/rosbag2/reader.py @@ -44,6 +44,14 @@ if TYPE_CHECKING: message_count: int topic_metadata: TopicMetadata + class FileInformation(TypedDict): + """Per file metadata.""" + + path: str + starting_time: StartingTime + duration: Duration + message_count: int + class Metadata(TypedDict): """Rosbag2 metadata file.""" @@ -56,6 +64,7 @@ if TYPE_CHECKING: compression_format: str compression_mode: str topics_with_message_count: list[TopicWithMessageCount] + files: list[FileInformation] class ReaderError(Exception): @@ -99,6 +108,8 @@ class Reader: - Version 2: Changed field sizes in C++ implementation. - Version 3: Added compression. - Version 4: Added QoS metadata to topics, changed relative file paths + - Version 5: Added per file metadata + """ def __init__(self, path: Union[Path, str]): @@ -125,7 +136,7 @@ class Reader: try: 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.') if storageid := self.metadata['storage_identifier'] != 'sqlite3': raise ReaderError( @@ -160,6 +171,8 @@ class Reader: if self.compression_mode and (cfmt := self.compression_format) != 'zstd': raise ReaderError(f'Compression format {cfmt!r} is not supported.') + + self.files: list[FileInformation] = self.metadata.get('files', [])[:] except KeyError as exc: raise ReaderError(f'A metadata key is missing {exc!r}.') from None diff --git a/src/rosbags/rosbag2/writer.py b/src/rosbags/rosbag2/writer.py index 6efaf197..6454562b 100644 --- a/src/rosbags/rosbag2/writer.py +++ b/src/rosbags/rosbag2/writer.py @@ -233,7 +233,7 @@ class Writer: # pylint: disable=too-many-instance-attributes metadata: dict[str, Metadata] = { 'rosbag2_bagfile_information': { - 'version': 4, + 'version': 5, 'storage_identifier': 'sqlite3', 'relative_file_paths': [self.dbpath.name], 'duration': { @@ -256,6 +256,18 @@ class Writer: # pylint: disable=too-many-instance-attributes ], 'compression_format': self.compression_format, '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: