Skip to content

Commit

Permalink
Override documentation html file
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsimsek committed Sep 6, 2024
1 parent 150d2f6 commit bb9ac2b
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 1 deletion.
5 changes: 4 additions & 1 deletion opendbt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
dbt.adapters.factory.AdapterContainer.get_custom_adapter_config_value = dbtcommon.get_custom_adapter_config_value
dbt.adapters.factory.AdapterContainer.get_custom_adapter_class_by_name = dbtcommon.get_custom_adapter_class_by_name
# # STEP-2 override existing method
dbt.task.generate.GenerateTask.dbt_run = dbt.task.generate.GenerateTask.run
dbt.task.generate.GenerateTask.run = dbt17.GenerateTask_run
dbt.task.serve.ServeTask.run = dbt17.ServeTask_run

if Version(DBT_VERSION.to_version_string(skip_matcher=True)) > Version("1.8.0"):
from opendbt import dbt18

Expand All @@ -26,7 +30,6 @@

dbt.adapters.factory.AdapterContainer.register_adapter = dbt17.register_adapter


class OpenDbtCli:

@staticmethod
Expand Down
59 changes: 59 additions & 0 deletions opendbt/dbtcommon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import importlib

DBT_CUSTOM_ADAPTER_VAR = 'dbt_custom_adapter'
import os
import shutil
import socketserver
import webbrowser
from http.server import SimpleHTTPRequestHandler
from pathlib import Path

import click
from dbt.include.global_project import DOCS_INDEX_FILE_PATH


def get_custom_adapter_config_value(self, config: 'AdapterRequiredConfig') -> str:
Expand Down Expand Up @@ -30,3 +39,53 @@ def get_custom_adapter_class_by_name(self, custom_adapter_class_name: str):
return user_adapter_class
except ModuleNotFoundError as mnfe:
raise Exception(f"Module of provided adapter not found, provided: {custom_adapter_class_name}") from mnfe


def GenerateTask_run(self):
# Call the original dbt run method
self.dbt_run()
target = Path(self.config.project_target_path).joinpath("index.html")
for dir in self.config.docs_paths:
index_html = Path(self.config.project_root).joinpath(dir).joinpath("index.html")
if index_html.is_file() and index_html.exists():
# override default dbt provided index.html with user index.html file
shutil.copyfile(index_html, target)
print(f"Using user provided documentation page: {index_html.as_posix()}")
break


def ServeTask_run(self):
# Call the original dbt run method
os.chdir(self.config.project_target_path)
target = Path(self.config.project_target_path).joinpath("index.html")
user_doc_found = False
for dir in self.config.docs_paths:
index_html = Path(self.config.project_root).joinpath(dir).joinpath('index.html')
print("Cheking: " + index_html.as_posix())
if index_html.is_file() and index_html.exists():
# override default dbt provided index.html with user index.html file
shutil.copyfile(index_html, target)
print(f"Using user provided documentation page: {index_html.as_posix()}")
# dbtdocs_css = index_html.parent.joinpath("dbtdocs.css")
# target_css = Path(self.config.project_target_path).joinpath("dbtdocs.css")
# if dbtdocs_css.is_file() and dbtdocs_css.exists():
# print(f"Using user provided CSS document: {index_html.as_posix()}")
# shutil.copyfile(dbtdocs_css, target_css)

user_doc_found = True
break
if user_doc_found is False:
shutil.copyfile(DOCS_INDEX_FILE_PATH, "index.html")

port = self.args.port
host = self.args.host

if self.args.browser:
webbrowser.open_new_tab(f"http://localhost:{port}")

with socketserver.TCPServer((host, port), SimpleHTTPRequestHandler) as httpd:
click.echo(f"Serving docs at {port}")
click.echo(f"To access from your browser, navigate to: http://localhost:{port}")
click.echo("\n\n")
click.echo("Press Ctrl+C to exit.")
httpd.serve_forever()
206 changes: 206 additions & 0 deletions opendbt/docs/index.html

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/resources/dbttest/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ seed-paths: [ "seeds" ]
# include "opendbt/macros/" macros!
macro-paths: [ "macros", "../../../opendbt/macros/" ]
snapshot-paths: [ "snapshots" ]
# include "opendbt/docs/" macros!
docs-paths: [ "../../../opendbt/docs/" ]

clean-targets: # directories to be removed by `dbt clean`
- "target"
Expand Down
19 changes: 19 additions & 0 deletions tests/test_dbt_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from pathlib import Path
from unittest import TestCase

from opendbt import OpenDbtProject


class TestDbtDocs(TestCase):
RESOURCES_DIR = Path(__file__).parent.joinpath("resources")
DBTTEST_DIR = RESOURCES_DIR.joinpath("dbttest")

def test_run_docs_generate(self):
dp = OpenDbtProject(project_dir=self.DBTTEST_DIR, profiles_dir=self.DBTTEST_DIR)
dp.run(command="docs", args=['generate'])

@unittest.skip("reason for skipping")
def test_run_docs_serve(self):
dp = OpenDbtProject(project_dir=self.DBTTEST_DIR, profiles_dir=self.DBTTEST_DIR)
dp.run(command="docs", args=['serve'])

0 comments on commit bb9ac2b

Please sign in to comment.