Skip to content

Commit

Permalink
rosbag2_py pybind wrapper for "record" - remove rosbag2_transport_py (r…
Browse files Browse the repository at this point in the history
…os2#702)

* rosbag2_py pybind wrapper for "record" verb

Signed-off-by: Emerson Knapp <[email protected]>
  • Loading branch information
emersonknapp authored Mar 31, 2021
1 parent 883819f commit 8b1308b
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 426 deletions.
17 changes: 8 additions & 9 deletions ros2bag/ros2bag/verb/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ def main(self, *, args): # noqa: D102
storage_id=args.storage,
storage_config_uri=storage_config_file,
)
play_options = PlayOptions(
read_ahead_queue_size=args.read_ahead_queue_size,
node_prefix=NODE_NAME_PREFIX,
rate=args.rate,
topics_to_filter=args.topics,
topic_qos_profile_overrides=qos_profile_overrides,
loop=args.loop,
topic_remapping_options=topic_remapping,
)
play_options = PlayOptions()
play_options.read_ahead_queue_size = args.read_ahead_queue_size
play_options.node_prefix = NODE_NAME_PREFIX
play_options.rate = args.rate
play_options.topics_to_filter = args.topics
play_options.topic_qos_profile_overrides = qos_profile_overrides
play_options.loop = args.loop
play_options.topic_remapping_options = topic_remapping

player = Player()
player.play(storage_options, play_options)
48 changes: 25 additions & 23 deletions ros2bag/ros2bag/verb/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from ros2bag.verb import VerbExtension
from ros2cli.node import NODE_NAME_PREFIX
from rosbag2_py import get_registered_writers
from rosbag2_py import Recorder
from rosbag2_py import RecordOptions
from rosbag2_py import StorageOptions
import yaml


Expand Down Expand Up @@ -174,35 +177,34 @@ def main(self, *, args): # noqa: D102
if args.storage_config_file:
storage_config_file = args.storage_config_file.name

# NOTE(hidmic): in merged install workspaces on Windows, Python entrypoint lookups
# combined with constrained environments (as imposed by colcon test)
# may result in DLL loading failures when attempting to import a C
# extension. Therefore, do not import rosbag2_transport at the module
# level but on demand, right before first use.
from rosbag2_transport import rosbag2_transport_py

rosbag2_transport_py.record(
storage_options = StorageOptions(
uri=uri,
storage_id=args.storage,
serialization_format=args.serialization_format,
node_prefix=NODE_NAME_PREFIX,
compression_mode=args.compression_mode,
compression_format=args.compression_format,
compression_queue_size=args.compression_queue_size,
compression_threads=args.compression_threads,
all=args.all,
no_discovery=args.no_discovery,
polling_interval=args.polling_interval,
max_bagfile_size=args.max_bag_size,
max_bagfile_duration=args.max_bag_duration,
max_cache_size=args.max_cache_size,
topics=args.topics,
regex=args.regex,
exclude=args.exclude,
include_hidden_topics=args.include_hidden_topics,
qos_profile_overrides=qos_profile_overrides,
storage_preset_profile=args.storage_preset_profile,
storage_config_file=storage_config_file)
storage_config_uri=storage_config_file,
)
record_options = RecordOptions()
record_options.all = args.all
record_options.is_discovery_disabled = args.no_discovery
record_options.topics = args.topics
record_options.rmw_serialization_format = args.serialization_format
record_options.topic_polling_interval = datetime.timedelta(
milliseconds=args.polling_interval)
record_options.regex = args.regex
record_options.exclude = args.exclude
record_options.node_prefix = NODE_NAME_PREFIX
record_options.compression_mode = args.compression_mode
record_options.compression_format = args.compression_format
record_options.compression_queue_size = args.compression_queue_size
record_options.compression_threads = args.compression_threads
record_options.topic_qos_profile_overrides = qos_profile_overrides
record_options.include_hidden_topics = args.include_hidden_topics

recorder = Recorder()
recorder.record(storage_options, record_options)

if os.path.isdir(uri) and not os.listdir(uri):
os.rmdir(uri)
4 changes: 2 additions & 2 deletions ros2bag/test/test_play_qos_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_qos_simple(self):
bag_command.wait_for_shutdown(timeout=5)
expected_string_regex = re.compile(ERROR_STRING)
matches = expected_string_regex.search(bag_command.output)
assert not matches, print('ros2bag CLI did not produce the expected output')
assert not matches, 'ros2bag CLI did not produce the expected output'

