Skip to content

Commit

Permalink
Merge pull request #213 from dandi/enh-cache-schema
Browse files Browse the repository at this point in the history
OPT: (memory) cache schema files upon first read from github
  • Loading branch information
yarikoptic authored Jan 22, 2024
2 parents c9b6989 + e780174 commit fcd0ba8
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions dandischema/metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
from functools import lru_cache
import json
from pathlib import Path
from typing import Any, Dict, Iterable, Optional, TypeVar, Union, cast
Expand Down Expand Up @@ -129,6 +130,14 @@ def _validate_asset_json(data: dict, schema_dir: Union[str, Path]) -> None:
_validate_obj_json(data, schema)


@lru_cache
def _get_schema(schema_version: str, schema_name: str) -> Any:
return requests.get(
"https://raw.githubusercontent.com/dandi/schema/"
f"master/releases/{schema_version}/{schema_name}"
).json()


def validate(
obj: dict,
schema_version: Optional[str] = None,
Expand Down Expand Up @@ -184,11 +193,7 @@ def validate(
"Only dandisets and assets can be validated "
"using json schema for older versions"
)
schema_filename = schema_map[schema_key]
schema = requests.get(
f"https://raw.githubusercontent.com/dandi/schema/"
f"master/releases/{schema_version}/{schema_filename}"
).json()
schema = _get_schema(schema_version, schema_map[schema_key])
_validate_obj_json(obj, schema, missing_ok)
klass = getattr(models, schema_key)
try:
Expand Down Expand Up @@ -223,10 +228,7 @@ def migrate(
if version2tuple(schema_version) > version2tuple(to_version):
raise ValueError(f"Cannot migrate from {schema_version} to lower {to_version}.")
if not (skip_validation):
schema = requests.get(
f"https://raw.githubusercontent.com/dandi/schema/"
f"master/releases/{schema_version}/dandiset.json"
).json()
schema = _get_schema(schema_version, "dandiset.json")
_validate_obj_json(obj, schema)
if version2tuple(schema_version) < version2tuple("0.6.0"):
for val in obj.get("about", []):
Expand Down

0 comments on commit fcd0ba8

Please sign in to comment.