Skip to content

Commit

Permalink
Flags work with Click (#5790)
Browse files Browse the repository at this point in the history
  • Loading branch information
iknox-fa authored Sep 19, 2022
1 parent e00eb9a commit 4c29d48
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 210 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20220908-123650.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Flags work with new Click CLI
time: 2022-09-08T12:36:50.386978-05:00
custom:
Author: iknox-fa
Issue: "5529"
PR: "5790"
44 changes: 44 additions & 0 deletions core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# TODO Move this to /core/dbt/flags.py when we're ready to break things
import os
from dataclasses import dataclass
from multiprocessing import get_context
from pprint import pformat as pf

from click import get_current_context

if os.name != "nt":
# https://bugs.python.org/issue41567
import multiprocessing.popen_spawn_posix # type: ignore # noqa: F401


@dataclass(frozen=True)
class Flags:
def __init__(self, ctx=None) -> None:

if ctx is None:
ctx = get_current_context()

def assign_params(ctx):
"""Recursively adds all click params to flag object"""
for param_name, param_value in ctx.params.items():
# N.B. You have to use the base MRO method (object.__setattr__) to set attributes
# when using frozen dataclasses.
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
if hasattr(self, param_name):
raise Exception(f"Duplicate flag names found in click command: {param_name}")
object.__setattr__(self, param_name.upper(), param_value)
if ctx.parent:
assign_params(ctx.parent)

assign_params(ctx)

# Hard coded flags
object.__setattr__(self, "WHICH", ctx.info_name)
object.__setattr__(self, "MP_CONTEXT", get_context("spawn"))

# Support console DO NOT TRACK initiave
if os.getenv("DO_NOT_TRACK", "").lower() in (1, "t", "true", "y", "yes"):
object.__setattr__(self, "ANONYMOUS_USAGE_STATS", False)

def __str__(self) -> str:
return str(pf(self.__dict__))
Loading

0 comments on commit 4c29d48

Please sign in to comment.