From 75a8524c2326358b95c348e22d850c3ee80d5a14 Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Tue, 30 Jan 2024 11:03:09 -0500 Subject: [PATCH] Add new invocation context class. --- dbt_common/context.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 dbt_common/context.py diff --git a/dbt_common/context.py b/dbt_common/context.py new file mode 100644 index 00000000..1d0b28b0 --- /dev/null +++ b/dbt_common/context.py @@ -0,0 +1,36 @@ +from contextvars import ContextVar +import os +from typing import List, Mapping + +from dbt_common.constants import SECRET_ENV_PREFIX + + +class InvocationContext: + def __init__(self): + self._env: Mapping[str, str] = None + self._env_secrets: List[str] + # This class will also eventually manage the invocation_id, flags, event manager, etc. + + @property + def env(self) -> Mapping[str, str]: + if self._env is None: + self._env = os.environ + + return self._env + + @property + def env_secrets(self) -> List[str]: + return [v for k, v in self.env.items() if k.startswith(SECRET_ENV_PREFIX) and v.strip()] + + + +_INVOCATION_CONTEXT_VAR: ContextVar[InvocationContext] = ContextVar("DBT_INVOCATION_CONTEXT_VAR") + + +def set_invocation_context() -> None: + _INVOCATION_CONTEXT_VAR.set(InvocationContext()) + + +def get_invocation_context() -> InvocationContext: + ctx = _INVOCATION_CONTEXT_VAR.get() + return ctx