-
Notifications
You must be signed in to change notification settings - Fork 166
Add StringJoinSubstitution substituion #843
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
Merged
christophebedard
merged 6 commits into
ros2:rolling
from
baconbrot:stringjoinsubstitution
Apr 6, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ccd0336
Add StringJoinSubstitution substituion
baconbrot 9878bc1
Update copyright year
baconbrot d3296ab
Replace delimiter with SomeSubstitutionType
baconbrot e0cf19d
Fix delimiter property type
baconbrot c69d032
Fix flake Q000 E302 and E501
baconbrot 5d124d3
Update launch/launch/substitutions/string_join_substitution.py
christophebedard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,98 @@ | ||
| # Copyright 2025 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 StringJoinSubstitution substitution.""" | ||
|
|
||
| from typing import Iterable, List, Text | ||
|
|
||
| from ..launch_context import LaunchContext | ||
| from ..some_substitutions_type import SomeSubstitutionsType | ||
| from ..substitution import Substitution | ||
| from ..utilities import perform_substitutions | ||
|
|
||
|
|
||
| class StringJoinSubstitution(Substitution): | ||
| """ | ||
| Substitution that joins strings and/or other substitutions. | ||
|
|
||
| This takes in a list of string components as substitutions. | ||
| The substitutions for each string component are performed and concatenated, | ||
| and then all string components are joined with a specified delimiter as seperation. | ||
|
|
||
| For example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| StringJoinSubstitution( | ||
| [['https', '://'], LaunchConfiguration('subdomain')], 'ros', 'org'], | ||
| delimiter='.' | ||
| ) | ||
|
|
||
| If the ``subdomain`` launch configuration was set to ``docs`` | ||
| and the ``delimiter`` to ``.``, this would result in a string equal to | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| 'https://docs.ros.org' | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| substitutions: Iterable[SomeSubstitutionsType], | ||
| delimiter: SomeSubstitutionsType = '', | ||
| ) -> None: | ||
| """ | ||
| Create a StringJoinSubstitution. | ||
|
|
||
| :param substitutions: the list of string component substitutions to join | ||
| :param delimiter: the text inbetween two consecutive components (default no text) | ||
| """ | ||
| from ..utilities import normalize_to_list_of_substitutions | ||
|
|
||
| self.__substitutions = [ | ||
| normalize_to_list_of_substitutions(string_component_substitutions) | ||
| for string_component_substitutions in substitutions | ||
| ] | ||
| self.__delimiter = normalize_to_list_of_substitutions(delimiter) | ||
|
|
||
| @property | ||
| def substitutions(self) -> List[List[Substitution]]: | ||
| """Getter for substitutions.""" | ||
| return self.__substitutions | ||
|
|
||
| @property | ||
| def delimiter(self) -> List[Substitution]: | ||
| """Getter for delimiter.""" | ||
| return self.__delimiter | ||
|
|
||
| def __repr__(self) -> Text: | ||
| """Return a description of this substitution as a string.""" | ||
| string_components = [ | ||
| ' + '.join([s.describe() for s in component_substitutions]) | ||
| for component_substitutions in self.substitutions | ||
| ] | ||
| delimiter_component = ' + '.join([d.describe() for d in self.delimiter]) | ||
| return ( | ||
| f'StringJoinSubstitution([' | ||
| f'{", ".join(string_components)}], delimiter={delimiter_component})' | ||
| ) | ||
|
|
||
| def perform(self, context: LaunchContext) -> Text: | ||
| """Perform the substitution by retrieving the local variable.""" | ||
| string_components = [ | ||
| perform_substitutions(context, component_substitutions) | ||
| for component_substitutions in self.substitutions | ||
| ] | ||
| delimiter_component = perform_substitutions(context, self.delimiter) | ||
| return delimiter_component.join(string_components) | ||
71 changes: 71 additions & 0 deletions
71
launch/test/launch/substitutions/test_string_join_substitution.py
This file contains hidden or 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,71 @@ | ||
| # Copyright 2025 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 StringJoinSubstitution substitution class.""" | ||
|
|
||
| from launch import LaunchContext | ||
| from launch.substitutions import StringJoinSubstitution | ||
| from launch.substitutions import TextSubstitution | ||
|
|
||
|
|
||
| def test_string_join(): | ||
| context = LaunchContext() | ||
|
|
||
| strings = ['abc', 'def', 'ghi'] | ||
| sub = StringJoinSubstitution(strings) | ||
| assert sub.perform(context) == ''.join(strings) | ||
|
|
||
| strings = ['abc', ['def'], [TextSubstitution(text='ghi'), 'jkl'], TextSubstitution(text='mno')] | ||
| sub_with_sub = StringJoinSubstitution(strings) | ||
| assert sub_with_sub.perform(context) == 'abcdefghijklmno' | ||
|
|
||
|
|
||
| def test_string_join_with_delimiter(): | ||
| context = LaunchContext() | ||
|
|
||
| strings = ['abc', 'def', 'ghi'] | ||
| sub = StringJoinSubstitution(strings, delimiter='.') | ||
| assert sub.perform(context) == '.'.join(strings) | ||
|
|
||
| strings = ['abc', ['def'], [TextSubstitution(text='ghi'), 'jkl'], TextSubstitution(text='mno')] | ||
| sub_with_sub = StringJoinSubstitution(strings, delimiter='.') | ||
| assert sub_with_sub.perform(context) == 'abc.def.ghijkl.mno' | ||
|
|
||
|
|
||
| def test_string_join_with_substitution_delimiter(): | ||
| context = LaunchContext() | ||
|
|
||
| strings = ['abc', 'def', 'ghi'] | ||
| sub = StringJoinSubstitution(strings, delimiter=['-', '.', '-']) | ||
| assert sub.perform(context) == '-.-'.join(strings) | ||
|
|
||
| strings = ['abc', ['def'], [TextSubstitution(text='ghi'), 'jkl'], TextSubstitution(text='mno')] | ||
| sub_with_sub = StringJoinSubstitution(strings, delimiter=['-', '.', '-']) | ||
| assert sub_with_sub.perform(context) == 'abc-.-def-.-ghijkl-.-mno' | ||
|
|
||
| strings = ['abc', 'def', 'ghi'] | ||
| sub = StringJoinSubstitution(strings, delimiter=TextSubstitution(text='_')) | ||
| assert sub.perform(context) == '_'.join(strings) | ||
|
|
||
| strings = ['abc', ['def'], [TextSubstitution(text='ghi'), 'jkl'], TextSubstitution(text='mno')] | ||
| sub_with_sub = StringJoinSubstitution(strings, delimiter=TextSubstitution(text='_')) | ||
| assert sub_with_sub.perform(context) == 'abc_def_ghijkl_mno' | ||
|
|
||
| strings = ['abc', 'def', 'ghi'] | ||
| sub = StringJoinSubstitution(strings, delimiter=['(^', TextSubstitution(text='_'), '^)']) | ||
| assert sub.perform(context) == '(^_^)'.join(strings) | ||
|
|
||
| strings = ['abc', ['def'], [TextSubstitution(text='ghi'), 'jkl'], TextSubstitution(text='mno')] | ||
| sub_with_sub = StringJoinSubstitution(strings, delimiter=[TextSubstitution(text='(^_'), '^)']) | ||
| assert sub_with_sub.perform(context) == 'abc(^_^)def(^_^)ghijkl(^_^)mno' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.