-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
create_adapter_plugins.py
286 lines (227 loc) · 8.04 KB
/
create_adapter_plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
import argparse
import sys
from pathlib import Path
PROJECT_TEMPLATE = '''
name: dbt_{adapter}
version: {version}
config-version: 2
macro-paths: ["macros"]
'''
SETUP_PY_TEMPLATE = '''
#!/usr/bin/env python
from setuptools import find_packages
from setuptools import setup
package_name = "dbt-{adapter}"
package_version = "{version}"
description = """The {adapter} adapter plugin for dbt (data build tool)"""
setup(
name=package_name,
version=package_version,
description=description,
long_description=description,
author={author_name},
author_email={author_email},
url={url},
packages=find_packages(),
package_data={{
'dbt': [
{package_data}
]
}},
install_requires=[
"{dbt_core_str}",{dependencies}
]
)
'''.lstrip()
ADAPTER_INIT_TEMPLATE = '''
from dbt.adapters.{adapter}.connections import {title_adapter}ConnectionManager
from dbt.adapters.{adapter}.connections import {title_adapter}Credentials
from dbt.adapters.{adapter}.impl import {title_adapter}Adapter
from dbt.adapters.base import AdapterPlugin
from dbt.include import {adapter}
Plugin = AdapterPlugin(
adapter={title_adapter}Adapter,
credentials={title_adapter}Credentials,
include_path={adapter}.PACKAGE_PATH)
'''.lstrip()
ADAPTER_CONNECTIONS_TEMPLATE = '''
from dataclasses import dataclass
from dbt.adapters.base import Credentials
from dbt.adapters.{adapter_src} import {connection_cls}
@dataclass
class {title_adapter}Credentials(Credentials):
# Add credentials members here, like:
# host: str
# port: int
# username: str
# password: str
@property
def type(self):
return '{adapter}'
def _connection_keys(self):
# return an iterator of keys to pretty-print in 'dbt debug'.
# Omit fields like 'password'!
raise NotImplementedError
class {title_adapter}ConnectionManager({connection_cls}):
TYPE = '{adapter}'
'''.lstrip()
ADAPTER_IMPL_TEMPLATE = '''
from dbt.adapters.{adapter_src} import {adapter_cls}
from dbt.adapters.{adapter} import {title_adapter}ConnectionManager
class {title_adapter}Adapter({adapter_cls}):
ConnectionManager = {title_adapter}ConnectionManager
'''.lstrip()
CATALOG_MACRO_TEMPLATE = """
{{% macro {adapter}__get_catalog(information_schema, schemas) -%}}
{{% set msg -%}}
get_catalog not implemented for {adapter}
{{%- endset %}}
{{{{ exceptions.raise_compiler_error(msg) }}}}
{{% endmacro %}}
"""
INCLUDE_INIT_TEXT = """
import os
PACKAGE_PATH = os.path.dirname(__file__)
""".lstrip()
class Builder:
def __init__(self, args):
self.args = args
self.adapter = self.args.adapter
self.dest = self.args.root / self.adapter
# self.dbt_dir = self.dest / 'dbt'
self.dbt_dir = Path('dbt')
self.adapters = self.dbt_dir / 'adapters' / self.adapter
self.include = self.dbt_dir / 'include' / self.adapter
if self.dest.exists():
raise Exception('path exists')
def go(self):
self.write_setup()
self.write_adapter()
self.write_include()
def include_paths(self):
return [
self.include / 'macros' / '*.sql',
self.include / 'dbt_project.yml',
]
def dest_path(self, *paths):
return self.dest.joinpath(*paths)
def write_setup(self):
self.dest.mkdir(parents=True, exist_ok=True)
dbt_core_str = 'dbt-core=={}'.format(self.args.dbt_core_version)
# 12-space indent, then single-quoted with a trailing comma. The path
# should not be the actual path from the root but from the 'dbt' dir
# (because this is in the 'dbt' package)
package_data = '\n'.join(
"{}'{!s}',".format(12*' ', p.relative_to(self.dbt_dir))
for p in self.include_paths()
)
setup_py_contents = SETUP_PY_TEMPLATE.format(
adapter=self.adapter,
version=self.args.package_version,
author_name=self.args.author,
author_email=self.args.email,
url=self.args.url,
dbt_core_str=dbt_core_str,
dependencies=self.args.dependency,
package_data=package_data,
)
self.dest_path('setup.py').write_text(setup_py_contents)
def _make_adapter_kwargs(self):
if self.args.sql:
kwargs = {
'adapter_src': 'sql',
'adapter_cls': 'SQLAdapter',
'connection_cls': 'SQLConnectionManager',
}
else:
kwargs = {
'adapter_src': 'base',
'adapter_cls': 'BaseAdapter',
'connection_cls': 'BaseConnectionManager',
}
kwargs.update({
'upper_adapter': self.adapter.upper(),
'title_adapter': self.args.title_case,
'adapter': self.adapter,
})
return kwargs
def write_adapter(self):
adapters_dest = self.dest_path(self.adapters)
adapters_dest.mkdir(parents=True, exist_ok=True)
kwargs = self._make_adapter_kwargs()
init_text = ADAPTER_INIT_TEMPLATE.format(
adapter=self.adapter,
title_adapter=self.args.title_case
)
connections_text = ADAPTER_CONNECTIONS_TEMPLATE.format(**kwargs)
impl_text = ADAPTER_IMPL_TEMPLATE.format(**kwargs)
(adapters_dest / '__init__.py').write_text(init_text)
(adapters_dest / 'connections.py').write_text(connections_text)
(adapters_dest / 'impl.py').write_text(impl_text)
def write_include(self):
include_dest = self.dest_path(self.include)
include_dest.mkdir(parents=True, exist_ok=True)
macros_dest = include_dest / 'macros'
macros_dest.mkdir(exist_ok=True)
dbt_project_text = PROJECT_TEMPLATE.format(
adapter=self.adapter,
version=self.args.project_version,
)
catalog_macro_text = CATALOG_MACRO_TEMPLATE.format(
adapter=self.adapter
)
(include_dest / '__init__.py').write_text(INCLUDE_INIT_TEXT)
(include_dest / 'dbt_project.yml').write_text(dbt_project_text)
# make sure something satisfies the 'include/macros/*.sql' in setup.py
(macros_dest / 'catalog.sql').write_text(catalog_macro_text)
def parse_args(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser()
parser.add_argument('root', type=Path)
parser.add_argument('adapter')
parser.add_argument('--title-case', '-t', default=None)
parser.add_argument('--dependency', action='append')
parser.add_argument('--dbt-core-version', default='0.16.1rc1')
parser.add_argument('--email')
parser.add_argument('--author')
parser.add_argument('--url')
parser.add_argument('--sql', action='store_true')
parser.add_argument('--package-version', default='0.0.1')
parser.add_argument('--project-version', default='1.0')
parser.add_argument(
'--no-dependency', action='store_false', dest='set_dependency'
)
parsed = parser.parse_args()
if parsed.title_case is None:
parsed.title_case = parsed.adapter.title()
if parsed.set_dependency:
prefix = '\n '
if parsed.dependency:
# ['a', 'b'] => "'a',\n 'b'"; ['a'] -> "'a',"
parsed.dependency = prefix + prefix.join(
"'{}',".format(d) for d in parsed.dependency
)
else:
parsed.dependency = prefix + '<INSERT DEPENDENCIES HERE>'
else:
parsed.dependency = ''
if parsed.email is not None:
parsed.email = "'{}'".format(parsed.email)
else:
parsed.email = '<INSERT EMAIL HERE>'
if parsed.author is not None:
parsed.author = "'{}'".format(parsed.author)
else:
parsed.author = '<INSERT AUTHOR HERE>'
if parsed.url is not None:
parsed.url = "'{}'".format(parsed.url)
else:
parsed.url = '<INSERT URL HERE>'
return parsed
def main():
builder = Builder(parse_args())
builder.go()
if __name__ == '__main__':
main()