Skip to content

Commit

Permalink
Fix Graph.parse URL handling on windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aucampia committed Oct 12, 2021
1 parent ab31c5e commit b1669cc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
11 changes: 7 additions & 4 deletions rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
19 changes: 19 additions & 0 deletions test/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
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"
Expand Down Expand Up @@ -326,6 +331,20 @@ def testGuessFormatForParse(self):
# 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")
Expand Down
25 changes: 24 additions & 1 deletion test/test_graph_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def log_message(self, *args):


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"]))
Expand All @@ -77,6 +77,29 @@ def content_negotiation(self) -> None:
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()
Expand Down

0 comments on commit b1669cc

Please sign in to comment.