forked from ros2/launch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add normalize_remap_rule and types (ros2#173)
* Add normalize_remap_rule and types Adds remap rule types to and utility functions to type check and normalize remap rules. Signed-off-by: Shane Loretz<[email protected]> * Add tests for normalize_remap_rule Signed-off-by: Shane Loretz <[email protected]>
- Loading branch information
Showing
5 changed files
with
164 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright 2019 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 remap rule types.""" | ||
|
||
from typing import Iterable | ||
from typing import Tuple | ||
|
||
from launch.some_substitutions_type import SomeSubstitutionsType | ||
from launch.substitution import Substitution | ||
|
||
|
||
SomeRemapRule = Tuple[SomeSubstitutionsType, SomeSubstitutionsType] | ||
SomeRemapRules = Iterable[SomeRemapRule] | ||
RemapRule = Tuple[Tuple[Substitution, ...], Tuple[Substitution, ...]] | ||
RemapRules = Iterable[RemapRule] |
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,28 @@ | ||
# Copyright 2019 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 descriptions of launchable entities. | ||
Descriptions are not executable and are immutable so they can be reused by launch entities. | ||
""" | ||
|
||
from .normalize_remap_rule import normalize_remap_rule | ||
from .normalize_remap_rule import normalize_remap_rules | ||
|
||
|
||
__all__ = [ | ||
'normalize_remap_rule', | ||
'normalize_remap_rules', | ||
] |
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,39 @@ | ||
# Copyright 2019 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 normalize_remap_rule utility.""" | ||
|
||
from launch.utilities import ensure_argument_type | ||
from launch.utilities import normalize_to_list_of_substitutions | ||
|
||
from ..remap_rule_type import RemapRule | ||
from ..remap_rule_type import RemapRules | ||
from ..remap_rule_type import SomeRemapRule | ||
from ..remap_rule_type import SomeRemapRules | ||
|
||
|
||
def normalize_remap_rule(remap_rule: SomeRemapRule) -> RemapRule: | ||
"""Normalize a remap rule to a specific type.""" | ||
ensure_argument_type(remap_rule, (tuple), 'remap_rule') | ||
if len(remap_rule) != 2: | ||
raise TypeError( | ||
'remap_rule must be a tuple of length 2, got length {}'.format(len(remap_rule))) | ||
from_sub = tuple(normalize_to_list_of_substitutions(remap_rule[0])) | ||
to_sub = tuple(normalize_to_list_of_substitutions(remap_rule[1])) | ||
return from_sub, to_sub | ||
|
||
|
||
def normalize_remap_rules(remap_rules: SomeRemapRules) -> RemapRules: | ||
"""Normalize multiple remap rules.""" | ||
yield from map(normalize_remap_rule, remap_rules) |
66 changes: 66 additions & 0 deletions
66
test_launch_ros/test/test_launch_ros/test_normalize_remap_rule.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,66 @@ | ||
# Copyright 2019 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 Node Action.""" | ||
|
||
from launch import LaunchContext | ||
from launch.substitutions import TextSubstitution | ||
from launch.utilities import perform_substitutions | ||
from launch_ros.utilities import normalize_remap_rule | ||
from launch_ros.utilities import normalize_remap_rules | ||
import pytest | ||
|
||
|
||
def test_not_a_tuple(): | ||
with pytest.raises(TypeError): | ||
normalize_remap_rule(['foo', 'bar']) | ||
|
||
|
||
def test_wrong_rule_length(): | ||
with pytest.raises(TypeError): | ||
normalize_remap_rule(('foo')) | ||
|
||
with pytest.raises(TypeError): | ||
normalize_remap_rule(('foo', 'bar', 'baz')) | ||
|
||
|
||
def test_plain_text(): | ||
lc = LaunchContext() | ||
rule = ('foo', 'bar') | ||
output_rule = normalize_remap_rule(rule) | ||
assert isinstance(output_rule, tuple) | ||
assert len(output_rule) == 2 | ||
assert rule[0] == perform_substitutions(lc, output_rule[0]) | ||
assert rule[1] == perform_substitutions(lc, output_rule[1]) | ||
|
||
|
||
def test_mixed_substitutions(): | ||
lc = LaunchContext() | ||
rule = (('foo', 'bar'), ['bar', TextSubstitution(text='baz')]) | ||
output_rule = normalize_remap_rule(rule) | ||
assert isinstance(output_rule, tuple) | ||
assert len(output_rule) == 2 | ||
assert 'foobar' == perform_substitutions(lc, output_rule[0]) | ||
assert 'barbaz' == perform_substitutions(lc, output_rule[1]) | ||
|
||
|
||
def test_multiple_rules(): | ||
lc = LaunchContext() | ||
rules = [('ping', 'pong'), (('baz', 'foo'), ['bar', TextSubstitution(text='baz')])] | ||
output_rules = list(normalize_remap_rules(rules)) | ||
assert len(rules) == len(output_rules) | ||
assert 'ping' == perform_substitutions(lc, output_rules[0][0]) | ||
assert 'pong' == perform_substitutions(lc, output_rules[0][1]) | ||
assert 'bazfoo' == perform_substitutions(lc, output_rules[1][0]) | ||
assert 'barbaz' == perform_substitutions(lc, output_rules[1][1]) |