env_var.py 748 B

123456789101112131415161718192021222324252627282930
  1. from __future__ import annotations
  2. from contextlib import suppress
  3. from .convert import convert
  4. def get_env_var(key, as_type, env):
  5. """
  6. Get the environment variable option.
  7. :param key: the config key requested
  8. :param as_type: the type we would like to convert it to
  9. :param env: environment variables to use
  10. :return:
  11. """
  12. environ_key = f"VIRTUALENV_{key.upper()}"
  13. if env.get(environ_key):
  14. value = env[environ_key]
  15. with suppress(Exception): # note the converter already logs a warning when failures happen
  16. source = f"env var {environ_key}"
  17. as_type = convert(value, as_type, source)
  18. return as_type, source
  19. return None
  20. __all__ = [
  21. "get_env_var",
  22. ]