sourceledger 0.5.1__tar.gz → 0.7.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sourceledger
3
- Version: 0.5.1
3
+ Version: 0.7.0
4
4
  Summary: Fetch or record a dataset's public files with provenance, and release it without files whose terms forbid it
5
5
  Keywords: provenance,data pipeline,reproducibility,open data,research software,FAIR
6
6
  Author: Simon van Lierde
@@ -52,6 +52,8 @@ CSV, JSON, Excel or binary files. List the sources and reuse terms in `sources.t
52
52
 
53
53
  Each table row identifies its download. A `datapackage.json` describes the outputs.
54
54
  The [documentation website](https://simonvanlierde.com/sourceledger/) follows a source through a publisher's revision.
55
+ In Python, [`align_rows()`](docs/recipes.md#compare-two-versions-by-row-key) matches two versions by the keys you name,
56
+ keeping the full rows and their provenance. Your code decides which values count as changed.
55
57
 
56
58
  ## Install
57
59
 
@@ -147,9 +149,6 @@ cheap. Older fetch logs stay readable. A record written by `record()`, or with
147
149
 
148
150
  - An error class per kind of failure in the other stages, under `ValueError` and `OSError`,
149
151
  once a caller needs to tell them apart. This will not break code that catches the base classes.
150
- - Comparing two versions of a table row by row, with the row keys the caller names, so a
151
- revision shows which values changed and not only that the bytes did. It starts as a pilot on
152
- real data, the archived World Development Indicators releases, before it becomes an API.
153
152
  - A review by [pyOpenSci](https://www.pyopensci.org) once the project has users besides
154
153
  its own template.
155
154
 
@@ -161,18 +160,32 @@ Not planned:
161
160
 
162
161
  Ideas and requests are welcome as [issues](https://github.com/simonvanlierde/sourceledger/issues).
163
162
 
163
+ ## Social card
164
+
165
+ [`docs/assets/og.png`](docs/assets/og.png) is the 1200x630 card shown when the link is shared. It is rendered from
166
+ [`docs/og-card.html`](docs/og-card.html), which draws the chain of custody of the revision example: the 2023 value
167
+ and the SHA-256 of the download, each before and after the publisher's change. Edit the HTML, then run:
168
+
169
+ ```sh
170
+ just og-card
171
+ ```
172
+
173
+ Zensical serves `docs/assets/`, and `docs-overrides/main.html` holds the tags.
174
+ Rendering a new card needs Node and pnpm for the Playwright screenshot command.
175
+
164
176
  ## Development
165
177
 
166
- You need [uv](https://docs.astral.sh/uv/), [just](https://just.systems), and Node 26 with
167
- [pnpm](https://pnpm.io) for the documentation site, which `just check` also builds.
178
+ You need [uv](https://docs.astral.sh/uv/) and [just](https://just.systems).
179
+ `just check` also builds the documentation site.
168
180
 
169
181
  ```sh
170
182
  uv sync
171
183
  just check
172
184
  ```
173
185
 
174
- The site is built with Astro from the Markdown in `docs/`. `just site` builds it into
175
- `site/dist/`, and every push to `main` deploys it to GitHub Pages at
186
+ Zensical builds the Markdown in `docs/`, including search and the Python API reference.
187
+ `just docs` builds it into `_docs/` in strict mode. `just docs-serve` previews it locally.
188
+ Every push to `main` deploys it to GitHub Pages at
176
189
  <https://simonvanlierde.com/sourceledger/>.
177
190
 
178
191
  [Contributing](.github/CONTRIBUTING.md) covers hooks, coding standards and tests.
@@ -20,6 +20,8 @@ CSV, JSON, Excel or binary files. List the sources and reuse terms in `sources.t
20
20
 
21
21
  Each table row identifies its download. A `datapackage.json` describes the outputs.
