|
5 | 5 | from curies import Converter, Record |
6 | 6 |
|
7 | 7 | 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 | + """ |
9 | 12 | def __init__(self) -> None: |
10 | 13 | self.namespaces = [] |
11 | 14 | self._converter: Optional[Converter] = None |
12 | 15 |
|
13 | 16 | @property |
14 | 17 | 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 | + """ |
15 | 23 | if not self._converter: |
16 | 24 | self._converter = self._buildConverter() |
17 | 25 | return self._converter |
@@ -43,28 +51,48 @@ def _buildConverter(self): |
43 | 51 |
|
44 | 52 |
|
45 | 53 |
|
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) |
47 | 66 | return self.converter.compress(uri) |
48 | 67 |
|
49 | 68 | 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 | + """ |
50 | 77 | expanded = self.converter.expand(curie) |
51 | 78 | return None if expanded is None else URIRef(expanded) |
52 | 79 |
|
53 | 80 | def add_namespace(self,ns: "CurieNamespace"): |
| 81 | + """ |
| 82 | + Adds a new namespace to the catalog. |
| 83 | + """ |
54 | 84 | self.namespaces.append(ns) |
55 | 85 | self._converter = None |
56 | 86 |
|
57 | 87 | @classmethod |
58 | 88 | def create(cls, *namespaces: List["CurieNamespace"]): |
| 89 | + """ |
| 90 | + creates a new catalog from the given namespaces |
| 91 | + """ |
59 | 92 | cat = CurieNamespaceCatalog() |
60 | 93 | [cat.add_namespace(x) for x in namespaces] |
61 | 94 | return cat |
62 | 95 |
|
63 | | - def clear(self): |
64 | | - self.catalog = dict() |
65 | | - |
66 | | - def as_dict(self): |
67 | | - return self.catalog.copy() |
68 | 96 |
|
69 | 97 |
|
70 | 98 | class CurieNamespace(Namespace): |
|
0 commit comments