From e13c6d33f276d874979fad1c8316b2b11fc204fd Mon Sep 17 00:00:00 2001 From: Jannik Jose Date: Fri, 11 Oct 2024 14:42:29 +0200 Subject: [PATCH 1/2] Add python binding to close the SequentialWriter Signed-off-by: Jannik Jose --- rosbag2_cpp/include/rosbag2_cpp/writer.hpp | 2 +- rosbag2_py/src/rosbag2_py/_writer.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rosbag2_cpp/include/rosbag2_cpp/writer.hpp b/rosbag2_cpp/include/rosbag2_cpp/writer.hpp index a61b5ca471..1df0818881 100644 --- a/rosbag2_cpp/include/rosbag2_cpp/writer.hpp +++ b/rosbag2_cpp/include/rosbag2_cpp/writer.hpp @@ -53,7 +53,7 @@ class BaseWriterInterface; * The Writer allows writing messages to a new bag. For every topic, information about its type * needs to be added before writing the first message. */ -class ROSBAG2_CPP_PUBLIC Writer final +class ROSBAG2_CPP_PUBLIC Writer { public: explicit Writer( diff --git a/rosbag2_py/src/rosbag2_py/_writer.cpp b/rosbag2_py/src/rosbag2_py/_writer.cpp index 6da330efad..025ee44d66 100644 --- a/rosbag2_py/src/rosbag2_py/_writer.cpp +++ b/rosbag2_py/src/rosbag2_py/_writer.cpp @@ -36,7 +36,7 @@ namespace rosbag2_py { template -class Writer +class Writer : public rosbag2_cpp::Writer { public: Writer() @@ -113,6 +113,7 @@ PYBIND11_MODULE(_writer, m) { .def(pybind11::init()) .def("open", &rosbag2_py::Writer::open) .def("write", &rosbag2_py::Writer::write) + .def("close", &rosbag2_py::Writer::close) .def("remove_topic", &rosbag2_py::Writer::remove_topic) .def("create_topic", &rosbag2_py::Writer::create_topic); From 90ad4d22e3cb088ebc0184034ff73fbc5dd9c4cc Mon Sep 17 00:00:00 2001 From: Emerson Knapp <537409+emersonknapp@users.noreply.github.com> Date: Mon, 9 Jan 2023 15:58:03 -0800 Subject: [PATCH 2/2] Expose more Writer methods in python interface (#1220) Signed-off-by: Emerson Knapp --- rosbag2_py/src/rosbag2_py/_writer.cpp | 73 ++++++++++++--------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/rosbag2_py/src/rosbag2_py/_writer.cpp b/rosbag2_py/src/rosbag2_py/_writer.cpp index 025ee44d66..ec74440254 100644 --- a/rosbag2_py/src/rosbag2_py/_writer.cpp +++ b/rosbag2_py/src/rosbag2_py/_writer.cpp @@ -39,27 +39,10 @@ template class Writer : public rosbag2_cpp::Writer { public: - Writer() - : writer_(std::make_unique(std::make_unique())) - { - } - - void open( - rosbag2_storage::StorageOptions & storage_options, - rosbag2_cpp::ConverterOptions & converter_options) - { - writer_->open(storage_options, converter_options); - } - - void create_topic(const rosbag2_storage::TopicMetadata & topic_with_type) - { - writer_->create_topic(topic_with_type); - } - - void remove_topic(const rosbag2_storage::TopicMetadata & topic_with_type) - { - writer_->remove_topic(topic_with_type); - } + template + explicit Writer(Args && ... args) + : rosbag2_cpp::Writer(std::make_unique(std::forward(args)...)) + {} /// Write a serialized message to a bag file void write( @@ -74,11 +57,8 @@ class Writer : public rosbag2_cpp::Writer rosbag2_storage::make_serialized_message(message.c_str(), message.length()); bag_message->time_stamp = time_stamp; - writer_->write(bag_message); + rosbag2_cpp::Writer::write(bag_message); } - -protected: - std::unique_ptr writer_; }; std::unordered_set get_registered_writers() @@ -105,29 +85,38 @@ std::unordered_set get_registered_serializers() } // namespace rosbag2_py +using PyWriter = rosbag2_py::Writer; +using PyCompressionWriter = rosbag2_py::Writer; + PYBIND11_MODULE(_writer, m) { m.doc() = "Python wrapper of the rosbag2_cpp writer API"; - pybind11::class_>( - m, "SequentialWriter") - .def(pybind11::init()) - .def("open", &rosbag2_py::Writer::open) - .def("write", &rosbag2_py::Writer::write) - .def("close", &rosbag2_py::Writer::close) - .def("remove_topic", &rosbag2_py::Writer::remove_topic) - .def("create_topic", &rosbag2_py::Writer::create_topic); - - pybind11::class_>( - m, "SequentialCompressionWriter") + pybind11::class_(m, "SequentialWriter") .def(pybind11::init()) - .def("open", &rosbag2_py::Writer::open) - .def("write", &rosbag2_py::Writer::write) .def( - "remove_topic", - &rosbag2_py::Writer::remove_topic) + "open", + pybind11::overload_cast< + const rosbag2_storage::StorageOptions &, const rosbag2_cpp::ConverterOptions & + >(&PyWriter::open)) + .def("write", &PyWriter::write) + .def("close", &PyWriter::close) + .def("remove_topic", &PyWriter::remove_topic) + .def("create_topic", &PyWriter::create_topic) + .def("take_snapshot", &PyWriter::take_snapshot) + ; + + pybind11::class_(m, "SequentialCompressionWriter") + .def(pybind11::init()) .def( - "create_topic", - &rosbag2_py::Writer::create_topic); + "open", + pybind11::overload_cast< + const rosbag2_storage::StorageOptions &, const rosbag2_cpp::ConverterOptions & + >(&PyCompressionWriter::open)) + .def("write", &PyCompressionWriter::write) + .def("remove_topic", &PyCompressionWriter::remove_topic) + .def("create_topic", &PyCompressionWriter::create_topic) + .def("take_snapshot", &PyCompressionWriter::take_snapshot) + ; m.def( "get_registered_writers",