forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
139 lines (114 loc) · 5.24 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
"""
The config module is responsible for managing the configuration of zarr and is based on the Donfig python library.
For selecting custom implementations of codecs, pipelines, buffers and ndbuffers, first register the implementations
in the registry and then select them in the config.
Example:
An implementation of the bytes codec in a class ``your.module.NewBytesCodec`` requires the value of ``codecs.bytes``
to be ``your.module.NewBytesCodec``. Donfig can be configured programmatically, by environment variables, or from
YAML files in standard locations.
.. code-block:: python
from your.module import NewBytesCodec
from zarr.core.config import register_codec, config
register_codec("bytes", NewBytesCodec)
config.set({"codecs.bytes": "your.module.NewBytesCodec"})
Instead of setting the value programmatically with ``config.set``, you can also set the value with an environment
variable. The environment variable ``ZARR_CODECS__BYTES`` can be set to ``your.module.NewBytesCodec``. The double
underscore ``__`` is used to indicate nested access.
.. code-block:: bash
export ZARR_CODECS__BYTES="your.module.NewBytesCodec"
For more information, see the Donfig documentation at https://github.com/pytroll/donfig.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal, cast
from donfig import Config as DConfig
if TYPE_CHECKING:
from donfig.config_obj import ConfigSet
class BadConfigError(ValueError):
_msg = "bad Config: %r"
class Config(DConfig): # type: ignore[misc]
"""The Config will collect configuration from config files and environment variables
Example environment variables:
Grabs environment variables of the form "ZARR_FOO__BAR_BAZ=123" and
turns these into config variables of the form ``{"foo": {"bar-baz": 123}}``
It transforms the key and value in the following way:
- Lower-cases the key text
- Treats ``__`` (double-underscore) as nested access
- Calls ``ast.literal_eval`` on the value
"""
def reset(self) -> None:
self.clear()
self.refresh()
def enable_gpu(self) -> ConfigSet:
"""
Configure Zarr to use GPUs where possible.
"""
return self.set(
{"buffer": "zarr.core.buffer.gpu.Buffer", "ndbuffer": "zarr.core.buffer.gpu.NDBuffer"}
)
# The default configuration for zarr
config = Config(
"zarr",
defaults=[
{
"default_zarr_format": 3,
"array": {
"order": "C",
"write_empty_chunks": False,
"v2_default_compressor": {
"numeric": {"id": "zstd", "level": 0, "checksum": False},
"string": {"id": "zstd", "level": 0, "checksum": False},
"bytes": {"id": "zstd", "level": 0, "checksum": False},
},
"v2_default_filters": {
"numeric": None,
"string": [{"id": "vlen-utf8"}],
"bytes": [{"id": "vlen-bytes"}],
"raw": None,
},
"v3_default_filters": {"numeric": [], "string": [], "bytes": []},
"v3_default_serializer": {
"numeric": {"name": "bytes", "configuration": {"endian": "little"}},
"string": {"name": "vlen-utf8"},
"bytes": {"name": "vlen-bytes"},
},
"v3_default_compressors": {
"numeric": [
{"name": "zstd", "configuration": {"level": 0, "checksum": False}},
],
"string": [
{"name": "zstd", "configuration": {"level": 0, "checksum": False}},
],
"bytes": [
{"name": "zstd", "configuration": {"level": 0, "checksum": False}},
],
},
},
"async": {"concurrency": 10, "timeout": None},
"threading": {"max_workers": None},
"json_indent": 2,
"codec_pipeline": {
"path": "zarr.core.codec_pipeline.BatchedCodecPipeline",
"batch_size": 1,
},
"codecs": {
"blosc": "zarr.codecs.blosc.BloscCodec",
"gzip": "zarr.codecs.gzip.GzipCodec",
"zstd": "zarr.codecs.zstd.ZstdCodec",
"bytes": "zarr.codecs.bytes.BytesCodec",
"endian": "zarr.codecs.bytes.BytesCodec", # compatibility with earlier versions of ZEP1
"crc32c": "zarr.codecs.crc32c_.Crc32cCodec",
"sharding_indexed": "zarr.codecs.sharding.ShardingCodec",
"transpose": "zarr.codecs.transpose.TransposeCodec",
"vlen-utf8": "zarr.codecs.vlen_utf8.VLenUTF8Codec",
"vlen-bytes": "zarr.codecs.vlen_utf8.VLenBytesCodec",
},
"buffer": "zarr.core.buffer.cpu.Buffer",
"ndbuffer": "zarr.core.buffer.cpu.NDBuffer",
}
],
)
def parse_indexing_order(data: Any) -> Literal["C", "F"]:
if data in ("C", "F"):
return cast(Literal["C", "F"], data)
msg = f"Expected one of ('C', 'F'), got {data} instead."
raise ValueError(msg)