Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[humble] Expose more Writer methods in python interface (backport #1220 and #1339) #1829

Open
wants to merge 2 commits into
base: humble
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rosbag2_cpp/include/rosbag2_cpp/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
74 changes: 32 additions & 42 deletions rosbag2_py/src/rosbag2_py/_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,13 @@ namespace rosbag2_py
{

template<typename T>
class Writer
class Writer : public rosbag2_cpp::Writer
{
public:
Writer()
: writer_(std::make_unique<rosbag2_cpp::Writer>(std::make_unique<T>()))
{
}

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<typename ... Args>
explicit Writer(Args && ... args)
: rosbag2_cpp::Writer(std::make_unique<T>(std::forward<Args>(args)...))
{}

/// Write a serialized message to a bag file
void write(
Expand All @@ -74,11 +57,8 @@ class 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<rosbag2_cpp::Writer> writer_;
};

std::unordered_set<std::string> get_registered_writers()
Expand All @@ -105,28 +85,38 @@ std::unordered_set<std::string> get_registered_serializers()

} // namespace rosbag2_py

using PyWriter = rosbag2_py::Writer<rosbag2_cpp::writers::SequentialWriter>;
using PyCompressionWriter = rosbag2_py::Writer<rosbag2_compression::SequentialCompressionWriter>;

PYBIND11_MODULE(_writer, m) {
m.doc() = "Python wrapper of the rosbag2_cpp writer API";

pybind11::class_<rosbag2_py::Writer<rosbag2_cpp::writers::SequentialWriter>>(
m, "SequentialWriter")
.def(pybind11::init())
.def("open", &rosbag2_py::Writer<rosbag2_cpp::writers::SequentialWriter>::open)
.def("write", &rosbag2_py::Writer<rosbag2_cpp::writers::SequentialWriter>::write)
.def("remove_topic", &rosbag2_py::Writer<rosbag2_cpp::writers::SequentialWriter>::remove_topic)
.def("create_topic", &rosbag2_py::Writer<rosbag2_cpp::writers::SequentialWriter>::create_topic);

pybind11::class_<rosbag2_py::Writer<rosbag2_compression::SequentialCompressionWriter>>(
m, "SequentialCompressionWriter")
pybind11::class_<PyWriter>(m, "SequentialWriter")
.def(pybind11::init())
.def("open", &rosbag2_py::Writer<rosbag2_compression::SequentialCompressionWriter>::open)
.def("write", &rosbag2_py::Writer<rosbag2_compression::SequentialCompressionWriter>::write)
.def(
"remove_topic",
&rosbag2_py::Writer<rosbag2_compression::SequentialCompressionWriter>::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)
;
Comment on lines +104 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JannikJose Out of curiosity why .def("split_bagfile", &PyWriter::split_bagfile) missed from the original PR https://github.com/ros2/rosbag2/pull/1220/files#diff-f3aa422fd35406262b9209321476913d230c5bcd81b395525719e0166395ce90R103 ?


pybind11::class_<PyCompressionWriter>(m, "SequentialCompressionWriter")
.def(pybind11::init())
.def(
"create_topic",
&rosbag2_py::Writer<rosbag2_compression::SequentialCompressionWriter>::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",
Expand Down