drift-quant / src /drift_quant /config.py
deveshc23
fix: Add Python 3.10 StrEnum backward compatibility and set python_version 3.11 in README
b277794
Raw History Blame Contribute Delete
2.13 kB
"""Validated, secret-free runtime configuration."""
from __future__ import annotations
import os
from collections.abc import Mapping
from dataclasses import dataclass
try:
from enum import StrEnum
except ImportError:
from enum import Enum
class StrEnum(str, Enum): # noqa: UP042
def __str__(self) -> str:
return str(self.value)
class ConfigurationError(ValueError):
"""Raised when non-secret runtime configuration is unsafe or invalid."""
class Environment(StrEnum):
DEVELOPMENT = "development"
RESEARCH = "research"
PAPER = "paper"
PRODUCTION = "production"
class LogLevel(StrEnum):
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
@dataclass(frozen=True, slots=True)
class Settings:
"""Non-secret process configuration.
Credentials are intentionally absent. Provider adapters receive secrets from a
dedicated future secret-provider boundary, not from this general settings object.
"""
environment: Environment
log_level: LogLevel
@property
def is_production(self) -> bool:
return self.environment is Environment.PRODUCTION
def load_settings(environ: Mapping[str, str] | None = None) -> Settings:
"""Load and validate supported non-secret environment variables."""
source = os.environ if environ is None else environ
environment_value = source.get("DRIFT_ENVIRONMENT", Environment.DEVELOPMENT.value).lower()
log_level_value = source.get("DRIFT_LOG_LEVEL", LogLevel.INFO.value).upper()
try:
environment = Environment(environment_value)
except ValueError as exc:
valid_values = ", ".join(member.value for member in Environment)
raise ConfigurationError(f"DRIFT_ENVIRONMENT must be one of: {valid_values}.") from exc
try:
log_level = LogLevel(log_level_value)
except ValueError as exc:
valid_values = ", ".join(member.value for member in LogLevel)
raise ConfigurationError(f"DRIFT_LOG_LEVEL must be one of: {valid_values}.") from exc
return Settings(environment=environment, log_level=log_level)