33import contextlib
44import os
55from 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.
99try :
@@ -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
2942def _disable_dbt_stdout () -> Iterator [None ]:
3043 with contextlib .redirect_stdout (None ):
3144 yield
3245
3346
47+ @dbt_required
3448def 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
5867def 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