Skip to content

Commit

Permalink
Merge pull request #307 from sneakers-the-rat/perf-local-types
Browse files Browse the repository at this point in the history
[perf] Use local schemas if available
  • Loading branch information
cmungall authored Mar 21, 2024
2 parents 23b5f39 + 33ca663 commit 3b0f817
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
19 changes: 19 additions & 0 deletions linkml_runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@

MAIN_SCHEMA_PATH = SCHEMA_DIRECTORY / "meta.yaml"

LINKML_ANNOTATIONS = SCHEMA_DIRECTORY / "annotations.yaml"
LINKML_ARRAY = SCHEMA_DIRECTORY / "array.yaml"
LINKML_EXTENSIONS = SCHEMA_DIRECTORY / "extensions.yaml"
LINKML_MAPPINGS = SCHEMA_DIRECTORY / "mappings.yaml"
LINKML_TYPES = SCHEMA_DIRECTORY / "types.yaml"
LINKML_UNITS = SCHEMA_DIRECTORY / "units.yaml"
LINKML_VALIDATION = SCHEMA_DIRECTORY / "validation.yaml"


URI_TO_LOCAL = {
'https://w3id.org/linkml/annotations.yaml': str(LINKML_ANNOTATIONS),
'https://w3id.org/linkml/array.yaml': str(LINKML_ARRAY),
'https://w3id.org/linkml/extensions.yaml': str(LINKML_EXTENSIONS),
'https://w3id.org/linkml/mappings.yaml': str(LINKML_MAPPINGS),
'https://w3id.org/linkml/meta.yaml': str(MAIN_SCHEMA_PATH),
'https://w3id.org/linkml/types.yaml': str(LINKML_TYPES),
'https://w3id.org/linkml/units.yaml': str(LINKML_UNITS),
'https://w3id.org/linkml/validation.yaml': str(LINKML_VALIDATION),
}

class MappingError(ValueError):
"""
Expand Down
19 changes: 17 additions & 2 deletions linkml_runtime/loaders/loader_root.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from abc import ABC, abstractmethod
from typing import TextIO, Union, Optional, Callable, Dict, Type, Any, List
from logging import getLogger

from pydantic import BaseModel
from hbreader import FileInfo, hbread
from jsonasobj2 import as_dict, JsonObj

from linkml_runtime.utils.yamlutils import YAMLRoot
from linkml_runtime import URI_TO_LOCAL

CACHE_SIZE = 1024



class Loader(ABC):
Expand Down Expand Up @@ -137,6 +142,7 @@ def _construct_target_class(self,
else:
return None


def _read_source(self,
source: Union[str, dict, TextIO],
*,
Expand All @@ -149,8 +155,17 @@ def _read_source(self,
metadata.base_path = base_dir

if not isinstance(source, dict):
data = hbread(source, metadata, metadata.base_path, accept_header)
# Try to get local version of schema, if one is known to exist
try:
if str(source) in URI_TO_LOCAL.keys():
source = str(URI_TO_LOCAL[str(source)])
except (TypeError, KeyError) as e:
# Fine, use original `source` value
logger = getLogger('linkml_runtime.loaders.Loader')
logger.debug(f"Error converting stringlike source to local linkml file: {source}, got: {e}")

data = hbread(source, metadata, base_dir, accept_header)
else:
data = source

return data
return data
2 changes: 1 addition & 1 deletion tests/test_issues/input/issue_1040.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ classes:
Person:
in_subset:
- a
-
-

0 comments on commit 3b0f817

Please sign in to comment.