Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TextJoinSubstitution #768

Closed
Closed
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 @@ -33,6 +33,7 @@
from .path_join_substitution import PathJoinSubstitution
from .python_expression import PythonExpression
from .substitution_failure import SubstitutionFailure
from .text_join_substitution import TextJoinSubstitution
from .text_substitution import TextSubstitution
from .this_launch_file import ThisLaunchFile
from .this_launch_file_dir import ThisLaunchFileDir
Expand All @@ -57,6 +58,7 @@
'PathJoinSubstitution',
'PythonExpression',
'SubstitutionFailure',
'TextJoinSubstitution',
'TextSubstitution',
'ThisLaunchFile',
'ThisLaunchFileDir',
Expand Down
56 changes: 56 additions & 0 deletions launch/launch/substitutions/text_join_substitution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 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 TextJoinSubstitution substitution."""

from typing import Iterable
from typing import Text
from typing import Union

from ..launch_context import LaunchContext
from ..substitution import Substitution


class TextJoinSubstitution(Substitution):
"""Substitution that join texts using a separator."""

def __init__(
self, substitutions: Iterable[Union[Text, Substitution]], separator: Text = ""
) -> None:
"""Create a TextJoinSubstitution."""
from ..utilities import normalize_to_list_of_substitutions

self.__substitutions = normalize_to_list_of_substitutions(substitutions)
self.__separator = separator

@property
def substitutions(self) -> Iterable[Substitution]:
"""Getter for variable_name."""
return self.__substitutions

@property
def separator(self) -> Text:
"""Getter for separator."""
return self.__separator

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 joining the texts."""
performed_substitutions = [sub.perform(context) for sub in self.__substitutions]
return self.__separator.join(performed_substitutions)
29 changes: 29 additions & 0 deletions launch/test/launch/substitutions/test_text_join_substitution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 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 TextJoinSubstitution substitution class."""

from launch.substitutions import TextJoinSubstitution


def test_text_join_default():
texts = ["ab", "cde", "f"]
sub = TextJoinSubstitution(texts)
assert sub.perform(None) == "abcdef"


def test_text_join():
texts = ["ab", "cde", "f"]
sub = TextJoinSubstitution(texts, separator=".")
assert sub.perform(None) == "ab.cde.f"