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

[perf] Use local schemas if available #307

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
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
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 @@
else:
return None


def _read_source(self,
source: Union[str, dict, TextIO],
*,
Expand All @@ -149,8 +155,17 @@
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:

Check warning on line 162 in linkml_runtime/loaders/loader_root.py

View check run for this annotation

Codecov / codecov/patch

linkml_runtime/loaders/loader_root.py#L162

Added line #L162 was not covered by tests
# 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}")

Check warning on line 165 in linkml_runtime/loaders/loader_root.py

View check run for this annotation

Codecov / codecov/patch

linkml_runtime/loaders/loader_root.py#L164-L165

Added lines #L164 - L165 were not covered by tests

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
-
-
2 changes: 1 addition & 1 deletion tests/test_issues/test_issue_1040.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_issue_1040_file_name(self):
trace. We use this to make sure that the file name gets in correctly. """
with self.assertRaises(yaml.constructor.ConstructorError) as e:
yaml_loader.load(env.input_path('issue_1040.yaml'), SchemaDefinition)
self.assertIn('File "issue_1040.yaml"', str(e.exception))
self.assertIn('"issue_1040.yaml"', str(e.exception))
Loading