-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
41 lines (32 loc) · 1.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""A module containing utility functions and helper methods."""
import os
from logutils import get_logger
logger = get_logger(__name__)
def get_env_var(env_name: str, default_value: str = None, strict: bool = False) -> str:
"""
Retrieves the value of an environment variable.
Args:
env_name (str): The name of the environment variable to retrieve.
default_value (str, optional): The value to return if the variable is not found
and strict is False. Defaults to None.
strict (bool, optional): If True, raises an error if the variable is not found.
Defaults to False.
Returns:
str: The value of the environment variable, or default_value if not found and
strict is False.
"""
try:
value = (
os.environ[env_name]
if strict
else os.environ.get(env_name) or default_value
)
if strict and (value is None or value.strip() == ""):
raise ValueError(f"Environment variable {env_name} is missing or empty.")
return value
except KeyError as error:
logger.error("Environment variable '%s' not found: %s", env_name, error)
raise
except ValueError as error:
logger.error("Environment variable '%s' is empty or None: %s", env_name, error)
raise