Skip to content

Commit

Permalink
piecing things together, use contextvar instead of sys.args
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenyuLInx committed Dec 8, 2022
1 parent b247973 commit 21e5888
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
20 changes: 11 additions & 9 deletions core/dbt/cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@
from dbt.cli.main import cli
from dbt.config.project import Project


class DBTUsageException(Exception):
pass


class DBTContext(Context):
def __init__(self, args: List[str]) -> None:
try:
try:
ctx = cli.make_context(cli.name, args)
if args:
cmd_name, cmd, cmd_args = cli.resolve_command(ctx, args)
cmd.make_context(cmd_name, cmd_args, parent=ctx)
cmd.make_context(cmd_name, cmd_args, parent=ctx) # type: ignore
except (NoSuchOption, UsageError) as e:
raise DBTUsageException(e.message)
raise DBTUsageException(e.message)

ctx.obj = {}
# yikes?
self.__dict__.update(ctx.__dict__)
# TODO: consider initializing Flags, ctx.obj here.

# @classmethod
# def from_args(cls, args: List[str]) -> "DBTContext":
# try:
# try:
# ctx = cli.make_context(cli.name, args)
# if args:
# cmd_name, cmd, cmd_args = cli.resolve_command(ctx, args)
# cmd.make_context(cmd_name, cmd_args, parent=ctx)
# except (NoSuchOption, UsageError) as e:
# raise DBTUsageException(e.message)
# raise DBTUsageException(e.message)

# ctx.obj = {}
# # yikes
# ctx.__class__ = cls
Expand All @@ -40,5 +42,5 @@ def __init__(self, args: List[str]) -> None:
def set_project(self, project: Project):
if not isinstance(project, Project):
raise ValueError(f"{project} is a {type(project)}, expected a Project object.")
self.obj["project"] = project

self.obj["project"] = project
15 changes: 13 additions & 2 deletions core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# TODO Move this to /core/dbt/flags.py when we're ready to break things
import os
import sys
from dataclasses import dataclass
from importlib import import_module
from multiprocessing import get_context
from pprint import pformat as pf
from typing import Set
from contextvars import ContextVar

from click import Context, get_current_context
from click.core import ParameterSource
Expand All @@ -18,6 +18,17 @@
import multiprocessing.popen_spawn_posix # type: ignore # noqa: F401


INVOCATION_ARGS: ContextVar[list[str]] = ContextVar("invocation_args")

This comment has been minimized.

Copy link
@ChenyuLInx

ChenyuLInx Dec 8, 2022

Author Contributor

This is probably not the best place to put things, we should have a new location for it



def set_invocation_args(args):
INVOCATION_ARGS.set(args)


def get_invocation_args():
return INVOCATION_ARGS.get()


@dataclass(frozen=True)
class Flags:
def __init__(self, ctx: Context = None, user_config: UserConfig = None) -> None:
Expand Down Expand Up @@ -50,7 +61,7 @@ def assign_params(ctx, params_assigned_from_default):
invoked_subcommand = getattr(import_module("dbt.cli.main"), invoked_subcommand_name)
invoked_subcommand.allow_extra_args = True
invoked_subcommand.ignore_unknown_options = True
invoked_subcommand_ctx = invoked_subcommand.make_context(None, sys.argv)
invoked_subcommand_ctx = invoked_subcommand.make_context(None, get_invocation_args())
assign_params(invoked_subcommand_ctx, params_assigned_from_default)

if not user_config:
Expand Down
4 changes: 3 additions & 1 deletion core/dbt/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import inspect # This is temporary for RAT-ing
from copy import copy
from pprint import pformat as pf # This is temporary for RAT-ing
import sys

import click
from dbt.adapters.factory import adapter_management
from dbt.cli import params as p
from dbt.cli.flags import Flags
from dbt.config import RuntimeConfig
from dbt.config.runtime import load_project, load_profile
from dbt.cli.flags import Flags, set_invocation_args
from dbt.events.functions import setup_event_logger
from dbt.profiler import profiler
from dbt.task.deps import DepsTask
Expand All @@ -20,6 +21,7 @@ def cli_runner():
ls = copy(cli.commands["list"])
ls.hidden = True
cli.add_command(ls, "ls")
set_invocation_args(sys.argv[1:])

# Run the cli
cli()
Expand Down

0 comments on commit 21e5888

Please sign in to comment.