- Added python-dotenv to requirements.txt - Config now automatically loads .env file if present - Allows local development without manually exporting env vars - Gracefully falls back if python-dotenv not installed 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
"""
|
|
Centralised configuration helpers for the Python search toolkit.
|
|
|
|
Environment Variables:
|
|
ELASTIC_URL: Base URL to the Elasticsearch node (default: http://localhost:9200).
|
|
ELASTIC_USERNAME / ELASTIC_PASSWORD: Optional basic auth credentials.
|
|
ELASTIC_INDEX: Target index name (default: this_little_corner_py).
|
|
LOCAL_DATA_DIR: Root folder containing JSON metadata (default: ../data/video_metadata).
|
|
YOUTUBE_API_KEY: Optional API key for pulling metadata directly from YouTube.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
# Load .env file if it exists
|
|
try:
|
|
from dotenv import load_dotenv
|
|
_env_path = Path(__file__).parent / ".env"
|
|
if _env_path.exists():
|
|
load_dotenv(_env_path)
|
|
except ImportError:
|
|
pass # python-dotenv not installed
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ElasticSettings:
|
|
url: str
|
|
username: Optional[str]
|
|
password: Optional[str]
|
|
index: str
|
|
ca_cert: Optional[Path]
|
|
verify_certs: bool
|
|
api_key: Optional[str]
|
|
debug: bool
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DataSettings:
|
|
root: Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class YoutubeSettings:
|
|
api_key: Optional[str]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AppConfig:
|
|
elastic: ElasticSettings
|
|
data: DataSettings
|
|
youtube: YoutubeSettings
|
|
|
|
|
|
def _env(name: str, default: Optional[str] = None) -> Optional[str]:
|
|
"""Return an environment variable value with optional default."""
|
|
value = os.environ.get(name)
|
|
if value is None:
|
|
return default
|
|
stripped = value.strip()
|
|
return stripped or default
|
|
|
|
|
|
def load_config() -> AppConfig:
|
|
"""Collect configuration from environment variables."""
|
|
elastic = ElasticSettings(
|
|
url=_env("ELASTIC_URL", "http://localhost:9200"),
|
|
username=_env("ELASTIC_USERNAME"),
|
|
password=_env("ELASTIC_PASSWORD"),
|
|
index=_env("ELASTIC_INDEX", "this_little_corner_py"),
|
|
ca_cert=Path(_env("ELASTIC_CA_CERT")).expanduser() if _env("ELASTIC_CA_CERT") else None,
|
|
verify_certs=_env("ELASTIC_VERIFY_CERTS", "1") not in {"0", "false", "False"},
|
|
api_key=_env("ELASTIC_API_KEY"),
|
|
debug=_env("ELASTIC_DEBUG", "0") in {"1", "true", "True"},
|
|
)
|
|
data_root = Path(
|
|
_env(
|
|
"LOCAL_DATA_DIR",
|
|
Path(__file__).resolve().parents[1] / "data" / "video_metadata",
|
|
)
|
|
)
|
|
data = DataSettings(root=data_root)
|
|
youtube = YoutubeSettings(api_key=_env("YOUTUBE_API_KEY"))
|
|
return AppConfig(elastic=elastic, data=data, youtube=youtube)
|
|
|
|
|
|
CONFIG = load_config()
|