|
17 | 17 | Optional, |
18 | 18 | Sequence, |
19 | 19 | Tuple, |
| 20 | + TypeVar, |
| 21 | + Union, |
20 | 22 | ) |
21 | 23 |
|
22 | 24 | from rdflib.exceptions import Error |
|
25 | 27 | from rdflib.serializer import Serializer |
26 | 28 | from rdflib.term import BNode, Literal, Node, URIRef |
27 | 29 |
|
| 30 | +_StrT = TypeVar("_StrT", bound=str) |
| 31 | + |
28 | 32 | if TYPE_CHECKING: |
29 | 33 | from rdflib.graph import _PredicateType, _SubjectType, _TripleType |
30 | 34 |
|
@@ -169,6 +173,18 @@ def write(self, text: str) -> None: |
169 | 173 | # type error: Item "None" of "Optional[IO[bytes]]" has no attribute "write" |
170 | 174 | self.stream.write(text.encode(self.encoding, "replace")) # type: ignore[union-attr] |
171 | 175 |
|
| 176 | + def relativize(self, uri: _StrT) -> Union[_StrT, URIRef]: |
| 177 | + base = self.base |
| 178 | + if ( |
| 179 | + base is not None |
| 180 | + and uri.startswith(base) |
| 181 | + and "#" not in uri.replace(base, "") |
| 182 | + and "/" not in uri.replace(base, "") |
| 183 | + ): |
| 184 | + # type error: Incompatible types in assignment (expression has type "str", variable has type "Node") |
| 185 | + uri = URIRef(uri.replace(base, "", 1)) # type: ignore[assignment] |
| 186 | + return uri |
| 187 | + |
172 | 188 |
|
173 | 189 | SUBJECT = 0 |
174 | 190 | VERB = 1 |
@@ -271,6 +287,8 @@ def preprocessTriple(self, triple: _TripleType) -> None: |
271 | 287 | self.base is not None |
272 | 288 | and isinstance(node, URIRef) |
273 | 289 | and node.startswith(self.base) |
| 290 | + and "#" not in node.replace(self.base, "") |
| 291 | + and "/" not in node.replace(self.base, "") |
274 | 292 | ): |
275 | 293 | # predicate corresponds to base namespace |
276 | 294 | continue |
|
0 commit comments