dbt-py-wrap 0.0.2__py3-none-any.whl
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.
dbt_py/__init__.py
ADDED
dbt_py/main.py
ADDED
|
@@ -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,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
|
+
[](https://www.python.org/downloads/release/python-3110/)
|
|
17
|
+
[](https://github.com/dbt-labs/dbt-core/)
|
|
18
|
+
[](https://python-poetry.org/)
|
|
19
|
+

|
|
20
|
+
|
|
21
|
+
[](https://results.pre-commit.ci/latest/github/Bilbottom/dbt-py/main)
|
|
22
|
+
[](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,7 @@
|
|
|
1
|
+
dbt_py/__init__.py,sha256=Y-a0s1rgt1AQCXNSZt6A87gnJzowUlZJQy8m2gA4xM4,160
|
|
2
|
+
dbt_py/main.py,sha256=Phv4Ozq-XXvGR4JwTkhPZtPabRZUDvRVaYDE94qFOQc,1902
|
|
3
|
+
dbt_py_wrap-0.0.2.dist-info/LICENSE,sha256=KIZIkSCNlhsx5KOQoMv-ezOJ5gt1s6Jq4vUs8YMYKoM,1066
|
|
4
|
+
dbt_py_wrap-0.0.2.dist-info/METADATA,sha256=T_pZ43__40RZB9_I1E8WNguyLCnHjTqM4Abw_E18EgI,5854
|
|
5
|
+
dbt_py_wrap-0.0.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
|
6
|
+
dbt_py_wrap-0.0.2.dist-info/entry_points.txt,sha256=SfYg4JlOsLzCUmCa0OCxRivVhsodQxfn3IsWrigGamo,38
|
|
7
|
+
dbt_py_wrap-0.0.2.dist-info/RECORD,,
|