-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add normalize_parameters and evaluate_paramters (#192)
* Add normalize_parameters and evaluate_paramters Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Misc mypy complaint fixes Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * More mypy appeasement Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * fix bugs introduced whil appeasing mypy Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Another mypy complaint fixed Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * fix checking str and substitution Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Avoid indentation Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * keys -> key Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * substututions -> substitutions Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Add test with float as first element in numeric list Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Add test with string before substitution Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Add more dissimilar tests and fix bugs Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * SomeParameterValue multi value substitutions Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
- Loading branch information
Showing
7 changed files
with
582 additions
and
106 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,64 @@ | ||
# 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 ROS parameter types.""" | ||
|
||
import pathlib | ||
|
||
from typing import Any | ||
from typing import Dict | ||
from typing import Mapping | ||
from typing import Sequence | ||
from typing import Union | ||
|
||
from launch.some_substitutions_type import SomeSubstitutionsType | ||
from launch.substitution import Substitution | ||
|
||
|
||
# Parameter value types used below | ||
_SingleValueType = Union[str, int, float, bool] | ||
_MultiValueType = Union[ | ||
Sequence[str], Sequence[int], Sequence[float], Sequence[bool], bytes] | ||
|
||
SomeParameterFile = Union[SomeSubstitutionsType, pathlib.Path] | ||
SomeParameterName = Sequence[Union[Substitution, str]] | ||
SomeParameterValue = Union[SomeSubstitutionsType, | ||
Sequence[SomeSubstitutionsType], | ||
_SingleValueType, | ||
_MultiValueType] | ||
|
||
# TODO(sloretz) Recursive type when mypy supports them python/mypy#731 | ||
_SomeParametersDict = Mapping[SomeParameterName, Any] | ||
SomeParametersDict = Mapping[SomeParameterName, Union[SomeParameterValue, _SomeParametersDict]] | ||
|
||
# Paths to yaml files with parameters, or dictionaries of parameters, or pairs of | ||
# parameter names and values | ||
SomeParameters = Sequence[Union[SomeParameterFile, Mapping[SomeParameterName, SomeParameterValue]]] | ||
|
||
ParameterFile = Sequence[Substitution] | ||
ParameterName = Sequence[Substitution] | ||
ParameterValue = Union[Sequence[Substitution], | ||
Sequence[Sequence[Substitution]], | ||
_SingleValueType, | ||
_MultiValueType] | ||
|
||
# Normalized (flattened to avoid having a recursive type) parameter dict | ||
ParametersDict = Dict[ParameterName, ParameterValue] | ||
|
||
# Normalized parameters | ||
Parameters = Sequence[Union[ParameterFile, ParametersDict]] | ||
|
||
EvaluatedParameterValue = Union[_SingleValueType, _MultiValueType] | ||
# Evaluated parameters: filenames or dictionary after substitutions have been evaluated | ||
EvaluatedParameters = Sequence[Union[pathlib.Path, Dict[str, EvaluatedParameterValue]]] |
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,86 @@ | ||
# 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 with utility for evaluating parametesr in a launch context.""" | ||
|
||
|
||
from collections.abc import Mapping | ||
from collections.abc import Sequence | ||
import pathlib | ||
from typing import cast | ||
from typing import Dict | ||
from typing import List | ||
from typing import Optional # noqa | ||
from typing import Union | ||
|
||
from launch.launch_context import LaunchContext | ||
from launch.substitution import Substitution | ||
from launch.utilities import ensure_argument_type | ||
from launch.utilities import perform_substitutions | ||
|
||
from ..parameters_type import EvaluatedParameters | ||
from ..parameters_type import EvaluatedParameterValue # noqa | ||
from ..parameters_type import Parameters | ||
|
||
|
||
def evaluate_parameters(context: LaunchContext, parameters: Parameters) -> EvaluatedParameters: | ||
""" | ||
Evaluate substitutions to produce paths and name/value pairs. | ||
The parameters must have been normalized with normalize_parameters() prior to calling this. | ||
:param parameters: normalized parameters | ||
:returns: values after evaluating lists of substitutions | ||
""" | ||
output_params = [] # type: List[Union[pathlib.Path, Dict[str, EvaluatedParameterValue]]] | ||
for param in parameters: | ||
# If it's a list of substitutions then evaluate them to a string and return a pathlib.Path | ||
if isinstance(param, tuple) and len(param) and isinstance(param[0], Substitution): | ||
# Evaluate a list of Substitution to a file path | ||
output_params.append(pathlib.Path(perform_substitutions(context, list(param)))) | ||
elif isinstance(param, Mapping): | ||
# It's a list of name/value pairs | ||
output_dict = {} # type: Dict[str, EvaluatedParameterValue] | ||
for name, value in param.items(): | ||
if not isinstance(name, tuple): | ||
raise TypeError('Expecting tuple of substitutions got {}'.format(repr(name))) | ||
evaluated_name = perform_substitutions(context, list(name)) # type: str | ||
evaluated_value = None # type: Optional[EvaluatedParameterValue] | ||
|
||
if isinstance(value, tuple) and len(value): | ||
if isinstance(value[0], Substitution): | ||
# Value is a list of substitutions, so perform them to make a string | ||
evaluated_value = perform_substitutions(context, list(value)) | ||
elif isinstance(value[0], Sequence): | ||
# Value is an array of a list of substitutions | ||
output_subvalue = [] # List[str] | ||
for subvalue in value: | ||
output_subvalue.append(perform_substitutions(context, list(subvalue))) | ||
evaluated_value = tuple(output_subvalue) | ||
else: | ||
# Value is an array of the same type, so nothing to evaluate. | ||
output_value = [] | ||
target_type = type(value[0]) | ||
for i, subvalue in enumerate(value): | ||
output_value.append(target_type(subvalue)) | ||
evaluated_value = tuple(output_value) | ||
else: | ||
# Value is a singular type, so nothing to evaluate | ||
ensure_argument_type(value, (float, int, str, bool, bytes), 'value') | ||
evaluated_value = cast(Union[float, int, str, bool, bytes], value) | ||
if evaluated_value is None: | ||
raise TypeError('given unnormalized parameters %r, %r' % (name, value)) | ||
output_dict[evaluated_name] = evaluated_value | ||
output_params.append(output_dict) | ||
return tuple(output_params) |
Oops, something went wrong.