bedrock.helpers.environment

 1import os
 2
 3
 4def env(variable: str, backup: str = None) -> str:  # pragma: unit
 5    """
 6    Returns the value of the specified environment variable, or the backup if the environment variable is not defined.
 7    :param variable: The environment variable to get
 8    :param backup: Value to return if the environment variable is not defined
 9    """
10    try:
11        return os.environ[variable]
12    except Exception:
13        if backup is None:
14            raise Exception(f"No backup defined for {variable}")
15        return backup
16
17
18def get_all_by_prefix(prefix: str = "") -> dict[str, str]:  # pragma: unit
19    """
20    Returns a dictionary of all environment variables starting with the given prefix
21    """
22    return {k: v for k, v in os.environ.items() if k.startswith(prefix)}
def env(variable: str, backup: str = None) -> str:
 5def env(variable: str, backup: str = None) -> str:  # pragma: unit
 6    """
 7    Returns the value of the specified environment variable, or the backup if the environment variable is not defined.
 8    :param variable: The environment variable to get
 9    :param backup: Value to return if the environment variable is not defined
10    """
11    try:
12        return os.environ[variable]
13    except Exception:
14        if backup is None:
15            raise Exception(f"No backup defined for {variable}")
16        return backup

Returns the value of the specified environment variable, or the backup if the environment variable is not defined.

Parameters
  • variable: The environment variable to get
  • backup: Value to return if the environment variable is not defined
def get_all_by_prefix(prefix: str = '') -> dict[str, str]:
19def get_all_by_prefix(prefix: str = "") -> dict[str, str]:  # pragma: unit
20    """
21    Returns a dictionary of all environment variables starting with the given prefix
22    """
23    return {k: v for k, v in os.environ.items() if k.startswith(prefix)}

Returns a dictionary of all environment variables starting with the given prefix