-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: small InputSource related issues
I have added a bunch of tests for `InputSource` handling, checking every kind of input source with every parser. During this, I detected the following issues that I fixed: - `rdflib.util._iri2uri()` should not URL quote the `netloc` parameter, the `idna` encoding already takes care of special characters. I removed the URL quoting of `netloc`. - HexTuple parsing was handling the input source in a way that would only work for some input sources, and not raising errors for other input sources. I changed the input source handling to be more generic. - `rdflib.parser.create_input_source()` incorrectly uses `file.buffer` instead of `source.buffer` when dealing with IO stream sources. Other changes with no runtime impact include: - extracted the logic to calculate the `Accept` HTTP header into a separate private function. - moved the inline function `_urlopen` out into a standalone function. - Changed the HTTP mocking stuff in test slightly to accommodate serving arbitrary files, as I used this in the `InputSource` tests.
- Loading branch information
Showing
22 changed files
with
1,224 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
import urllib.request | ||
from typing import TYPE_CHECKING, Optional | ||
from urllib.error import HTTPError | ||
|
||
if TYPE_CHECKING: | ||
from urllib.request import Request | ||
from urllib.response import addinfourl | ||
|
||
|
||
__all__ = ["_get_accept_header", "_urlopen"] | ||
|
||
|
||
def _urlopen(url: Request) -> addinfourl: | ||
""" | ||
Wrapper around urllib.request.urlopen that handles HTTP 308 redirects. | ||
This is a temporary workaround for https://bugs.python.org/issue40321 | ||
:param req: The request to open. | ||
:return: The response which is the same as :py:func:`urllib.request.urlopen` | ||
responses. | ||
""" | ||
try: | ||
return urllib.request.urlopen(url) | ||
except HTTPError as ex: | ||
# 308 (Permanent Redirect) is not supported by current python version(s) | ||
# See https://bugs.python.org/issue40321 | ||
# This custom error handling should be removed once all | ||
# supported versions of python support 308. | ||
if ex.code == 308: | ||
url.full_url = ex.headers.get("Location") | ||
return _urlopen(url) | ||
else: | ||
raise | ||
|
||
|
||
def _get_accept_header(format: Optional[str]) -> str: | ||
""" | ||
Create an Accept header for the given format. | ||
:param format: The format to create an Accept header for. | ||
:return: The Accept header value. | ||
""" | ||
if format == "xml": | ||
return "application/rdf+xml, */*;q=0.1" | ||
elif format == "n3": | ||
return "text/n3, */*;q=0.1" | ||
elif format in ["turtle", "ttl"]: | ||
return "text/turtle, application/x-turtle, */*;q=0.1" | ||
elif format == "nt": | ||
return "text/plain, */*;q=0.1" | ||
elif format == "trig": | ||
return "application/trig, */*;q=0.1" | ||
elif format == "trix": | ||
return "application/trix, */*;q=0.1" | ||
elif format == "json-ld": | ||
return "application/ld+json, application/json;q=0.9, */*;q=0.1" | ||
else: | ||
# if format not given, create an Accept header from all registered | ||
# parser Media Types | ||
from rdflib.parser import Parser | ||
from rdflib.plugin import plugins | ||
|
||
acc = [] | ||
for p in plugins(kind=Parser): # only get parsers | ||
if "/" in p.name: # all Media Types known have a / in them | ||
acc.append(p.name) | ||
|
||
return ", ".join(acc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<rdf:RDF | ||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
xmlns:eghttp="http://example.com/" | ||
xmlns:egurn="urn:example:" | ||
xmlns:egschema="example:" | ||
xmlns:xsd="http://www.w3.org/2001/XMLSchema#" > | ||
<rdf:Description rdf:about="example:object"> | ||
<eghttp:predicate>XSD string</eghttp:predicate> | ||
</rdf:Description> | ||
<rdf:Description rdf:about="http://example.com/subject"> | ||
<eghttp:predicate xml:lang="jpx">日本語の表記体系</eghttp:predicate> | ||
</rdf:Description> | ||
<rdf:Description rdf:about="urn:example:subject"> | ||
<egschema:predicate rdf:resource="example:subject"/> | ||
</rdf:Description> | ||
<rdf:Description rdf:about="example:subject"> | ||
<egschema:predicate rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">12</egschema:predicate> | ||
<egschema:predicate rdf:resource="example:object"/> | ||
</rdf:Description> | ||
</rdf:RDF> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"@id": "http://example.org/subject", | ||
"http://example.org/predicate": { | ||
"@id": "http://example.org/object" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<http://example.org/subject> | ||
<http://example.org/predicate> <http://example.org/object> . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<rdf:RDF | ||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
xmlns:j.0="http://example.org/" > | ||
<rdf:Description rdf:about="http://example.org/subject"> | ||
<j.0:predicate rdf:resource="http://example.org/object"/> | ||
</rdf:Description> | ||
</rdf:RDF> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
from typing import List | ||
|
||
from rdflib import parser, plugin, serializer | ||
|
||
assert plugin | ||
assert serializer | ||
assert parser | ||
import json | ||
|
||
__all__: List[str] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.