Skip to content

Commit

Permalink
Add strict expansion and contraction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Jul 22, 2023
1 parent 355f659 commit 0b57723
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ def __str__(self) -> str: # noqa:D105
return f"Duplicate prefixes:\n{self._str()}"


class ConversionError(ValueError):
"""An error raised on conversion."""


class ExpansionError(ConversionError):
"""An error raised on expansion if the prefix can't be looked up."""


class CompressionError(ConversionError):
"""An error raised on expansion if the URI prefix can't be matched."""


def _get_duplicate_uri_prefixes(records: List[Record]) -> List[Tuple[Record, Record, str]]:
return [
(record_1, record_2, uri_prefix)
Expand Down Expand Up @@ -684,6 +696,13 @@ def format_curie(self, prefix: str, identifier: str) -> str:
"""Format a prefix and identifier into a CURIE string."""
return f"{prefix}{self.delimiter}{identifier}"

def compress_strict(self, uri: str) -> str:
"""Compress a URI to a CURIE, and raise an error of not possible."""
rv = self.compress(uri)
if rv is None:
raise CompressionError(uri)
return rv

def compress(self, uri: str) -> Optional[str]:
"""Compress a URI to a CURIE, if possible.
Expand Down Expand Up @@ -733,6 +752,13 @@ def parse_uri(self, uri: str) -> Union[ReferenceTuple, Tuple[None, None]]:
else:
return ReferenceTuple(prefix, uri[len(value) :])

def expand_strict(self, curie: str) -> str:
"""Expand a CURIE to a URI, and raise an error of not possible."""
rv = self.expand(curie)
if rv is None:
raise ExpansionError(curie)
return rv

def expand(self, curie: str) -> Optional[str]:
"""Expand a CURIE to a URI, if possible.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from bioregistry.export.prefix_maps import EXTENDED_PREFIX_MAP_PATH

from curies.api import (
CompressionError,
Converter,
DuplicatePrefixes,
DuplicateURIPrefixes,
ExpansionError,
Record,
Reference,
ReferenceTuple,
Expand Down Expand Up @@ -113,10 +115,17 @@ def _assert_convert(self, converter: Converter):
("OBO:unnamespaced", "http://purl.obolibrary.org/obo/unnamespaced"),
]:
self.assertEqual(curie, converter.compress(uri))
self.assertEqual(curie, converter.compress_strict(uri))
self.assertEqual(uri, converter.expand(curie))
self.assertEqual(uri, converter.expand_strict(curie))

self.assertIsNone(converter.compress("http://example.org/missing:00000"))
with self.assertRaises(CompressionError):
converter.compress_strict("http://example.org/missing:00000")

self.assertIsNone(converter.expand("missing:00000"))
with self.assertRaises(ExpansionError):
converter.expand_strict("missing:00000")

self.assertLess(0, len(converter.records), msg="converter has no records")
self.assertIsNone(converter.get_record("nope"))
Expand Down

0 comments on commit 0b57723

Please sign in to comment.