-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconf.py
More file actions
332 lines (274 loc) · 10.3 KB
/
conf.py
File metadata and controls
332 lines (274 loc) · 10.3 KB
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
# This file defines the configuration for the DANDI schema
from __future__ import annotations
from datetime import datetime
from enum import Enum
from importlib.resources import files
import logging
from typing import TYPE_CHECKING, Annotated, Any, Optional, Union
from pydantic import (
AliasChoices,
AnyHttpUrl,
AnyUrl,
BaseModel,
Field,
StringConstraints,
model_validator,
)
from pydantic.fields import FieldInfo
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
)
from typing_extensions import Self
_MODELS_MODULE_NAME = "dandischema.models"
"""The full import name of the module containing the DANDI Pydantic models"""
INSTANCE_IDENTIFIER_PATTERN = r"RRID:\S+"
"""
The pattern of the ID identifying the DANDI service instance
Note
----
This pattern currently only allows Research Resource Identifiers (RRIDs).
"""
UNVENDORED_ID_PATTERN = r"[A-Z][-A-Z]*"
UNVENDORED_DOI_PREFIX_PATTERN = r"10\.\d{4,}"
logger = logging.getLogger(__name__)
DEFAULT_INSTANCE_NAME = "DANDI-ADHOC"
"""
The default name of the DANDI instance
"""
class SpdxLicenseListInfo(BaseModel):
"""
Represents information about the SPDX License List.
"""
version: str
release_date: datetime
url: AnyUrl
reference: AnyUrl = AnyUrl("https://spdx.org/licenses/")
class SpdxLicenseIdList(BaseModel):
"""
Represents a list of SPDX license IDs.
"""
source: SpdxLicenseListInfo
license_ids: list[str]
_license_id_file_path = files(__package__) / "_resources" / "spdx_license_ids.json"
_spdx_license_id_list = SpdxLicenseIdList.model_validate_json(
_license_id_file_path.read_text()
)
if TYPE_CHECKING:
# This is just a placeholder for static type checking
class License(Enum):
... # fmt: skip
else:
License = Enum(
"License",
[("spdx:" + id_,) * 2 for id_ in _spdx_license_id_list.license_ids],
)
class Config(BaseSettings):
"""
Configuration for the DANDI schema
Note
----
Since this class is subclass of `pydantic.BaseSettings`, each field of an
instance of this class can be populated from an environment variable of the
same name prefixed with the prefix defined in `model_config` with the name
of the environment variable interpreted **case-insensitively**.
For details, see https://docs.pydantic.dev/latest/concepts/pydantic_settings/
"""
model_config = SettingsConfigDict(
# TODO: currently `validate_by_name` is set to `False` because of the limitation
# imposed by a bug, https://github.com/pydantic/pydantic/issues/12191, at
# Pydantic. Once that bug is fixed, we should considered setting
# `validate_by_name` to `True` and redefine the fields to allow population
# of field values by field names.
validate_by_name=False,
validate_by_alias=True,
)
instance_name: Annotated[
str,
StringConstraints(pattern=rf"^{UNVENDORED_ID_PATTERN}$"),
Field(
validation_alias=AliasChoices(
"dandi_instance_name", "django_dandi_instance_name"
)
),
] = DEFAULT_INSTANCE_NAME
"""Name of the DANDI instance"""
instance_identifier: Optional[
Annotated[
str,
StringConstraints(pattern=rf"^{INSTANCE_IDENTIFIER_PATTERN}$"),
]
] = Field(
default=None,
validation_alias=AliasChoices(
"dandi_instance_identifier", "django_dandi_instance_identifier"
),
)
"""
ID identifying the DANDI service instance
Note
----
This field currently only accepts Research Resource Identifiers (RRIDs).
"""
instance_url: Optional[AnyHttpUrl] = Field(
default=None,
validation_alias=AliasChoices("dandi_instance_url", "django_dandi_web_app_url"),
)
"""
The URL of the DANDI instance
"""
doi_prefix: Optional[
Annotated[str, StringConstraints(pattern=rf"^{UNVENDORED_DOI_PREFIX_PATTERN}$")]
] = Field(
default=None,
validation_alias=AliasChoices(
"dandi_doi_prefix", "django_dandi_doi_api_prefix"
),
)
"""
The DOI prefix at DataCite
"""
licenses: set[License] = Field(
default={License("spdx:CC0-1.0"), License("spdx:CC-BY-4.0")},
validation_alias=AliasChoices("dandi_licenses", "django_dandi_licenses"),
)
"""
Set of licenses to be supported by the DANDI instance
Currently, the values for this set must be the identifier of a license in the
list at https://spdx.org/licenses/ prefixed with "spdx:" when set with the
corresponding environment variable. E.g.
```shell
export DANDI_LICENSES='["spdx:CC0-1.0", "spdx:CC-BY-4.0"]'
```
"""
@model_validator(mode="after")
def _ensure_non_none_instance_identifier_if_non_none_doi_prefix(
self,
) -> Self:
"""
Ensure that if `doi_prefix` is not `None`, then `instance_identifier`
must not be `None`.
"""
if self.doi_prefix is not None and self.instance_identifier is None:
raise ValueError(
"If `doi_prefix` is set (not `None`), "
"`instance_identifier` must also be set."
)
return self
# This is a workaround for the limitation imposed by the bug at
# https://github.com/pydantic/pydantic/issues/12191 mentioned above.
# TODO: This will no longer be needed once that bug is fixed and
# should be removed along with other workarounds in this model because
# of that bug.
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
def wrap(source: PydanticBaseSettingsSource) -> PydanticBaseSettingsSource:
class Wrapped(PydanticBaseSettingsSource):
def get_field_value(
self, field: FieldInfo, field_name: str
) -> tuple[Any, str, bool]:
raise NotImplementedError(
"If this method is ever called, there is a bug"
)
def __call__(self) -> dict[str, Any]:
result = source().copy()
for field_name in cls.model_fields:
if field_name in result:
alias = f"dandi_{field_name}"
# This overwrites the `alias` key if it already exists
result[alias] = result[field_name]
del result[field_name]
return result
return Wrapped(settings_cls)
return (
wrap(init_settings),
env_settings,
dotenv_settings,
file_secret_settings,
)
_instance_config = Config() # Initial value is set by env vars alone
"""
Configuration of the DANDI instance
This configuration holds the information used to customize the DANDI schema to a
specific vendor, but it should not be accessed directly. Use `get_instance_config()`
to obtain its value and `set_instance_config()` to set its value.
"""
def get_instance_config() -> Config:
"""
Get the configuration of the DANDI instance
This configuration holds the information used to customize the DANDI schema to a
specific vendor.
Returns
-------
Config
The configuration of the DANDI instance
"""
return _instance_config.model_copy(deep=True)
def set_instance_config(
config: Optional[Union[Config, dict]] = None, /, **kwargs: Any
) -> None:
"""
Set the DANDI instance configuration returned by `get_instance_config()`
This setting is done by creating a new instance of `Config` with the positional
argument of type of `Config` or `dict` or the keyword
arguments passed to this function and overwriting the existing one.
Parameters
----------
config : Optional[Union[Config, dict]], optional
An instance of `Config` or a dictionary with the configuration. If an instance
of `Config` is provided, a copy will be made to use to set the DANDI instance
configuration. If a dictionary is provided, it will be validated and converted
to an instance of `Config`. If this argument is provided, no keyword arguments
should be provided. Defaults to `None`.
**kwargs
Keyword arguments to pass to `Config.model_validate()` to create a new
instance of `Config` to set the DANDI instance configuration.
Raises
------
ValueError
If both a non-none positional argument and keyword arguments are provided
Note
----
Use this function to override the initial configuration set by the environment
variables.
This function should be called before importing `dandischema.models` or the
new configuration will not have any affect in the models defined in
`dandischema.models`.
"""
if config is not None and kwargs:
raise ValueError(
"Either a positional argument or a set of keyword arguments should be "
"provided, but not both."
)
import sys
global _instance_config
if config is not None:
if isinstance(config, Config):
new_config = config.model_copy(deep=True)
else:
new_config = Config.model_validate(config)
else:
new_config = Config.model_validate(kwargs)
if _MODELS_MODULE_NAME in sys.modules:
if new_config != _instance_config:
logger.warning(
f"`{_MODELS_MODULE_NAME}` is already imported. Resetting the DANDI "
f"instance configuration to a different value will not have any affect "
f"in the models defined in `{_MODELS_MODULE_NAME}`."
)
else:
logger.debug(
f"`{_MODELS_MODULE_NAME}` is already imported. Attempt to "
f"reset the DANDI instance configuration to the same value by "
f"keyword argument has been ignored."
)
return
_instance_config = new_config