-
Couldn't load subscription status.
- Fork 158
Capture the environment variables in TimerAction #728
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
Open
sjdalessandro
wants to merge
3
commits into
ros2:rolling
Choose a base branch
from
sjdalessandro:dalessandro/capture_envs_in_timer_action
base: rolling
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2023 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 ReplaceEnvironmentVariables action.""" | ||
|
|
||
| from typing import List | ||
| from typing import Mapping | ||
| from typing import Text | ||
|
|
||
| from ..action import Action | ||
| from ..frontend import Entity | ||
| from ..frontend import expose_action | ||
| from ..frontend import Parser | ||
| from ..launch_context import LaunchContext | ||
| from ..utilities import normalize_to_list_of_substitutions | ||
| from ..utilities import perform_substitutions | ||
|
|
||
|
|
||
| @expose_action('rep_env') | ||
| class ReplaceEnvironmentVariables(Action): | ||
| """ | ||
| Action that replaces the environment variables in the current context. | ||
|
|
||
| The previous state can be saved by pushing the stack with the | ||
| :py:class:`launch.actions.PushEnvironment` action. | ||
| And can be restored by popping the stack with the | ||
| :py:class:`launch.actions.PopEnvironment` action. | ||
| """ | ||
|
|
||
| def __init__(self, environment: Mapping[Text, Text] = {}, **kwargs) -> None: | ||
| """Create a ReplaceEnvironmentVariables action.""" | ||
| super().__init__(**kwargs) | ||
| self.__environment = environment | ||
|
|
||
| @classmethod | ||
| def parse( | ||
| cls, | ||
| entity: Entity, | ||
| parser: Parser | ||
| ): | ||
| """Return the `ReplaceEnvironmentVariables` action and kwargs for constructing it.""" | ||
| _, kwargs = super().parse(entity, parser) | ||
| env = entity.get_attr('env', data_type=List[Entity], optional=True) | ||
|
|
||
| if env is not None: | ||
| kwargs['environment'] = { | ||
| tuple(parser.parse_substitution(e.get_attr('name'))): | ||
| parser.parse_substitution(e.get_attr('value')) for e in env | ||
| } | ||
| for e in env: | ||
| e.assert_entity_completely_parsed() | ||
| return cls, kwargs | ||
|
|
||
| @property | ||
| def environment(self): | ||
| """Getter for environment.""" | ||
| return self.__environment | ||
|
|
||
| def execute(self, context: LaunchContext): | ||
| """Execute the action.""" | ||
| evaluated_environment = {} | ||
| for k, v in self.__environment.items(): | ||
| evaluated_k = perform_substitutions(context, normalize_to_list_of_substitutions(k)) | ||
| evaluated_v = perform_substitutions(context, normalize_to_list_of_substitutions(v)) | ||
| evaluated_environment[evaluated_k] = evaluated_v | ||
|
|
||
| context._replace_environment(evaluated_environment) | ||
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
93 changes: 93 additions & 0 deletions
93
launch/test/launch/actions/test_replace_environment_variables.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,93 @@ | ||
| # Copyright 2023 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 ReplaceEnvironmentVariables action classes.""" | ||
|
|
||
| import os | ||
|
|
||
| from launch import LaunchContext | ||
| from launch.actions import PopEnvironment | ||
| from launch.actions import PushEnvironment | ||
| from launch.actions import ReplaceEnvironmentVariables | ||
|
|
||
| from temporary_environment import sandbox_environment_variables | ||
|
|
||
|
|
||
| @sandbox_environment_variables | ||
| def test_replace_environment_constructors(): | ||
| """Test the constructors for ReplaceEnvironmentVariables class.""" | ||
| ReplaceEnvironmentVariables({}) | ||
| ReplaceEnvironmentVariables({'foo': 'bar', 'spam': 'eggs'}) | ||
|
|
||
|
|
||
| @sandbox_environment_variables | ||
| def test_replace_environment_execute(): | ||
| """Test the execute() of the ReplaceEnvironmentVariables class.""" | ||
| assert isinstance(os.environ, os._Environ) | ||
|
|
||
| # replaces empty state | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| assert len(context.environment) == 0 | ||
| ReplaceEnvironmentVariables({'foo': 'bar', 'spam': 'eggs'}).visit(context) | ||
| assert len(context.environment) == 2 | ||
| assert 'foo' in context.environment | ||
| assert context.environment['foo'] == 'bar' | ||
| assert 'spam' in context.environment | ||
| assert context.environment['spam'] == 'eggs' | ||
|
|
||
| # replaces non empty state | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| assert len(context.environment) == 0 | ||
| context.environment['quux'] = 'quuux' | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' | ||
| ReplaceEnvironmentVariables({'foo': 'bar', 'spam': 'eggs'}).visit(context) | ||
| assert len(context.environment) == 2 | ||
| assert 'foo' in context.environment | ||
| assert context.environment['foo'] == 'bar' | ||
| assert 'spam' in context.environment | ||
| assert context.environment['spam'] == 'eggs' | ||
|
|
||
| # replaces existing environment variable | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| assert len(context.environment) == 0 | ||
| context.environment['quux'] = 'quuux' | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' | ||
| ReplaceEnvironmentVariables({'quux': 'eggs'}).visit(context) | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'eggs' | ||
|
|
||
| # Replacing the environment should not change the type of os.environ | ||
| assert isinstance(os.environ, os._Environ) | ||
|
|
||
| # does not interfere with PopEnvironment and PushEnvironment action classes | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| context.environment['quux'] = 'quuux' | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' | ||
| PushEnvironment().visit(context) | ||
| ReplaceEnvironmentVariables({'foo': 'bar', 'spam': 'eggs'}).visit(context) | ||
| PopEnvironment().visit(context) | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' |
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.