Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPT: (memory) cache schema files upon first read from github #213

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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