diff --git a/linkml_runtime/__init__.py b/linkml_runtime/__init__.py index 9f93fcb7..6cc39aa1 100644 --- a/linkml_runtime/__init__.py +++ b/linkml_runtime/__init__.py @@ -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): """ diff --git a/linkml_runtime/loaders/loader_root.py b/linkml_runtime/loaders/loader_root.py index 5fdd1e41..e690c4b3 100644 --- a/linkml_runtime/loaders/loader_root.py +++ b/linkml_runtime/loaders/loader_root.py @@ -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): @@ -137,6 +142,7 @@ def _construct_target_class(self, else: return None + def _read_source(self, source: Union[str, dict, TextIO], *, @@ -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 \ No newline at end of file diff --git a/tests/test_issues/input/issue_1040.yaml b/tests/test_issues/input/issue_1040.yaml index dbe42d4e..a436c631 100644 --- a/tests/test_issues/input/issue_1040.yaml +++ b/tests/test_issues/input/issue_1040.yaml @@ -17,4 +17,4 @@ classes: Person: in_subset: - a - - + - \ No newline at end of file diff --git a/tests/test_issues/test_issue_1040.py b/tests/test_issues/test_issue_1040.py index fd4fa5f9..d51d1352 100644 --- a/tests/test_issues/test_issue_1040.py +++ b/tests/test_issues/test_issue_1040.py @@ -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))