def test_qos_incomplete(self):
"""Test a partially filled QoS profile for a single topic."""
Expand All @@ -81,4 +81,4 @@ def test_qos_incomplete(self):
bag_command.wait_for_shutdown(timeout=5)
expected_string_regex = re.compile(ERROR_STRING)
matches = expected_string_regex.search(bag_command.output)
assert not matches, print('ros2bag CLI did not produce the expected output')
assert not matches, 'ros2bag CLI did not produce the expected output'
12 changes: 4 additions & 8 deletions ros2bag/test/test_record_qos_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def test_qos_simple(self):
bag_command.wait_for_shutdown(timeout=SHUTDOWN_TIMEOUT)
assert bag_command.terminated
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
assert matches, ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output)

def test_incomplete_qos_profile(self):
profile_path = PROFILE_PATH / 'incomplete_qos_profile.yaml'
Expand All @@ -110,8 +109,7 @@ def test_incomplete_qos_profile(self):
bag_command.wait_for_shutdown(timeout=SHUTDOWN_TIMEOUT)
assert bag_command.terminated
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
assert matches, ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output)

def test_incomplete_qos_duration(self):
profile_path = PROFILE_PATH / 'incomplete_qos_duration.yaml'
Expand All @@ -128,8 +126,7 @@ def test_incomplete_qos_duration(self):
assert bag_command.terminated
assert bag_command.exit_code != launch_testing.asserts.EXIT_OK
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
assert matches, ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output)

def test_nonexistent_qos_profile(self):
profile_path = PROFILE_PATH / 'foobar.yaml'
Expand All @@ -146,5 +143,4 @@ def test_nonexistent_qos_profile(self):
assert bag_command.terminated
assert bag_command.exit_code != launch_testing.asserts.EXIT_OK
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
assert matches, ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output)
5 changes: 5 additions & 0 deletions rosbag2_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ if(BUILD_TESTING)
PYTHON_EXECUTABLE "${_PYTHON_EXECUTABLE}"
APPEND_ENV "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}" ${other_environment_vars}
)
ament_add_pytest_test(test_transport_py
"test/test_transport.py"
PYTHON_EXECUTABLE "${_PYTHON_EXECUTABLE}"
APPEND_ENV "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}" ${other_environment_vars}
)
endif()

ament_package()
4 changes: 4 additions & 0 deletions rosbag2_py/rosbag2_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from rosbag2_py._transport import (
Player,
PlayOptions,
Recorder,
RecordOptions,
)

__all__ = [
Expand All @@ -60,4 +62,6 @@
'Info',
'Player',
'PlayOptions',
'Recorder',
'RecordOptions',
]
7 changes: 6 additions & 1 deletion rosbag2_py/src/rosbag2_py/_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ PYBIND11_MODULE(_storage, m) {

pybind11::class_<rosbag2_storage::StorageOptions>(m, "StorageOptions")
.def(
pybind11::init<std::string, std::string, uint64_t, uint64_t, uint64_t, std::string>(),
pybind11::init<
std::string, std::string, uint64_t, uint64_t, uint64_t, std::string, std::string>(),
pybind11::arg("uri"),
pybind11::arg("storage_id"),
pybind11::arg("max_bagfile_size") = 0,
pybind11::arg("max_bagfile_duration") = 0,
pybind11::arg("max_cache_size") = 0,
pybind11::arg("storage_preset_profile") = "",
pybind11::arg("storage_config_uri") = "")
.def_readwrite("uri", &rosbag2_storage::StorageOptions::uri)
.def_readwrite("storage_id", &rosbag2_storage::StorageOptions::storage_id)
Expand All @@ -60,6 +62,9 @@ PYBIND11_MODULE(_storage, m) {
.def_readwrite(
"max_cache_size",
&rosbag2_storage::StorageOptions::max_cache_size)
.def_readwrite(
"storage_preset_profile",
&rosbag2_storage::StorageOptions::storage_preset_profile)
.def_readwrite(
"storage_config_uri",
&rosbag2_storage::StorageOptions::storage_config_uri);
Expand Down
Loading

0 comments on commit 8b1308b

Please sign in to comment.