Skip to content

Commit

Permalink
Add --quiet flag and print Jinja function (#4701)
Browse files Browse the repository at this point in the history
* Add `--quiet` flag

* Add print() macro

* Update tests for --quiet and print()

* Updating changelog

* Apply suggestions from PR review
  • Loading branch information
ehmartens authored Feb 10, 2022
1 parent 72ecd1c commit 41c6177
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added Support for Semantic Versioning ([#4644](https://github.com/dbt-labs/dbt-core/pull/4644))
- New Dockerfile to support specific db adapters and platforms. See docker/README.md for details ([#4495](https://github.com/dbt-labs/dbt-core/issues/4495), [#4487](https://github.com/dbt-labs/dbt-core/pull/4487))
- Allow unique_key to take a list ([#2479](https://github.com/dbt-labs/dbt-core/issues/2479), [#4618](https://github.com/dbt-labs/dbt-core/pull/4618))
- Add `--quiet` global flag and `print` Jinja function ([#3451](https://github.com/dbt-labs/dbt-core/issues/3451), [#4701](https://github.com/dbt-labs/dbt-core/pull/4701))

### Fixes
- User wasn't asked for permission to overwite a profile entry when running init inside an existing project ([#4375](https://github.com/dbt-labs/dbt-core/issues/4375), [#4447](https://github.com/dbt-labs/dbt-core/pull/4447))
Expand All @@ -19,6 +20,7 @@
Contributors:
- [@NiallRees](https://github.com/NiallRees) ([#4447](https://github.com/dbt-labs/dbt-core/pull/4447))
- [@alswang18](https://github.com/alswang18) ([#4644](https://github.com/dbt-labs/dbt-core/pull/4644))
- [@emartens](https://github.com/ehmartens) ([#4701](https://github.com/dbt-labs/dbt-core/pull/4701))

## dbt-core 1.0.2 (TBD)

Expand Down
16 changes: 16 additions & 0 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,22 @@ def flags(self) -> Any:
"""
return flags

@contextmember
@staticmethod
def print(msg: str) -> str:
"""Prints a line to stdout.
:param msg: The message to print
> macros/my_log_macro.sql
{% macro some_macro(arg1, arg2) %}
{{ print("Running some_macro: " ~ arg1 ~ ", " ~ arg2) }}
{% endmacro %}"
"""
print(msg)
return ''


def generate_base_context(cli_vars: Dict[str, Any]) -> Dict[str, Any]:
ctx = BaseContext(cli_vars)
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/events/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ def fire_event(e: Event) -> None:
# log messages are not constructed if debug messages are never shown.
if e.level_tag() == 'debug' and not flags.DEBUG:
return # eat the message in case it was one of the expensive ones
if e.level_tag() != 'error' and flags.QUIET:
return # eat all non-exception messages in quiet mode

log_line = create_log_line(e)
if log_line:
Expand Down
10 changes: 7 additions & 3 deletions core/dbt/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
INDIRECT_SELECTION = None
LOG_CACHE_EVENTS = None
EVENT_BUFFER_SIZE = 100000
QUIET = None

# Global CLI defaults. These flags are set from three places:
# CLI args, environment variables, and user_config (profiles.yml).
Expand All @@ -55,7 +56,8 @@
"PRINTER_WIDTH": 80,
"INDIRECT_SELECTION": 'eager',
"LOG_CACHE_EVENTS": False,
"EVENT_BUFFER_SIZE": 100000
"EVENT_BUFFER_SIZE": 100000,
"QUIET": False
}


Expand Down Expand Up @@ -103,7 +105,7 @@ def set_from_args(args, user_config):
USE_EXPERIMENTAL_PARSER, STATIC_PARSER, WRITE_JSON, PARTIAL_PARSE, \
USE_COLORS, STORE_FAILURES, PROFILES_DIR, DEBUG, LOG_FORMAT, INDIRECT_SELECTION, \
VERSION_CHECK, FAIL_FAST, SEND_ANONYMOUS_USAGE_STATS, PRINTER_WIDTH, \
WHICH, LOG_CACHE_EVENTS, EVENT_BUFFER_SIZE
WHICH, LOG_CACHE_EVENTS, EVENT_BUFFER_SIZE, QUIET

STRICT_MODE = False # backwards compatibility
# cli args without user_config or env var option
Expand All @@ -128,6 +130,7 @@ def set_from_args(args, user_config):
INDIRECT_SELECTION = get_flag_value('INDIRECT_SELECTION', args, user_config)
LOG_CACHE_EVENTS = get_flag_value('LOG_CACHE_EVENTS', args, user_config)
EVENT_BUFFER_SIZE = get_flag_value('EVENT_BUFFER_SIZE', args, user_config)
QUIET = get_flag_value('QUIET', args, user_config)


def get_flag_value(flag, args, user_config):
Expand Down Expand Up @@ -179,5 +182,6 @@ def get_flag_dict():
"printer_width": PRINTER_WIDTH,
"indirect_selection": INDIRECT_SELECTION,
"log_cache_events": LOG_CACHE_EVENTS,
"event_buffer_size": EVENT_BUFFER_SIZE
"event_buffer_size": EVENT_BUFFER_SIZE,
"quiet": QUIET
}
11 changes: 11 additions & 0 deletions core/dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,17 @@ def parse_args(args, cls=DBTArgumentParser):
'''
)

p.add_argument(
'-q',
'--quiet',
action='store_true',
default=None,
help='''
Suppress all non-error logging to stdout. Does not affect
{{ print() }} macro calls.
'''
)

subs = p.add_subparsers(title="Available sub-commands")

base_subparser = _build_base_subparser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@
{{ log((node | string), info=True)}}
{% endfor %}
{% endmacro %}


{% macro print_something() %}
{{ print("You're doing awesome!") }}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ def test__postgres_select(self):
@use_profile('postgres')
def test__postgres_access_graph(self):
self.run_operation('log_graph')

@use_profile('postgres')
def test__postgres_print(self):
# Tests that calling the `print()` macro does not cause an exception
self.run_operation('print_something')
1 change: 1 addition & 0 deletions test/unit/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def assert_has_keys(
'invocation_id',
'modules',
'flags',
'print',
})

REQUIRED_TARGET_KEYS = REQUIRED_BASE_KEYS | {'target'}
Expand Down
9 changes: 8 additions & 1 deletion test/unit/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test__flags(self):
os.environ['DBT_DEBUG'] = 'True'
flags.set_from_args(self.args, self.user_config)
self.assertEqual(flags.DEBUG, True)
os.environ['DBT_DEUBG'] = 'False'
os.environ['DBT_DEBUG'] = 'False'
setattr(self.args, 'debug', True)
flags.set_from_args(self.args, self.user_config)
self.assertEqual(flags.DEBUG, True)
Expand Down Expand Up @@ -217,3 +217,10 @@ def test__flags(self):
os.environ.pop('DBT_INDIRECT_SELECTION')
delattr(self.args, 'indirect_selection')
self.user_config.indirect_selection = None

# quiet
self.user_config.quiet = True
flags.set_from_args(self.args, self.user_config)
self.assertEqual(flags.QUIET, True)
# cleanup
self.user_config.quiet = None

0 comments on commit 41c6177

Please sign in to comment.