Skip to content

Commit 3b0f817

Browse files
authored
Merge pull request #307 from sneakers-the-rat/perf-local-types
[perf] Use local schemas if available
2 parents 23b5f39 + 33ca663 commit 3b0f817

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

linkml_runtime/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525

2626
MAIN_SCHEMA_PATH = SCHEMA_DIRECTORY / "meta.yaml"
2727

28+
LINKML_ANNOTATIONS = SCHEMA_DIRECTORY / "annotations.yaml"
29+
LINKML_ARRAY = SCHEMA_DIRECTORY / "array.yaml"
30+
LINKML_EXTENSIONS = SCHEMA_DIRECTORY / "extensions.yaml"
31+
LINKML_MAPPINGS = SCHEMA_DIRECTORY / "mappings.yaml"
32+
LINKML_TYPES = SCHEMA_DIRECTORY / "types.yaml"
33+
LINKML_UNITS = SCHEMA_DIRECTORY / "units.yaml"
34+
LINKML_VALIDATION = SCHEMA_DIRECTORY / "validation.yaml"
35+
36+
37+
URI_TO_LOCAL = {
38+
'https://w3id.org/linkml/annotations.yaml': str(LINKML_ANNOTATIONS),
39+
'https://w3id.org/linkml/array.yaml': str(LINKML_ARRAY),
40+
'https://w3id.org/linkml/extensions.yaml': str(LINKML_EXTENSIONS),
41+
'https://w3id.org/linkml/mappings.yaml': str(LINKML_MAPPINGS),
42+
'https://w3id.org/linkml/meta.yaml': str(MAIN_SCHEMA_PATH),
43+
'https://w3id.org/linkml/types.yaml': str(LINKML_TYPES),
44+
'https://w3id.org/linkml/units.yaml': str(LINKML_UNITS),
45+
'https://w3id.org/linkml/validation.yaml': str(LINKML_VALIDATION),
46+
}
2847

2948
class MappingError(ValueError):
3049
"""

linkml_runtime/loaders/loader_root.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from abc import ABC, abstractmethod
22
from typing import TextIO, Union, Optional, Callable, Dict, Type, Any, List
3+
from logging import getLogger
34

45
from pydantic import BaseModel
56
from hbreader import FileInfo, hbread
67
from jsonasobj2 import as_dict, JsonObj
78

89
from linkml_runtime.utils.yamlutils import YAMLRoot
10+
from linkml_runtime import URI_TO_LOCAL
11+
12+
CACHE_SIZE = 1024
13+
914

1015

1116
class Loader(ABC):
@@ -137,6 +142,7 @@ def _construct_target_class(self,
137142
else:
138143
return None
139144

145+
140146
def _read_source(self,
141147
source: Union[str, dict, TextIO],
142148
*,
@@ -149,8 +155,17 @@ def _read_source(self,
149155
metadata.base_path = base_dir
150156

151157
if not isinstance(source, dict):
152-
data = hbread(source, metadata, metadata.base_path, accept_header)
158+
# Try to get local version of schema, if one is known to exist
159+
try:
160+
if str(source) in URI_TO_LOCAL.keys():
161+
source = str(URI_TO_LOCAL[str(source)])
162+
except (TypeError, KeyError) as e:
163+
# Fine, use original `source` value
164+
logger = getLogger('linkml_runtime.loaders.Loader')
165+
logger.debug(f"Error converting stringlike source to local linkml file: {source}, got: {e}")
166+
167+
data = hbread(source, metadata, base_dir, accept_header)
153168
else:
154169
data = source
155170

156-
return data
171+
return data

tests/test_issues/input/issue_1040.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ classes:
1717
Person:
1818
in_subset:
1919
- a
20-
-
20+
-

0 commit comments

Comments
 (0)