From a4bb79dc83bf37496c59791b69c469c37c18d37e Mon Sep 17 00:00:00 2001 From: ivanpauno Date: Mon, 24 Jun 2019 13:02:17 -0300 Subject: [PATCH 1/2] Add PathJoinSubstitution Signed-off-by: ivanpauno --- launch/launch/substitutions/__init__.py | 2 + .../substitutions/path_join_substitution.py | 45 +++++++++++++++++++ .../test_path_join_substitution.py | 26 +++++++++++ 3 files changed, 73 insertions(+) create mode 100644 launch/launch/substitutions/path_join_substitution.py create mode 100644 launch/test/launch/substitutions/test_path_join_substitution.py 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..3d605b343 --- /dev/null +++ b/launch/launch/substitutions/path_join_substitution.py @@ -0,0 +1,45 @@ +# 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 LocalSubstitution 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: + 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..2cf07dde5 --- /dev/null +++ b/launch/test/launch/substitutions/test_path_join_substitution.py @@ -0,0 +1,26 @@ +# 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 ThisLaunchFileDir substitution class.""" + +import os + +from launch.substitutions import PathJoinSubstitution + + +def test_this_launch_file_path(): + """Test the constructors for ThisLaunchFileDir class.""" + path = ['asd', 'bsd', 'cds'] + sub = PathJoinSubstitution(path) + assert sub.perform(None) == os.path.join(*path) From 469d0af66b39f84514ae3e8dc1696b79c1f949df Mon Sep 17 00:00:00 2001 From: ivanpauno Date: Mon, 24 Jun 2019 14:01:18 -0300 Subject: [PATCH 2/2] Correct with PR comment Signed-off-by: ivanpauno --- launch/launch/substitutions/path_join_substitution.py | 3 ++- .../test/launch/substitutions/test_path_join_substitution.py | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/launch/launch/substitutions/path_join_substitution.py b/launch/launch/substitutions/path_join_substitution.py index 3d605b343..834bdd70a 100644 --- a/launch/launch/substitutions/path_join_substitution.py +++ b/launch/launch/substitutions/path_join_substitution.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Module for the LocalSubstitution substitution.""" +"""Module for the PathJoinSubstitution substitution.""" import os from typing import Iterable @@ -27,6 +27,7 @@ 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) diff --git a/launch/test/launch/substitutions/test_path_join_substitution.py b/launch/test/launch/substitutions/test_path_join_substitution.py index 2cf07dde5..1f6bec38d 100644 --- a/launch/test/launch/substitutions/test_path_join_substitution.py +++ b/launch/test/launch/substitutions/test_path_join_substitution.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Tests for the ThisLaunchFileDir substitution class.""" +"""Tests for the PathJoinSubstitution substitution class.""" import os @@ -20,7 +20,6 @@ def test_this_launch_file_path(): - """Test the constructors for ThisLaunchFileDir class.""" path = ['asd', 'bsd', 'cds'] sub = PathJoinSubstitution(path) assert sub.perform(None) == os.path.join(*path)