Skip to content

Commit 2ca9c98

Browse files
Add dbt check as decorator
1 parent 9b7fea6 commit 2ca9c98

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/dbt_score/dbt_utils.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import contextlib
44
import os
55
from pathlib import Path
6-
from typing import Iterable, Iterator, cast
6+
from typing import Any, Callable, Iterable, Iterator, cast
77

88
# Conditionally import dbt objects.
99
try:
@@ -25,12 +25,26 @@ class DbtLsException(Exception):
2525
"""Raised when dbt ls fails."""
2626

2727

28+
def dbt_required(func: Callable[..., Any]) -> Callable[..., Any]:
29+
"""Decorator for methods that require dbt to be installed."""
30+
31+
def inner() -> None:
32+
if not DBT_INSTALLED:
33+
raise DbtNotInstalledException(
34+
"This option requires dbt to be installed in the same Python"
35+
"environment as dbt-score."
36+
)
37+
38+
return inner
39+
40+
2841
@contextlib.contextmanager
2942
def _disable_dbt_stdout() -> Iterator[None]:
3043
with contextlib.redirect_stdout(None):
3144
yield
3245

3346

47+
@dbt_required
3448
def dbt_parse() -> "dbtRunnerResult":
3549
"""Parse a dbt project.
3650
@@ -40,12 +54,6 @@ def dbt_parse() -> "dbtRunnerResult":
4054
Raises:
4155
DbtParseException: dbt parse failed.
4256
"""
43-
if not DBT_INSTALLED:
44-
raise DbtNotInstalledException(
45-
"This option requires dbt to be installed in the same Python"
46-
"environment as dbt-score."
47-
)
48-
4957
with _disable_dbt_stdout():
5058
result: "dbtRunnerResult" = dbtRunner().invoke(["parse"])
5159

@@ -55,14 +63,9 @@ def dbt_parse() -> "dbtRunnerResult":
5563
return result
5664

5765

66+
@dbt_required
5867
def dbt_ls(select: Iterable[str] | None) -> Iterable[str]:
5968
"""Run dbt ls."""
60-
if not DBT_INSTALLED:
61-
raise DbtNotInstalledException(
62-
"This option requires dbt to be installed in the same Python"
63-
"environment as dbt-score."
64-
)
65-
6669
cmd = ["ls", "--resource-type", "model", "--output", "name"]
6770
if select:
6871
cmd += ["--select", *select]

0 commit comments

Comments
 (0)