Skip to content

Commit 2fc42c0

Browse files
authored
Merge pull request #273 from linkml/linkml_1512
remove check if identifier is python identifier, add tests
2 parents 4ff93fc + 37e37e2 commit 2fc42c0

File tree

3 files changed

+80
-21
lines changed

3 files changed

+80
-21
lines changed

linkml_runtime/utils/namespaces.py

+33-20
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def curie_for(self, uri: Any, default_ok: bool = True, pythonform: bool = False)
139139
present, None is returned
140140
141141
@param uri: URI to create the CURIE for
142-
@param default_ok: True means the default prefix is ok. Otherwise we have to have a reql prefix
142+
@param default_ok: True means the default prefix is ok. Otherwise, we have to have a real prefix
143143
@param pythonform: True means take the python/rdflib uppercase format
144144
"""
145145
if ':' in uri and ':/' not in uri:
@@ -148,28 +148,41 @@ def curie_for(self, uri: Any, default_ok: bool = True, pythonform: bool = False)
148148
if pythonform:
149149
default_ok = False
150150
match: Tuple[str, Optional[Namespace]] = ('', None) # match string / prefix
151-
u = str(uri)
152-
153-
# Find the longest match
154-
for k, v in self.items():
155-
vs = str(v)
156-
if u.startswith(vs):
157-
if len(vs) > len(match[0]) and (default_ok or k not in (Namespaces._default_key, Namespaces._base_key)):
158-
match = (vs, k)
151+
uri_string = str(uri)
152+
153+
# Find the longest match for the URI, self.items() is a list of (prefix/namespace, uri base prefix) tuples
154+
for namespace, uri_base in self.items():
155+
uri_base_string = str(uri_base)
156+
# uri_string is passed into this method as the full URI to be converted to a CURIE
157+
if uri_string.startswith(uri_base_string):
158+
# default key and base key are `@default` `@base` respectively
159+
# at this point match[0] is '', match[1] is None
160+
if len(uri_base_string) > len(match[0]) and \
161+
(default_ok or namespace not in (Namespaces._default_key, Namespaces._base_key)):
162+
match = (uri_base_string, namespace)
163+
164+
# check if length of uri_base_string is > 0, now after basically assigning it to be the URI base string
165+
# that matches the start of the URI coming into the method
159166
if len(match[0]):
160167
if pythonform:
161-
ns = match[1].upper()
162-
ln = u.replace((match[0]), '')
163-
if not ln:
164-
return f"URIRef(str({ns}))"
165-
elif ln.isidentifier():
166-
return f"{ns}.{ln}"
168+
# uppercase the namespace
169+
namespace = match[1].upper()
170+
# match[0] is the URI base string, so we remove that from the incoming URI
171+
leftover_uri = uri_string.replace((match[0]), '')
172+
if not leftover_uri:
173+
return f"URIRef(str({namespace}))"
174+
# why?
175+
# elif leftover_uri.isidentifier():
176+
# return f"{namespace}.{leftover_uri}"
167177
else:
168-
return f'{ns}["{ln}"]'
178+
return f'{namespace}["{leftover_uri}"]'
169179
else:
170-
return u.replace(match[0],
171-
':' if match[1] == Namespaces._default_key else
172-
'' if match[1] == Namespaces._base_key else match[1] + ':')
180+
if match[1] == Namespaces._default_key:
181+
return uri_string.replace(match[0], ':')
182+
elif match[1] == Namespaces._base_key:
183+
return uri_string.replace(match[0], '')
184+
else:
185+
return uri_string.replace(match[0], match[1] + ':')
173186
return None
174187

175188
def prefix_for(self, uri_or_curie: Any, case_shift: bool = True) -> Optional[str]:
@@ -213,7 +226,7 @@ def uri_for(self, uri_or_curie: Any) -> URIRef:
213226
return URIRef(self.join(self[prefix], local))
214227

215228
def uri_or_curie_for(self, prefix: Union[str, URIRef], suffix: str) -> str:
216-
""" Return a CURIE for prefix/suffix in possible, else a URI """
229+
""" Return a CURIE for prefix/suffix if possible, else a URI """
217230
if isinstance(prefix, URIRef) or ':/' in str(prefix):
218231
prefix_as_uri = str(prefix)
219232
for k, v in self.items():

0 commit comments

Comments
 (0)