From f2c7e02a34154d7d83e82dc92d15e23f37c9ba95 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Fri, 26 Feb 2021 17:39:13 -0300 Subject: [PATCH] Expose .msg/.srv/.action to .idl conversion via rosidl translate CLI Signed-off-by: Michel Hidalgo --- rosidl_adapter/package.xml | 1 + rosidl_adapter/rosidl_adapter/cli.py | 59 +++++++++++++++++++++++++++- rosidl_adapter/setup.cfg | 5 +++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 rosidl_adapter/setup.cfg diff --git a/rosidl_adapter/package.xml b/rosidl_adapter/package.xml index c546c3208..9f047bc0b 100644 --- a/rosidl_adapter/package.xml +++ b/rosidl_adapter/package.xml @@ -14,6 +14,7 @@ ament_cmake python3-empy + rosidl_cli ament_cmake_pytest ament_lint_common diff --git a/rosidl_adapter/rosidl_adapter/cli.py b/rosidl_adapter/rosidl_adapter/cli.py index dad3f0b39..5db46d4f5 100644 --- a/rosidl_adapter/rosidl_adapter/cli.py +++ b/rosidl_adapter/rosidl_adapter/cli.py @@ -1,4 +1,4 @@ -# Copyright 2018 Open Source Robotics Foundation, Inc. +# Copyright 2018-2021 Open Source Robotics Foundation, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,6 +19,9 @@ from catkin_pkg.package import package_exists_at from catkin_pkg.package import parse_package +from rosidl_cli.command.helpers import interface_path_as_tuple +from rosidl_cli.command.translate.extensions import TranslateCommandExtension + def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]): parser = argparse.ArgumentParser( @@ -48,3 +51,57 @@ def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]): package_dir, pkg.name, interface_file.absolute().relative_to(package_dir), interface_file.parent) + + +class TranslateToIDL(TranslateCommandExtension): + + output_format = 'idl' + + def translate( + self, + package_name, + interface_files, + include_paths, + output_path + ): + translated_interface_files = [] + for interface_file in interface_files: + prefix, interface_file = interface_path_as_tuple(interface_file) + output_dir = output_path / interface_file.parent + translated_interface_file = self.conversion_function( + prefix, package_name, interface_file, output_dir) + translated_interface_file = \ + translated_interface_file.relative_to(output_path) + translated_interface_files.append( + f'{output_path}:{translated_interface_file}' + ) + return translated_interface_files + + +class TranslateMsgToIDL(TranslateToIDL): + + input_format = 'msg' + + @property + def conversion_function(self): + from rosidl_adapter.msg import convert_msg_to_idl + return convert_msg_to_idl + + +class TranslateSrvToIDL(TranslateToIDL): + + input_format = 'srv' + + @property + def conversion_function(self): + from rosidl_adapter.srv import convert_srv_to_idl + return convert_srv_to_idl + + +class TranslateActionToIDL(TranslateToIDL): + input_format = 'action' + + @property + def conversion_function(self): + from rosidl_adapter.action import convert_action_to_idl + return convert_action_to_idl diff --git a/rosidl_adapter/setup.cfg b/rosidl_adapter/setup.cfg new file mode 100644 index 000000000..bfb5cb4b2 --- /dev/null +++ b/rosidl_adapter/setup.cfg @@ -0,0 +1,5 @@ +[options.entry_points] +rosidl_cli.command.translate.extensions = + msg2idl = rosidl_adapter.cli:TranslateMsgToIDL + srv2idl = rosidl_adapter.cli:TranslateSrvToIDL + action2idl = rosidl_adapter.cli:TranslateActionToIDL