-
Notifications
You must be signed in to change notification settings - Fork 170
[backport humble] Implement Any, All, Equals, and NotEquals substitutions (#649) #871
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 3 commits into
ros2:humble
from
emersonknapp:backport-649-humble
May 18, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,119 @@ | ||
| # Copyright 2022 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 EqualsSubstitution substitution.""" | ||
|
|
||
| import math | ||
|
|
||
| from typing import Any | ||
| from typing import Iterable | ||
| from typing import Optional | ||
| from typing import Text | ||
| from typing import Union | ||
|
|
||
| from ..frontend import expose_substitution | ||
| from ..launch_context import LaunchContext | ||
| from ..some_substitutions_type import SomeSubstitutionsType | ||
| from ..substitution import Substitution | ||
| from ..utilities import normalize_to_list_of_substitutions | ||
| from ..utilities.type_utils import is_substitution, perform_substitutions | ||
|
|
||
|
|
||
| def _str_is_bool(input_str: Text) -> bool: | ||
| """Check if string input is convertible to a boolean.""" | ||
| if not isinstance(input_str, Text): | ||
| return False | ||
| else: | ||
| return input_str.lower() in ('true', 'false', '1', '0') | ||
|
|
||
|
|
||
| def _str_is_float(input_str: Text) -> bool: | ||
| """Check if string input is convertible to a float.""" | ||
| try: | ||
| float(input_str) | ||
| return True | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| @expose_substitution('equals') | ||
| class EqualsSubstitution(Substitution): | ||
| """ | ||
| Substitution that checks if two inputs are equal. | ||
|
|
||
| Returns 'true' or 'false' strings depending on the result. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| left: Optional[Union[Any, Iterable[Any]]], | ||
| right: Optional[Union[Any, Iterable[Any]]] | ||
| ) -> None: | ||
| """Create an EqualsSubstitution substitution.""" | ||
| super().__init__() | ||
|
|
||
| if not is_substitution(left): | ||
| if left is None: | ||
| left = '' | ||
| elif isinstance(left, bool): | ||
| left = str(left).lower() | ||
| else: | ||
| left = str(left) | ||
|
|
||
| if not is_substitution(right): | ||
| if right is None: | ||
| right = '' | ||
| elif isinstance(right, bool): | ||
| right = str(right).lower() | ||
| else: | ||
| right = str(right) | ||
|
|
||
| self.__left = normalize_to_list_of_substitutions(left) | ||
| self.__right = normalize_to_list_of_substitutions(right) | ||
|
|
||
| @classmethod | ||
| def parse(cls, data: Iterable[SomeSubstitutionsType]): | ||
| """Parse `EqualsSubstitution` substitution.""" | ||
| if len(data) != 2: | ||
| raise TypeError('and substitution expects 2 arguments') | ||
| return cls, {'left': data[0], 'right': data[1]} | ||
|
|
||
| @property | ||
| def left(self) -> Substitution: | ||
| """Getter for left.""" | ||
| return self.__left | ||
|
|
||
| @property | ||
| def right(self) -> Substitution: | ||
| """Getter for right.""" | ||
| return self.__right | ||
|
|
||
| def describe(self) -> Text: | ||
| """Return a description of this substitution as a string.""" | ||
| return f'EqualsSubstitution({self.left} {self.right})' | ||
|
|
||
| def perform(self, context: LaunchContext) -> Text: | ||
| """Perform the substitution.""" | ||
| left = perform_substitutions(context, self.left) | ||
| right = perform_substitutions(context, self.right) | ||
|
|
||
| # Special case for booleans | ||
| if _str_is_bool(left) and _str_is_bool(right): | ||
| return str((left.lower() in ('true', '1')) == (right.lower() in ('true', '1'))).lower() | ||
|
|
||
| # Special case for floats (epsilon closeness) | ||
| if _str_is_float(left) and _str_is_float(right): | ||
| return str(math.isclose(float(left), float(right))).lower() | ||
|
|
||
| return str(left == right).lower() |
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,50 @@ | ||
| # Copyright 2022 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 NotEqualsSubstitution substitution.""" | ||
|
|
||
| from typing import Any | ||
| from typing import Iterable | ||
| from typing import Optional | ||
| from typing import Text | ||
| from typing import Union | ||
|
|
||
| from .equals_substitution import EqualsSubstitution | ||
| from ..frontend import expose_substitution | ||
| from ..launch_context import LaunchContext | ||
|
|
||
|
|
||
| @expose_substitution('not-equals') | ||
| class NotEqualsSubstitution(EqualsSubstitution): | ||
| """ | ||
| Substitution that checks if two inputs are not equal. | ||
|
|
||
| Returns 'true' or 'false' strings depending on the result. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| left: Optional[Union[Any, Iterable[Any]]], | ||
| right: Optional[Union[Any, Iterable[Any]]] | ||
| ) -> None: | ||
| """Create a NotEqualsSubstitution substitution.""" | ||
| super().__init__(left, right) | ||
|
|
||
| def describe(self) -> Text: | ||
| """Return a description of this substitution as a string.""" | ||
| return f'NotEqualsSubstitution({self.left} {self.right})' | ||
|
|
||
| def perform(self, context: LaunchContext) -> Text: | ||
| """Perform the substitution.""" | ||
| return str(not (super().perform(context) == 'true')).lower() |
Oops, something went wrong.
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.