Skip to content

Commit d14cb8b

Browse files
authored
feat: Add Channel Methods (#126)
1 parent 30639b6 commit d14cb8b

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/pybag/bag_reader.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ def get_topics(self) -> list[str]:
152152
"""
153153
return list(set(c.topic for c in self._connections.values()))
154154

155+
def get_connections(self) -> list[ConnectionRecord]:
156+
"""Get all connection records in the bag file.
157+
158+
Connection records contain metadata about each topic including
159+
the message type and message definition.
160+
161+
Returns:
162+
List of ConnectionRecord objects.
163+
"""
164+
return list(self._connections.values())
165+
155166
def get_message_count(self, topic: str) -> int:
156167
"""Get the number of messages for a topic.
157168

src/pybag/cli/mcap_convert.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,9 @@ def convert_bag_to_mcap(
161161
chunk_size=chunk_size,
162162
chunk_compression=chunk_compression,
163163
) as writer:
164-
# TODO: Make this nicer
165164
# Pre-register all channels with translated schemas
166165
# This ensures the schema is correct for the target format
167-
for conn in reader._connections.values():
166+
for conn in reader.get_connections():
168167
conn_header = conn.connection_header
169168
schema = translate_schema_ros1_to_ros2(
170169
conn_header.type,
@@ -233,12 +232,10 @@ def convert_mcap_to_bag(
233232
compression=chunk_compression,
234233
chunk_size=chunk_size,
235234
) as writer:
236-
# TODO: Make this nicer
237235
# Pre-register all connections with translated schemas
238236
# This ensures the schema is correct for the target format
239-
channels = reader._reader.get_channels()
240-
for channel in channels.values():
241-
schema_record = reader._reader.get_channel_schema(channel.id)
237+
for channel in reader.get_channels():
238+
schema_record = reader.get_schema(channel.topic)
242239
if schema_record is None:
243240
logger.warning(f"No schema found for channel {channel.topic}")
244241
continue

src/pybag/mcap_reader.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
BaseMcapRecordReader,
1414
McapRecordReaderFactory
1515
)
16-
from pybag.mcap.records import AttachmentRecord, MetadataRecord
16+
from pybag.mcap.records import (
17+
AttachmentRecord,
18+
ChannelRecord,
19+
MetadataRecord,
20+
SchemaRecord
21+
)
1722

1823
logger = logging.getLogger(__name__)
1924

@@ -71,6 +76,28 @@ def get_topics(self) -> list[str]:
7176
"""Get all topics in the MCAP file."""
7277
return [c.topic for c in self._reader.get_channels().values()] # TODO: Use a set?
7378

79+
def get_channels(self) -> list[ChannelRecord]:
80+
"""Get all channels in the MCAP file.
81+
82+
Returns:
83+
List of ChannelRecord objects.
84+
"""
85+
return list(self._reader.get_channels().values())
86+
87+
def get_schema(self, topic: str) -> SchemaRecord | None:
88+
"""Get the schema for a particular topic.
89+
90+
Args:
91+
topic: The topic name to get the schema for.
92+
93+
Returns:
94+
SchemaRecord for the topic, or None if not found.
95+
"""
96+
channel_id = self._reader.get_channel_id(topic)
97+
if channel_id is None:
98+
return None
99+
return self._reader.get_channel_schema(channel_id)
100+
74101
def get_message_count(self, topic: str) -> int:
75102
"""Get the number of messages in a given topic."""
76103
channel_id = self._reader.get_channel_id(topic)

0 commit comments

Comments
 (0)