54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
"""Configuration management utilities."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextlib import ExitStack
|
|
|
|
from prefect.settings import (
|
|
PREFECT_API_KEY,
|
|
PREFECT_API_URL,
|
|
PREFECT_DEFAULT_WORK_POOL_NAME,
|
|
Setting,
|
|
temporary_settings,
|
|
)
|
|
|
|
from .settings import Settings, get_settings
|
|
|
|
__all__ = ["Settings", "get_settings", "configure_prefect"]
|
|
|
|
_prefect_settings_stack: ExitStack | None = None
|
|
|
|
|
|
def configure_prefect(settings: Settings) -> None:
|
|
"""Apply Prefect settings from the application configuration."""
|
|
global _prefect_settings_stack
|
|
|
|
overrides: dict[Setting, str] = {}
|
|
|
|
if settings.prefect_api_url is not None:
|
|
overrides[PREFECT_API_URL] = str(settings.prefect_api_url)
|
|
if settings.prefect_api_key:
|
|
overrides[PREFECT_API_KEY] = settings.prefect_api_key
|
|
if settings.prefect_work_pool:
|
|
overrides[PREFECT_DEFAULT_WORK_POOL_NAME] = settings.prefect_work_pool
|
|
|
|
if not overrides:
|
|
return
|
|
|
|
filtered_overrides = {
|
|
setting: value
|
|
for setting, value in overrides.items()
|
|
if setting.value() != value
|
|
}
|
|
|
|
if not filtered_overrides:
|
|
return
|
|
|
|
new_stack = ExitStack()
|
|
new_stack.enter_context(temporary_settings(updates=filtered_overrides))
|
|
|
|
if _prefect_settings_stack is not None:
|
|
_prefect_settings_stack.close()
|
|
|
|
_prefect_settings_stack = new_stack
|