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
8 changes: 8 additions & 0 deletions launch/launch/substitutions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
"""Package for substitutions."""

from .anon_name import AnonName
from .boolean_substitution import AllSubstitution
from .boolean_substitution import AndSubstitution
from .boolean_substitution import AnySubstitution
from .boolean_substitution import NotSubstitution
from .boolean_substitution import OrSubstitution
from .command import Command
from .environment_variable import EnvironmentVariable
from .equals_substitution import EqualsSubstitution
from .find_executable import FindExecutable
from .launch_configuration import LaunchConfiguration
from .local_substitution import LocalSubstitution
from .not_equals_substitution import NotEqualsSubstitution
from .path_join_substitution import PathJoinSubstitution
from .python_expression import PythonExpression
from .substitution_failure import SubstitutionFailure
Expand All @@ -31,14 +35,18 @@
from .this_launch_file_dir import ThisLaunchFileDir

__all__ = [
'AllSubstitution',
'AndSubstitution',
'AnySubstitution',
'AnonName',
'Command',
'EqualsSubstitution',
'EnvironmentVariable',
'FindExecutable',
'LaunchConfiguration',
'LocalSubstitution',
'NotSubstitution',
'NotEqualsSubstitution',
'OrSubstitution',
'PathJoinSubstitution',
'PythonExpression',
Expand Down
97 changes: 94 additions & 3 deletions launch/launch/substitutions/boolean_substitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AndSubstitution(Substitution):
"""Substitution that returns 'and' of the input boolean values."""

def __init__(self, left: SomeSubstitutionsType, right: SomeSubstitutionsType) -> None:
"""Create a AndSubstitution substitution."""
"""Create an AndSubstitution substitution."""
super().__init__()

self.__left = normalize_to_list_of_substitutions(left)
Expand Down Expand Up @@ -113,15 +113,15 @@ class OrSubstitution(Substitution):
"""Substitution that returns 'or' of the input boolean values."""

def __init__(self, left: SomeSubstitutionsType, right: SomeSubstitutionsType) -> None:
"""Create a AndSubstitution substitution."""
"""Create an OrSubstitution substitution."""
super().__init__()

self.__left = normalize_to_list_of_substitutions(left)
self.__right = normalize_to_list_of_substitutions(right)

@classmethod
def parse(cls, data: Iterable[SomeSubstitutionsType]):
"""Parse `AndSubstitution` substitution."""
"""Parse `OrSubstitution` substitution."""
if len(data) != 2:
raise TypeError('and substitution expects 2 arguments')
return cls, {'left': data[0], 'right': data[1]}
Expand Down Expand Up @@ -152,3 +152,94 @@ def perform(self, context: LaunchContext) -> Text:
raise SubstitutionFailure(e)

return str(left_condition or right_condition).lower()


@expose_substitution('any')
class AnySubstitution(Substitution):
"""
Substitutes to the string 'true' if at least one of the input arguments evaluates to true.

If none of the arguments evaluate to true, then this substitution returns the string 'false'.
"""

def __init__(self, *args: SomeSubstitutionsType) -> None:
"""
Create an AnySubstitution substitution.

The following string arguments evaluate to true: '1', 'true', 'True', 'on'
"""
super().__init__()

self.__args = [normalize_to_list_of_substitutions(arg) for arg in args]

@classmethod
def parse(cls, data: Iterable[SomeSubstitutionsType]):
"""Parse `AnySubstitution` substitution."""
return cls, {'args': data}

@property
def args(self) -> Substitution:
"""Getter for args."""
return self.__args

def describe(self) -> Text:
"""Return a description of this substitution as a string."""
return f'AnySubstitution({" ".join(self.args)})'

def perform(self, context: LaunchContext) -> Text:
"""Perform the substitution."""
substituted_conditions = []
for arg in self.args:
try:
arg_condition = perform_typed_substitution(context, arg, bool)
substituted_conditions.append(arg_condition)
except (TypeError, ValueError) as e:
raise SubstitutionFailure(e)

return str(any(substituted_conditions)).lower()


@expose_substitution('all')
class AllSubstitution(Substitution):
"""
Substitutes to the string 'true' if all of the input arguments evaluate to true.

If any of the arguments evaluates to false, then this substitution returns the string 'false'.
"""

def __init__(self, *args: SomeSubstitutionsType) -> None:
"""
Create an AllSubstitution substitution.

The following string arguments evaluate to true: '1', 'true', 'True', 'on'
The following string arguments evaluate to false: '0', 'false', 'False', 'off'
"""
super().__init__()

self.__args = [normalize_to_list_of_substitutions(arg) for arg in args]

@classmethod
def parse(cls, data: Iterable[SomeSubstitutionsType]):
"""Parse `AllSubstitution` substitution."""
return cls, {'args': data}

@property
def args(self) -> Substitution:
"""Getter for args."""
return self.__args

def describe(self) -> Text:
"""Return a description of this substitution as a string."""
return f'AllSubstitution({" ".join(self.args)})'

def perform(self, context: LaunchContext) -> Text:
"""Perform the substitution."""
substituted_conditions = []
for arg in self.args:
try:
arg_condition = perform_typed_substitution(context, arg, bool)
substituted_conditions.append(arg_condition)
except (TypeError, ValueError) as e:
raise SubstitutionFailure(e)

return str(all(substituted_conditions)).lower()
119 changes: 119 additions & 0 deletions launch/launch/substitutions/equals_substitution.py
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()
50 changes: 50 additions & 0 deletions launch/launch/substitutions/not_equals_substitution.py
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()
Loading