From 7f4f61b7f52d50d246995e8a7cecf78d79fad79e Mon Sep 17 00:00:00 2001 From: Ruslan Date: Wed, 27 Nov 2024 15:56:27 +0300 Subject: [PATCH] added prefix verb --- README.md | 1 + ros2bag_tools/ros2bag_tools/filter/prefix.py | 43 ++++++++++++++++++++ ros2bag_tools/ros2bag_tools/verb/prefix.py | 23 +++++++++++ ros2bag_tools/setup.py | 2 + 4 files changed, 69 insertions(+) create mode 100644 ros2bag_tools/ros2bag_tools/filter/prefix.py create mode 100644 ros2bag_tools/ros2bag_tools/verb/prefix.py diff --git a/README.md b/README.md index 4c4a39d..a604f62 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ ros2bag_tools adds verb extensions to the ros2bag cli. | plot | plot message data to a new window, see [plot](#plot) | | process | chain multiple filters, see [chaining](#chaining) | | **prune** | remove topics without messages | +| **prefix** | add a fixed prefix to all topics inside a bag file | | **reframe** | change frame_id on messages with headers | | **rename** | change name of a topic | | **replace** | replace messages of a specific topic with message data specified in a yaml file | diff --git a/ros2bag_tools/ros2bag_tools/filter/prefix.py b/ros2bag_tools/ros2bag_tools/filter/prefix.py new file mode 100644 index 0000000..eb3e0b3 --- /dev/null +++ b/ros2bag_tools/ros2bag_tools/filter/prefix.py @@ -0,0 +1,43 @@ +# Copyright 2021 AIT Austrian Institute of Technology GmbH +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ros2bag_tools.filter import FilterExtension + + +class PrefixAppendFilter(FilterExtension): + + def __init__(self): + self._topic = None + self._new_name = None + + def add_arguments(self, parser): + parser.add_argument('-p', '--prefix', help='prefix to add') + # parser.add_argument('--name', required=True, help='new name to set') + + def set_args(self, _metadata, args): + self._prefix = args.prefix + + def _add_prefix(self, topic_name): + return f"{self._prefix}{topic_name}" + + def filter_topic(self, topic_metadata): + # if topic_metadata.name == self._topic: + + topic_metadata.name = self._add_prefix(topic_metadata.name) + return topic_metadata + + def filter_msg(self, msg): + (topic, data, t) = msg + + return (self._add_prefix(topic), data, t) \ No newline at end of file diff --git a/ros2bag_tools/ros2bag_tools/verb/prefix.py b/ros2bag_tools/ros2bag_tools/verb/prefix.py new file mode 100644 index 0000000..b81585a --- /dev/null +++ b/ros2bag_tools/ros2bag_tools/verb/prefix.py @@ -0,0 +1,23 @@ +# Copyright 2021 AIT Austrian Institute of Technology GmbH +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ros2bag_tools.filter.prefix import PrefixAppendFilter +from ros2bag_tools.verb import FilterVerb + + +class PrefixVerb(FilterVerb): + """Rename specific topics in a bag, and write to new bag.""" + + def __init__(self): + FilterVerb.__init__(self, PrefixAppendFilter()) diff --git a/ros2bag_tools/setup.py b/ros2bag_tools/setup.py index fd6792e..671e595 100644 --- a/ros2bag_tools/setup.py +++ b/ros2bag_tools/setup.py @@ -39,6 +39,7 @@ 'export = ros2bag_tools.verb.export:ExportVerb', 'extract = ros2bag_tools.verb.extract:ExtractVerb', 'plot = ros2bag_tools.verb.plot:PlotVerb', + 'prefix = ros2bag_tools.verb.prefix:PrefixVerb', 'prune = ros2bag_tools.verb.prune:PruneVerb', 'reframe = ros2bag_tools.verb.reframe:ReframeVerb', 'rename = ros2bag_tools.verb.rename:RenameVerb', @@ -55,6 +56,7 @@ 'drop = ros2bag_tools.filter.drop:DropFilter', 'extract = ros2bag_tools.filter.extract:ExtractFilter', 'image = ros2bag_tools.filter.image:ImageFilter', + 'prefix = ros2bag_tools.filter.prefix:PrefixAppendFilter', 'prune = ros2bag_tools.filter.prune:PruneFilter', 'reframe = ros2bag_tools.filter.reframe:ReframeFilter', 'rename = ros2bag_tools.filter.rename:RenameFilter',