Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new invocation context class. #52

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading