diff --git a/launch/launch/substitutions/__init__.py b/launch/launch/substitutions/__init__.py index 7c3f0edd0..426e8e9c5 100644 --- a/launch/launch/substitutions/__init__.py +++ b/launch/launch/substitutions/__init__.py @@ -18,6 +18,7 @@ from .find_executable import FindExecutable from .launch_configuration import LaunchConfiguration from .local_substitution import LocalSubstitution +from .path_join_substitution import PathJoinSubstitution from .python_expression import PythonExpression from .substitution_failure import SubstitutionFailure from .text_substitution import TextSubstitution @@ -28,6 +29,7 @@ 'FindExecutable', 'LaunchConfiguration', 'LocalSubstitution', + 'PathJoinSubstitution', 'PythonExpression', 'SubstitutionFailure', 'TextSubstitution', diff --git a/launch/launch/substitutions/path_join_substitution.py b/launch/launch/substitutions/path_join_substitution.py new file mode 100644 index 000000000..834bdd70a --- /dev/null +++ b/launch/launch/substitutions/path_join_substitution.py @@ -0,0 +1,46 @@ +# 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 the PathJoinSubstitution substitution.""" + +import os +from typing import Iterable +from typing import Text + +from ..launch_context import LaunchContext +from ..some_substitutions_type import SomeSubstitutionsType +from ..substitution import Substitution + + +class PathJoinSubstitution(Substitution): + """Substitution that join paths, in a platform independent way.""" + + def __init__(self, substitutions: Iterable[SomeSubstitutionsType]) -> None: + """Constructor.""" + from ..utilities import normalize_to_list_of_substitutions + self.__substitutions = normalize_to_list_of_substitutions(substitutions) + + @property + def substitutions(self) -> Iterable[Substitution]: + """Getter for variable_name.""" + return self.__substitutions + + def describe(self) -> Text: + """Return a description of this substitution as a string.""" + return "LocalVar('{}')".format(' + '.join([s.describe() for s in self.substitutions])) + + def perform(self, context: LaunchContext) -> Text: + """Perform the substitution by retrieving the local variable.""" + performed_substitutions = [sub.perform(context) for sub in self.__substitutions] + return os.path.join(*performed_substitutions) diff --git a/launch/test/launch/substitutions/test_path_join_substitution.py b/launch/test/launch/substitutions/test_path_join_substitution.py new file mode 100644 index 000000000..1f6bec38d --- /dev/null +++ b/launch/test/launch/substitutions/test_path_join_substitution.py @@ -0,0 +1,25 @@ +# 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 PathJoinSubstitution substitution class.""" + +import os + +from launch.substitutions import PathJoinSubstitution + + +def test_this_launch_file_path(): + path = ['asd', 'bsd', 'cds'] + sub = PathJoinSubstitution(path) + assert sub.perform(None) == os.path.join(*path)