Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,49 @@ exact same arguments.
are skipped. Every target is checked before any view is created: an existing view is dropped
and created again over the version (`overwrite=True`, the default — re-pointing the views at a
new version is the normal use; it can't be undone), `overwrite=False` makes it an error
instead, and an existing table is always an error.
instead, and an existing table is always an error. Each view gets the `#public` tag (other
tags are kept), and `#public` is removed from every table and view under `versions`, so only
the latest release carries it.
- `%catalog publish(path=None, overwrite=True, partition_by=None)` — copies every `#public` view
of a dataflow folder into the public bucket as a Parquet table at the same full catalog path:
`catalog.<domain>.<subdomain>.<dataflow>.eu_sdg_06_30.monitoringsites` goes to
`public_s3."dh-prod-data-public".catalog.<domain>.<subdomain>.<dataflow>.eu_sdg_06_30.monitoringsites`.
Run it after `update_latest_release`. The dataflow folder is `path` when given (absolute, or relative to the
context), otherwise the `%catalog use(...)` context; with neither, it raises. It must have
`draft`, `versions` and `reference` (or `references`) folders just below it, checked as soon as
`publish` is called. Views and tables in those three folders are ignored, and views without the tag are skipped (listed
in the report's notes). Every target is checked before anything is copied: an existing table
is dropped and copied again (`overwrite=True`, the default; it can't be undone), and
`overwrite=False` makes it an error, so nothing is copied. Each copy first runs
`DROP TABLE IF EXISTS` on the target (always, since the check can't see a bucket folder Dremio
hasn't promoted), then `CREATE TABLE <target> STORE AS (type => 'parquet') AS SELECT * FROM
<view>`, so the bucket holds plain Parquet files. It then runs `ALTER TABLE <target> REFRESH
METADATA AUTO PROMOTION` and a `SELECT COUNT(*)` on the new table to check that it reads
back. The report's notes give each table's row count.

Publishing is all or nothing. Before anything is replaced, every table already in the bucket
is copied to `public_s3."dh-prod-data-public".<dataflow path>__publish_backup.<same path>` and
read back. If any table then fails (the copy, the refresh or the check), every table the
publish touched is dropped, the replaced ones are copied back from their backups, and the
publish raises an error that names the table that failed and says everything was rolled
back. The backups are dropped afterwards. One that could not be restored is kept, and the
error names it so it can be restored by hand. A restored table comes back unpartitioned.

`partition_by` (optional) partitions the Parquet tables by existing fields, in the order given
(`PARTITION BY ("year", "country")` in the `CREATE TABLE`). A list applies to every published
view; a dict gives each view its own fields, keyed by its path below the Area, and views it
leaves out are not partitioned. Field names are matched ignoring case, and a set is sorted
because it has no order. The fields are checked against the views as soon as `publish` is
called: a field a view doesn't have, or a dict key that names no published view, raises at
once and nothing is queued. They are checked again when the publish runs, before anything is
dropped or copied.

```python
%catalog publish() # the %catalog use(...) dataflow
%catalog publish("catalog.water_management_resources.bathing_water.bwd")
%catalog publish(partition_by=["year", "country"])
%catalog publish(partition_by={"eu_sdg_06_30.monitoringsites": ["year"]})
```
- `draft2version(draft_path, version_path)` / `publishversion(consumer_view_path, version_path)`
— **not implemented yet** (both raise `NotImplementedError`); promoting a draft table into a
permanent version and repointing a consumer-facing view at it.
Expand Down Expand Up @@ -321,6 +363,7 @@ import eea_datalakehouse.notebook # registers %catalog/%ingest/%sdi/%dds
%catalog use("catalog.water_management_resources.bathing_water.bwd")
%dds push(DELIVERABLES, "report.pdf", "annex/*.xlsx")
record = %sdi get_xml("070d9baa-448d-4168-8514-7dadb3ad876d")
%sdi validate(record) # ISO 19115-3 XSD check; also a local path or a %dds get file
%dds push(METADATA, record, overwrite=False)
%dds list(DELIVERABLES) # report.pdf and the annexes, sorted
%dds list(DELIVERABLES, "*.pdf") # filter: a wildcard, ".xlsx", "report", or a list of them
Expand All @@ -329,6 +372,12 @@ f = %dds get(DELIVERABLES, "report.pdf") # -> ./report.pdf; f is the local fil
%dds help
```

`%sdi validate(...)` checks a metadata file against the ISO 19115-3 XML schema (`mdb` 2.0 and
every schema it imports, bundled under `sdi/schemas/`, so it works offline). It lists each
error with its line and element path, and the result is truthy when the file is valid. It
checks only the XSD, not ISO 19115-3's Schematron rules. To refresh the bundled schemas, run
`python scripts/fetch_iso19115_3_schemas.py`.

`DDS_BASE_URL` is read from the first `.env` in the notebook's folder or a parent when `%dds`
builds its session (the kernel environment's value only applies if that `.env` doesn't set one;
`%dds dds_base_url()` shows which is in use). The Dremio identity is `_DREMIO_USER` /
Expand Down Expand Up @@ -486,8 +535,19 @@ create_folder(".2027") # creates bwd.reference.2027 for real, but leaves conte
# ^ starts with 'catalog' — absolute, not appended either
%catalog data_copy(".water_temperature", "archive")
# ^ bare, no dot — relative too, appends to the context
%catalog data_copy(".water_temperature", "archive/")
# ^ ends in '/' — a folder: creates
# bwd.reference.2027.archive if missing and copies
# into bwd.reference.2027.archive.water_temperature
```

A `data_copy`/`data_move` `target_path` that ends in `/` names a folder, not the table. Everything
before the `/` is resolved like any other path (`"archive/"`, `"./archive/"`, `"../archive/"`,
`"catalog/x/archive/"`, or `"./"` for the context itself). That folder, with every missing level,
is created first if it doesn't exist (the source/space itself never is), and the table lands
inside it under the source's own name. Undo removes the folders the call created. Without the
trailing `/`, the target is the table's own path, as before.

`get_wiki(path)`, `get_tags(path)`, `list(path)` (every table/view under `path`, at any depth)
and `schema(path)` (column types + row count) answer immediately, like `get_context()` — nothing
to commit or undo. `delete_view(path)`/`delete_table(path)` queue like every other write, but —
Expand Down Expand Up @@ -524,7 +584,8 @@ context itself — raises `CatalogSessionError` if none is set yet:
| `geopackage/spatial.py` | spatial-or-tabular detection, geometry decoding, CRS lookup and reprojection |
| `geopackage/writer.py` | `GeoPackageWriter` — the `.gpkg` file itself, with `sqlite3`, bulk-loaded |
| `geopackage/session.py` | `GpkgSession` — what `%gpkg` dispatches onto |
| `sdi/controller.py` | `SdiController` — `get_xml()`, `resolve_series()` |
| `sdi/controller.py` | `SdiController` — `get_xml()`, `resolve_series()`, `validate()` |
| `sdi/validation.py` | ISO 19115-3 XSD validation against the schemas bundled in `sdi/schemas/` |
| `sdi/catalogue.py` | `SdiCatalogue` — read-only GeoNetwork REST client |
| `sdi/session.py` | `SdiSession` — what `%sdi` dispatches onto |
| `session_utils.py` | friendly session errors, strict argument types, `.env` lookup |
Expand Down Expand Up @@ -609,7 +670,7 @@ To pin to one specific release instead, use the exact tag the live badges under
[Releasing a new version](#releasing-a-new-version)):

```python
%pip install "git+https://github.com/eeadata/EEALakeHouse.python.git@v0.1.35"
%pip install "git+https://github.com/eeadata/EEALakeHouse.python.git@v0.1.36"

# staging's latest release (early access)
%pip install "git+https://github.com/eeadata/EEALakeHouse.python.git@v0.1.36-staging"
Expand Down
160 changes: 99 additions & 61 deletions debugger/test_catalog_magic.ipynb

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ where = ["src"]

[tool.setuptools.package-data]
"eea_datalakehouse.geopackage" = ["epsg_definitions.json"] # CRS definitions for reprojection
"eea_datalakehouse.sdi" = ["schemas/**/*.xsd"] # ISO 19115-3 XSDs, so validating works offline

[tool.ruff]
target-version = "py311"
Expand All @@ -67,7 +68,7 @@ strict = true
warn_return_any = false

[[tool.mypy.overrides]]
module = ["respx.*", "pyarrow.*", "shapely.*"] # none ships type stubs
module = ["respx.*", "pyarrow.*", "shapely.*", "lxml.*"] # none ships type stubs
ignore_missing_imports = true

[tool.pytest.ini_options]
Expand Down
51 changes: 51 additions & 0 deletions scripts/fetch_iso19115_3_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Refresh the ISO 19115-3 XSDs bundled in ``eea_datalakehouse/sdi/schemas``.

Downloads ``mdb.xsd`` and every schema it imports or includes, transitively
(GML 3.2.1, xlink, ISO 19157-2, ISO 19110...), into
``schemas/<host>/<path>``. ``eea_datalakehouse.sdi.validation`` maps each
``http(s)://<host>/<path>`` schema location onto that file, so validating
never needs the network. Run it from the repository root::

python scripts/fetch_iso19115_3_schemas.py
"""

from __future__ import annotations

import pathlib
import sys
import urllib.parse
import urllib.request

from lxml import etree

ROOT_SCHEMA = "https://schemas.isotc211.org/19115/-3/mdb/2.0/mdb.xsd"
OUT = pathlib.Path(__file__).resolve().parents[1] / "src/eea_datalakehouse/sdi/schemas"
_XS = "{http://www.w3.org/2001/XMLSchema}"


def fetch(root_url: str, out: pathlib.Path) -> set[str]:
seen: set[str] = set()
todo = [root_url]
while todo:
url = todo.pop()
if url in seen:
continue
seen.add(url)
with urllib.request.urlopen(url, timeout=60) as response: # noqa: S310 — fixed hosts
data = response.read()
parts = urllib.parse.urlsplit(url)
dest = out / parts.netloc / parts.path.lstrip("/")
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(data)
parser = etree.XMLParser(resolve_entities=False, no_network=True)
schema = etree.fromstring(data, parser)
for ref in schema.iter(f"{_XS}import", f"{_XS}include", f"{_XS}redefine"):
location = ref.get("schemaLocation")
if location:
todo.append(urllib.parse.urljoin(url, location))
return seen


if __name__ == "__main__":
urls = fetch(ROOT_SCHEMA, OUT)
print(f"{len(urls)} schema(s) under {OUT}", file=sys.stderr)
Loading
Loading