Skip to content

Commit b75feaa

Browse files
authored
Merge pull request #162 from linkml/hsolbrig/parse_import_map
Enhance import_map functionality to support namespace as well as indi…
2 parents 1130b29 + 9b63569 commit b75feaa

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

linkml_runtime/utils/context_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import os
33
from io import TextIOWrapper
4-
from typing import Optional, Union, List, Any, Dict
4+
from typing import Optional, Union, List, Any, Dict, Callable
55

66
import yaml
77
from jsonasobj2 import JsonObj, loads
@@ -50,6 +50,17 @@ def to_file_uri(fname: str) -> str:
5050
JsonObj(**{"@context": context_list[0] if len(context_list) == 1 else context_list})
5151

5252

53+
def map_import(importmap: Dict[str, str], namespaces: Callable[[None], "Namespaces"], imp: Any) -> str:
54+
sname = str(imp)
55+
if ':' in sname:
56+
prefix, lname = sname.split(':', 1)
57+
prefix += ':'
58+
sname = importmap.get(prefix, prefix) + lname
59+
sname = importmap.get(sname, sname) # Import map may use CURIE
60+
sname = str(namespaces().uri_for(sname)) if ':' in sname else sname
61+
return importmap.get(sname, sname) # It may also use URI or other forms
62+
63+
5364
def parse_import_map(map_: Optional[Union[str, Dict[str, str], TextIOWrapper]],
5465
base: Optional[str] = None) -> Dict[str, str]:
5566
"""

linkml_runtime/utils/schemaview.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Mapping, Tuple, Type
99
from linkml_runtime.utils.namespaces import Namespaces
1010
from deprecated.classic import deprecated
11-
from linkml_runtime.utils.context_utils import parse_import_map
11+
from linkml_runtime.utils.context_utils import parse_import_map, map_import
1212
from linkml_runtime.linkml_model.meta import *
1313
from enum import Enum
1414
logger = logging.getLogger(__name__)
@@ -140,10 +140,7 @@ def namespaces(self) -> Namespaces:
140140
def load_import(self, imp: str, from_schema: SchemaDefinition = None):
141141
if from_schema is None:
142142
from_schema = self.schema
143-
# TODO: this code is copied from linkml.utils.schemaloader; put this somewhere reusable
144-
sname = self.importmap.get(str(imp), imp) # Import map may use CURIE
145-
sname = self.namespaces().uri_for(sname) if ':' in sname else sname
146-
sname = self.importmap.get(str(sname), sname) # It may also use URI or other forms
143+
sname = map_import(self.importmap, self.namespaces, imp)
147144
logging.info(f'Loading schema {sname} from {from_schema.source_file}')
148145
schema = load_schema_wrap(sname + '.yaml',
149146
base_dir=os.path.dirname(from_schema.source_file) if from_schema.source_file else None)

tests/test_utils/input/core.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
id: https://w3id.org/linkml/tests/core
22
name: core
33
description: |-
4-
core schema imported by kitchen_sink
4+
core schema imported by kitchen_sink
55
default_curi_maps:
66
- semweb_context
77
imports:
@@ -33,7 +33,7 @@ classes:
3333
- description
3434
exact_mappings:
3535
- prov:Activity
36-
36+
3737
agent:
3838
description: "a provence-generating agent"
3939
slots:
@@ -43,7 +43,7 @@ classes:
4343
class_uri: prov:Agent
4444

4545
TestClass:
46-
46+
4747
slots:
4848

4949
id:
@@ -54,7 +54,7 @@ slots:
5454

5555
description:
5656
range: NarrativeText
57-
57+
5858
started at time:
5959
slot_uri: prov:startedAtTime
6060
range: date

0 commit comments

Comments
 (0)