fetch
fetch
Hash-verified, mirrored, offline-first file fetching.
The fetch order for every file is:
- The file already exists under the data root with a matching checksum.
- The file exists in the read-only shared cache (
FWL_DATA_CACHE) with a matching checksum and is copied into the data root atomically. - Offline mode is active: raise
OfflineDataError. - Download from each mirror in turn (Zenodo first, then Dataverse), verify the checksum, and move the file into place atomically.
Downloads are performed by pooch, which resolves doi: URLs for Zenodo
and Dataverse records natively. Files are never written directly to their
final location: every write lands in a staging directory on the same
filesystem (<data_root>/.fwl-io-staging) and is moved into place with
os.replace, so a crashed download can never leave a corrupt file that
later runs would trust. Stale staging entries are pruned opportunistically.
Mirror failures (network, checksum) and local placement failures (read-only tree, full disk) are reported as distinct errors: a permission problem on the data root is never disguised as a download problem.
A dataset pinned to a Zenodo version DOI resolves into a per-version
directory <subdir>/r<record-id> so successive deposits coexist, and
fetch_all writes a .fwl-io.json stamp (DOI, checksums, fetch date)
there so a completed dataset directory or shared cache is self-describing.
DownloadError
Bases: RuntimeError
A file could not be obtained from any configured mirror.
Fetcher(subdir, registry, zenodo=None, dataverse=None, base_urls=None, data_root=None, progress=False)
Fetches the files of one dataset below the data root.
A dataset pinned to a Zenodo version DOI lands in a per-version directory
<data_root>/<subdir>/r<record-id>, so an updated deposit is placed
beside its predecessor rather than overwriting it, and :meth:fetch_all
writes a .fwl-io.json provenance stamp there so the completed
directory is self-describing. A source given only as base_urls (no
Zenodo pin) keeps the bare <data_root>/<subdir>.
Source code in src/fwl_io/fetch.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
fetch(fname, offline=None)
Return a verified local path for fname, downloading if needed.
Source code in src/fwl_io/fetch.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
fetch_all(offline=None)
Fetch every registry file, then stamp the version directory.
The stamp is written here (the whole-dataset operation), not by an
individual :meth:fetch, so a version directory populated one file at
a time is not stamped until a fetch_all completes it. A stamp-write
failure never fails the fetch: the data is already in place.
Source code in src/fwl_io/fetch.py
228 229 230 231 232 233 234 235 236 237 238 | |
provenance()
Return (file, source, checksum) records for run-provenance manifests.
source is the actual origin of files resolved by this Fetcher
instance (local, cache:<path>, or the mirror that served the
download). Files not yet fetched in this session are marked with a
declared: prefix on the primary mirror, since their true origin
is unknown.
Source code in src/fwl_io/fetch.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
OfflineDataError
Bases: RuntimeError
A required file is unavailable locally while offline mode is active.
create_fetcher(subdir, zenodo=None, dataverse=None, registry=None, base_urls=None, data_root=None, progress=False)
Create a :class:Fetcher for one dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subdir
|
str
|
Location of the dataset below the data root. When a Zenodo pin is
given the files land in a version directory |
required |
zenodo
|
str | None
|
Version DOIs of the primary record and its mirror. |
None
|
dataverse
|
str | None
|
Version DOIs of the primary record and its mirror. |
None
|
registry
|
dict | str | Path | None
|
Name-to-hash mapping, or the path of a committed registry file. |
None
|
base_urls
|
list[str] | None
|
Direct base URLs, tried before the DOI mirrors (used in tests and for non-DOI sources). |
None
|
data_root
|
str | Path | None
|
Override for the data root; defaults to the resolved FWL_DATA tree. |
None
|
progress
|
bool
|
Show a download progress bar (requires tqdm; useful for large files). |
False
|
Source code in src/fwl_io/fetch.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |