climate_ref_core.regression.store
#
Data store for native bundles.
This module provides the :class:NativeStore Protocol and three implementations:
- :class:
LocalFilesystemStore: local filesystem store for tests and development. Supports both read and write. - :class:
PoochReadStore: anonymous public-read store backed by a URL, using :mod:poochfor caching, retry, and hash verification. Write is intentionally unsupported (putraises :class:NotImplementedError). - :class:
R2WriteStore: credentialed S3-compatible write backend for Cloudflare R2. Used by themintverb to upload native blobs; reads go through :class:PoochReadStoreagainst the public read URL.
The factory :func:build_native_store selects the appropriate implementation
based on the application :class:~climate_ref.config.Config and the writable flag.
writable=False never requires credentials.
Write credentials are never read from the persisted config: the S3 endpoint and
bucket are non-secret routing config, while authentication is resolved at client-build time
only, in precedence order: explicit REF_NATIVE_STORE_ACCESS_KEY_ID /
REF_NATIVE_STORE_SECRET_ACCESS_KEY env vars, then a named REF_NATIVE_STORE_PROFILE,
then boto3's default credential chain (which honours an ambient AWS_PROFILE).
Blobs are keyed by their sha256 hex digest.
The :class:LocalFilesystemStore uses a two-level directory layout
<root>/<digest[:2]>/<digest> similar to git's object storage.
The remote stores (:class:PoochReadStore, :class:R2WriteStore) use a flat layout
(object key == digest), so a blob is served at {public_url}/{digest}.
LocalFilesystemStore
#
Content-addressed blob store backed by a local filesystem directory.
Intended for tests and development. Supports both read and write without credentials.
Blobs are stored under root using a two-level layout::
<root>/<digest[:2]>/<digest>
This mirrors the git object-store convention, keeping individual subdirectories manageable for large collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Root directory for the content-addressed store. Created on first write if it does not exist. |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 | |
fetch(digest, dest)
#
Copy the blob to dest and verify its sha256 matches digest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob to fetch. |
required |
dest
|
Path
|
Destination path to write the blob to. Parent directories are created if they do not exist. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the blob is not present in the store. |
ValueError
|
If the blob's sha256 does not match |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
has(digest)
#
Return True if the blob is present on disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
preflight()
#
Verify the store root exists (creating it if needed) and is writable.
Raises:
| Type | Description |
|---|---|
NativeStoreUnavailableError
|
If the root cannot be created or is not writable. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
put(path)
#
Store the file at path in the content-addressed store.
Computes the sha256 digest, copies the file to its canonical location, and returns the digest. If a blob with the same digest already exists, the copy is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file to store. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The sha256 hex digest of the stored blob. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
NativeStore
#
Bases: Protocol
Protocol for a content-addressed native-bundle blob store.
Blobs are keyed by their sha256 hex digest.
Read operations (has, fetch) are anonymous and credential-free.
put requires write credentials and raises :class:NotImplementedError on read-only implementations.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
fetch(digest, dest)
#
Fetch the blob identified by digest and write it to dest.
The sha256 of the written file is verified to equal digest.
Raises :class:ValueError on hash mismatch and
:class:FileNotFoundError if the blob is not found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob to fetch. |
required |
dest
|
Path
|
Destination path to write the blob to. Parent directories are created if they do not exist. |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
has(digest)
#
Return True if the blob identified by digest is available in the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
preflight()
#
Verify the store is reachable and usable before relying on it.
For writable stores this checks the credentials and target (bucket, or that the local
directory is writable); for anonymous read-only stores it may be a no-op. Intended to
be called once up front (e.g. before a slow mint run) so a misconfiguration is
caught early.
Raises:
| Type | Description |
|---|---|
NativeStoreUnavailableError
|
If the store cannot be reached or used, with an operator-facing message. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
put(path)
#
Store the file at path and return its sha256 hex digest.
Requires write credentials.
Read-only implementations raise :class:NotImplementedError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file to store. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The sha256 hex digest of the stored blob. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
NativeStoreUnavailableError
#
Bases: RuntimeError
Raised when a native store cannot be reached or used.
Covers rejected credentials, a missing bucket, or an unwritable local directory. The message is operator-facing and actionable (it names the env vars / path to check), so callers can surface it directly.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
PoochReadStore
#
Anonymous public-read blob store backed by a remote URL.
Uses :mod:pooch for caching, retry, and hash verification,
mirroring the pattern in :mod:climate_ref_core.dataset_registry.
Blobs are fetched from {base_url}/{digest} and cached under cache_dir.
put is intentionally unsupported; minting uses the write backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
Base URL from which blobs are served (no trailing slash).
Example: |
required | |
cache_dir
|
Local directory used by pooch to cache downloaded blobs. |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
fetch(digest, dest)
#
Download the blob from {base_url}/{digest} and write it to dest.
Uses pooch for caching and retry.
Verifies the sha256 of the downloaded file against digest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob to fetch. |
required |
dest
|
Path
|
Destination path to write the blob to. Parent directories are created if they do not exist. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the downloaded blob's sha256 does not match |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
has(digest)
#
Return True if the blob is already in the local pooch cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
preflight()
#
No-op: an anonymous public-read store has nothing to verify up front.
It has no credentials, and every read is hash-checked per blob; this exists only to
satisfy the :class:NativeStore protocol.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
put(path)
#
Not supported on this read-only store.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always; minting uses the write backend, not the read store. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
R2WriteStore
#
Credentialed S3-compatible write backend for a Cloudflare R2 bucket.
Used by the mint verb to upload native blobs. Reads in CI and for local replay go
through :class:PoochReadStore against the public read URL, so this store's fetch /
has exist mainly for mint-time idempotence and verification.
Blobs are content-addressed with a flat key layout (object key == key_prefix +
digest), so a blob is served at {public_url}/{key_prefix}{digest}. The public read
domain is expected to map to the bucket root, so key_prefix defaults to "".
boto3 is imported lazily (see :func:_s3_client); constructing this store does not
require boto3, only the endpoint and bucket. Credentials are passed in explicitly and
are never sourced from the persisted config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint_url
|
S3 API endpoint for the bucket's account, without the bucket
(e.g. |
required | |
bucket
|
Name of the R2 bucket (e.g. |
required | |
access_key_id
|
R2 access-key id, or |
required | |
secret_access_key
|
R2 secret-access-key, or |
required | |
profile
|
Named AWS/R2 profile to authenticate with, or |
required | |
key_prefix
|
Optional object-key prefix. Defaults to |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
__attrs_post_init__()
#
Fail fast at construction (mint startup) when routing config is missing.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
fetch(digest, dest)
#
Download the blob to dest and verify its sha256 matches digest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob to fetch. |
required |
dest
|
Path
|
Destination path to write the blob to. Parent directories are created if they do not exist. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the blob is not present in the bucket. |
ValueError
|
If the downloaded blob's sha256 does not match |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
has(digest)
#
Return True if the blob is present in the bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
preflight()
#
Verify the bucket is reachable and the credentials are accepted, before any upload.
Performs a cheap authenticated HEAD on a sentinel key (expected to be absent). A
404 means the request authenticated and the store is usable; 401 / 403 are
translated into actionable :class:NativeStoreUnavailableError messages so a
misconfigured credential is caught before the (slow) diagnostic run rather than after.
head_object is used rather than head_bucket so the check works with
least-privilege, object-scoped tokens (which cannot perform bucket-level operations).
Raises:
| Type | Description |
|---|---|
NativeStoreUnavailableError
|
If the credentials are rejected (401), access is denied (403), or the probe otherwise fails. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
put(path)
#
Upload the file at path to the bucket and return its sha256 hex digest.
The blob is content-addressed: the upload is skipped when an object with the same digest already exists, so minting is idempotent and re-mints are cheap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file to store. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The sha256 hex digest of the stored blob. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
build_native_store(config, *, writable)
#
Build an appropriate :class:NativeStore from a native-store config object.
Accepts any object that exposes url: str and cache_dir: Path
(satisfying :class:_NativeStoreConfigProtocol), so callers pass
config.native_store rather than the full :class:~climate_ref.config.Config.
With writable=False the returned store is always anonymous and
credential-free (suitable for CI read/replay paths).
With writable=True and a local URL/path a :class:LocalFilesystemStore is returned;
with a remote (http(s)) URL a credentialed :class:R2WriteStore is returned. The S3
endpoint and bucket come from the config; authentication is read from the environment
(REF_NATIVE_STORE_ACCESS_KEY_ID / REF_NATIVE_STORE_SECRET_ACCESS_KEY, else
REF_NATIVE_STORE_PROFILE, else boto3's default chain), so secrets never live in the
persisted config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
_NativeStoreConfigProtocol
|
A config object providing |
required |
writable
|
bool
|
When |
required |
Returns:
| Type | Description |
|---|---|
NativeStore
|
A :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL scheme is unrecognised, or a writable remote store is requested without an S3 endpoint / bucket configured. |