Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
add support for newlines in output
  • Loading branch information
Jacob Beck committed Jan 16, 2020
1 parent f92f389 commit 75c330a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
37 changes: 24 additions & 13 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from dbt.include.global_project import PACKAGES
from dbt.logger import GLOBAL_LOGGER as logger
from dbt.node_types import NodeType
from dbt.ui import printer
from dbt import deprecations
from dbt import tracking
import dbt.utils
Expand Down Expand Up @@ -445,20 +446,30 @@ def patch_nodes(self, patches):
patch = patches.pop(node.name, None)
if not patch:
continue
if node.resource_type.pluralize() == patch.yaml_key:
expected_key = node.resource_type.pluralize()
if expected_key == patch.yaml_key:
node.patch(patch)
elif patch.yaml_key == 'models':
deprecations.warn(
'models-key-mismatch', patch=patch, node=node
)
else:
raise_compiler_error(
f'patch instruction in {patch.original_file_path} under '
f'key "{patch.yaml_key}" was for node "{node.name}", but '
f'the node with the same name (from '
f'{node.original_file_path}) had resource type '
f'"{node.resource_type}"'
)
if expected_key != patch.yaml_key:
if patch.yaml_key == 'models':
deprecations.warn(
'models-key-mismatch',
patch=patch, node=node, expected_key=expected_key
)
else:
msg = printer.line_wrap_message(
f'''\
'{node.name}' is a {node.resource_type} node, but it is
specified in the {patch.yaml_key} section of
{patch.original_file_path}.
To fix this error, place the `{node.name}`
specification under the {expected_key} key instead.
'''
)
raise_compiler_error(msg)
node.patch(patch)

# log debug-level warning about nodes we couldn't find
if patches:
Expand Down
21 changes: 14 additions & 7 deletions core/dbt/deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dbt.links
import dbt.exceptions
import dbt.flags
from dbt.ui import printer


class DBTDeprecation:
Expand Down Expand Up @@ -99,13 +100,19 @@ class ColumnQuotingDeprecation(DBTDeprecation):
class ModelsKeyNonModelDeprecation(DBTDeprecation):
_name = 'models-key-mismatch'

_description = '''
patch instruction in {patch.original_file_path} under key
"models" was for node "{node.name}", but the node with the same
name (from {node.original_file_path}) had resource type
"{node.resource_type}". Nodes should be described under their resource
specific key. Support for this will be removed in a future release.
'''.strip()
_description = printer.line_wrap_message(
'''\
"{node.name}" is a {node.resource_type} node, but it is specified in
the {patch.yaml_key} section of {patch.original_file_path}.
To fix this warning, place the `{node.name}` specification under
the {expected_key} key instead.
This warning will become an error in a future release.
'''
).strip()


_adapter_renamed_description = """\
Expand Down
34 changes: 15 additions & 19 deletions core/dbt/parser/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,21 @@ def add_macro(self, source_file: SourceFile, macro: ParsedMacro):
# subtract 2 for the "Compilation Error" indent
# note that the line wrap eats newlines, so if you want newlines,
# this is the result :(
msg = '\n'.join([
printer.line_wrap_message(
f'''\
dbt found two macros named "{macro.name}" in the project
"{macro.package_name}".
''',
subtract=2
),
'',
printer.line_wrap_message(
f'''\
To fix this error, rename or remove one of the following
macros:
''',
subtract=2
),
f' - {macro.original_file_path}',
f' - {other_path}'
])
msg = printer.line_wrap_message(
f'''\
dbt found two macros named "{macro.name}" in the project
"{macro.package_name}".
To fix this error, rename or remove one of the following
macros:
- {macro.original_file_path}
- {other_path}
''',
subtract=2
)
raise_compiler_error(msg)

self.macros[macro.unique_id] = macro
Expand Down
12 changes: 11 additions & 1 deletion core/dbt/ui/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,17 @@ def print_run_end_messages(results, early_exit: bool = False) -> None:


def line_wrap_message(msg: str, subtract: int = 0, dedent: bool = True) -> str:
'''
Line wrap the given message to PRINTER_WIDTH - {subtract}. Convert double
newlines to newlines and avoid calling textwrap.fill() on them (like
markdown)
'''
width = PRINTER_WIDTH - subtract
if dedent:
msg = textwrap.dedent(msg)
return textwrap.fill(msg, width=width)

# If the input had an explicit double newline, we want to preserve that
# (we'll turn it into a single line soon). Support windows, too.
splitter = '\r\n\r\n' if '\r\n\r\n' in msg else '\n\n'
chunks = msg.split(splitter)
return '\n'.join(textwrap.fill(chunk, width=width) for chunk in chunks)

0 comments on commit 75c330a

Please sign in to comment.