Skip to content

settings

settings

Application configuration and settings management.

Classes

AppConfig

Bases: BaseSettings

Application configuration loaded from .env file and environment variables.

Functions
validate_ssl_cert_path
validate_ssl_cert_path()

Validate that SSL cert path exists if provided.

Source code in src/pyetm/config/settings.py
@model_validator(mode="after")
def validate_ssl_cert_path(self) -> "AppConfig":
    """Validate that SSL cert path exists if provided."""
    if self.ssl_cert_path is not None and not self.ssl_cert_path.exists():
        raise ValueError(
            f"SSL certificate file not found: {self.ssl_cert_path}. "
            f"Please provide a valid path to a CA certificate bundle."
        )
    return self
model_post_init
model_post_init(__context)

Post-initialization to handle base_url inference and token warnings.

Source code in src/pyetm/config/settings.py
def model_post_init(self, __context: Any) -> None:
    """Post-initialization to handle base_url inference and token warnings."""
    if not self.base_url:
        self.base_url = HttpUrl(_infer_base_url_from_env(self.environment or "production"))

    # Warn if no token is provided
    if not self.etm_api_token:
        logger.warning(
            "No ETM_API_TOKEN provided. You will only be able to access public scenarios without authentication."
        )

Functions

get_settings cached

get_settings()

Load AppConfig from .env file and environment variables.

Cached to ensure only one AppConfig instance is created per session. To reload configuration after changing environment variables, use reload_configuration().

Source code in src/pyetm/config/settings.py
@functools.lru_cache(maxsize=1)
def get_settings() -> AppConfig:
    """
    Load AppConfig from .env file and environment variables.

    Cached to ensure only one AppConfig instance is created per session.
    To reload configuration after changing environment variables, use reload_configuration().
    """
    try:
        return AppConfig()  # type: ignore[call-arg]
    except ValidationError as exc:
        missing_or_invalid: List[str] = []
        for err in exc.errors():
            loc = ".".join(str(x) for x in err["loc"])
            msg = err["msg"]
            missing_or_invalid.append(f"• {loc}: {msg}")

        detail = "\n".join(missing_or_invalid)
        raise RuntimeError(
            f"\nConfiguration error: one or more required settings are missing or invalid:\n\n"
            f"{detail}\n\n"
            f"Please set them via environment variables (e.g., ETM_API_TOKEN=...) or in a .env file in your working directory."
        ) from exc

reload_configuration

reload_configuration()

Clear cached configuration and error policy to reload from environment.

This function clears both the settings cache (get_settings) and the error policy cache (get_error_policy), ensuring that the next call to either function will reload configuration from environment variables.

Use this when you need to change configuration at runtime, for example:

import os
os.environ["PYETM_ERROR_MODE"] = "safe"
reload_configuration()
# Next call to get_settings() or get_error_policy() will see the new value

Note: In production code, configuration should typically be set once at startup. This function is primarily useful for testing or interactive environments.

Source code in src/pyetm/config/settings.py
def reload_configuration() -> None:
    """
    Clear cached configuration and error policy to reload from environment.

    This function clears both the settings cache (get_settings) and the error policy
    cache (get_error_policy), ensuring that the next call to either function will
    reload configuration from environment variables.

    Use this when you need to change configuration at runtime, for example:

        import os
        os.environ["PYETM_ERROR_MODE"] = "safe"
        reload_configuration()
        # Next call to get_settings() or get_error_policy() will see the new value

    Note: In production code, configuration should typically be set once at startup.
    This function is primarily useful for testing or interactive environments.
    """
    get_settings.cache_clear()
    # Import here to avoid circular dependency
    from pyetm.models.error_policy import get_error_policy
    get_error_policy.cache_clear()