|
| 1 | +# Copyright 2023 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Module for the IfElseSubstitution substitution.""" |
| 16 | + |
| 17 | +from typing import List |
| 18 | +from typing import Sequence |
| 19 | +from typing import Text |
| 20 | + |
| 21 | +from .substitution_failure import SubstitutionFailure |
| 22 | +from ..frontend import expose_substitution |
| 23 | +from ..launch_context import LaunchContext |
| 24 | +from ..some_substitutions_type import SomeSubstitutionsType |
| 25 | +from ..substitution import Substitution |
| 26 | +from ..utilities import normalize_to_list_of_substitutions |
| 27 | +from ..utilities import perform_substitutions |
| 28 | +from ..utilities.type_utils import perform_typed_substitution |
| 29 | + |
| 30 | + |
| 31 | +@expose_substitution('if') |
| 32 | +class IfElseSubstitution(Substitution): |
| 33 | + """ |
| 34 | + Substitution that conditionally returns one of two substitutions. |
| 35 | +
|
| 36 | + Depending on whether the condition substitution evaluates to true, either it returns |
| 37 | + the if_value substitution or the else_value substitution. |
| 38 | +
|
| 39 | + Example with a boolean launch configuration: |
| 40 | +
|
| 41 | + .. doctest:: |
| 42 | +
|
| 43 | + >>> from launch.substitutions import LaunchConfiguration |
| 44 | + >>> subst = IfElseSubstitution( |
| 45 | + ... LaunchConfiguration("arg"), |
| 46 | + ... if_value="arg_evaluated_to_true", |
| 47 | + ... else_value="arg_evaluated_to_false") |
| 48 | +
|
| 49 | + Combine with boolean substitutions to create more complex conditions. |
| 50 | + Example with multiple boolean launch configurations: |
| 51 | +
|
| 52 | + .. doctest:: |
| 53 | +
|
| 54 | + >>> from launch.substitutions import AllSubstitution |
| 55 | + >>> from launch.substitutions import EqualsSubstitution |
| 56 | + >>> from launch.substitutions import LaunchConfiguration |
| 57 | + >>> from launch.substitutions import NotSubstitution |
| 58 | + >>> subst = IfElseSubstitution( |
| 59 | + ... AllSubstitution(EqualsSubstitution(LaunchConfiguration("arg1"), |
| 60 | + ... LaunchConfiguration("arg2")), |
| 61 | + ... NotSubstitution(LaunchConfiguration("arg3"))), |
| 62 | + ... if_value="all_args_evaluated_to_true", |
| 63 | + ... else_value="at_least_one_arg_evaluated_to_false") |
| 64 | +
|
| 65 | + """ |
| 66 | + |
| 67 | + def __init__(self, condition: SomeSubstitutionsType, |
| 68 | + if_value: SomeSubstitutionsType = '', |
| 69 | + else_value: SomeSubstitutionsType = '') -> None: |
| 70 | + """Create a IfElseSubstitution substitution.""" |
| 71 | + super().__init__() |
| 72 | + if if_value == else_value == '': |
| 73 | + raise RuntimeError('One of if_value and else_value must be specified') |
| 74 | + self._condition = normalize_to_list_of_substitutions(condition) |
| 75 | + self._if_value = normalize_to_list_of_substitutions(if_value) |
| 76 | + self._else_value = normalize_to_list_of_substitutions(else_value) |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def parse(cls, data: Sequence[SomeSubstitutionsType]): |
| 80 | + """Parse `IfElseSubstitution` substitution.""" |
| 81 | + if len(data) < 2 or len(data) > 3: |
| 82 | + raise TypeError('if substitution expects from 2 or 3 arguments') |
| 83 | + kwargs = {'condition': data[0], 'if_value': data[1]} |
| 84 | + if len(data) == 3: |
| 85 | + kwargs['else_value'] = data[2] |
| 86 | + return cls, kwargs |
| 87 | + |
| 88 | + @property |
| 89 | + def condition(self) -> List[Substitution]: |
| 90 | + """Getter for condition.""" |
| 91 | + return self._condition |
| 92 | + |
| 93 | + @property |
| 94 | + def if_value(self) -> List[Substitution]: |
| 95 | + """Getter for if value.""" |
| 96 | + return self._if_value |
| 97 | + |
| 98 | + @property |
| 99 | + def else_value(self) -> List[Substitution]: |
| 100 | + """Getter for else value.""" |
| 101 | + return self._else_value |
| 102 | + |
| 103 | + def describe(self) -> Text: |
| 104 | + """Return a description of this substitution as a string.""" |
| 105 | + return f'IfElseSubstitution({self.condition}, {self.if_value}, {self.else_value})' |
| 106 | + |
| 107 | + def perform(self, context: LaunchContext) -> Text: |
| 108 | + """Perform the substitution by evaluating the condition.""" |
| 109 | + try: |
| 110 | + condition = perform_typed_substitution(context, self.condition, bool) |
| 111 | + except (TypeError, ValueError) as e: |
| 112 | + raise SubstitutionFailure(e) |
| 113 | + |
| 114 | + if condition: |
| 115 | + return perform_substitutions(context, self.if_value) |
| 116 | + else: |
| 117 | + return perform_substitutions(context, self.else_value) |
0 commit comments