sourceledger 0.1.0__tar.gz → 0.3.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.
@@ -0,0 +1,299 @@
1
+ Metadata-Version: 2.4
2
+ Name: sourceledger
3
+ Version: 0.3.0
4
+ Summary: Fetch, verify and validate the public sources a dataset is built from, with a provenance record for every download
5
+ Keywords: provenance,data pipeline,reproducibility,open data,research software,FAIR
6
+ Author: Simon van Lierde
7
+ Author-email: Simon van Lierde <code@duinlab.nl>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Operating System :: POSIX
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Programming Language :: Python :: 3.15
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Classifier: Typing :: Typed
20
+ Requires-Dist: openpyxl>=3.1 ; extra == 'xlsx'
21
+ Requires-Python: >=3.12
22
+ Project-URL: Homepage, https://github.com/simonvanlierde/sourceledger
23
+ Project-URL: Issues, https://github.com/simonvanlierde/sourceledger/issues
24
+ Provides-Extra: xlsx
25
+ Description-Content-Type: text/markdown
26
+
27
+ # sourceledger
28
+
29
+ [![python](https://github.com/simonvanlierde/sourceledger/actions/workflows/python.yml/badge.svg)](https://github.com/simonvanlierde/sourceledger/actions/workflows/python.yml)
30
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
31
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.23460694.svg)](https://doi.org/10.5281/zenodo.23460694)
32
+ [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/simonvanlierde/sourceledger/badge)](https://scorecard.dev/viewer/?uri=github.com/simonvanlierde/sourceledger)
33
+ [![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://app.codspeed.io/simonvanlierde/sourceledger?utm_source=badge)
34
+
35
+ Build a dataset from public sources where every row says which download it came from.
36
+
37
+ A project lists its inputs in `sources.toml`: each source's URL, the publisher's reuse
38
+ terms, and whether the raw file may be republished. sourceledger runs three stages over it.
39
+ [How it works](docs/architecture.md) has the detail of each.
40
+
41
+ - `fetch` downloads each source into `data/raw/`, with a record beside it: the URL, the
42
+ time, the SHA-256 and size, the vintage, and the server's ETag and Last-Modified. An
43
+ unchanged source isn't downloaded again. Each new version is added to
44
+ `data/fetches.jsonl`.
45
+ - `build` writes `data/processed/<id>.csv` with five provenance columns in front, and a
46
+ [Frictionless Data Package](https://datapackage.org), `datapackage.json`, that describes
47
+ every output. The same records always give the same files.
48
+ - `validate` re-reads the outputs and lists every problem it finds, such as an output from
49
+ an older fetch, an edited raw file, or a source without reuse terms.
50
+
51
+ `init`, `status`, `attribution`, `verify` and `revisions` are commands around them: see
52
+ [Other commands](#other-commands).
53
+
54
+ There are no dependencies. Excel files need the optional `xlsx` extra. The
55
+ [docs page](https://simonvanlierde.github.io/sourceledger/) walks through a fetch and a
56
+ publisher's silent revision.
57
+
58
+ ## Install
59
+
60
+ ```sh
61
+ uv add sourceledger # or 'sourceledger[xlsx]' to read Excel files
62
+ ```
63
+
64
+ It needs Python 3.12 or later, and runs on Linux and macOS only (POSIX).
65
+
66
+ ### Verify a release
67
+
68
+ Each release is built and published by the
69
+ [release workflow](.github/workflows/python-release.yml), with no API token. PyPI keeps a
70
+ signed record of the workflow run that built each file. To check a wheel you downloaded,
71
+ pass its path:
72
+
73
+ ```sh
74
+ uvx pypi-attestations verify pypi --repository https://github.com/simonvanlierde/sourceledger \
75
+ sourceledger-0.3.0-py3-none-any.whl
76
+ ```
77
+
78
+ From 0.2.0, GitHub also keeps a signed build-provenance attestation for each file:
79
+
80
+ ```sh
81
+ gh attestation verify sourceledger-0.3.0-py3-none-any.whl --repo simonvanlierde/sourceledger
82
+ ```
83
+
84
+ Both fail unless the file is byte for byte the one this repository's workflow built.
85
+
86
+ ## Quick start
87
+
88
+ Write a `sources.toml` in the project root (`sourceledger init` writes a starter):
89
+
90
+ ```toml
91
+ [[source]]
92
+ id = "co2"
93
+ status = "implemented"
94
+ url = "https://raw.githubusercontent.com/datasets/co2-ppm/main/data/co2-annmean-mlo.csv"
95
+ vintage = "main branch, unversioned"
96
+ terms = "ODC-PDDL-1.0 (datasets/co2-ppm package; original data from NOAA GML)"
97
+ terms_url = "https://opendatacommons.org/licenses/pddl/"
98
+ license = "PDDL-1.0"
99
+ redistribute = true
100
+ ```
101
+
102
+ Then run the stages from that directory:
103
+
104
+ ```sh
105
+ sourceledger fetch
106
+ sourceledger build
107
+ sourceledger validate
108
+ ```
109
+
110
+ `sourceledger` reads CSV (gzipped or not), JSON and Excel, by each source's `format` or its
111
+ URL's extension. For other formats, see [Your own readers](#your-own-readers).
112
+
113
+ ## What to commit
114
+
115
+ Commit `sources.toml` and `data/fetches.jsonl`. The log holds checksums and URLs, not
116
+ data: it's the history of every version you have fetched.
117
+
118
+ Keep `data/raw/` and `data/processed/` out of git, so the repository never redistributes
119
+ a publisher's data. The `.fetch.json` records live in `data/raw/` too, so a fresh clone
120
+ runs `fetch` before `build` and `validate`.
121
+
122
+ ## sources.toml
123
+
124
+ Each `[[source]]` table takes these keys:
125
+
126
+ | Key | Required | Meaning |
127
+ | --- | --- | --- |
128
+ | `id` | yes | ASCII letters, digits, `_` and `-`. Names the raw file (`id` plus the URL's extension) and the output. |
129
+ | `name` | no | A title for people. The data package and `attribution` show it in place of the id. |
130
+ | `status` | yes | `"implemented"`, read by every stage, or `"unimplemented"`: a source you know about but don't read yet. `validate` fails if one of those has output. |
131
+ | `url` | for implemented sources | What `fetch` downloads. Change it and `build` refuses the old download until you fetch again. |
132
+ | `terms`, `terms_url` | for implemented sources | The publisher's reuse terms in their words, and where they are stated. |
133
+ | `license` | for implemented sources | The same terms as an [SPDX id](https://spdx.org/licenses/), such as `"CC-BY-4.0"`, or `"LicenseRef-<name>"` for terms with none. |
134
+ | `redistribute` | for implemented sources | `true` if the terms let you republish the raw file, `false` if not. |
135
+ | `vintage` | no | The edition you expect, such as `"2024 release"` or a TOML date. Recorded with each fetch and stamped on each row. `"not stated"` when absent. The old name `release` is refused. |
136
+ | `sha256` | no | The SHA-256 the download must have. Pin it for a fixed release, and `fetch` refuses bytes that changed upstream. |
137
+ | `max_bytes` | no | The largest download `fetch` accepts. 200 MiB unless set. A gzipped file or Excel workbook that unpacks past it is refused. |
138
+ | `format` | no | `"csv"`, `"json"` (an array of objects) or `"xlsx"`. Otherwise from the URL's extension: `.json`, `.xlsx`, and CSV for anything else. A gzipped CSV or JSON file is decompressed, within `max_bytes`. JSON numbers are written as they appear in the file (`1e5` stays `1e5`, `1.10` stays `1.10`). |
139
+ | `sheet` | no | The Excel sheet to read. The first unless set. A cell right of the last header cell is refused, and a missing cell in a row reads as `""`. |
140
+ | `headers` | no | Extra request headers, such as an API key, each value naming an environment variable: `{ Authorization = "env:EIA_TOKEN" }`. A literal value is refused, and so is a `url` that is not https. The record keeps the header names, not the values. A redirect to another scheme, host or port drops them. |
141
+ | `archive` | no | `true` to ask the Internet Archive's [Save Page Now](https://web.archive.org/save) to snapshot the URL after each fetch that brings new bytes. The record keeps the snapshot's address as `archive_url`. Only for http and https URLs, and not for a source that needs `headers`: the archive's request sends none. |
142
+ | `reason` | no | A note for people, such as why a source is unimplemented. sourceledger doesn't read it. |
143
+
144
+ `validate` reports a missing `terms`, `terms_url`, `license` or `redistribute`. The other
145
+ keys are checked when the manifest loads. Keys not listed here are kept for your own reader.
146
+
147
+ For checks in an editor with a TOML language server (Tombi, Taplo), put this line at the
148
+ top of `sources.toml`:
149
+
150
+ ```toml
151
+ #:schema https://raw.githubusercontent.com/simonvanlierde/sourceledger/main/src/sourceledger/sources.schema.json
152
+ ```
153
+
154
+ The schema accepts exactly what sourceledger accepts. A test keeps the two in step.
155
+
156
+ ## How fetch behaves
157
+
158
+ - It tries four times, waiting 5, 10 and 20 s between attempts, on 429, 500, 502, 503,
159
+ 504, network errors and a body cut short. A `Retry-After` header, in seconds or as a
160
+ date, raises the wait to at most 120 s. Any other error status fails at once.
161
+ - Each connect and read times out after 30 s, and one source's whole fetch after 300 s. The
162
+ 300 s holds against a server that sends bytes slowly, give or take one 30 s socket timeout.
163
+ - Every source is tried. When some fail, the others are still written, and the run
164
+ exits 1 with one line per failed source.
165
+ - A second fetch in the same project, while one runs, exits at once (a lock on
166
+ `data/raw/.lock`).
167
+ - Each line it prints says `new` or `unchanged`, against the last version in the log.
168
+ - For a source with `archive = true`, the Internet Archive request times out after 120 s
169
+ per connect and read. When it fails, fetch prints a warning, leaves `archive_url` empty,
170
+ and still succeeds. Save Page Now answers only once the snapshot is taken (49 s for a
171
+ GitHub page on 2026-10-20), and it captures one URL at most 5 times a day across all its
172
+ users. Past that it answers HTTP 429 with no `Retry-After`, and that fetch records no
173
+ snapshot.
174
+
175
+ The checks on redirects, compression and keys are in [Use sourceledger
176
+ safely](docs/secure-use.md). [How it works](docs/architecture.md#fetch) has the steps and
177
+ the crash safety.
178
+
179
+ ## Other commands
180
+
181
+ - `init` writes a starter `sources.toml` in the current directory, or at `--manifest PATH`:
182
+ the schema line and one example source, commented out. It won't overwrite an existing one.
183
+ - `revisions OLD_LOG` lists the sources whose bytes changed since an earlier copy of the
184
+ log. Copy the log before you fetch (`cp data/fetches.jsonl before.jsonl`), then run
185
+ `sourceledger revisions before.jsonl`. A weekly CI run can use it to report a
186
+ publisher's revision.
187
+ - `status` prints one line per source: its id, status and vintage, when its last logged
188
+ version was fetched (or `never`), the start of its SHA-256, its licence, and whether
189
+ the raw file may be republished. It reads the manifest and the log, never the network.
190
+ - `attribution` prints a Markdown table of the implemented sources, in manifest order:
191
+ name, terms in the publisher's words, licence, terms URL, and whether the raw file may be
192
+ republished. Paste it into a dataset's README or release notes.
193
+ - `verify DIR` is for someone who downloaded a published dataset. It reads
194
+ `DIR/datapackage.json` and checks that each file it lists exists, with the size and
195
+ SHA-256 it gives. It prints one line per problem and exits 1 if there are any. It needs
196
+ no `sources.toml` and no network.
197
+
198
+ Every command exits 1 on a failure or a problem. `revisions` exits 0 when sources changed,
199
+ by design: a test decides what a revision breaks.
200
+
201
+ `fetch`, `build` and `validate` take `--only ID`, as often as needed, to work on just those
202
+ sources. An id that is not in `sources.toml` is an error. `build --only` keeps the other
203
+ sources' entries in `datapackage.json` as the last build wrote them, and
204
+ `validate --only` skips the checks that cover the whole output.
205
+
206
+ Every command that reads the manifest reads `./sources.toml` unless given `--manifest PATH`.
207
+ The data folder stays `data/` in the current directory either way.
208
+
209
+ ## Your own readers
210
+
211
+ `sourceledger` reads every source with `parse_source`, which hands it to `parse_csv`,
212
+ `parse_json` or `parse_xlsx`. For another format, a project writes its own reader, a
213
+ function from a source's manifest table and raw bytes to columns and rows, and its own
214
+ command:
215
+
216
+ ```python
217
+ # src/my_dataset/pipeline.py
218
+ import sourceledger
219
+ from sourceledger import Source
220
+
221
+
222
+ def parse(source: Source, payload: bytes) -> tuple[list[str], list[dict[str, str]]]:
223
+ """Branch on source["id"] for a source in a format sourceledger doesn't read."""
224
+ return sourceledger.parse_source(source, payload)
225
+
226
+
227
+ def main(argv: list[str] | None = None) -> int:
228
+ return sourceledger.main(argv, parse=parse, prog="my-dataset", user_agent="my-dataset")
229
+ ```
230
+
231
+ ```toml
232
+ # pyproject.toml
233
+ [project.scripts]
234
+ my-dataset = "my_dataset.pipeline:main"
235
+ ```
236
+
237
+ `my-dataset fetch`, `build` and `validate` then use that reader, and publishers see
238
+ `my-dataset` as the User-Agent. A project that ships its `sources.toml` inside its package
239
+ passes the path as `manifest=Path(__file__).with_name("sources.toml")`. The
240
+ [project-templates](https://github.com/simonvanlierde/project-templates) `data-pipeline`
241
+ kind sets this up, with CI and a weekly revision report. The stages are also
242
+ functions: `load_manifest`, `fetch`, `build`, `validate`, `revisions`, `status`,
243
+ `attribution` and `verify`. `verified(source)` returns a source's fetch record and raw
244
+ bytes after checking that both are current, for a reader that needs them.
245
+
246
+ ## Compared with
247
+
248
+ Other tools cover parts of this. Use one of them when that part is all you need.
249
+
250
+ | Tool | What it does well | What sourceledger adds |
251
+ | --- | --- | --- |
252
+ | [pooch](https://www.fatiando.org/pooch/) | Downloads files with a known hash, caches them, retries. Made for packages that ship sample data. | A record of every fetch on disk, conditional re-fetches, a history of versions, provenance in each output row, and reuse terms checked per source. |
253
+ | [DVC](https://dvc.org) `import-url` | Tracks a downloaded file's URL with its checksum or ETag, and `dvc update` notices a change. The closest overlap. | No cache or `.dvc` files to adopt, reuse terms per source, provenance in each output row, and a Data Package for the outputs. |
254
+ | [DataLad](https://www.datalad.org) `download-url` | Records where every file came from in git and git-annex, as part of full dataset version control. | A plain manifest and one committed log, no git-annex, and reuse terms per source. |
255
+ | [frictionless-py](https://framework.frictionlessdata.io) | Describes and validates Data Packages, with licences, sources and hashes per resource. | The fetch, verify and revision steps. `build` writes a Data Package, so the two work together. |
256
+
257
+ Checked against each project's code and docs on 2026-10-18.
258
+
259
+ ## Stability
260
+
261
+ Until 1.0, the public API is `sourceledger`'s `__all__`, the keys of `sources.toml`, and
262
+ the format of fetch records and the log. A breaking change to any of them comes only in a
263
+ minor version, listed in [CHANGELOG.md](CHANGELOG.md), with a warning first where that's
264
+ cheap. Older fetch logs stay readable.
265
+
266
+ ## Roadmap
267
+
268
+ - A `release` stage that writes the metadata for a dataset release (RO-Crate, Croissant,
269
+ CodeMeta, Zenodo and a `SHA256SUMS` file), checks each with its official validator, and
270
+ keeps the data of `redistribute = false` sources out of the release.
271
+ - An error class per kind of failure, under `ValueError` and `OSError`, once the release
272
+ stage needs to tell them apart. This will not break code that catches the base classes.
273
+ - A review by [pyOpenSci](https://www.pyopensci.org) once the project has users besides
274
+ its own template.
275
+
276
+ Ideas and requests are welcome as [issues](https://github.com/simonvanlierde/sourceledger/issues).
277
+
278
+ ## Development
279
+
280
+ ```sh
281
+ uv sync
282
+ uv tool install prek # once per machine
283
+ prek install # git hooks
284
+ just check # lint, types and tests, as CI runs them
285
+ ```
286
+
287
+ [CONTRIBUTING.md](.github/CONTRIBUTING.md) has the coding standard, the test policy and
288
+ how dependencies are chosen. [GOVERNANCE.md](GOVERNANCE.md) says who decides what.
289
+ [docs/architecture.md](docs/architecture.md) explains how the stages fit together, and
290
+ [docs/secure-use.md](docs/secure-use.md) what sourceledger checks for you. Report a
291
+ vulnerability as [SECURITY.md](.github/SECURITY.md) describes.
292
+
293
+ The tests never touch the network, except those marked `live`. The mutation-testing run
294
+ (`just py-mutation`, about 2 minutes) and its baseline are explained in
295
+ [CONTRIBUTING.md](.github/CONTRIBUTING.md#tests).
296
+
297
+ ## License
298
+
299
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,273 @@
1
+ # sourceledger
2
+
3
+ [![python](https://github.com/simonvanlierde/sourceledger/actions/workflows/python.yml/badge.svg)](https://github.com/simonvanlierde/sourceledger/actions/workflows/python.yml)
4
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
5
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.23460694.svg)](https://doi.org/10.5281/zenodo.23460694)
6
+ [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/simonvanlierde/sourceledger/badge)](https://scorecard.dev/viewer/?uri=github.com/simonvanlierde/sourceledger)
7
+ [![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://app.codspeed.io/simonvanlierde/sourceledger?utm_source=badge)
8
+
9
+ Build a dataset from public sources where every row says which download it came from.
10
+
11
+ A project lists its inputs in `sources.toml`: each source's URL, the publisher's reuse
12
+ terms, and whether the raw file may be republished. sourceledger runs three stages over it.
13
+ [How it works](docs/architecture.md) has the detail of each.
14
+
15
+ - `fetch` downloads each source into `data/raw/`, with a record beside it: the URL, the
16
+ time, the SHA-256 and size, the vintage, and the server's ETag and Last-Modified. An
17
+ unchanged source isn't downloaded again. Each new version is added to
18
+ `data/fetches.jsonl`.
19
+ - `build` writes `data/processed/<id>.csv` with five provenance columns in front, and a
20
+ [Frictionless Data Package](https://datapackage.org), `datapackage.json`, that describes
21
+ every output. The same records always give the same files.
22
+ - `validate` re-reads the outputs and lists every problem it finds, such as an output from
23
+ an older fetch, an edited raw file, or a source without reuse terms.
24
+
25
+ `init`, `status`, `attribution`, `verify` and `revisions` are commands around them: see
26
+ [Other commands](#other-commands).
27
+
28
+ There are no dependencies. Excel files need the optional `xlsx` extra. The
29
+ [docs page](https://simonvanlierde.github.io/sourceledger/) walks through a fetch and a
30
+ publisher's silent revision.
31
+
32
+ ## Install
33
+
34
+ ```sh
35
+ uv add sourceledger # or 'sourceledger[xlsx]' to read Excel files
36
+ ```
37
+
38
+ It needs Python 3.12 or later, and runs on Linux and macOS only (POSIX).
39
+
40
+ ### Verify a release
41
+
42
+ Each release is built and published by the
43
+ [release workflow](.github/workflows/python-release.yml), with no API token. PyPI keeps a
44
+ signed record of the workflow run that built each file. To check a wheel you downloaded,
45
+ pass its path:
46
+
47
+ ```sh
48
+ uvx pypi-attestations verify pypi --repository https://github.com/simonvanlierde/sourceledger \
49
+ sourceledger-0.3.0-py3-none-any.whl
50
+ ```
51
+
52
+ From 0.2.0, GitHub also keeps a signed build-provenance attestation for each file:
53
+
54
+ ```sh
55
+ gh attestation verify sourceledger-0.3.0-py3-none-any.whl --repo simonvanlierde/sourceledger
56
+ ```
57
+
58
+ Both fail unless the file is byte for byte the one this repository's workflow built.
59
+
60
+ ## Quick start
61
+
62
+ Write a `sources.toml` in the project root (`sourceledger init` writes a starter):
63
+
64
+ ```toml
65
+ [[source]]
66
+ id = "co2"
67
+ status = "implemented"
68
+ url = "https://raw.githubusercontent.com/datasets/co2-ppm/main/data/co2-annmean-mlo.csv"
69
+ vintage = "main branch, unversioned"
70
+ terms = "ODC-PDDL-1.0 (datasets/co2-ppm package; original data from NOAA GML)"
71
+ terms_url = "https://opendatacommons.org/licenses/pddl/"
72
+ license = "PDDL-1.0"
73
+ redistribute = true
74
+ ```
75
+
76
+ Then run the stages from that directory:
77
+
78
+ ```sh
79
+ sourceledger fetch
80
+ sourceledger build
81
+ sourceledger validate
82
+ ```
83
+
84
+ `sourceledger` reads CSV (gzipped or not), JSON and Excel, by each source's `format` or its
85
+ URL's extension. For other formats, see [Your own readers](#your-own-readers).
86
+
87
+ ## What to commit
88
+
89
+ Commit `sources.toml` and `data/fetches.jsonl`. The log holds checksums and URLs, not
90
+ data: it's the history of every version you have fetched.
91
+
92
+ Keep `data/raw/` and `data/processed/` out of git, so the repository never redistributes
93
+ a publisher's data. The `.fetch.json` records live in `data/raw/` too, so a fresh clone
94
+ runs `fetch` before `build` and `validate`.
95
+
96
+ ## sources.toml
97
+
98
+ Each `[[source]]` table takes these keys:
99
+
100
+ | Key | Required | Meaning |
101
+ | --- | --- | --- |
102
+ | `id` | yes | ASCII letters, digits, `_` and `-`. Names the raw file (`id` plus the URL's extension) and the output. |
103
+ | `name` | no | A title for people. The data package and `attribution` show it in place of the id. |
104
+ | `status` | yes | `"implemented"`, read by every stage, or `"unimplemented"`: a source you know about but don't read yet. `validate` fails if one of those has output. |
105
+ | `url` | for implemented sources | What `fetch` downloads. Change it and `build` refuses the old download until you fetch again. |
106
+ | `terms`, `terms_url` | for implemented sources | The publisher's reuse terms in their words, and where they are stated. |
107
+ | `license` | for implemented sources | The same terms as an [SPDX id](https://spdx.org/licenses/), such as `"CC-BY-4.0"`, or `"LicenseRef-<name>"` for terms with none. |
108
+ | `redistribute` | for implemented sources | `true` if the terms let you republish the raw file, `false` if not. |
109
+ | `vintage` | no | The edition you expect, such as `"2024 release"` or a TOML date. Recorded with each fetch and stamped on each row. `"not stated"` when absent. The old name `release` is refused. |
110
+ | `sha256` | no | The SHA-256 the download must have. Pin it for a fixed release, and `fetch` refuses bytes that changed upstream. |
111
+ | `max_bytes` | no | The largest download `fetch` accepts. 200 MiB unless set. A gzipped file or Excel workbook that unpacks past it is refused. |
112
+ | `format` | no | `"csv"`, `"json"` (an array of objects) or `"xlsx"`. Otherwise from the URL's extension: `.json`, `.xlsx`, and CSV for anything else. A gzipped CSV or JSON file is decompressed, within `max_bytes`. JSON numbers are written as they appear in the file (`1e5` stays `1e5`, `1.10` stays `1.10`). |
113
+ | `sheet` | no | The Excel sheet to read. The first unless set. A cell right of the last header cell is refused, and a missing cell in a row reads as `""`. |
114
+ | `headers` | no | Extra request headers, such as an API key, each value naming an environment variable: `{ Authorization = "env:EIA_TOKEN" }`. A literal value is refused, and so is a `url` that is not https. The record keeps the header names, not the values. A redirect to another scheme, host or port drops them. |
115
+ | `archive` | no | `true` to ask the Internet Archive's [Save Page Now](https://web.archive.org/save) to snapshot the URL after each fetch that brings new bytes. The record keeps the snapshot's address as `archive_url`. Only for http and https URLs, and not for a source that needs `headers`: the archive's request sends none. |
116
+ | `reason` | no | A note for people, such as why a source is unimplemented. sourceledger doesn't read it. |
117
+
118
+ `validate` reports a missing `terms`, `terms_url`, `license` or `redistribute`. The other
119
+ keys are checked when the manifest loads. Keys not listed here are kept for your own reader.
120
+
121
+ For checks in an editor with a TOML language server (Tombi, Taplo), put this line at the
122
+ top of `sources.toml`:
123
+
124
+ ```toml
125
+ #:schema https://raw.githubusercontent.com/simonvanlierde/sourceledger/main/src/sourceledger/sources.schema.json
126
+ ```
127
+
128
+ The schema accepts exactly what sourceledger accepts. A test keeps the two in step.
129
+
130
+ ## How fetch behaves
131
+
132
+ - It tries four times, waiting 5, 10 and 20 s between attempts, on 429, 500, 502, 503,
133
+ 504, network errors and a body cut short. A `Retry-After` header, in seconds or as a
134
+ date, raises the wait to at most 120 s. Any other error status fails at once.
135
+ - Each connect and read times out after 30 s, and one source's whole fetch after 300 s. The
136
+ 300 s holds against a server that sends bytes slowly, give or take one 30 s socket timeout.
137
+ - Every source is tried. When some fail, the others are still written, and the run
138
+ exits 1 with one line per failed source.
139
+ - A second fetch in the same project, while one runs, exits at once (a lock on
140
+ `data/raw/.lock`).
141
+ - Each line it prints says `new` or `unchanged`, against the last version in the log.
142
+ - For a source with `archive = true`, the Internet Archive request times out after 120 s
143
+ per connect and read. When it fails, fetch prints a warning, leaves `archive_url` empty,
144
+ and still succeeds. Save Page Now answers only once the snapshot is taken (49 s for a
145
+ GitHub page on 2026-10-20), and it captures one URL at most 5 times a day across all its
146
+ users. Past that it answers HTTP 429 with no `Retry-After`, and that fetch records no
147
+ snapshot.
148
+
149
+ The checks on redirects, compression and keys are in [Use sourceledger
150
+ safely](docs/secure-use.md). [How it works](docs/architecture.md#fetch) has the steps and
151
+ the crash safety.
152
+
153
+ ## Other commands
154
+
155
+ - `init` writes a starter `sources.toml` in the current directory, or at `--manifest PATH`:
156
+ the schema line and one example source, commented out. It won't overwrite an existing one.
157
+ - `revisions OLD_LOG` lists the sources whose bytes changed since an earlier copy of the
158
+ log. Copy the log before you fetch (`cp data/fetches.jsonl before.jsonl`), then run
159
+ `sourceledger revisions before.jsonl`. A weekly CI run can use it to report a
160
+ publisher's revision.
161
+ - `status` prints one line per source: its id, status and vintage, when its last logged
162
+ version was fetched (or `never`), the start of its SHA-256, its licence, and whether
163
+ the raw file may be republished. It reads the manifest and the log, never the network.
164
+ - `attribution` prints a Markdown table of the implemented sources, in manifest order:
165
+ name, terms in the publisher's words, licence, terms URL, and whether the raw file may be
166
+ republished. Paste it into a dataset's README or release notes.
167
+ - `verify DIR` is for someone who downloaded a published dataset. It reads
168
+ `DIR/datapackage.json` and checks that each file it lists exists, with the size and
169
+ SHA-256 it gives. It prints one line per problem and exits 1 if there are any. It needs
170
+ no `sources.toml` and no network.
171
+
172
+ Every command exits 1 on a failure or a problem. `revisions` exits 0 when sources changed,
173
+ by design: a test decides what a revision breaks.
174
+
175
+ `fetch`, `build` and `validate` take `--only ID`, as often as needed, to work on just those
176
+ sources. An id that is not in `sources.toml` is an error. `build --only` keeps the other
177
+ sources' entries in `datapackage.json` as the last build wrote them, and
178
+ `validate --only` skips the checks that cover the whole output.
179
+
180
+ Every command that reads the manifest reads `./sources.toml` unless given `--manifest PATH`.
181
+ The data folder stays `data/` in the current directory either way.
182
+
183
+ ## Your own readers
184
+
185
+ `sourceledger` reads every source with `parse_source`, which hands it to `parse_csv`,
186
+ `parse_json` or `parse_xlsx`. For another format, a project writes its own reader, a
187
+ function from a source's manifest table and raw bytes to columns and rows, and its own
188
+ command:
189
+
190
+ ```python
191
+ # src/my_dataset/pipeline.py
192
+ import sourceledger
193
+ from sourceledger import Source
194
+
195
+
196
+ def parse(source: Source, payload: bytes) -> tuple[list[str], list[dict[str, str]]]:
197
+ """Branch on source["id"] for a source in a format sourceledger doesn't read."""
198
+ return sourceledger.parse_source(source, payload)
199
+
200
+
201
+ def main(argv: list[str] | None = None) -> int:
202
+ return sourceledger.main(argv, parse=parse, prog="my-dataset", user_agent="my-dataset")
203
+ ```
204
+
205
+ ```toml
206
+ # pyproject.toml
207
+ [project.scripts]
208
+ my-dataset = "my_dataset.pipeline:main"
209
+ ```
210
+
211
+ `my-dataset fetch`, `build` and `validate` then use that reader, and publishers see
212
+ `my-dataset` as the User-Agent. A project that ships its `sources.toml` inside its package
213
+ passes the path as `manifest=Path(__file__).with_name("sources.toml")`. The
214
+ [project-templates](https://github.com/simonvanlierde/project-templates) `data-pipeline`
215
+ kind sets this up, with CI and a weekly revision report. The stages are also
216
+ functions: `load_manifest`, `fetch`, `build`, `validate`, `revisions`, `status`,
217
+ `attribution` and `verify`. `verified(source)` returns a source's fetch record and raw
218
+ bytes after checking that both are current, for a reader that needs them.
219
+
220
+ ## Compared with
221
+
222
+ Other tools cover parts of this. Use one of them when that part is all you need.
223
+
224
+ | Tool | What it does well | What sourceledger adds |
225
+ | --- | --- | --- |
226
+ | [pooch](https://www.fatiando.org/pooch/) | Downloads files with a known hash, caches them, retries. Made for packages that ship sample data. | A record of every fetch on disk, conditional re-fetches, a history of versions, provenance in each output row, and reuse terms checked per source. |
227
+ | [DVC](https://dvc.org) `import-url` | Tracks a downloaded file's URL with its checksum or ETag, and `dvc update` notices a change. The closest overlap. | No cache or `.dvc` files to adopt, reuse terms per source, provenance in each output row, and a Data Package for the outputs. |
228
+ | [DataLad](https://www.datalad.org) `download-url` | Records where every file came from in git and git-annex, as part of full dataset version control. | A plain manifest and one committed log, no git-annex, and reuse terms per source. |
229
+ | [frictionless-py](https://framework.frictionlessdata.io) | Describes and validates Data Packages, with licences, sources and hashes per resource. | The fetch, verify and revision steps. `build` writes a Data Package, so the two work together. |
230
+
231
+ Checked against each project's code and docs on 2026-10-18.
232
+
233
+ ## Stability
234
+
235
+ Until 1.0, the public API is `sourceledger`'s `__all__`, the keys of `sources.toml`, and
236
+ the format of fetch records and the log. A breaking change to any of them comes only in a
237
+ minor version, listed in [CHANGELOG.md](CHANGELOG.md), with a warning first where that's
238
+ cheap. Older fetch logs stay readable.
239
+
240
+ ## Roadmap
241
+
242
+ - A `release` stage that writes the metadata for a dataset release (RO-Crate, Croissant,
243
+ CodeMeta, Zenodo and a `SHA256SUMS` file), checks each with its official validator, and
244
+ keeps the data of `redistribute = false` sources out of the release.
245
+ - An error class per kind of failure, under `ValueError` and `OSError`, once the release
246
+ stage needs to tell them apart. This will not break code that catches the base classes.
247
+ - A review by [pyOpenSci](https://www.pyopensci.org) once the project has users besides
248
+ its own template.
249
+
250
+ Ideas and requests are welcome as [issues](https://github.com/simonvanlierde/sourceledger/issues).
251
+
252
+ ## Development
253
+
254
+ ```sh
255
+ uv sync
256
+ uv tool install prek # once per machine
257
+ prek install # git hooks
258
+ just check # lint, types and tests, as CI runs them
259
+ ```
260
+
261
+ [CONTRIBUTING.md](.github/CONTRIBUTING.md) has the coding standard, the test policy and
262
+ how dependencies are chosen. [GOVERNANCE.md](GOVERNANCE.md) says who decides what.
263
+ [docs/architecture.md](docs/architecture.md) explains how the stages fit together, and
264
+ [docs/secure-use.md](docs/secure-use.md) what sourceledger checks for you. Report a
265
+ vulnerability as [SECURITY.md](.github/SECURITY.md) describes.
266
+
267
+ The tests never touch the network, except those marked `live`. The mutation-testing run
268
+ (`just py-mutation`, about 2 minutes) and its baseline are explained in
269
+ [CONTRIBUTING.md](.github/CONTRIBUTING.md#tests).
270
+
271
+ ## License
272
+
273
+ MIT. See [LICENSE](LICENSE).
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sourceledger"
3
- version = "0.1.0"
3
+ version = "0.3.0"
4
4
  description = "Fetch, verify and validate the public sources a dataset is built from, with a provenance record for every download"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -17,7 +17,12 @@ keywords = [
17
17
  classifiers = [
18
18
  "Development Status :: 3 - Alpha",
19
19
  "Intended Audience :: Science/Research",
20
+ "Operating System :: POSIX",
20
21
  "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Programming Language :: Python :: 3.14",
25
+ "Programming Language :: Python :: 3.15",
21
26
  "Topic :: Scientific/Engineering",
22
27
  "Typing :: Typed",
23
28
  ]
@@ -25,7 +30,7 @@ dependencies = []
25
30
 
26
31
  [[project.authors]]
27
32
  name = "Simon van Lierde"
28
- email = "90462640+simonvanlierde@users.noreply.github.com"
33
+ email = "code@duinlab.nl"
29
34
 
30
35
  [project.scripts]
31
36
  sourceledger = "sourceledger.cli:main"
@@ -34,13 +39,22 @@ sourceledger = "sourceledger.cli:main"
34
39
  Homepage = "https://github.com/simonvanlierde/sourceledger"
35
40
  Issues = "https://github.com/simonvanlierde/sourceledger/issues"
36
41
 
42
+ [project.optional-dependencies]
43
+ xlsx = ["openpyxl>=3.1"]
44
+
37
45
  [dependency-groups]
38
46
  dev = [
47
+ "frictionless>=5.20",
48
+ "hypothesis>=6.170.1",
49
+ "jsonschema>=4.26",
50
+ "openpyxl>=3.1",
39
51
  "pytest>=9.1.1",
52
+ "pytest-codspeed>=5.0.3",
40
53
  "pytest-cov>=7.1.0",
41
54
  "ruff>=0.16.10",
42
55
  "ty>=0.0.84",
43
56
  ]
57
+ mutation = ["mutmut>=3.8.0"]
44
58
 
45
59
  [build-system]
46
60
  requires = ["uv_build>=0.12.19,<0.14.0"]
@@ -93,7 +107,10 @@ addopts = [
93
107
  "--strict-config",
94
108
  "--strict-markers",
95
109
  "--import-mode=importlib",
110
+ "-m",
111
+ "not live",
96
112
  ]
113
+ markers = ["live: calls a real external service; deselected unless run with -m live"]
97
114
  testpaths = ["tests"]
98
115
 
99
116
  [tool.coverage.run]
@@ -102,7 +119,16 @@ branch = true
102
119
 
103
120
  [tool.coverage.report]
104
121
  show_missing = true
122
+ fail_under = 95
105
123
  exclude_also = [
106
124
  "if TYPE_CHECKING:",
107
125
  "raise NotImplementedError",
108
126
  ]
127
+
128
+ [tool.mutmut]
129
+ source_paths = ["src"]
130
+ also_copy = [
131
+ "README.md",
132
+ "docs",
133
+ ]
134
+ do_not_mutate_patterns = ['^\s*_logger\.\w+\(']