Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions launch/launch/substitutions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,7 @@
'FindExecutable',
'LaunchConfiguration',
'LocalSubstitution',
'PathJoinSubstitution',
'PythonExpression',
'SubstitutionFailure',
'TextSubstitution',
Expand Down
46 changes: 46 additions & 0 deletions launch/launch/substitutions/path_join_substitution.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions launch/test/launch/substitutions/test_path_join_substitution.py
Original file line number Diff line number Diff line change
@@ -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)