Skip to content

Commit

Permalink
Move utility functions to another file
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffrey Biggs <gbiggs@killbots.net>
  • Loading branch information
gbiggs committed Mar 18, 2021
1 parent 40ee929 commit 6754e9a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 44 deletions.
61 changes: 17 additions & 44 deletions launch_ros/launch_ros/actions/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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


Expand Down Expand Up @@ -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}")
Expand All @@ -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))
53 changes: 53 additions & 0 deletions launch_ros/launch_ros/utilities/plugin_support.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 6754e9a

Please sign in to comment.