satchel-adapter-sdk 1.2.0__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.
- satchel_adapter_sdk-1.2.0/.gitignore +33 -0
- satchel_adapter_sdk-1.2.0/LICENSE +202 -0
- satchel_adapter_sdk-1.2.0/PKG-INFO +111 -0
- satchel_adapter_sdk-1.2.0/README.md +100 -0
- satchel_adapter_sdk-1.2.0/pyproject.toml +40 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/__init__.py +53 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/adapter.py +125 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/ir.py +230 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/mdl.py +208 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/py.typed +0 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/pytest_plugin.py +21 -0
- satchel_adapter_sdk-1.2.0/satchel_adapter_sdk/testing.py +252 -0
- satchel_adapter_sdk-1.2.0/sdk_tests/__init__.py +0 -0
- satchel_adapter_sdk-1.2.0/sdk_tests/conftest.py +1 -0
- satchel_adapter_sdk-1.2.0/sdk_tests/test_sdk.py +185 -0
- satchel_adapter_sdk-1.2.0/sdk_tests/toy.py +62 -0
- satchel_adapter_sdk-1.2.0/uv.lock +567 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.venv/
|
|
5
|
+
venv/
|
|
6
|
+
.env
|
|
7
|
+
.env.*
|
|
8
|
+
!.env.example
|
|
9
|
+
*.egg-info/
|
|
10
|
+
dist/
|
|
11
|
+
build/
|
|
12
|
+
.pytest_cache/
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
.ruff_cache/
|
|
15
|
+
.coverage
|
|
16
|
+
htmlcov/
|
|
17
|
+
|
|
18
|
+
# Node
|
|
19
|
+
node_modules/
|
|
20
|
+
.next/
|
|
21
|
+
*.log
|
|
22
|
+
|
|
23
|
+
# Local data
|
|
24
|
+
.satchel/
|
|
25
|
+
*.duckdb
|
|
26
|
+
*.parquet
|
|
27
|
+
|
|
28
|
+
# OS / editors
|
|
29
|
+
.DS_Store
|
|
30
|
+
.idea/
|
|
31
|
+
.vscode/
|
|
32
|
+
|
|
33
|
+
# Private design docs (kept local, not pushed)
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: satchel-adapter-sdk
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: SDK for writing satchel source adapters: the canonical IR, the adapter protocol and a conformance kit
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.12
|
|
8
|
+
Requires-Dist: pydantic>=2
|
|
9
|
+
Requires-Dist: pyyaml
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# satchel-adapter-sdk
|
|
13
|
+
|
|
14
|
+
The small public contract for Satchel **source adapters**: packages that read an upstream tool's
|
|
15
|
+
metadata (dbt, Cube, SQLMesh, ...) and describe it in Satchel's canonical IR. The Satchel server turns
|
|
16
|
+
that IR into ordinary context items, keeps them in sync, and never lets a sync overwrite what
|
|
17
|
+
analysts added on top.
|
|
18
|
+
|
|
19
|
+
This distribution depends on `pydantic` and `pyyaml` only. It has no web framework, ORM or
|
|
20
|
+
database driver; an adapter must not need them either.
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from satchel_adapter_sdk import AdapterInfo, SourceAdapter, SourceFiles, SourceSnapshot
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## What you write
|
|
27
|
+
|
|
28
|
+
An adapter is an object with an `info: AdapterInfo` manifest and a pure `load` method:
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
class MyAdapter:
|
|
32
|
+
info = AdapterInfo(kind="mytool", display_name="My tool", version="0.1.0",
|
|
33
|
+
maturity="experimental", capabilities=frozenset({"assets"}),
|
|
34
|
+
config_model=MyConfig, file_inputs=(FileInput(name="export", required=True),))
|
|
35
|
+
|
|
36
|
+
def load(self, config: MyConfig, files: SourceFiles) -> SourceSnapshot: ...
|
|
37
|
+
|
|
38
|
+
adapter = MyAdapter()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Register it under the `satchel.sources` entry-point group in your `pyproject.toml`:
|
|
42
|
+
|
|
43
|
+
```toml
|
|
44
|
+
[project.entry-points."satchel.sources"]
|
|
45
|
+
mytool = "satchel_source_mytool:adapter"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`pip install satchel-source-mytool` is then enough; `satchel source list` shows it as *external*.
|
|
49
|
+
Start from [`templates/adapter/`](../templates/adapter) and read
|
|
50
|
+
[`docs/adapters/writing-an-adapter.md`](../docs/adapters/writing-an-adapter.md).
|
|
51
|
+
|
|
52
|
+
### Rules
|
|
53
|
+
|
|
54
|
+
* **Pure.** `load(config, files)` is deterministic and does no database access. It does no network
|
|
55
|
+
access either, unless you declare the `network` capability. The host reads the input files
|
|
56
|
+
(named by `AdapterInfo.file_inputs`, a path in the config or an upload) and hands you bytes.
|
|
57
|
+
* **No server imports.** Never import `satchel.*`, SQLAlchemy, a database driver or FastAPI. Built-in
|
|
58
|
+
adapters are held to the same rule by a test.
|
|
59
|
+
* **Folders and optional dependencies.** `FileInput(multiple=True, extensions=...)` lets the path be a
|
|
60
|
+
folder (read it with `SourceFiles.group(name)`); `AdapterInfo.requires` names the optional modules
|
|
61
|
+
an adapter imports lazily, so the host can list it as unavailable with an install hint.
|
|
62
|
+
* **Normalised names.** Emit `schema.table` fqns in lower case (`normalise_fqn`). Put the database
|
|
63
|
+
in `SourceAsset.database`.
|
|
64
|
+
* **Warnings, not failures.** A construct you cannot map goes into `SourceSnapshot.warnings`;
|
|
65
|
+
one bad node must not fail the sync.
|
|
66
|
+
* **Trust.** Installed adapters run in the Satchel process as trusted code. Install only adapters you
|
|
67
|
+
would run on the server.
|
|
68
|
+
|
|
69
|
+
## IR versioning
|
|
70
|
+
|
|
71
|
+
`satchel_adapter_sdk.IR_VERSION` is a semantic version carried by every snapshot.
|
|
72
|
+
|
|
73
|
+
| Change | Bump |
|
|
74
|
+
|-----------------------------------------------------|-------|
|
|
75
|
+
| New optional field, new optional model, new capability | minor |
|
|
76
|
+
| Removed or renamed field, changed meaning, new required field | major |
|
|
77
|
+
|
|
78
|
+
The server accepts a snapshot whose **major** equals its own SDK's major and refuses others with a
|
|
79
|
+
clear message. Within a major, the server ignores nothing it does not understand and requires
|
|
80
|
+
nothing new, so an adapter written against `1.0.0` keeps working on every `1.x`.
|
|
81
|
+
|
|
82
|
+
## Conformance kit
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from satchel_adapter_sdk.testing import assert_conformant
|
|
86
|
+
|
|
87
|
+
def test_conformance(update_golden):
|
|
88
|
+
assert_conformant(adapter, MyConfig(export="..."), files,
|
|
89
|
+
golden=Path(__file__).parent / "golden" / "snapshot.json",
|
|
90
|
+
update_golden=update_golden)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
It checks the manifest, that the snapshot validates and round-trips, the IR version, determinism
|
|
94
|
+
(two loads equal), a stable fingerprint, normalised fqns and unique keys, valid MDL, declared
|
|
95
|
+
capabilities, no network (unless declared) and no server imports, and compares the snapshot to a
|
|
96
|
+
committed golden file. Add `pytest_plugins = ["satchel_adapter_sdk.pytest_plugin"]` to your
|
|
97
|
+
`conftest.py` for the `--update-golden` flag (or set `SATCHEL_UPDATE_GOLDEN=1`).
|
|
98
|
+
|
|
99
|
+
## Wren MDL
|
|
100
|
+
|
|
101
|
+
If your tool has governed metrics, build a Wren MDL manifest with `MdlBuilder` (models over
|
|
102
|
+
warehouse tables plus *cubes* of measures and dimensions) and return it in a
|
|
103
|
+
`SourceSemanticModel`. Satchel serves it through `query_metrics`.
|
|
104
|
+
|
|
105
|
+
## Develop
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
cd sdk && uv sync && uv run pytest -q && uv run ruff check . && uv run mypy satchel_adapter_sdk
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Licence: Apache-2.0.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# satchel-adapter-sdk
|
|
2
|
+
|
|
3
|
+
The small public contract for Satchel **source adapters**: packages that read an upstream tool's
|
|
4
|
+
metadata (dbt, Cube, SQLMesh, ...) and describe it in Satchel's canonical IR. The Satchel server turns
|
|
5
|
+
that IR into ordinary context items, keeps them in sync, and never lets a sync overwrite what
|
|
6
|
+
analysts added on top.
|
|
7
|
+
|
|
8
|
+
This distribution depends on `pydantic` and `pyyaml` only. It has no web framework, ORM or
|
|
9
|
+
database driver; an adapter must not need them either.
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from satchel_adapter_sdk import AdapterInfo, SourceAdapter, SourceFiles, SourceSnapshot
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What you write
|
|
16
|
+
|
|
17
|
+
An adapter is an object with an `info: AdapterInfo` manifest and a pure `load` method:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
class MyAdapter:
|
|
21
|
+
info = AdapterInfo(kind="mytool", display_name="My tool", version="0.1.0",
|
|
22
|
+
maturity="experimental", capabilities=frozenset({"assets"}),
|
|
23
|
+
config_model=MyConfig, file_inputs=(FileInput(name="export", required=True),))
|
|
24
|
+
|
|
25
|
+
def load(self, config: MyConfig, files: SourceFiles) -> SourceSnapshot: ...
|
|
26
|
+
|
|
27
|
+
adapter = MyAdapter()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Register it under the `satchel.sources` entry-point group in your `pyproject.toml`:
|
|
31
|
+
|
|
32
|
+
```toml
|
|
33
|
+
[project.entry-points."satchel.sources"]
|
|
34
|
+
mytool = "satchel_source_mytool:adapter"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`pip install satchel-source-mytool` is then enough; `satchel source list` shows it as *external*.
|
|
38
|
+
Start from [`templates/adapter/`](../templates/adapter) and read
|
|
39
|
+
[`docs/adapters/writing-an-adapter.md`](../docs/adapters/writing-an-adapter.md).
|
|
40
|
+
|
|
41
|
+
### Rules
|
|
42
|
+
|
|
43
|
+
* **Pure.** `load(config, files)` is deterministic and does no database access. It does no network
|
|
44
|
+
access either, unless you declare the `network` capability. The host reads the input files
|
|
45
|
+
(named by `AdapterInfo.file_inputs`, a path in the config or an upload) and hands you bytes.
|
|
46
|
+
* **No server imports.** Never import `satchel.*`, SQLAlchemy, a database driver or FastAPI. Built-in
|
|
47
|
+
adapters are held to the same rule by a test.
|
|
48
|
+
* **Folders and optional dependencies.** `FileInput(multiple=True, extensions=...)` lets the path be a
|
|
49
|
+
folder (read it with `SourceFiles.group(name)`); `AdapterInfo.requires` names the optional modules
|
|
50
|
+
an adapter imports lazily, so the host can list it as unavailable with an install hint.
|
|
51
|
+
* **Normalised names.** Emit `schema.table` fqns in lower case (`normalise_fqn`). Put the database
|
|
52
|
+
in `SourceAsset.database`.
|
|
53
|
+
* **Warnings, not failures.** A construct you cannot map goes into `SourceSnapshot.warnings`;
|
|
54
|
+
one bad node must not fail the sync.
|
|
55
|
+
* **Trust.** Installed adapters run in the Satchel process as trusted code. Install only adapters you
|
|
56
|
+
would run on the server.
|
|
57
|
+
|
|
58
|
+
## IR versioning
|
|
59
|
+
|
|
60
|
+
`satchel_adapter_sdk.IR_VERSION` is a semantic version carried by every snapshot.
|
|
61
|
+
|
|
62
|
+
| Change | Bump |
|
|
63
|
+
|-----------------------------------------------------|-------|
|
|
64
|
+
| New optional field, new optional model, new capability | minor |
|
|
65
|
+
| Removed or renamed field, changed meaning, new required field | major |
|
|
66
|
+
|
|
67
|
+
The server accepts a snapshot whose **major** equals its own SDK's major and refuses others with a
|
|
68
|
+
clear message. Within a major, the server ignores nothing it does not understand and requires
|
|
69
|
+
nothing new, so an adapter written against `1.0.0` keeps working on every `1.x`.
|
|
70
|
+
|
|
71
|
+
## Conformance kit
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from satchel_adapter_sdk.testing import assert_conformant
|
|
75
|
+
|
|
76
|
+
def test_conformance(update_golden):
|
|
77
|
+
assert_conformant(adapter, MyConfig(export="..."), files,
|
|
78
|
+
golden=Path(__file__).parent / "golden" / "snapshot.json",
|
|
79
|
+
update_golden=update_golden)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
It checks the manifest, that the snapshot validates and round-trips, the IR version, determinism
|
|
83
|
+
(two loads equal), a stable fingerprint, normalised fqns and unique keys, valid MDL, declared
|
|
84
|
+
capabilities, no network (unless declared) and no server imports, and compares the snapshot to a
|
|
85
|
+
committed golden file. Add `pytest_plugins = ["satchel_adapter_sdk.pytest_plugin"]` to your
|
|
86
|
+
`conftest.py` for the `--update-golden` flag (or set `SATCHEL_UPDATE_GOLDEN=1`).
|
|
87
|
+
|
|
88
|
+
## Wren MDL
|
|
89
|
+
|
|
90
|
+
If your tool has governed metrics, build a Wren MDL manifest with `MdlBuilder` (models over
|
|
91
|
+
warehouse tables plus *cubes* of measures and dimensions) and return it in a
|
|
92
|
+
`SourceSemanticModel`. Satchel serves it through `query_metrics`.
|
|
93
|
+
|
|
94
|
+
## Develop
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
cd sdk && uv sync && uv run pytest -q && uv run ruff check . && uv run mypy satchel_adapter_sdk
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Licence: Apache-2.0.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "satchel-adapter-sdk"
|
|
3
|
+
version = "1.2.0"
|
|
4
|
+
description = "SDK for writing satchel source adapters: the canonical IR, the adapter protocol and a conformance kit"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = { text = "Apache-2.0" }
|
|
8
|
+
dependencies = ["pydantic>=2", "pyyaml"]
|
|
9
|
+
|
|
10
|
+
[dependency-groups]
|
|
11
|
+
dev = ["pytest", "ruff", "mypy", "types-pyyaml"]
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["hatchling"]
|
|
15
|
+
build-backend = "hatchling.build"
|
|
16
|
+
|
|
17
|
+
[tool.hatch.build.targets.wheel]
|
|
18
|
+
packages = ["satchel_adapter_sdk"]
|
|
19
|
+
|
|
20
|
+
[tool.ruff]
|
|
21
|
+
line-length = 100
|
|
22
|
+
target-version = "py312"
|
|
23
|
+
|
|
24
|
+
[tool.ruff.lint]
|
|
25
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
26
|
+
|
|
27
|
+
[tool.ruff.lint.per-file-ignores]
|
|
28
|
+
"sdk_tests/*.py" = ["E501"]
|
|
29
|
+
|
|
30
|
+
[tool.mypy]
|
|
31
|
+
python_version = "3.12"
|
|
32
|
+
plugins = ["pydantic.mypy"]
|
|
33
|
+
strict = true
|
|
34
|
+
|
|
35
|
+
[tool.pytest.ini_options]
|
|
36
|
+
testpaths = ["sdk_tests"]
|
|
37
|
+
|
|
38
|
+
[tool.uv]
|
|
39
|
+
# Rebuild the (non-editable) server install of this package when its sources change.
|
|
40
|
+
cache-keys = [{ file = "pyproject.toml" }, { file = "satchel_adapter_sdk/**/*.py" }]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""satchel-adapter-sdk: everything needed to write a satchel source adapter.
|
|
2
|
+
|
|
3
|
+
from satchel_adapter_sdk import AdapterInfo, SourceAdapter, SourceFiles, SourceSnapshot
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from satchel_adapter_sdk.adapter import (
|
|
7
|
+
KIND_PATTERN,
|
|
8
|
+
AdapterInfo,
|
|
9
|
+
Capability,
|
|
10
|
+
FileInput,
|
|
11
|
+
Maturity,
|
|
12
|
+
SourceAdapter,
|
|
13
|
+
SourceFiles,
|
|
14
|
+
)
|
|
15
|
+
from satchel_adapter_sdk.ir import (
|
|
16
|
+
IR_VERSION,
|
|
17
|
+
SourceAsset,
|
|
18
|
+
SourceColumn,
|
|
19
|
+
SourceQuery,
|
|
20
|
+
SourceRelationship,
|
|
21
|
+
SourceSemanticModel,
|
|
22
|
+
SourceSnapshot,
|
|
23
|
+
SourceTerm,
|
|
24
|
+
SourceWarning,
|
|
25
|
+
fingerprint_bytes,
|
|
26
|
+
is_compatible,
|
|
27
|
+
normalise_fqn,
|
|
28
|
+
)
|
|
29
|
+
from satchel_adapter_sdk.mdl import MdlBuilder, validate_mdl
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"IR_VERSION",
|
|
33
|
+
"KIND_PATTERN",
|
|
34
|
+
"AdapterInfo",
|
|
35
|
+
"Capability",
|
|
36
|
+
"FileInput",
|
|
37
|
+
"Maturity",
|
|
38
|
+
"MdlBuilder",
|
|
39
|
+
"SourceAdapter",
|
|
40
|
+
"SourceAsset",
|
|
41
|
+
"SourceColumn",
|
|
42
|
+
"SourceFiles",
|
|
43
|
+
"SourceQuery",
|
|
44
|
+
"SourceRelationship",
|
|
45
|
+
"SourceSemanticModel",
|
|
46
|
+
"SourceSnapshot",
|
|
47
|
+
"SourceTerm",
|
|
48
|
+
"SourceWarning",
|
|
49
|
+
"fingerprint_bytes",
|
|
50
|
+
"is_compatible",
|
|
51
|
+
"normalise_fqn",
|
|
52
|
+
"validate_mdl",
|
|
53
|
+
]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""The adapter contract: what an adapter package must provide."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
from collections.abc import Iterable, Mapping
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any, Literal, Protocol, runtime_checkable
|
|
10
|
+
|
|
11
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
12
|
+
|
|
13
|
+
from satchel_adapter_sdk.ir import SourceSnapshot
|
|
14
|
+
|
|
15
|
+
Maturity = Literal["experimental", "beta", "stable"]
|
|
16
|
+
Capability = Literal["assets", "semantic", "queries", "glossary", "lineage", "network"]
|
|
17
|
+
KIND_PATTERN = r"^[a-z][a-z0-9_]{1,31}$"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class FileInput(BaseModel):
|
|
21
|
+
"""A file the adapter reads. The host loads it (from the path in ``config[name]`` or an
|
|
22
|
+
upload) so the adapter itself never opens files or sockets."""
|
|
23
|
+
|
|
24
|
+
name: str
|
|
25
|
+
description: str = ""
|
|
26
|
+
required: bool = False
|
|
27
|
+
multiple: bool = Field(
|
|
28
|
+
default=False,
|
|
29
|
+
description="The path may be a file or a folder (a Cube data model, a SQLMesh project). "
|
|
30
|
+
"The host reads every matching file under it and hands them over as "
|
|
31
|
+
"'<name>/<relative path>'; read them with SourceFiles.group(name).",
|
|
32
|
+
)
|
|
33
|
+
extensions: tuple[str, ...] = Field(
|
|
34
|
+
default=(),
|
|
35
|
+
description="With multiple: only read files with these lower-case suffixes, e.g. "
|
|
36
|
+
"('.yml', '.yaml'). Empty means every file.",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class AdapterInfo(BaseModel):
|
|
41
|
+
"""The adapter's manifest. Its ``config_model`` JSON schema drives the CLI flags of
|
|
42
|
+
``satchel source add`` and the UI form."""
|
|
43
|
+
|
|
44
|
+
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
45
|
+
|
|
46
|
+
kind: str = Field(pattern=KIND_PATTERN)
|
|
47
|
+
display_name: str
|
|
48
|
+
version: str
|
|
49
|
+
maturity: Maturity = "experimental"
|
|
50
|
+
capabilities: frozenset[Capability]
|
|
51
|
+
config_model: type[BaseModel]
|
|
52
|
+
file_inputs: tuple[FileInput, ...] = ()
|
|
53
|
+
extras: tuple[str, ...] = Field(
|
|
54
|
+
default=(), description="pip extras the adapter needs, e.g. ('cube',)"
|
|
55
|
+
)
|
|
56
|
+
requires: tuple[str, ...] = Field(
|
|
57
|
+
default=(),
|
|
58
|
+
description="Importable top-level modules the adapter needs at load time (an optional "
|
|
59
|
+
"dependency). When one is missing the host lists the adapter as unavailable with an "
|
|
60
|
+
"install hint instead of failing; import them lazily inside load().",
|
|
61
|
+
)
|
|
62
|
+
description: str = ""
|
|
63
|
+
homepage: str | None = None
|
|
64
|
+
|
|
65
|
+
def config_schema(self) -> dict[str, Any]:
|
|
66
|
+
return self.config_model.model_json_schema()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class SourceFiles:
|
|
70
|
+
"""Named input files, as bytes. Built by the host; adapters only read."""
|
|
71
|
+
|
|
72
|
+
def __init__(self, data: Mapping[str, bytes] | None = None) -> None:
|
|
73
|
+
self._data = dict(data or {})
|
|
74
|
+
|
|
75
|
+
@classmethod
|
|
76
|
+
def from_paths(cls, paths: Mapping[str, str | Path | None]) -> SourceFiles:
|
|
77
|
+
return cls({n: Path(p).read_bytes() for n, p in paths.items() if p})
|
|
78
|
+
|
|
79
|
+
def has(self, name: str) -> bool:
|
|
80
|
+
return name in self._data
|
|
81
|
+
|
|
82
|
+
def names(self) -> list[str]:
|
|
83
|
+
return sorted(self._data)
|
|
84
|
+
|
|
85
|
+
def read(self, name: str) -> bytes:
|
|
86
|
+
try:
|
|
87
|
+
return self._data[name]
|
|
88
|
+
except KeyError:
|
|
89
|
+
raise FileNotFoundError(f"input file {name!r} was not provided") from None
|
|
90
|
+
|
|
91
|
+
def read_text(self, name: str) -> str:
|
|
92
|
+
return self.read(name).decode("utf-8")
|
|
93
|
+
|
|
94
|
+
def read_json(self, name: str) -> Any:
|
|
95
|
+
return json.loads(self.read(name))
|
|
96
|
+
|
|
97
|
+
def group(self, name: str) -> dict[str, bytes]:
|
|
98
|
+
"""The files of a ``FileInput(multiple=True)``: ``{relative path: bytes}``, sorted by
|
|
99
|
+
path. A plain file stored under exactly ``name`` is returned as ``{name: bytes}``."""
|
|
100
|
+
prefix = f"{name}/"
|
|
101
|
+
out = {k[len(prefix):]: v for k, v in self._data.items() if k.startswith(prefix)}
|
|
102
|
+
if name in self._data:
|
|
103
|
+
out[name] = self._data[name]
|
|
104
|
+
return dict(sorted(out.items()))
|
|
105
|
+
|
|
106
|
+
def has_group(self, name: str) -> bool:
|
|
107
|
+
return bool(self.group(name))
|
|
108
|
+
|
|
109
|
+
def fingerprint(self, names: Iterable[str] | None = None) -> str:
|
|
110
|
+
"""sha256 over (name, bytes) of the given inputs (default: all), order independent."""
|
|
111
|
+
h = hashlib.sha256()
|
|
112
|
+
for n in sorted(names if names is not None else self._data):
|
|
113
|
+
if n in self._data:
|
|
114
|
+
h.update(n.encode() + b"\0" + hashlib.sha256(self._data[n]).digest())
|
|
115
|
+
return h.hexdigest()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@runtime_checkable
|
|
119
|
+
class SourceAdapter(Protocol):
|
|
120
|
+
"""``load`` is pure: deterministic, no database, and no network unless the ``network``
|
|
121
|
+
capability is declared. Installed adapters run in-process as trusted code."""
|
|
122
|
+
|
|
123
|
+
info: AdapterInfo
|
|
124
|
+
|
|
125
|
+
def load(self, config: Any, files: SourceFiles) -> SourceSnapshot: ...
|