From 6754e9a366589dec1b1535eeef2e08ef765b1301 Mon Sep 17 00:00:00 2001 From: Geoffrey Biggs Date: Thu, 18 Mar 2021 09:00:43 +0900 Subject: [PATCH] Move utility functions to another file Signed-off-by: Geoffrey Biggs --- launch_ros/launch_ros/actions/node.py | 61 ++++++------------- .../launch_ros/utilities/plugin_support.py | 53 ++++++++++++++++ 2 files changed, 70 insertions(+), 44 deletions(-) create mode 100644 launch_ros/launch_ros/utilities/plugin_support.py diff --git a/launch_ros/launch_ros/actions/node.py b/launch_ros/launch_ros/actions/node.py index ab60d7fa..c5cb81b4 100644 --- a/launch_ros/launch_ros/actions/node.py +++ b/launch_ros/launch_ros/actions/node.py @@ -50,12 +50,12 @@ from launch_ros.utilities import normalize_parameters from launch_ros.utilities import normalize_remap_rules from launch_ros.utilities import prefix_namespace +from launch_ros.utilities import plugin_support from rclpy.validate_namespace import validate_namespace from rclpy.validate_node_name import validate_node_name import importlib_metadata -from packaging.version import Version import yaml from ..descriptions import Parameter @@ -70,13 +70,8 @@ class NodeActionExtension: * `NAME` (will be set to the entry point name) The following methods may be defined: - * `command_extension` to extend the command used to launch the node's - process. This method must return a list of parameters with which to - extend the command. - * `execute` to perform any actions prior to the node's process being - launched. `node_info` is an instance of `NodeInfo`. - This method must return `ros_specific_arguments` with any modifications - made to it. + * `command_extension` + * `execute` """ class NodeInfo: @@ -93,9 +88,22 @@ def __init__(self): satisfies_version(self.EXTENSION_POINT_VERSION, '^0.1') def command_extension(self, context): + """ + Extend the command used to launch the node's process. + + This method must return a list of parameters with which to extend the + command. + """ return [] def execute(self, context, ros_specific_arguments, node_info): + """ Perform any actions prior to the node's process being launched. + + `node_info` is an instance of `NodeInfo`. + + This method must return `ros_specific_arguments` with any modifications + made to it. + """ return ros_specific_arguments @@ -514,7 +522,7 @@ def _instantiate_extension( try: extension_instance = extension_class() - except PluginException as e: # noqa: F841 + except plugin_support.PluginException as e: # noqa: F841 logger.warning( f"Failed to instantiate '{group_name}' extension " f"'{extension_name}': {e}") @@ -529,38 +537,3 @@ def _instantiate_extension( return extension_instance -class PluginException(Exception): - """Base class for all exceptions within the plugin system.""" - - pass - - -def satisfies_version(version, caret_range): - assert caret_range.startswith('^'), 'Only supports caret ranges' - extension_point_version = Version(version) - extension_version = Version(caret_range[1:]) - next_extension_version = get_upper_bound_caret_version( - extension_version) - - if extension_point_version < extension_version: - raise PluginException( - 'Extension point is too old (%s), the extension requires ' - "'%s'" % (extension_point_version, extension_version)) - - if extension_point_version >= next_extension_version: - raise PluginException( - 'Extension point is newer (%s), than what the extension ' - "supports '%s'" % (extension_point_version, extension_version)) - - -def get_upper_bound_caret_version(version): - parts = version.base_version.split('.') - if len(parts) < 2: - parts += [0] * (2 - len(parts)) - major, minor = [int(p) for p in parts[:2]] - if major > 0: - major += 1 - minor = 0 - else: - minor += 1 - return Version('%d.%d.0' % (major, minor)) diff --git a/launch_ros/launch_ros/utilities/plugin_support.py b/launch_ros/launch_ros/utilities/plugin_support.py new file mode 100644 index 00000000..bcce1b59 --- /dev/null +++ b/launch_ros/launch_ros/utilities/plugin_support.py @@ -0,0 +1,53 @@ +# Copyright 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. +# 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 packaging.version import Version + + +class PluginException(Exception): + """Base class for all exceptions within the plugin system.""" + + pass + + +def satisfies_version(version, caret_range): + assert caret_range.startswith('^'), 'Only supports caret ranges' + extension_point_version = Version(version) + extension_version = Version(caret_range[1:]) + next_extension_version = get_upper_bound_caret_version( + extension_version) + + if extension_point_version < extension_version: + raise PluginException( + 'Extension point is too old (%s), the extension requires ' + "'%s'" % (extension_point_version, extension_version)) + + if extension_point_version >= next_extension_version: + raise PluginException( + 'Extension point is newer (%s), than what the extension ' + "supports '%s'" % (extension_point_version, extension_version)) + + +def get_upper_bound_caret_version(version): + parts = version.base_version.split('.') + if len(parts) < 2: + parts += [0] * (2 - len(parts)) + major, minor = [int(p) for p in parts[:2]] + if major > 0: + major += 1 + minor = 0 + else: + minor += 1 + return Version('%d.%d.0' % (major, minor)) +