Skip to content

Commit

Permalink
Add new invocation context class.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterallenwebb committed Jan 30, 2024
1 parent 0910ca4 commit 75a8524
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions dbt_common/context.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 75a8524

Please sign in to comment.