summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2021-10-12 17:35:11 +0200
committerIwan Aucamp <aucampia@gmail.com>2021-10-12 17:35:11 +0200
commitb1669cc82f16f6ee2c5f634e671dd2eae28d03ec (patch)
treee07e031cb259bb37461f2e990d3cc14105415645
parentab31c5ed3772cb63806a4614ca4431e9bd7de8d3 (diff)
downloadrdflib-b1669cc82f16f6ee2c5f634e671dd2eae28d03ec.tar.gz
Fix Graph.parse URL handling on windows
Using `pathlib.Path("http://example.com/").exists()` on windows causes an exception as a URL is not a valid path, while `os.path.exists("http://example.com/")` just returns false. This patch reverts _create_input_source_from_location to using `os.path.exists()` instead of pathlib.Path to make it possible to parse graphs from http URLs on windows.
-rw-r--r--rdflib/parser.py11
-rw-r--r--test/test_graph.py19
-rw-r--r--test/test_graph_http.py25
3 files changed, 50 insertions, 5 deletions
diff --git a/rdflib/parser.py b/rdflib/parser.py
index 51e06fbb..f0014150 100644
--- a/rdflib/parser.py
+++ b/rdflib/parser.py
@@ -307,10 +307,13 @@ def create_input_source(
def _create_input_source_from_location(file, format, input_source, location):
- # Fix for Windows problem https://github.com/RDFLib/rdflib/issues/145
- path = pathlib.Path(location)
- if path.exists():
- location = path.absolute().as_uri()
+ # Fix for Windows problem https://github.com/RDFLib/rdflib/issues/145 and
+ # https://github.com/RDFLib/rdflib/issues/1430
+ # NOTE: using pathlib.Path.exists on a URL fails on windows as it is not a
+ # valid path. However os.path.exists() returns false for a URL on windows
+ # which is why it is being used instead.
+ if os.path.exists(location):
+ location = pathlib.Path(location).absolute().as_uri()
base = pathlib.Path.cwd().as_uri()
diff --git a/test/test_graph.py b/test/test_graph.py
index 08c112a5..8143eff9 100644
--- a/test/test_graph.py
+++ b/test/test_graph.py
@@ -9,9 +9,14 @@ from urllib.error import URLError, HTTPError
from rdflib import URIRef, Graph, plugin
from rdflib.exceptions import ParserError
from rdflib.plugin import PluginException
+from rdflib.namespace import Namespace
from nose.exc import SkipTest
+from pathlib import Path
+
+from test.testutils import GraphHelper
+
class GraphTestCase(unittest.TestCase):
store = "default"
@@ -326,6 +331,20 @@ class GraphTestCase(unittest.TestCase):
# this endpoint is currently not available, ignore this test.
pass
+ def test_parse_file_uri(self):
+ EG = Namespace("http://example.org/#")
+ g = Graph()
+ g.parse(Path("./test/nt/simple-04.nt").absolute().as_uri())
+ triple_set = GraphHelper.triple_set(g)
+ self.assertEqual(
+ triple_set,
+ {
+ (EG["Subject"], EG["predicate"], EG["ObjectP"]),
+ (EG["Subject"], EG["predicate"], EG["ObjectQ"]),
+ (EG["Subject"], EG["predicate"], EG["ObjectR"]),
+ },
+ )
+
def testTransitive(self):
person = URIRef("ex:person")
dad = URIRef("ex:dad")
diff --git a/test/test_graph_http.py b/test/test_graph_http.py
index 1ee8292e..927fdc2e 100644
--- a/test/test_graph_http.py
+++ b/test/test_graph_http.py
@@ -63,7 +63,7 @@ class ContentNegotiationHandler(BaseHTTPRequestHandler):
class TestGraphHTTP(unittest.TestCase):
- def content_negotiation(self) -> None:
+ def test_content_negotiation(self) -> None:
EG = Namespace("http://example.org/")
expected = Graph()
expected.add((EG["a"], EG["b"], EG["c"]))
@@ -77,6 +77,29 @@ class TestGraphHTTP(unittest.TestCase):
graph.parse(url, format=format)
self.assertEqual(expected_triples, GraphHelper.triple_set(graph))
+ def test_source(self) -> None:
+ EG = Namespace("http://example.org/")
+ expected = Graph()
+ expected.add((EG["a"], EG["b"], EG["c"]))
+ expected_triples = GraphHelper.triple_set(expected)
+
+ httpmock = SimpleHTTPMock()
+ with ctx_http_server(httpmock.Handler) as server:
+ (host, port) = server.server_address
+ url = f"http://{host}:{port}/"
+
+ httpmock.do_get_responses.append(
+ MockHTTPResponse(
+ 200,
+ "OK",
+ f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
+ {"Content-Type": ["text/turtle"]},
+ )
+ )
+ graph = Graph()
+ graph.parse(source=url)
+ self.assertEqual(expected_triples, GraphHelper.triple_set(graph))
+
def test_3xx(self) -> None:
EG = Namespace("http://example.com/")
expected = Graph()