Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakers-the-rat committed Mar 26, 2024
2 parents c3ee1a4 + 146d305 commit 0bd751f
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 192 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ jobs:
- name: Install library
run: poetry install --no-interaction

#----------------------------------------------
# run test suite
#----------------------------------------------
- name: Run tests
run: poetry run python -m unittest discover

#----------------------------------------------
# coverage report
#----------------------------------------------
Expand Down
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
2 changes: 2 additions & 0 deletions linkml_runtime/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class OrderingError(RuntimeError):
"""Exception raised when there is a problem with SchemaView ordering"""
21 changes: 18 additions & 3 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 Iterator, 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 @@ -128,7 +133,7 @@ def iter_instances(self) -> Iterator[Any]:
pass

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

View check run for this annotation

Codecov / codecov/patch

linkml_runtime/loaders/loader_root.py#L133

Added line #L133 was not covered by tests


def _construct_target_class(self,
def _construct_target_class(self,
data_as_dict: Union[dict, List[dict]],
target_class: Union[Type[YAMLRoot], Type[BaseModel]]) -> Optional[Union[BaseModel, YAMLRoot, List[BaseModel], List[YAMLRoot]]]:
if data_as_dict:
Expand All @@ -151,6 +156,7 @@ def _construct_target_class(self,
else:
return None


def _read_source(self,
source: Union[str, dict, TextIO],
*,
Expand All @@ -163,8 +169,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
15 changes: 8 additions & 7 deletions linkml_runtime/utils/eval_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

def eval_conditional(*conds: List[Tuple[bool, Any]]) -> Any:
"""
>>> cond(x < 25 : 'low', x > 25 : 'high', True: 'low')
Evaluate a collection of expression,value tuples, returing the first value whose expression is true
>>> x= 40
>>> eval_conditional((x < 25, 'low'), (x > 25, 'high'), (True, 'low'))
'high'
:param subj:
:return:
"""
Expand Down Expand Up @@ -58,10 +63,9 @@ def eval_expr(expr: str, **kwargs) -> Any:
Nulls:
- If a variable is enclosed in {}s then entire expression will eval to None if variable is unset
- If a variable is enclosed in {}s then entire expression will eval to None if any variable is unset
>>> eval_expr('{x} + {y}', x=None, y=2)
None
>>> assert eval_expr('{x} + {y}', x=None, y=2) is None
Functions:
Expand Down Expand Up @@ -92,9 +96,6 @@ def eval_expr(expr: str, **kwargs) -> Any:






def eval_(node, bindings={}):
if isinstance(node, ast.Num):
return node.n
Expand Down
6 changes: 4 additions & 2 deletions linkml_runtime/utils/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def _base(self) -> None:
def curie_for(self, uri: Any, default_ok: bool = True, pythonform: bool = False) -> Optional[str]:
"""
Return the most appropriate CURIE for URI. The first longest matching prefix used, if any. If no CURIE is
present, None is returned
present, None is returned.
Please see https://www.w3.org/TR/curie/ for more details about CURIEs.
@param uri: URI to create the CURIE for
@param default_ok: True means the default prefix is ok. Otherwise, we have to have a real prefix
Expand Down Expand Up @@ -191,7 +193,7 @@ def prefix_for(self, uri_or_curie: Any, case_shift: bool = True) -> Optional[str

def prefix_suffix(self, uri_or_curie: Any, case_shift: bool = True) -> Tuple[Optional[str], Optional[str]]:
uri_or_curie = str(uri_or_curie)
if ':/' in uri_or_curie:
if '://' in uri_or_curie:
uri_or_curie = self.curie_for(uri_or_curie)
if not uri_or_curie:
return None, None
Expand Down
Loading

0 comments on commit 0bd751f

Please sign in to comment.