22
22
  The [documentation website](https://simonvanlierde.com/sourceledger/) follows a source through a publisher's revision.
23
+ In Python, [`align_rows()`](docs/recipes.md#compare-two-versions-by-row-key) matches two versions by the keys you name,
24
+ keeping the full rows and their provenance. Your code decides which values count as changed.
23
25
 
24
26
  ## Install
25
27
 
@@ -115,9 +117,6 @@ cheap. Older fetch logs stay readable. A record written by `record()`, or with
115
117
 
116
118
  - An error class per kind of failure in the other stages, under `ValueError` and `OSError`,
117
119
  once a caller needs to tell them apart. This will not break code that catches the base classes.
118
- - Comparing two versions of a table row by row, with the row keys the caller names, so a
119
- revision shows which values changed and not only that the bytes did. It starts as a pilot on
120
- real data, the archived World Development Indicators releases, before it becomes an API.
121
120
  - A review by [pyOpenSci](https://www.pyopensci.org) once the project has users besides
122
121
  its own template.
123
122
 
@@ -129,18 +128,32 @@ Not planned:
129
128
 
130
129
  Ideas and requests are welcome as [issues](https://github.com/simonvanlierde/sourceledger/issues).
131
130
 
131
+ ## Social card
132
+
133
+ [`docs/assets/og.png`](docs/assets/og.png) is the 1200x630 card shown when the link is shared. It is rendered from
134
+ [`docs/og-card.html`](docs/og-card.html), which draws the chain of custody of the revision example: the 2023 value
135
+ and the SHA-256 of the download, each before and after the publisher's change. Edit the HTML, then run:
136
+
137
+ ```sh
138
+ just og-card
139
+ ```
140
+
141
+ Zensical serves `docs/assets/`, and `docs-overrides/main.html` holds the tags.
142
+ Rendering a new card needs Node and pnpm for the Playwright screenshot command.
143
+
132
144
  ## Development
133
145
 
134
- You need [uv](https://docs.astral.sh/uv/), [just](https://just.systems), and Node 26 with
135
- [pnpm](https://pnpm.io) for the documentation site, which `just check` also builds.
146
+ You need [uv](https://docs.astral.sh/uv/) and [just](https://just.systems).
147
+ `just check` also builds the documentation site.
136
148
 
137
149
  ```sh
138
150
  uv sync
139
151
  just check
140
152
  ```
141
153
 
142
- The site is built with Astro from the Markdown in `docs/`. `just site` builds it into
143
- `site/dist/`, and every push to `main` deploys it to GitHub Pages at
154
+ Zensical builds the Markdown in `docs/`, including search and the Python API reference.
155
+ `just docs` builds it into `_docs/` in strict mode. `just docs-serve` previews it locally.
156
+ Every push to `main` deploys it to GitHub Pages at
144
157
  <https://simonvanlierde.com/sourceledger/>.
145
158
 
146
159
  [Contributing](.github/CONTRIBUTING.md) covers hooks, coding standards and tests.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sourceledger"
3
- version = "0.5.1"
3
+ version = "0.7.0"
4
4
  description = "Fetch or record a dataset's public files with provenance, and release it without files whose terms forbid it"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -131,18 +131,24 @@ exclude = ["docs/examples"]
131
131
  [tool.ty.terminal]
132
132
  error-on-warning = false
133
133
 
134
- [tool.pytest.ini_options]
134
+ [tool.pytest]
135
+ minversion = "9.0"
135
136
  addopts = [
136
137
  "-ra",
137
- "--strict-config",
138
- "--strict-markers",
139
138
  "--import-mode=importlib",
140
139
  "-m",
141
140
  "not live",
142
141
  ]
143
142
  markers = ["live: calls a real external service; deselected unless run with -m live"]
144
- testpaths = ["tests"]
145
143
  pythonpath = ["tests"]
144
+ testpaths = ["tests"]
145
+ strict = true
146
+ filterwarnings = [
147
+ "error",
148
+ 'ignore:os.path.commonprefix\(\) is deprecated:DeprecationWarning',
149
+ "ignore:ConjunctiveGraph is deprecated:DeprecationWarning:rdflib.plugins.parsers.jsonld",
150
+ ]
151
+ log_level = "INFO"
146
152
 
147
153
  [tool.coverage.run]
148
154
  source = ["src"]
@@ -166,3 +172,14 @@ also_copy = [
166
172
  "src/sourceledger/sources.schema.json",
167
173
  ]
168
174
  do_not_mutate_patterns = ['^\s*_logger\.\w+\(']
175
+
176
+ [tool.repo-review.ignore]
177
+ PY007 = "just is the task runner: `just check` runs what CI runs"
178
+ PC110 = "ruff format runs as a local hook from the venv, the same ruff CI runs"
179
+ PC140 = "ty runs as a local hook from the venv"
180
+ PC170 = "no reStructuredText: rumdl checks the Markdown docs"
181
+ PC190 = "ruff check runs as a local hook from the venv"
182
+ PC901 = "the hooks run in CI's checks workflow, not on pre-commit.ci"
183
+ PC902 = "the hooks run in CI's checks workflow, not on pre-commit.ci"
184
+ PC903 = "the hooks run in CI's checks workflow, not on pre-commit.ci"
185
+ GH200 = "Renovate updates the actions and hook revs (renovate.json5)"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sourceledger"
3
- version = "0.5.1"
3
+ version = "0.7.0"
4
4
  description = "Fetch or record a dataset's public files with provenance, and release it without files whose terms forbid it"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -124,13 +124,25 @@ exclude = ["docs/examples"]
124
124
  [tool.ty.terminal]
125
125
  error-on-warning = false
126
126
 
127
- [tool.pytest.ini_options]
128
- # `live` tests reach real services; `pytest -m live` runs only them (the last -m wins).
129
- addopts = ["-ra", "--strict-config", "--strict-markers", "--import-mode=importlib", "-m", "not live"]
127
+ # pytest 9's native table. strict turns unknown config keys, unknown markers and
128
+ # an xfail test that passes into errors, and filterwarnings turns warnings into
129
+ # failures, so a deprecation shows up in CI, not after the upgrade that removes it.
130
+ [tool.pytest]
131
+ minversion = "9.0"
132
+ addopts = ["-ra", "--import-mode=importlib", "-m", "not live"]
130
133
  markers = ["live: calls a real external service; deselected unless run with -m live"]
131
- testpaths = ["tests"]
132
134
  # The test modules import their shared helpers from tests/support.py.
133
135
  pythonpath = ["tests"]
136
+ testpaths = ["tests"]
137
+ strict = true
138
+ filterwarnings = [
139
+ "error",
140
+ # frictionless and its validators still call the deprecated helper on Python 3.15.
141
+ "ignore:os.path.commonprefix\\(\\) is deprecated:DeprecationWarning",
142
+ # rdflib's JSON-LD parser constructs its own deprecated graph class.
143
+ "ignore:ConjunctiveGraph is deprecated:DeprecationWarning:rdflib.plugins.parsers.jsonld",
144
+ ]
145
+ log_level = "INFO"
134
146
 
135
147
  # Coverage is opt-in via `pytest --cov` (`just py-cov`), not addopts, because it
136
148
  # slows every plain test run. CI's run fails under the fail_under below.
@@ -153,3 +165,16 @@ source_paths = ["src"]
153
165
  also_copy = ["README.md", "docs", ".github/scripts", "src/sourceledger/sources.schema.json"]
154
166
  # Log calls only change diagnostics; their mutants would all survive.
155
167
  do_not_mutate_patterns = ['^\s*_logger\.\w+\(']
168
+
169
+ # Checks from Scientific Python's repo-review (the sp-repo-review git hook) that this
170
+ # project skips on purpose. `prek run sp-repo-review --all-files` runs the rest.
171
+ [tool.repo-review.ignore]
172
+ PY007 = "just is the task runner: `just check` runs what CI runs"
173
+ PC110 = "ruff format runs as a local hook from the venv, the same ruff CI runs"
174
+ PC140 = "ty runs as a local hook from the venv"
175
+ PC170 = "no reStructuredText: rumdl checks the Markdown docs"
176
+ PC190 = "ruff check runs as a local hook from the venv"
177
+ PC901 = "the hooks run in CI's checks workflow, not on pre-commit.ci"
178
+ PC902 = "the hooks run in CI's checks workflow, not on pre-commit.ci"
179
+ PC903 = "the hooks run in CI's checks workflow, not on pre-commit.ci"
180
+ GH200 = "Renovate updates the actions and hook revs (renovate.json5)"
@@ -9,8 +9,9 @@ changelog. The submodules, such as `sourceledger.ledger`, may change in any rele
9
9
 
10
10
  from sourceledger.builder import Parser, build, parse_csv, parse_json, parse_source, parse_xlsx, read_output
11
11
  from sourceledger.cli import entry, main
12
+ from sourceledger.comparison import align_rows
12
13
  from sourceledger.fetcher import USER_AGENT, fetch
13
- from sourceledger.ledger import FetchError, FetchRecord, record, revisions, verified
14
+ from sourceledger.ledger import FetchBusyError, FetchError, FetchRecord, record, revisions, verified
14
15
  from sourceledger.manifest import Source, load_manifest, load_output_format, load_package_name, load_release_formats
15
16
  from sourceledger.releaser import (
16
17
  ReleaseError,
@@ -25,6 +26,7 @@ from sourceledger.report import attribution, report, status, validate, verify
25
26
 
26
27
  __all__ = [
27
28
  "USER_AGENT",
29
+ "FetchBusyError",
28
30
  "FetchError",
29
31
  "FetchRecord",
30
32
  "Parser",
@@ -34,6 +36,7 @@ __all__ = [
34
36
  "ValidatorCouldNotRun",
35
37
  "ValidatorFailed",
36
38
  "ValidatorMissing",
39
+ "align_rows",
37
40
  "attribution",
38
41
  "build",
39
42
  "check_release",
@@ -335,6 +335,7 @@ def build(
335
335
  only: Collection[str] | None = None,
336
336
  output_format: str = "csv",
337
337
  name: str | None = None,
338
+ build_revision: str | None = None,
338
339
  ) -> list[Path]:
339
340
  """Write implemented sources to `processed`: unchanged files, or tables stamped with their fetch records.
340
341
 
@@ -345,21 +346,43 @@ def build(
345
346
  Then writes `processed/datapackage.json`, a Frictionless Data Package (v2) describing every
346
347
  output: its columns, size and SHA-256, its source URL and licence, and its fetch record.
347
348
  The package is called `name`; without one, the `[project]` name in ./pyproject.toml, or else
348
- the current folder's name, either made lower case. The same records and name give the same
349
- file, byte for byte.
349
+ the current folder's name, either made lower case. `build_revision`, if supplied, records
350
+ the code revision in each rebuilt resource. Identical inputs and revision give the same files.
350
351
 
351
352
  `only` restricts the build to those source ids. The package keeps each other source's
352
- entry as the last build wrote it. Raises ValueError for an id the manifest does not have,
353
- or a file output named `datapackage.json`, which is reserved for the descriptor.
353
+ entry as the last build wrote it. Raises ValueError for an unknown source id, a build revision
354
+ that is empty or cannot be encoded as UTF-8, or a file output named `datapackage.json`, which
355
+ is reserved for the descriptor.
354
356
  """
355
357
  return [
356
358
  out
357
359
  for out, _count in build_sources(
358
- sources, raw=raw, processed=processed, parse=parse, only=only, output_format=output_format, name=name
360
+ sources,
361
+ raw=raw,
362
+ processed=processed,
363
+ parse=parse,
364
+ only=only,
365
+ output_format=output_format,
366
+ name=name,
367
+ build_revision=build_revision,
359
368
  )
360
369
  ]
361
370
 
362
371
 
372
+ def check_build_revision(build_revision: str | None) -> None:
373
+ """Refuse revision text that the UTF-8 descriptor cannot store."""
374
+ if build_revision is None:
375
+ return
376
+ if not isinstance(build_revision, str) or not build_revision.strip():
377
+ msg = "build_revision must be nonempty text"
378
+ raise ValueError(msg)
379
+ try:
380
+ build_revision.encode()
381
+ except UnicodeEncodeError as error:
382
+ msg = "build_revision must be valid UTF-8 text"
383
+ raise ValueError(msg) from error
384
+
385
+
363
386
  def build_sources(
364
387
  sources: list[Source],
365
388
  *,
@@ -369,10 +392,12 @@ def build_sources(
369
392
  only: Collection[str] | None = None,
370
393
  output_format: str = "csv",
371
394
  name: str | None = None,
395
+ build_revision: str | None = None,
372
396
  ) -> list[tuple[Path, int | None]]:
373
397
  """Build outputs with row counts for tables, or None for unchanged file copies."""
374
398
  _check_output_format(output_format)
375
399
  _check_name(name)
400
+ check_build_revision(build_revision)
376
401
  # Before any output is written: a broken pyproject.toml must not cost a whole build.
377
402
  name = name or _package_name()
378
403
  selected = {s["id"] for s in select(sources, only)}
@@ -391,7 +416,7 @@ def build_sources(
391
416
  raise ValueError(msg)
392
417
  record, fields, count = _build_one(source, raw, out, parse)
393
418
  written.append((out, count))
394
- resources.append(_resource(source, record, fields, out))
419
+ resources.append(_resource(source, record, fields, out, build_revision=build_revision))
395
420
  package = {"$schema": _DATAPACKAGE_PROFILE, "name": name, "resources": resources}
396
421
  _write_atomic(processed / DATAPACKAGE, json_bytes(package))
397
422
  return written
@@ -542,7 +567,9 @@ def _package_name() -> str:
542
567
  return re.sub(r"[^a-z0-9._-]+", "-", letters.lower()).strip("-") or "dataset"
543
568
 
544
569
 
545
- def _resource(source: Source, record: FetchRecord, fields: list[str], out: Path) -> dict[str, Any]:
570
+ def _resource(
571
+ source: Source, record: FetchRecord, fields: list[str], out: Path, *, build_revision: str | None = None
572
+ ) -> dict[str, Any]:
546
573
  """One output as a Data Package resource: a file, or a table whose columns hold text."""
547
574
  resource: dict[str, Any] = {
548
575
  "name": source["id"],
@@ -565,13 +592,13 @@ def _resource(source: Source, record: FetchRecord, fields: list[str], out: Path)
565
592
  if source.get("license"):
566
593
  licence = {"name": source["license"], "path": source.get("terms_url"), "title": source.get("terms")}
567
594
  resource["licenses"] = [{k: v for k, v in licence.items() if v}]
568
- # Custom properties are allowed in Data Package v2; these name the bytes the rows came from.
569
- resource["sourceledger"] = _provenance(source, record)
595
+ # Data Package v2 allows custom properties for the fetch and build provenance.
596
+ resource["sourceledger"] = _provenance(source, record, build_revision=build_revision)
570
597
  return resource
571
598
 
572
599
 
573
- def _provenance(source: Source, record: FetchRecord) -> dict[str, Any]:
574
- """A resource's `sourceledger` block: the fetch or record its output was built from."""
600
+ def _provenance(source: Source, record: FetchRecord, *, build_revision: str | None = None) -> dict[str, Any]:
601
+ """A resource's fetch provenance and optional build revision."""
575
602
  stamp = {
576
603
  "retrieved_at": record.retrieved_at,
577
604
  "vintage": record.vintage,
@@ -583,6 +610,8 @@ def _provenance(source: Source, record: FetchRecord) -> dict[str, Any]:
583
610
  stamp["recorded_at"] = record.recorded_at
584
611
  if record.parameters:
585
612
  stamp["parameters"] = record.parameters
613
+ if build_revision is not None:
614
+ stamp["build_revision"] = build_revision
586
615
  return stamp
587
616
 
588
617