Skip to content

Commit 79a852e

Browse files
committed
cleanup and add documentation
1 parent cb51521 commit 79a852e

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

linkml_runtime/utils/curienamespace.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
from curies import Converter, Record
66

77
class CurieNamespaceCatalog(object):
8-
catalog: Dict[str, "CurieNamespace"]
8+
"""
9+
A CurieNamespaceCatalog is a catalog of CurieNamespace objects
10+
its main purpose is to convert between uri's and curies for the namespaces in the catalog
11+
"""
912
def __init__(self) -> None:
1013
self.namespaces = []
1114
self._converter: Optional[Converter] = None
1215

1316
@property
1417
def converter(self):
18+
"""
19+
return a curies.Converter that knows all namespaces.
20+
When multiple namespaces have the same prefix, they are added as uri synonyms to the converter.
21+
When there are two prefixes leading to the same uri, they are added as prefix synonyms to the converter.
22+
"""
1523
if not self._converter:
1624
self._converter = self._buildConverter()
1725
return self._converter
@@ -43,28 +51,48 @@ def _buildConverter(self):
4351

4452

4553

46-
def to_curie(self, uri: Union[str, URIRef]) -> str:
54+
def to_curie(self, uri: Union[str, URIRef]) -> Optional[str]:
55+
"""
56+
Compress a URI to a CURIE, if possible.
57+
58+
:param uri:
59+
A string representing a valid uniform resource identifier (URI)
60+
:returns:
61+
A compact URI if this converter could find an appropriate URI prefix, otherwise None.
62+
63+
"""
64+
if isinstance(uri, URIRef):
65+
uri = str(uri)
4766
return self.converter.compress(uri)
4867

4968
def to_uri(self, curie: str) -> Optional[URIRef]:
69+
"""
70+
Expand a CURIE to a URI, if possible.
71+
72+
:param curie:
73+
A string representing a compact URI
74+
:returns:
75+
A URIRef if this converter contains a URI prefix for the prefix in this CURIE, otherwise None
76+
"""
5077
expanded = self.converter.expand(curie)
5178
return None if expanded is None else URIRef(expanded)
5279

5380
def add_namespace(self,ns: "CurieNamespace"):
81+
"""
82+
Adds a new namespace to the catalog.
83+
"""
5484
self.namespaces.append(ns)
5585
self._converter = None
5686

5787
@classmethod
5888
def create(cls, *namespaces: List["CurieNamespace"]):
89+
"""
90+
creates a new catalog from the given namespaces
91+
"""
5992
cat = CurieNamespaceCatalog()
6093
[cat.add_namespace(x) for x in namespaces]
6194
return cat
6295

63-
def clear(self):
64-
self.catalog = dict()
65-
66-
def as_dict(self):
67-
return self.catalog.copy()
6896

6997

7098
class CurieNamespace(Namespace):

0 commit comments

Comments
 (0)