Skip to content

Commit e780174

Browse files
committed
OPT: (memory) cache schema files upon first read from github
Transpired by looking at never finalized #155 as a much less intrusive and simpler approach to avoid repetative requests for the same schema files.
1 parent a9d7f89 commit e780174

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Diff for: dandischema/metadata.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from copy import deepcopy
2+
from functools import lru_cache
23
import json
34
from pathlib import Path
45
from typing import Any, Dict, Iterable, Optional, TypeVar, Union, cast
@@ -129,6 +130,14 @@ def _validate_asset_json(data: dict, schema_dir: Union[str, Path]) -> None:
129130
_validate_obj_json(data, schema)
130131

131132

133+
@lru_cache
134+
def _get_schema(schema_version: str, schema_name: str) -> Any:
135+
return requests.get(
136+
"https://raw.githubusercontent.com/dandi/schema/"
137+
f"master/releases/{schema_version}/{schema_name}"
138+
).json()
139+
140+
132141
def validate(
133142
obj: dict,
134143
schema_version: Optional[str] = None,
@@ -184,11 +193,7 @@ def validate(
184193
"Only dandisets and assets can be validated "
185194
"using json schema for older versions"
186195
)
187-
schema_filename = schema_map[schema_key]
188-
schema = requests.get(
189-
f"https://raw.githubusercontent.com/dandi/schema/"
190-
f"master/releases/{schema_version}/{schema_filename}"
191-
).json()
196+
schema = _get_schema(schema_version, schema_map[schema_key])
192197
_validate_obj_json(obj, schema, missing_ok)
193198
klass = getattr(models, schema_key)
194199
try:
@@ -223,10 +228,7 @@ def migrate(
223228
if version2tuple(schema_version) > version2tuple(to_version):
224229
raise ValueError(f"Cannot migrate from {schema_version} to lower {to_version}.")
225230
if not (skip_validation):
226-
schema = requests.get(
227-
f"https://raw.githubusercontent.com/dandi/schema/"
228-
f"master/releases/{schema_version}/dandiset.json"
229-
).json()
231+
schema = _get_schema(schema_version, "dandiset.json")
230232
_validate_obj_json(obj, schema)
231233
if version2tuple(schema_version) < version2tuple("0.6.0"):
232234
for val in obj.get("about", []):

0 commit comments

Comments
 (0)