climate_ref_core.data
#
Resolution of the data files that are distributed with the REF
Several files the REF depends on are shipped inside the wheels: the controlled vocabulary, the grey list, and the dataset registry manifests. Some of them also have a newer copy available over the network.
LayeredResource resolves one logical file across three layers:
an operator override, a cached download, and the copy shipped in the package.
The packaged copy is always present and always readable,
so resolution never requires the network or a writable filesystem.
Packaged files are read through importlib.resources rather than being converted
to a filesystem path, so an installation that is not unpacked on disk still works.
DataResourceError
#
Bases: RefException
Raised when a data file cannot be resolved or read.
FileResource
#
A data file at a plain filesystem path
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
as_path()
#
exists()
#
Check whether the file exists
Returns:
| Type | Description |
|---|---|
bool
|
True if the path points at a readable file. |
read_text(encoding='utf-8')
#
Read the file as text
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding
|
str
|
Text encoding of the file. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
The contents of the file. |
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
LayeredResource
#
A data file resolved across an override, a cache, and the packaged copy
Resolution happens on every access rather than being fixed at construction, so a cache that is populated after the object is built is picked up without the object being rebuilt.
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
cache = field(default=None)
class-attribute
instance-attribute
#
A local cache location. Used only when the file is actually present there.
origin
property
#
The layer that currently supplies the file
override = field(default=None)
class-attribute
instance-attribute
#
An explicit path supplied by the operator. Takes precedence over everything else.
packaged
instance-attribute
#
The copy shipped in the package. This is the floor, and is always available.
describe()
#
Describe where the file is being read from
This never raises, so it is safe to use when building a log message for a resolution that has itself failed.
Returns:
| Type | Description |
|---|---|
str
|
A short description suitable for a log message. |
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
read_text(encoding='utf-8')
#
resolve()
#
Determine which layer supplies the file
Returns:
| Type | Description |
|---|---|
tuple[ResourceOrigin, Resource]
|
The origin, and the resource that supplies the file. |
Raises:
| Type | Description |
|---|---|
DataResourceError
|
If an override was supplied but does not point at a readable file. |
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
PackagedResource
#
A data file shipped inside an installed package
The file is always accessed through importlib.resources.
It is never converted to a filesystem path unless a caller explicitly asks for one
via as_path, which extracts it to a temporary location when needed.
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
81 82 83 84 85 86 87 88 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 132 133 134 135 136 137 138 139 140 141 142 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 | |
package
instance-attribute
#
Import name of the package that contains the file, for example climate_ref_core.pycmec.
resource
instance-attribute
#
Name of the file within that package, for example cv_cmip7_aft.yaml.
as_path()
#
Expose the file as a filesystem path for the duration of the context
Prefer read_text.
Use this only for third-party APIs that insist on a path.
The path may point at a temporary file that is removed on exit,
so it must not be retained beyond the context.
Yields:
| Type | Description |
|---|---|
Generator[Path]
|
A path that exists for the duration of the context. |
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
describe()
#
exists()
#
Check whether the file is present in the installed package
Returns:
| Type | Description |
|---|---|
bool
|
True if the file can be read. |
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
read_text(encoding='utf-8')
#
Read the file as text
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding
|
str
|
Text encoding of the file. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
The contents of the file. |
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
Resource
#
Bases: Protocol
Something a data file can be read from
This is the contract LayeredResource resolves to,
and the narrowest one its consumers need.
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
describe()
#
ResourceOrigin
#
Bases: Enum
Which layer a resolved data file came from
Source code in packages/climate-ref-core/src/climate_ref_core/data.py
cache = 'cache'
class-attribute
instance-attribute
#
A copy downloaded into the local cache.
override = 'override'
class-attribute
instance-attribute
#
An explicit path supplied by the operator via configuration or an environment variable.
package = 'package'
class-attribute
instance-attribute
#
The copy shipped inside the installed package.
resolve_cache_dir(cache_name)
#
Resolve a cache directory used to hold downloaded data
If the REF_DATASET_CACHE_DIR environment variable is set, use that as the root.
Otherwise, fall back to the OS cache under climate_ref.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_name
|
str
|
Subdirectory name within the cache root. |
required |
Returns:
| Type | Description |
|---|---|
The resolved cache directory path.
|
|