dbt-py-wrap 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Bilbottom
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,144 @@
1
+ Metadata-Version: 2.1
2
+ Name: dbt-py-wrap
3
+ Version: 0.0.2
4
+ Summary: Python wrapper for dbt-core to extend dbt with custom Python.
5
+ Home-page: https://github.com/Bilbottom/dbt-py
6
+ Author: Bilbottom
7
+ Requires-Python: >=3.11,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Project-URL: Repository, https://github.com/Bilbottom/dbt-py
12
+ Description-Content-Type: text/markdown
13
+
14
+ <div align="center">
15
+
16
+ [![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)](https://www.python.org/downloads/release/python-3110/)
17
+ [![Python 3.11](https://img.shields.io/badge/dbt-1.5+-blue.svg)](https://github.com/dbt-labs/dbt-core/)
18
+ [![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
19
+ ![GitHub last commit](https://img.shields.io/github/last-commit/Bilbottom/dbt-py)
20
+
21
+ [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Bilbottom/dbt-py/main.svg)](https://results.pre-commit.ci/latest/github/Bilbottom/dbt-py/main)
22
+ [![application-tests](https://github.com/Bilbottom/db-query-profiler/actions/workflows/application-tests.yaml/badge.svg)](https://github.com/Bilbottom/db-query-profiler/actions/workflows/application-tests.yaml)
23
+
24
+ </div>
25
+
26
+ ---
27
+
28
+ # dbt-Ο€ 🧬
29
+
30
+ Python wrapper for [dbt-core](https://github.com/dbt-labs/dbt-core) to extend dbt with custom Python.
31
+
32
+ ## Shimmy shimmy shim πŸ•ΊπŸ•ΊπŸ•Ί
33
+
34
+ This package is a [shim](<https://en.wikipedia.org/wiki/Shim_(computing)>) for [dbt-core](https://github.com/dbt-labs/dbt-core), inspired by (_cough_ stolen from _cough_) my old boss, [@darkdreamingdan](https://github.com/darkdreamingdan):
35
+
36
+ - https://gist.github.com/darkdreamingdan/c5ded709a90fc3c5b420cee5f644f499
37
+
38
+ Before using this package, it's recommended to get up to speed with the Python modules that are already available in dbt:
39
+
40
+ - https://docs.getdbt.com/reference/dbt-jinja-functions/modules
41
+
42
+ The existing Python modules are available in the dbt Jinja context under the `modules` object, for example:
43
+
44
+ ```jinja
45
+ {{ modules.datetime.datetime.now() }}
46
+ ```
47
+
48
+ ## Installation ⬇️
49
+
50
+ While in preview, this package is only available from GitHub:
51
+
52
+ ```
53
+ pip install git+https://github.com/Bilbottom/dbt-py@v0.0.2
54
+ ```
55
+
56
+ This will be made available on PyPI once it's ready for general use.
57
+
58
+ ## Usage πŸ“–
59
+
60
+ This package adds a new executable, `dbt-py`, which injects your custom Python into dbt and then runs dbt. Either a custom module or a custom package can be injected. A custom module is the simplest to get started with.
61
+
62
+ The default module/package name is `custom` which would make custom Python available in the dbt Jinja context under the `modules.custom` object. This can be configured (see the [Configuration](#configuration-) section below).
63
+
64
+ ### Custom Module 🐍
65
+
66
+ Create a module called `custom.py` in the root of your dbt project. This module can contain any Python code you like, for example:
67
+
68
+ ```python
69
+ def salutation(name: str) -> str:
70
+ return f"Hello, {name}!"
71
+ ```
72
+
73
+ Reference this module and function in the dbt Jinja context of a dbt model:
74
+
75
+ ```jinja
76
+ {{ modules.custom.salutation("World") }}
77
+ ```
78
+
79
+ Rather than run dbt with the `dbt` command, instead run it with `dbt-py`:
80
+
81
+ ```
82
+ dbt-py clean
83
+ dbt-py build
84
+ ```
85
+
86
+ Note that `dbt-py` is a wrapper around `dbt` so all the usual dbt commands are available -- all the arguments passed to `dbt-py` are passed through to `dbt`, too.
87
+
88
+ ```
89
+ dbt-py --help
90
+ dbt-py run --select my_model
91
+ dbt-py test --select tag:unit-test
92
+ ```
93
+
94
+ ### Custom Package πŸ“¦
95
+
96
+ Using a custom package is similar to using a custom module: create a package called `custom` in the root of your dbt project.
97
+
98
+ The submodules of this package will be available in the dbt Jinja context too. For example, suppose you have a package called `custom` with a submodule called `greetings`:
99
+
100
+ ```
101
+ custom/
102
+ __init__.py
103
+ greetings.py
104
+ ```
105
+
106
+ If the `greetings.py` submodule contains the same `salutation` function as above, then it can be referenced in the dbt Jinja context as follows:
107
+
108
+ ```jinja
109
+ {{ modules.custom.greetings.salutation("World") }}
110
+ ```
111
+
112
+ Alternatively, you can expose the `salutation` function via the `__init__.py` file and then reference it directly via `custom`:
113
+
114
+ ```jinja
115
+ {{ modules.custom.salutation("World") }}
116
+ ```
117
+
118
+ ### Configuration πŸ› οΈ
119
+
120
+ The default module/package and Jinja context name is `custom` but both can be configured with the following environment variables:
121
+
122
+ - `DBT_PY_PACKAGE_ROOT`: The Python-style ref to the custom module/package, e.g. `package.module.submodule`
123
+ - `DBT_PY_PACKAGE_NAME`: The name to give the custom module/package in the dbt Jinja context, e.g. `custom_py`. Defaults to the value of `DBT_PY_PACKAGE_ROOT`
124
+
125
+ In particular, you can use the `DBT_PY_PACKAGE_ROOT` environment variable to reference a custom module/package that is not at the root of your dbt project.
126
+
127
+ #### Warning ⚠️
128
+
129
+ If you set the `DBT_PY_PACKAGE_ROOT` environment variable to a name that already exists, this package will use the existing module/package rather than your custom one. Make sure that your custom module/package name does not clash with any existing modules/packages.
130
+
131
+ This is likely to change in a future release, but for now you may choose to exploit this behaviour to use an existing module/package in your dbt Jinja context. For example, you could set `DBT_PY_PACKAGE_ROOT` to `math` and then reference the `math` standard library in your dbt Jinja context:
132
+
133
+ ```jinja
134
+ {{ modules.math.pi }}
135
+ ```
136
+
137
+ ## Future Work 🚧
138
+
139
+ This is still in preview, and there are a few things to be added before it's ready for general use:
140
+
141
+ - Support for importing any number of packages (currently only one package is supported)
142
+ - Configuration via config files and CLI arguments (currently only environment variables are supported)
143
+ - More robust testing
144
+
@@ -0,0 +1,130 @@
1
+ <div align="center">
2
+
3
+ [![Python 3.11](https://img.shields.io/badge/python-3.11-blue.svg)](https://www.python.org/downloads/release/python-3110/)
4
+ [![Python 3.11](https://img.shields.io/badge/dbt-1.5+-blue.svg)](https://github.com/dbt-labs/dbt-core/)
5
+ [![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
6
+ ![GitHub last commit](https://img.shields.io/github/last-commit/Bilbottom/dbt-py)
7
+
8
+ [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Bilbottom/dbt-py/main.svg)](https://results.pre-commit.ci/latest/github/Bilbottom/dbt-py/main)
9
+ [![application-tests](https://github.com/Bilbottom/db-query-profiler/actions/workflows/application-tests.yaml/badge.svg)](https://github.com/Bilbottom/db-query-profiler/actions/workflows/application-tests.yaml)
10
+
11
+ </div>
12
+
13
+ ---
14
+
15
+ # dbt-Ο€ 🧬
16
+
17
+ Python wrapper for [dbt-core](https://github.com/dbt-labs/dbt-core) to extend dbt with custom Python.
18
+
19
+ ## Shimmy shimmy shim πŸ•ΊπŸ•ΊπŸ•Ί
20
+
21
+ This package is a [shim](<https://en.wikipedia.org/wiki/Shim_(computing)>) for [dbt-core](https://github.com/dbt-labs/dbt-core), inspired by (_cough_ stolen from _cough_) my old boss, [@darkdreamingdan](https://github.com/darkdreamingdan):
22
+
23
+ - https://gist.github.com/darkdreamingdan/c5ded709a90fc3c5b420cee5f644f499
24
+
25
+ Before using this package, it's recommended to get up to speed with the Python modules that are already available in dbt:
26
+
27
+ - https://docs.getdbt.com/reference/dbt-jinja-functions/modules
28
+
29
+ The existing Python modules are available in the dbt Jinja context under the `modules` object, for example:
30
+
31
+ ```jinja
32
+ {{ modules.datetime.datetime.now() }}
33
+ ```
34
+
35
+ ## Installation ⬇️
36
+
37
+ While in preview, this package is only available from GitHub:
38
+
39
+ ```
40
+ pip install git+https://github.com/Bilbottom/dbt-py@v0.0.2
41
+ ```
42
+
43
+ This will be made available on PyPI once it's ready for general use.
44
+
45
+ ## Usage πŸ“–
46
+
47
+ This package adds a new executable, `dbt-py`, which injects your custom Python into dbt and then runs dbt. Either a custom module or a custom package can be injected. A custom module is the simplest to get started with.
48
+
49
+ The default module/package name is `custom` which would make custom Python available in the dbt Jinja context under the `modules.custom` object. This can be configured (see the [Configuration](#configuration-) section below).
50
+
51
+ ### Custom Module 🐍
52
+
53
+ Create a module called `custom.py` in the root of your dbt project. This module can contain any Python code you like, for example:
54
+
55
+ ```python
56
+ def salutation(name: str) -> str:
57
+ return f"Hello, {name}!"
58
+ ```
59
+
60
+ Reference this module and function in the dbt Jinja context of a dbt model:
61
+
62
+ ```jinja
63
+ {{ modules.custom.salutation("World") }}
64
+ ```
65
+
66
+ Rather than run dbt with the `dbt` command, instead run it with `dbt-py`:
67
+
68
+ ```
69
+ dbt-py clean
70
+ dbt-py build
71
+ ```
72
+
73
+ Note that `dbt-py` is a wrapper around `dbt` so all the usual dbt commands are available -- all the arguments passed to `dbt-py` are passed through to `dbt`, too.
74
+
75
+ ```
76
+ dbt-py --help
77
+ dbt-py run --select my_model
78
+ dbt-py test --select tag:unit-test
79
+ ```
80
+
81
+ ### Custom Package πŸ“¦
82
+
83
+ Using a custom package is similar to using a custom module: create a package called `custom` in the root of your dbt project.
84
+
85
+ The submodules of this package will be available in the dbt Jinja context too. For example, suppose you have a package called `custom` with a submodule called `greetings`:
86
+
87
+ ```
88
+ custom/
89
+ __init__.py
90
+ greetings.py
91
+ ```
92
+
93
+ If the `greetings.py` submodule contains the same `salutation` function as above, then it can be referenced in the dbt Jinja context as follows:
94
+
95
+ ```jinja
96
+ {{ modules.custom.greetings.salutation("World") }}
97
+ ```
98
+
99
+ Alternatively, you can expose the `salutation` function via the `__init__.py` file and then reference it directly via `custom`:
100
+
101
+ ```jinja
102
+ {{ modules.custom.salutation("World") }}
103
+ ```
104
+
105
+ ### Configuration πŸ› οΈ
106
+
107
+ The default module/package and Jinja context name is `custom` but both can be configured with the following environment variables:
108
+
109
+ - `DBT_PY_PACKAGE_ROOT`: The Python-style ref to the custom module/package, e.g. `package.module.submodule`
110
+ - `DBT_PY_PACKAGE_NAME`: The name to give the custom module/package in the dbt Jinja context, e.g. `custom_py`. Defaults to the value of `DBT_PY_PACKAGE_ROOT`
111
+
112
+ In particular, you can use the `DBT_PY_PACKAGE_ROOT` environment variable to reference a custom module/package that is not at the root of your dbt project.
113
+
114
+ #### Warning ⚠️
115
+
116
+ If you set the `DBT_PY_PACKAGE_ROOT` environment variable to a name that already exists, this package will use the existing module/package rather than your custom one. Make sure that your custom module/package name does not clash with any existing modules/packages.
117
+
118
+ This is likely to change in a future release, but for now you may choose to exploit this behaviour to use an existing module/package in your dbt Jinja context. For example, you could set `DBT_PY_PACKAGE_ROOT` to `math` and then reference the `math` standard library in your dbt Jinja context:
119
+
120
+ ```jinja
121
+ {{ modules.math.pi }}
122
+ ```
123
+
124
+ ## Future Work 🚧
125
+
126
+ This is still in preview, and there are a few things to be added before it's ready for general use:
127
+
128
+ - Support for importing any number of packages (currently only one package is supported)
129
+ - Configuration via config files and CLI arguments (currently only environment variables are supported)
130
+ - More robust testing
@@ -0,0 +1,9 @@
1
+ """
2
+ Python wrapper for dbt-core to extend dbt with custom Python.
3
+ """
4
+ from dbt_py.main import PROJECT_ROOT, main
5
+
6
+ __all__ = [
7
+ "PROJECT_ROOT",
8
+ "main",
9
+ ]
@@ -0,0 +1,66 @@
1
+ """
2
+ Shim the dbt CLI to include our custom modules.
3
+ """
4
+ import importlib
5
+ import os
6
+ import pathlib
7
+ import pkgutil
8
+ import sys
9
+ from types import ModuleType
10
+ from typing import Any
11
+
12
+ import dbt.cli.main
13
+ import dbt.context.base
14
+ from dbt.context.base import get_context_modules as _get_context_modules
15
+
16
+ PROJECT_ROOT = pathlib.Path(__file__).parent.parent
17
+ # Python-style ref, e.g. `package.module.submodule`
18
+ PACKAGE_ROOT: str = os.environ.get("DBT_PY_PACKAGE_ROOT")
19
+
20
+
21
+ def import_submodules(
22
+ package_name: str,
23
+ recursive: bool = True,
24
+ ) -> dict[str, ModuleType]:
25
+ """
26
+ Import all submodules of a module, recursively, including subpackages.
27
+
28
+ - https://stackoverflow.com/a/25562415/10730311
29
+ """
30
+ package = importlib.import_module(package_name)
31
+ if not hasattr(package, "__path__"):
32
+ # `package` is a module, don't recurse any further
33
+ return {}
34
+
35
+ results = {}
36
+ for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
37
+ full_name = f"{package.__name__}.{name}"
38
+ results[full_name] = importlib.import_module(full_name)
39
+ if recursive and is_pkg:
40
+ results |= import_submodules(full_name)
41
+
42
+ return results
43
+
44
+
45
+ def new_get_context_modules() -> dict[str, dict[str, Any]]:
46
+ """
47
+ Append the custom modules into the whitelisted dbt modules.
48
+ """
49
+ package_root: str = PACKAGE_ROOT or "custom"
50
+ package_name: str = os.environ.get("DBT_PY_PACKAGE_NAME") or package_root
51
+
52
+ import_submodules(package_root)
53
+ modules = _get_context_modules()
54
+ modules[package_name] = importlib.import_module(package_root) # type: ignore
55
+
56
+ return modules
57
+
58
+
59
+ def main() -> None:
60
+ """
61
+ Shim the dbt CLI to include our custom modules.
62
+
63
+ - https://docs.getdbt.com/reference/programmatic-invocations
64
+ """
65
+ dbt.context.base.get_context_modules = new_get_context_modules
66
+ dbt.cli.main.dbtRunner().invoke(sys.argv[1:])
@@ -0,0 +1,47 @@
1
+ [build-system]
2
+ requires = ["poetry-core"]
3
+ build-backend = "poetry.core.masonry.api"
4
+
5
+
6
+ [tool.poetry]
7
+ name = "dbt-py-wrap" # Not `dbt-py` to avoid confusion with the DbtPy package (PyPI requirement)
8
+ version = "0.0.2"
9
+ description = "Python wrapper for dbt-core to extend dbt with custom Python."
10
+ readme = "README.md"
11
+ authors = ["Bilbottom"]
12
+ repository = "https://github.com/Bilbottom/dbt-py"
13
+ packages = [
14
+ { include = "dbt_py" },
15
+ ]
16
+
17
+ [tool.poetry.scripts]
18
+ dbt-py = "dbt_py:main"
19
+
20
+ [tool.poetry.dependencies]
21
+ python = "^3.11"
22
+
23
+ [tool.poetry.group]
24
+ dev.optional = true
25
+ test.optional = true
26
+ ide.optional = true
27
+
28
+ [tool.poetry.group.dev.dependencies]
29
+ pre-commit = "^3.3.3"
30
+
31
+ [tool.poetry.group.test.dependencies]
32
+ dbt-core = "^1.5.0"
33
+ dbt-duckdb = "^1.5.0"
34
+ pytest = "^7.4.0"
35
+ pytest-cov = "^4.1.0"
36
+
37
+ # Packages just for IDE integration
38
+ [tool.poetry.group.ide.dependencies]
39
+ black = "*"
40
+
41
+
42
+ [tool.pytest.ini_options]
43
+ testpaths = ["tests"]
44
+
45
+
46
+ [tool.isort]
47
+ profile = "black"