From 50cfccee5f0f8d4b1f738465cab17182218f8e10 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Sat, 13 Apr 2024 09:24:32 -0700 Subject: [PATCH] support `ros2 action type `. (#894) * support `ros2 action type `. Signed-off-by: Tomoya.Fujita * add review comments. Signed-off-by: Tomoya Fujita --------- Signed-off-by: Tomoya.Fujita Signed-off-by: Tomoya Fujita --- ros2action/ros2action/verb/type.py | 40 ++++++++++++++++++++++++++++++ ros2action/setup.py | 2 +- ros2action/test/test_cli.py | 10 ++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 ros2action/ros2action/verb/type.py diff --git a/ros2action/ros2action/verb/type.py b/ros2action/ros2action/verb/type.py new file mode 100644 index 000000000..acf881200 --- /dev/null +++ b/ros2action/ros2action/verb/type.py @@ -0,0 +1,40 @@ +# Copyright 2024 Sony Group Corporation. +# +# 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 ros2action.api import action_name_completer +from ros2action.api import get_action_names_and_types +from ros2action.verb import VerbExtension +from ros2cli.node.strategy import NodeStrategy + + +class TypeVerb(VerbExtension): + """Print a action's type.""" + + def add_arguments(self, parser, cli_name): + arg = parser.add_argument( + 'action_name', + help="Name of the ROS action to get info (e.g. '/fibonacci')") + arg.completer = action_name_completer + + def main(self, *, args): + with NodeStrategy(args) as node: + action_names_and_types = get_action_names_and_types(node=node) + + for (action_name, action_types) in action_names_and_types: + if args.action_name == action_name: + for action_type in action_types: + print(action_type) + return 0 + + return 1 diff --git a/ros2action/setup.py b/ros2action/setup.py index 272b01a6e..809ec8eed 100644 --- a/ros2action/setup.py +++ b/ros2action/setup.py @@ -40,10 +40,10 @@ 'ros2action.verb = ros2action.verb:VerbExtension', ], 'ros2action.verb': [ - # 'echo = ros2action.verb.echo:EchoVerb', 'info = ros2action.verb.info:InfoVerb', 'list = ros2action.verb.list:ListVerb', 'send_goal = ros2action.verb.send_goal:SendGoalVerb', + 'type = ros2action.verb.type:TypeVerb', ], } ) diff --git a/ros2action/test/test_cli.py b/ros2action/test/test_cli.py index 2d6c8d8c0..bd3b86f66 100644 --- a/ros2action/test/test_cli.py +++ b/ros2action/test/test_cli.py @@ -221,6 +221,16 @@ def test_list_count(self): assert len(command_output_lines) == 1 assert int(command_output_lines[0]) == 1 + @launch_testing.markers.retry_on_failure(times=5, delay=1) + def test_type(self): + with self.launch_action_command(arguments=['type', '/fibonacci']) as action_command: + assert action_command.wait_for_shutdown(timeout=10) + assert action_command.exit_code == launch_testing.asserts.EXIT_OK + assert launch_testing.tools.expect_output( + expected_lines=['test_msgs/action/Fibonacci'], + text=action_command.output, strict=True + ) + @launch_testing.markers.retry_on_failure(times=5, delay=1) def test_send_fibonacci_goal(self): with self.launch_action_command(