-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d37d023
commit 3efaf94
Showing
3 changed files
with
39 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,41 @@ | ||
from typing import List | ||
from click import Context | ||
from click import Context, Group, Command | ||
from click.exceptions import NoSuchOption, UsageError | ||
from dbt.cli.main import cli | ||
from dbt.cli.flags import Flags | ||
from dbt.config.project import Project | ||
import sys | ||
|
||
|
||
class DBTUsageException(Exception): | ||
pass | ||
|
||
|
||
class DBTContext(Context): | ||
def __init__(self, args: List[str]) -> None: | ||
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) | ||
def __init__(self, command: Command, **kwargs) -> None: | ||
if isinstance(kwargs.get("parent"), DBTContext): | ||
self.invocation_args = kwargs["parent"].invocation_args | ||
else: | ||
self.invocation_args = kwargs.pop("args", sys.argv[1:]) | ||
|
||
super().__init__(command, **kwargs) | ||
|
||
# Bubble up validation errors for top-level commands | ||
if not self.parent: | ||
self._validate_args(command, self.invocation_args) | ||
|
||
self.obj = self.obj or {} | ||
self.flags = Flags(self) | ||
|
||
def _validate_args(self, command, args) -> None: | ||
try: | ||
command.parse_args(self, args) | ||
if isinstance(command, Group): | ||
_, cmd, cmd_args = command.resolve_command(self, args) | ||
self._validate_args(cmd, cmd_args) | ||
except (NoSuchOption, UsageError) as e: | ||
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: | ||
# 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) | ||
|
||
# ctx.obj = {} | ||
# # yikes | ||
# ctx.__class__ = cls | ||
# return ctx | ||
raise DBTUsageException(e.message) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters