-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to set remapping rules for all nodes in the same scope (#163)
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
- Loading branch information
1 parent
c3b3be0
commit b845504
Showing
5 changed files
with
231 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright 2020 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. | ||
|
||
"""Module for the `SetRemap` action.""" | ||
|
||
from typing import List | ||
|
||
from launch import Action | ||
from launch import Substitution | ||
from launch.frontend import Entity | ||
from launch.frontend import expose_action | ||
from launch.frontend import Parser | ||
from launch.launch_context import LaunchContext | ||
from launch.some_substitutions_type import SomeSubstitutionsType | ||
from launch.utilities import normalize_to_list_of_substitutions | ||
from launch.utilities import perform_substitutions | ||
|
||
|
||
@expose_action('set_remap') | ||
class SetRemap(Action): | ||
""" | ||
Action that sets a remapping rule in the current context. | ||
This remapping rule will be passed to all the nodes launched in the same scope, overriding | ||
the ones specified in the `Node` action constructor. | ||
e.g.: | ||
```python3 | ||
LaunchDescription([ | ||
..., | ||
GroupAction( | ||
actions = [ | ||
..., | ||
SetRemap(src='asd', dst='bsd'), | ||
..., | ||
Node(...), // the remap rule will be passed to this node | ||
..., | ||
] | ||
), | ||
Node(...), // here it won't be passed, as it's not in the same scope | ||
... | ||
]) | ||
``` | ||
""" | ||
|
||
def __init__( | ||
self, | ||
src: SomeSubstitutionsType, | ||
dst: SomeSubstitutionsType, | ||
**kwargs | ||
) -> None: | ||
"""Create a SetRemap action.""" | ||
super().__init__(**kwargs) | ||
self.__src = normalize_to_list_of_substitutions(src) | ||
self.__dst = normalize_to_list_of_substitutions(dst) | ||
|
||
@classmethod | ||
def parse(cls, entity: Entity, parser: Parser): | ||
"""Return `SetRemap` action and kwargs for constructing it.""" | ||
_, kwargs = super().parse(entity, parser) | ||
kwargs['src'] = parser.parse_substitution(entity.get_attr('from')) | ||
kwargs['dst'] = parser.parse_substitution(entity.get_attr('to')) | ||
return cls, kwargs | ||
|
||
@property | ||
def src(self) -> List[Substitution]: | ||
"""Getter for src.""" | ||
return self.__src | ||
|
||
@property | ||
def dst(self) -> List[Substitution]: | ||
"""Getter for dst.""" | ||
return self.__dst | ||
|
||
def execute(self, context: LaunchContext): | ||
"""Execute the action.""" | ||
src = perform_substitutions(context, self.__src) | ||
dst = perform_substitutions(context, self.__dst) | ||
global_remaps = context.launch_configurations.get('ros_remaps', []) | ||
global_remaps.append((src, dst)) | ||
context.launch_configurations['ros_remaps'] = global_remaps |
106 changes: 106 additions & 0 deletions
106
test_launch_ros/test/test_launch_ros/actions/test_set_remap.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Copyright 2020 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. | ||
|
||
"""Tests for the SetRemap Action.""" | ||
|
||
from launch import LaunchContext | ||
from launch.actions import PopLaunchConfigurations | ||
from launch.actions import PushLaunchConfigurations | ||
|
||
from launch_ros.actions import Node | ||
from launch_ros.actions import SetRemap | ||
from launch_ros.actions.load_composable_nodes import get_composable_node_load_request | ||
from launch_ros.descriptions import ComposableNode | ||
|
||
import pytest | ||
|
||
|
||
class MockContext: | ||
|
||
def __init__(self): | ||
self.launch_configurations = {} | ||
|
||
def perform_substitution(self, sub): | ||
return sub.perform(None) | ||
|
||
|
||
def get_set_remap_test_remaps(): | ||
return [ | ||
pytest.param( | ||
[('from', 'to')], | ||
id='One remapping rule' | ||
), | ||
pytest.param( | ||
[('from1', 'to1'), ('from2', 'to2')], | ||
id='Two remapping rules' | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'remapping_rules', | ||
get_set_remap_test_remaps() | ||
) | ||
def test_set_remap(remapping_rules): | ||
lc = MockContext() | ||
for src, dst in remapping_rules: | ||
SetRemap(src, dst).execute(lc) | ||
assert lc.launch_configurations == {'ros_remaps': remapping_rules} | ||
|
||
|
||
def test_set_remap_is_scoped(): | ||
lc = LaunchContext() | ||
push_conf = PushLaunchConfigurations() | ||
pop_conf = PopLaunchConfigurations() | ||
set_remap = SetRemap('from', 'to') | ||
|
||
push_conf.execute(lc) | ||
set_remap.execute(lc) | ||
assert lc.launch_configurations == {'ros_remaps': [('from', 'to')]} | ||
pop_conf.execute(lc) | ||
assert lc.launch_configurations == {} | ||
|
||
|
||
def test_set_remap_with_node(): | ||
lc = MockContext() | ||
node = Node( | ||
package='asd', | ||
executable='bsd', | ||
name='my_node', | ||
namespace='my_ns', | ||
remappings=[('from2', 'to2')] | ||
) | ||
set_remap = SetRemap('from1', 'to1') | ||
set_remap.execute(lc) | ||
node._perform_substitutions(lc) | ||
assert len(node.expanded_remapping_rules) == 2 | ||
assert node.expanded_remapping_rules == [('from1', 'to1'), ('from2', 'to2')] | ||
|
||
|
||
def test_set_remap_with_composable_node(): | ||
lc = MockContext() | ||
node_description = ComposableNode( | ||
package='asd', | ||
plugin='my_plugin', | ||
name='my_node', | ||
namespace='my_ns', | ||
remappings=[('from2', 'to2')] | ||
) | ||
set_remap = SetRemap('from1', 'to1') | ||
set_remap.execute(lc) | ||
request = get_composable_node_load_request(node_description, lc) | ||
remappings = request.remap_rules | ||
assert len(remappings) == 2 | ||
assert remappings[0] == 'from1:=to1' | ||
assert remappings[1] == 'from2:=to2' |