-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathflags.py
More file actions
110 lines (91 loc) · 3.2 KB
/
flags.py
File metadata and controls
110 lines (91 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Do not import the os package because we expose this package in jinja
from os import getenv as os_getenv
from argparse import Namespace
from multiprocessing import get_context
from typing import Optional
def env_set_truthy(key: str) -> Optional[str]:
"""Return the value if it was set to a "truthy" string value or None
otherwise.
"""
value = os_getenv(key)
if not value or value.lower() in ("0", "false", "f"):
return None
return value
# for setting up logger for legacy logger
ENABLE_LEGACY_LOGGER = env_set_truthy("DBT_ENABLE_LEGACY_LOGGER")
LOG_FORMAT = None
DEBUG = None
USE_COLORS = None
LOG_CACHE_EVENTS = None
QUIET = None
# This is not a flag, it's a place to store the lock
MP_CONTEXT = get_context()
# this roughly follows the patten of EVENT_MANAGER in dbt/events/functions.py
# During de-globlization, we'll need to handle both similarly
GLOBAL_FLAGS = Namespace() # type: ignore
FLAGS_SET = False
def set_flags(flags):
global GLOBAL_FLAGS
global FLAGS_SET
FLAGS_SET = True
GLOBAL_FLAGS = flags
def get_flags():
global FLAGS_SET
# this allow use the defualt via get_flags()
if not FLAGS_SET:
set_from_args(Namespace(), None)
return GLOBAL_FLAGS
def set_from_args(args: Namespace, user_config):
global GLOBAL_FLAGS
from dbt.cli.main import cli
from dbt.cli.flags import Flags
# make a dummy context to get the flags
ctx = cli.make_context("run", ["run"])
flags = Flags(ctx, user_config)
for arg_name, args_param_value in vars(args).items():
if arg_name in ["warn_error_options"]:
from dbt.cli.option_types import WarnErrorOptionsType
args_param_value = WarnErrorOptionsType().convert(args_param_value, None, None)
object.__setattr__(flags, arg_name.upper(), args_param_value)
object.__setattr__(flags, arg_name.lower(), args_param_value)
GLOBAL_FLAGS = flags # type: ignore
global FLAGS_SET
FLAGS_SET = True
def get_flag_dict():
flag_attr = {
"use_experimental_parser",
"static_parser",
"warn_error",
"warn_error_options",
"write_json",
"partial_parse",
"use_colors",
"profiles_dir",
"debug",
"log_format",
"version_check",
"fail_fast",
"send_anonymous_usage_stats",
"anonymous_usage_stats",
"printer_width",
"indirect_selection",
"log_cache_events",
"quiet",
"no_print",
"cache_selected_only",
"target_path",
"log_path",
}
return {key: getattr(GLOBAL_FLAGS, key.upper(), None) for key in flag_attr}
# This is used by core/dbt/context/base.py to return a flag object
# in Jinja.
def get_flag_obj():
new_flags = Namespace()
for key, val in get_flag_dict().items():
setattr(new_flags, key.upper(), val)
# The following 3 are CLI arguments only so they're not full-fledged flags,
# but we put in flags for users.
setattr(new_flags, "FULL_REFRESH", getattr(GLOBAL_FLAGS, "FULL_REFRESH", None))
setattr(new_flags, "STORE_FAILURES", getattr(GLOBAL_FLAGS, "STORE_FAILURES", None))
setattr(new_flags, "WHICH", getattr(GLOBAL_FLAGS, "WHICH", None))
return new_flags