Skip to content

Commit 8b5ed23

Browse files
authored
improve dbt plugin code (#20)
1 parent 16b181c commit 8b5ed23

File tree

7 files changed

+73
-45
lines changed

7 files changed

+73
-45
lines changed

opendbt/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import sys
5+
import tempfile
56
from pathlib import Path
67

78
from dbt.cli.main import dbtRunner as DbtCliRunner
@@ -94,15 +95,18 @@ def run(self, command: str = "build", target: str = None, args: list = None, use
9495
if write_json:
9596
run_args.remove("--no-write-json")
9697

97-
if use_subprocess:
98+
if False:
9899
shell = False
99100
self.log.info("Working dir is %s" % os.getcwd())
100101
self.log.info("Running command (shell=%s) `%s`" % (shell, " ".join(command)))
101102
Utils.runcommand(command=['opendbt'] + run_args)
102103
return None
103104
else:
104-
self.log.info(f"Running `dbt {' '.join(run_args)}`")
105-
return OpenDbtCli.run(args=run_args)
105+
with tempfile.TemporaryDirectory() as tmp_working_dir:
106+
os.chdir(tmp_working_dir)
107+
self.log.info(f"Running `dbt {' '.join(run_args)}`")
108+
self.log.info("CWD is %s" % os.getcwd())
109+
return OpenDbtCli.run(args=run_args)
106110

107111
def manifest(self, partial_parse=True, no_write_manifest=True) -> Manifest:
108112
args = []

opendbt/airflow/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def execute(self, context):
4646
"""
4747
Execute the dbt command.
4848
"""
49-
runner = opendbt.OpenDbtProject(project_dir=self.project_dir,
50-
profiles_dir=self.profiles_dir,
51-
target=self.target)
49+
runner = OpenDbtAirflowProject(project_dir=self.project_dir,
50+
profiles_dir=self.profiles_dir,
51+
target=self.target)
5252
runner.run(command=self.command, args=self.args, use_subprocess=self.use_subprocess)
5353

5454

opendbt/airflow/dbtdocs.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

opendbt/airflow/plugin.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from pathlib import Path
2+
3+
def init_plugins_dbtdocs_page(dbt_docs_dir: Path):
4+
from airflow.plugins_manager import AirflowPlugin
5+
from flask import Blueprint
6+
from flask_appbuilder import BaseView, expose
7+
from flask import abort
8+
from airflow.www.auth import has_access
9+
from airflow.security import permissions
10+
11+
class DBTDocsView(BaseView):
12+
route_base = "/dbt"
13+
default_view = "dbt_docs_index"
14+
15+
@expose("/dbt_docs_index.html") # type: ignore[misc]
16+
@has_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_WEBSITE)])
17+
def dbt_docs_index(self):
18+
if not dbt_docs_dir.joinpath("index.html").is_file():
19+
abort(404)
20+
else:
21+
return dbt_docs_dir.joinpath("index.html").read_text()
22+
# return self.render_template("index.html", content="")
23+
24+
@expose("/catalog.json") # type: ignore[misc]
25+
@has_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_WEBSITE)])
26+
def catalog(self):
27+
if not dbt_docs_dir.joinpath("catalog.json").is_file():
28+
abort(404)
29+
else:
30+
data = dbt_docs_dir.joinpath("catalog.json").read_text()
31+
return data, 200, {"Content-Type": "application/json"}
32+
# return self.render_template("index.html", content="")
33+
34+
@expose("/manifest.json") # type: ignore[misc]
35+
@has_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_WEBSITE)])
36+
def manifest(self):
37+
if not dbt_docs_dir.joinpath("manifest.json").is_file():
38+
abort(404)
39+
else:
40+
data = dbt_docs_dir.joinpath("manifest.json").read_text()
41+
return data, 200, {"Content-Type": "application/json"}
42+
# return self.render_template("index.html", content="")
43+
44+
# Creating a flask blueprint to integrate the templates and static folder
45+
bp = Blueprint(
46+
"DBT Plugin",
47+
__name__,
48+
template_folder=dbt_docs_dir.as_posix(),
49+
static_folder=dbt_docs_dir.as_posix(),
50+
# static_url_path='/dbtdocsview'
51+
)
52+
53+
54+
class AirflowDbtDocsPlugin(AirflowPlugin):
55+
name = "DBT Docs Plugin"
56+
flask_blueprints = [bp]
57+
appbuilder_views = [{"name": "DBT Docs", "category": "", "view": DBTDocsView()}]
58+
59+
return AirflowDbtDocsPlugin

opendbt/dbt/shared/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22

3-
from opendbt.airflow import dbtdocs
3+
from opendbt.airflow import plugin
44

55
# create public page on airflow server to serve DBT docs
6-
airflow_dbtdocs_page = dbtdocs.init_plugins_dbtdocs_page(Path("/opt/dbttest/target"))
6+
airflow_dbtdocs_page = plugin.init_plugins_dbtdocs_page(Path("/opt/dbttest/target"))

tests/test_airflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class TestAirflowBase(unittest.TestCase):
1919

2020
@classmethod
2121
def setUpClass(cls):
22-
cls._compose = DockerCompose(filepath=cls.resources_dir.joinpath('airflow').as_posix(),
22+
cls._compose = DockerCompose(cls.resources_dir.joinpath('airflow').as_posix(),
2323
compose_file_name="docker-compose.yaml",
24-
# build=True
24+
build=True
2525
)
2626
cls._compose.stop()
2727
cls._compose.start()

0 commit comments

Comments
 (0)