Source code for yt_framework.utils.env
"""Load `KEY=value` files such as `configs/secrets.env` into plain dicts."""
import warnings
from pathlib import Path
from typing import Dict
[docs]
def load_env_file(env_path: Path) -> Dict[str, str]:
"""
Load environment variables from a .env file.
File format: KEY=VALUE (one per line, # for comments)
Missing file is optional and returns empty dict.
Args:
env_path: Path to the .env file
Returns:
Dictionary of loaded environment variables (key -> value).
Returns empty dict if file doesn't exist or cannot be read.
Warns:
UserWarning: If the file exists but cannot be read or parsed (non-fatal).
Missing file is silent (returns empty dict).
Example:
>>> env_vars = load_env_file(Path("configs/secrets.env"))
>>> print(env_vars.get("YT_TOKEN"))
"""
env_vars = {}
# Skip if file doesn't exist (optional file — no warning; callers treat as empty)
if not env_path.exists():
return env_vars
try:
with open(env_path, "r") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
env_vars[key] = value
except Exception as e:
warnings.warn(f"Could not load {env_path}: {e}", UserWarning)
return env_vars
[docs]
def load_secrets(secrets_dir: Path, env_file: str = "secrets.env") -> Dict[str, str]:
"""
Load secrets from secrets.env file in the specified directory.
Args:
secrets_dir: Directory containing the secrets.env file
env_file: Name of the environment file (default: "secrets.env")
Returns:
Dictionary of loaded secrets (key -> value).
Returns empty dict if file doesn't exist or cannot be read.
Warns:
UserWarning: If the secrets file exists but cannot be read or parsed (non-fatal).
Example:
>>> secrets = load_secrets(Path("configs"))
>>> yt_token = secrets.get("YT_TOKEN")
"""
secrets_path = secrets_dir / env_file
return load_env_file(secrets_path)