Environment Variable Context Manager
Project description
tempenv
Manage environment variables in a temporary scope.
Some products use environment variables as a primary means to supply credentials. To ensure the lifetime of exposed credentials is short, wrap them in a TemporaryEnvironment so that they are automatically destroyed on scope exit.
You can:
- Set or unset environment variables inside a
with
code block, - Get a warning if the code block modifies one of the environment variables,
- Optionally bypass restoration of the original environment variable value if the code block modifies the environment variable.
Install
Install the latest version of tempenv:
pip install tempenv
Examples
Each of these examples can be found in the tests.
Set some environment variables temporarily: (see tests/example_set_test.py):
def test_set(self):
user_before = os.environ.get("USER")
with TemporaryEnvironment({"USER": "nobody", "OTHER": "foo"}):
assert os.environ.get("USER") == "nobody"
assert os.environ.get("OTHER") == "foo"
assert os.environ.get("USER") == user_before
Changing the value to None
will unset the environment variable during
the code block
(see tests/example_unset_test.py):
def test_unset(self):
os.environ["DEBUG"] = "1"
with TemporaryEnvironment({"DEBUG": None}):
assert "DEBUG" not in os.environ
assert "DEBUG" in os.environ
Changing a temporary environment variable during the scope will cause a warning (see tests/example_overwrite_test.py):
def test_overwritten_in_context(self):
with self.assertWarnsRegex(EnvironmentVariableChangedWarning, "FOO"):
with TemporaryEnvironment({"FOO": "BAR"}):
os.environ["FOO"] = "SAM"
If you set the optional argument restore_if_changed=False
then a change
during the scope of the TemporaryEnvironment will not issue a warning and will
not restore to the original value
(see tests/example_ignore_test.py):
def test_ignored_overwrite_in_context(self):
os.environ["FOO"] = "BAR"
with TemporaryEnvironment({"FOO": "SAM"}, restore_if_changed=False):
os.environ["FOO"] = "DEAN"
assert os.environ["FOO"] == "DEAN"
You can use TemporaryEnvironment in a unittest scope as follows (see tests/example_unittest_test.py):
@TemporaryEnvironment({"USER": "Crowley"})
def test_check(self):
assert os.environ.get("USER") == "Crowley"
License
Released under the Apache Software License, Version 2.0 (see LICENSE
):
Copyright (C) 2019 - 2022 James E. King III (@jeking3) <jking@apache.org>
Bugs
Please report any bugs that you find on GitHub.
Or, even better, fork the repository on GitHub
and create a pull request (PR). We welcome all changes, big or small, and we
will help you make the PR if you are new to git
(just ask on the issue and/or
see CONTRIBUTING.rst
).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tempenv-2.0.0.tar.gz
.
File metadata
- Download URL: tempenv-2.0.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdd29b419d701b177e358052d4ab362b66a39fe030ca0a30cf720ea8e4f35d54 |
|
MD5 | e2a629c4d796135f5d271b06408de678 |
|
BLAKE2b-256 | e78cf7a6b5402f8ec07b0d3a1d2c321d01fab372a633f11b42b5b98a180be403 |
File details
Details for the file tempenv-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: tempenv-2.0.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 942766e7c1decd898c81c8a4138d236df6685df42b13e739012d62d92eef35dc |
|
MD5 | 449cb022750587531f41563cc35721bc |
|
BLAKE2b-256 | 2c4b4fe6de3ff063f5b86ebbd0e7f0d684cbc3962a16e3d7fab8acf8674ba4d5 |