|
| 1 | +# Copyright 2021 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 AppendEnvironmentVariable action.""" |
| 16 | + |
| 17 | +import os |
| 18 | +from typing import List |
| 19 | +from typing import Union |
| 20 | + |
| 21 | +from ..action import Action |
| 22 | +from ..frontend import Entity |
| 23 | +from ..frontend import expose_action |
| 24 | +from ..frontend import Parser |
| 25 | +from ..launch_context import LaunchContext |
| 26 | +from ..some_substitutions_type import SomeSubstitutionsType |
| 27 | +from ..substitution import Substitution |
| 28 | +from ..utilities import normalize_to_list_of_substitutions |
| 29 | +from ..utilities import perform_substitutions |
| 30 | +from ..utilities.type_utils import normalize_typed_substitution |
| 31 | +from ..utilities.type_utils import NormalizedValueType |
| 32 | +from ..utilities.type_utils import perform_typed_substitution |
| 33 | + |
| 34 | + |
| 35 | +@expose_action('append_env') |
| 36 | +class AppendEnvironmentVariable(Action): |
| 37 | + """ |
| 38 | + Action that appends to an environment variable if it exists and sets it if it does not. |
| 39 | +
|
| 40 | + It can optionally prepend instead of appending. |
| 41 | + It can also optionally use a custom separator, with the default being a platform-specific |
| 42 | + separator, `os.pathsep`. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + name: SomeSubstitutionsType, |
| 48 | + value: SomeSubstitutionsType, |
| 49 | + prepend: Union[bool, SomeSubstitutionsType] = False, |
| 50 | + separator: SomeSubstitutionsType = os.pathsep, |
| 51 | + **kwargs, |
| 52 | + ) -> None: |
| 53 | + """ |
| 54 | + Create an AppendEnvironmentVariable action. |
| 55 | +
|
| 56 | + All parameters can be provided as substitutions. |
| 57 | + A substitution for the prepend parameter will be coerced to `bool` following YAML rules. |
| 58 | +
|
| 59 | + :param name: the name of the environment variable |
| 60 | + :param value: the value to set or append |
| 61 | + :param prepend: whether the value should be prepended instead |
| 62 | + :param separator: the separator to use, defaulting to a platform-specific separator |
| 63 | + """ |
| 64 | + super().__init__(**kwargs) |
| 65 | + self.__name = normalize_to_list_of_substitutions(name) |
| 66 | + self.__value = normalize_to_list_of_substitutions(value) |
| 67 | + self.__prepend = normalize_typed_substitution(prepend, bool) |
| 68 | + self.__separator = normalize_to_list_of_substitutions(separator) |
| 69 | + |
| 70 | + @classmethod |
| 71 | + def parse( |
| 72 | + cls, |
| 73 | + entity: Entity, |
| 74 | + parser: Parser, |
| 75 | + ): |
| 76 | + """Parse an 'append_env' entity.""" |
| 77 | + _, kwargs = super().parse(entity, parser) |
| 78 | + kwargs['name'] = parser.parse_substitution(entity.get_attr('name')) |
| 79 | + kwargs['value'] = parser.parse_substitution(entity.get_attr('value')) |
| 80 | + prepend = entity.get_attr('prepend', optional=True, data_type=bool, can_be_str=True) |
| 81 | + if prepend is not None: |
| 82 | + kwargs['prepend'] = parser.parse_if_substitutions(prepend) |
| 83 | + separator = entity.get_attr('separator', optional=True) |
| 84 | + if separator is not None: |
| 85 | + kwargs['separator'] = parser.parse_substitution(separator) |
| 86 | + return cls, kwargs |
| 87 | + |
| 88 | + @property |
| 89 | + def name(self) -> List[Substitution]: |
| 90 | + """Getter for the name of the environment variable to be set or appended to.""" |
| 91 | + return self.__name |
| 92 | + |
| 93 | + @property |
| 94 | + def value(self) -> List[Substitution]: |
| 95 | + """Getter for the value of the environment variable to be set or appended.""" |
| 96 | + return self.__value |
| 97 | + |
| 98 | + @property |
| 99 | + def prepend(self) -> NormalizedValueType: |
| 100 | + """Getter for the prepend flag.""" |
| 101 | + return self.__prepend |
| 102 | + |
| 103 | + @property |
| 104 | + def separator(self) -> List[Substitution]: |
| 105 | + """Getter for the separator.""" |
| 106 | + return self.__separator |
| 107 | + |
| 108 | + def execute(self, context: LaunchContext) -> None: |
| 109 | + """Execute the action.""" |
| 110 | + name = perform_substitutions(context, self.name) |
| 111 | + value = perform_substitutions(context, self.value) |
| 112 | + prepend = perform_typed_substitution(context, self.prepend, bool) |
| 113 | + separator = perform_substitutions(context, self.separator) |
| 114 | + if name in os.environ: |
| 115 | + os.environ[name] = \ |
| 116 | + os.environ[name] + separator + value \ |
| 117 | + if not prepend \ |
| 118 | + else value + separator + os.environ[name] |
| 119 | + else: |
| 120 | + os.environ[name] = value |
| 121 | + return None |
0 commit comments