Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix namespaces CURIE prefix & suffix extraction #308

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix namespaces CURIE prefix & suffix extraction
Signed-off-by: Vincent Kelleher <vincent.kelleher@gaia-x.eu>
Vincent Kelleher committed Mar 15, 2024
commit d409e574e3cfcb9ddae1d30d4c8b048ccdbb557f
6 changes: 4 additions & 2 deletions linkml_runtime/utils/namespaces.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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
22 changes: 19 additions & 3 deletions tests/test_utils/test_namespaces.py
Original file line number Diff line number Diff line change
@@ -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"
@@ -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")))
@@ -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()