From d409e574e3cfcb9ddae1d30d4c8b048ccdbb557f Mon Sep 17 00:00:00 2001 From: Vincent Kelleher Date: Fri, 15 Mar 2024 18:23:20 +0100 Subject: [PATCH] fix namespaces CURIE prefix & suffix extraction Signed-off-by: Vincent Kelleher --- linkml_runtime/utils/namespaces.py | 6 ++++-- tests/test_utils/test_namespaces.py | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/linkml_runtime/utils/namespaces.py b/linkml_runtime/utils/namespaces.py index a49a2143..a7232210 100644 --- a/linkml_runtime/utils/namespaces.py +++ b/linkml_runtime/utils/namespaces.py @@ -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 diff --git a/tests/test_utils/test_namespaces.py b/tests/test_utils/test_namespaces.py index bd90c7f7..fe08fc91 100644 --- a/tests/test_utils/test_namespaces.py +++ b/tests/test_utils/test_namespaces.py @@ -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()