Skip to main content

Environment Variable Context Manager

Project description

tempenv

pypi Build Status codecov Code Style

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

tempenv-2.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tempenv-2.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file tempenv-2.1.0.tar.gz.

File metadata

  • Download URL: tempenv-2.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for tempenv-2.1.0.tar.gz
Algorithm Hash digest
SHA256 45439419d9ff63a338af323f01d69fa20627bab94f37605ef7f47b7bc7f17b96
MD5 aeaaa613ca8ad32db5a7363b20726f49
BLAKE2b-256 7aafac55e665dafa99311d1397b1a28c0ce289c987e3afbbd24b7a18a3084bbc

See more details on using hashes here.

File details

Details for the file tempenv-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: tempenv-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for tempenv-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb19bec4f516e551cd0ce33b1d69993261986cfd503d09a4f2201560f9c31a4e
MD5 a06a3fc442aebc1d32e049f033ca26be
BLAKE2b-256 d08141ae0ee580b5436f9e9c39c537aed7cf37fc47f5fd07fe574af254235259

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page