Skip to content

Commit

Permalink
Merge pull request #308 from vincentkelleher/fix/curie_prefix_suffix
Browse files Browse the repository at this point in the history
Fix namespaces CURIE prefix & suffix extraction
  • Loading branch information
cmungall authored Mar 18, 2024
2 parents 7c311d9 + d409e57 commit 90215d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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
22 changes: 19 additions & 3 deletions tests/test_utils/test_namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_namespaces(self):
self.assertEqual(str(ns.skos), str(SKOS))
self.assertEqual(ns.skos.note, SKOS.note)
ns.OIO = URIRef("http://www.geneontology.org/formats/oboInOwl")
ns['dc'] = "http://example.org/dc/" # Overrides 'dc' in semweb_context
ns['dc'] = "http://example.org/dc/" # Overrides 'dc' in semweb_context
ns['l1'] = "http://example.org/subset/"
ns['l2'] = "http://example.org/subset/test/"
ns['l3'] = "http://example.org/subset/t"
Expand Down Expand Up @@ -63,8 +63,8 @@ def test_namespaces(self):
self.assertEqual('u1:foo', ns.curie_for("urn:example:foo"))
with self.assertRaises(ValueError):
ns.curie_for("1abc\junk")
#no comment in skos?
#self.assertEqual(SKOS.comment, ns.uri_for("skos:comment"))
# no comment in skos?
# self.assertEqual(SKOS.comment, ns.uri_for("skos:comment"))
self.assertEqual(URIRef('http://example.org/dc/table'), ns.uri_for("dc:table"))
self.assertEqual(ns.uri_for("http://something.org"), URIRef("http://something.org"))
self.assertEqual('https://w3id.org/biolink/metamodel/Schema', str(ns.uri_for(":Schema")))
Expand Down Expand Up @@ -100,5 +100,21 @@ def test_prefixmaps_integration(self):
self.assertEqual(prefixmap_merged.curie_for(test_NCIT_uri), test_NCIT_curie)
self.assertEqual(prefixmap_merged.uri_for(test_NCIT_curie), test_NCIT_uri)

def test_prefix_suffix(self):
ns = Namespaces()
ns['farm'] = 'https://example.org/farm'
ns['farm_slash'] = 'https://slash.org/farm/'

self.assertEqual(('farm', 'cow'), ns.prefix_suffix('farm:cow'))
self.assertEqual(('farm', '/cow'), ns.prefix_suffix('https://example.org/farm/cow'))
self.assertEqual(('farm_slash', 'cow'), ns.prefix_suffix('https://slash.org/farm/cow'))
self.assertEqual(('farm_slash', 'cow/horns'), ns.prefix_suffix('farm_slash:cow/horns'))
self.assertEqual(('farm', '/cow/horns'), ns.prefix_suffix('farm:/cow/horns'))
self.assertEqual(('farm', '#cow/horns'), ns.prefix_suffix('farm:#cow/horns'))
self.assertEqual(('farm', ''), ns.prefix_suffix('farm:'))
self.assertEqual(('', 'cow'), ns.prefix_suffix(':cow'))
self.assertEqual((None, None), ns.prefix_suffix('https://missing-prefix.org/farm/cow'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 90215d2

Please sign in to comment.