|
import os |
|
from typing import Optional, Any, Dict |
|
|
|
from dotenv import find_dotenv, load_dotenv |
|
|
|
load_dotenv(find_dotenv()) |
|
|
|
|
|
def get_from_dict_or_env( |
|
data: Dict[str, Any], key: str, env_key: str, default: Optional[str] = None |
|
) -> str: |
|
"""Get a value from a dictionary or an environment variable.""" |
|
if key in data and data[key]: |
|
return data[key] |
|
else: |
|
return get_from_env(env_key, default=default) |
|
|
|
|
|
def get_from_env(env_key: str, default: Optional[str] = None) -> str: |
|
"""Get a value from a dictionary or an environment variable.""" |
|
if env_key in os.environ and os.environ[env_key]: |
|
return os.environ[env_key] |
|
elif default is not None: |
|
return default |
|
else: |
|
raise ValueError( |
|
f"Did not find {env_key}, please add an environment variable" |
|
f" `{env_key}` which contains it, or pass" |
|
f" `{env_key}` as a named parameter." |
|
) |
|
|