Skip to content

Commit 6f12bca

Browse files
committed
feat: support vendorization through config
This commit uses `pydantic-settings` to establish a configuration mechanism, and through this mechanism, it adds support for "vendorization".
1 parent d9a24c5 commit 6f12bca

4 files changed

Lines changed: 53 additions & 21 deletions

File tree

dandischema/conf.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file defines the configuration for the DANDI schema
2+
3+
from __future__ import annotations
4+
5+
from pydantic_settings import BaseSettings, SettingsConfigDict
6+
7+
8+
class Config(BaseSettings):
9+
"""
10+
Configuration for the DANDI schema
11+
12+
Note
13+
----
14+
Since this class is subclass of `pydantic.BaseSettings`, each field of an
15+
instance of this class can be populated from an environment variable of the
16+
same name prefixed with the prefix defined in `model_config` with the name
17+
of the environment variable interpreted **case-insensitively**.
18+
For details, see https://docs.pydantic.dev/latest/concepts/pydantic_settings/
19+
"""
20+
21+
model_config = SettingsConfigDict(env_prefix="dandi_schema_")
22+
23+
id_pattern: str = r"[A-Z]+"
24+
"""Regex pattern for the prefix of identifiers"""
25+
26+
datacite_doi_id_pattern: str = r"\d{4,}"
27+
"""
28+
The registrant code pattern of the DOI prefix at DataCite
29+
30+
The number sequence that follows "10." within the DOI prefix as documented
31+
at https://support.datacite.org/docs/prefixes.
32+
"""
33+
34+
35+
CONFIG = Config()

dandischema/consts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DANDI_SCHEMA_VERSION = "0.6.11"
1+
DANDI_SCHEMA_VERSION = "0.6.12"
22
ALLOWED_INPUT_SCHEMAS = [
33
"0.4.4",
44
"0.5.1",
@@ -14,6 +14,7 @@
1414
"0.6.8",
1515
"0.6.9",
1616
"0.6.10",
17+
"0.6.11",
1718
DANDI_SCHEMA_VERSION,
1819
]
1920

dandischema/models.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@
3636
from pydantic_core import CoreSchema
3737
from zarr_checksum.checksum import InvalidZarrChecksum, ZarrDirectoryDigest
3838

39+
from dandischema.conf import CONFIG
40+
3941
from .consts import DANDI_SCHEMA_VERSION
4042
from .digests.dandietag import DandiETag
4143
from .types import ByteSizeJsonSchema
4244
from .utils import name2title
4345

46+
# Load needed configurations into constants
47+
ID_PATTERN = CONFIG.id_pattern
48+
DATACITE_DOI_ID_PATTERN = CONFIG.datacite_doi_id_pattern
49+
4450
# Use DJANGO_DANDI_WEB_APP_URL to point to a specific deployment.
4551
DANDI_INSTANCE_URL: Optional[str]
4652
try:
@@ -58,18 +64,11 @@
5864
)
5965
ASSET_UUID_PATTERN = r"^dandiasset:" + UUID_PATTERN
6066
VERSION_PATTERN = r"\d{6}/\d+\.\d+\.\d+"
61-
# Vendored
62-
# DANDI_DOI_PATTERN = rf"^10.(48324|80507)/dandi\.{VERSION_PATTERN}"
63-
# Unvendored, only with 10. prefix, as likely all would have for datacite
64-
DANDI_DOI_PATTERN = rf"^10.\d+/[a-z]+\.{VERSION_PATTERN}"
65-
# Vendored:
66-
# DANDI_PUBID_PATTERN = rf"^DANDI:{VERSION_PATTERN}"
67-
# Unvendored:
68-
DANDI_PUBID_PATTERN = rf"^[A-Z]+:{VERSION_PATTERN}"
69-
# Vendored:
70-
# potentially could be set to alternative namespace
71-
# Unvendored: use "dandi" by default
72-
DANDI_NSKEY = "dandi"
67+
DANDI_DOI_PATTERN = (
68+
rf"^10\.{DATACITE_DOI_ID_PATTERN}/{ID_PATTERN.lower()}\.{VERSION_PATTERN}"
69+
)
70+
DANDI_PUBID_PATTERN = rf"^{ID_PATTERN}:{VERSION_PATTERN}"
71+
DANDI_NSKEY = "dandi" # Namespace for DANDI ontology
7372

7473
PUBLISHED_VERSION_URL_PATTERN = (
7574
rf"^{DANDI_INSTANCE_URL_PATTERN}/dandiset/{VERSION_PATTERN}$"
@@ -1624,20 +1623,16 @@ def contributor_musthave_contact(
16241623

16251624
id: str = Field(
16261625
description="Uniform resource identifier",
1627-
# Vendored:
1628-
# pattern=r"^(dandi|DANDI):\d{6}(/(draft|\d+\.\d+\.\d+))$",
1629-
# Unvendored:
1630-
pattern=r"^([a-z]+|[A-Z]+):\d{6}(/(draft|\d+\.\d+\.\d+))$",
1626+
pattern=(
1627+
rf"^({ID_PATTERN}|{ID_PATTERN.lower()}):\d{{6}}(/(draft|\d+\.\d+\.\d+))$"
1628+
),
16311629
json_schema_extra={"readOnly": True},
16321630
)
16331631

16341632
identifier: DANDI = Field(
16351633
title="Dandiset identifier",
16361634
description="A Dandiset identifier that can be resolved by identifiers.org.",
1637-
# Vendored:
1638-
# pattern=r"^DANDI:\d{6}$",
1639-
# Unvendored:
1640-
pattern=r"^[A-Z]+:\d{6}$",
1635+
pattern=rf"^{ID_PATTERN}:\d{{6}}$",
16411636
json_schema_extra={"readOnly": True, "nskey": "schema"},
16421637
)
16431638
name: str = Field(

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ project_urls =
2929
python_requires = >=3.9
3030
install_requires =
3131
jsonschema[format]
32+
pydantic-settings
3233
pydantic[email] ~= 2.4
3334
requests
3435
zarr_checksum

0 commit comments

Comments
 (0)