-
Couldn't load subscription status.
- Fork 91
Add a way to set remapping rules for all nodes in the same scope #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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') | ||
hidmic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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' | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.