Skip to content

Commit

Permalink
Merge pull request #1163 from anatoly-scherbakov/issue-1160-missing-u…
Browse files Browse the repository at this point in the history
…rl-fragment

Issue 1160 missing url fragment
  • Loading branch information
nicholascar committed Sep 17, 2020
2 parents 81c4dbb + 7c7f05c commit 63f46e4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 24 deletions.
66 changes: 42 additions & 24 deletions rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,15 @@ def create_input_source(
"""

# test that exactly one of source, location, file, and data is not None.
if (
sum(
(
source is not None,
location is not None,
file is not None,
data is not None,
)
non_empty_arguments = list(filter(
lambda v: v is not None,
[source, location, file, data],
))

if len(non_empty_arguments) != 1:
raise ValueError(
"exactly one of source, location, file or data must be given",
)
!= 1
):
raise ValueError("exactly one of source, location, file or data must be given")

input_source = None

Expand Down Expand Up @@ -254,20 +251,19 @@ def create_input_source(
absolute_location = None # Further to fix for issue 130

auto_close = False # make sure we close all file handles we open

if location is not None:
# Fix for Windows problem https://github.com/RDFLib/rdflib/issues/145
if os.path.exists(location):
location = pathname2url(location)
base = urljoin("file:", "%s/" % pathname2url(os.getcwd()))
absolute_location = URIRef(location, base=base).defrag()
if absolute_location.startswith("file:///"):
filename = url2pathname(absolute_location.replace("file:///", "/"))
file = open(filename, "rb")
else:
input_source = URLInputSource(absolute_location, format)
auto_close = True
# publicID = publicID or absolute_location # Further to fix
# for issue 130
(
absolute_location,
auto_close,
file,
input_source,
) = _create_input_source_from_location(
file=file,
format=format,
input_source=input_source,
location=location,
)

if file is not None:
input_source = FileInputSource(file)
Expand All @@ -288,3 +284,25 @@ def create_input_source(
elif input_source.getPublicId() is None:
input_source.setPublicId(absolute_location or "")
return input_source


def _create_input_source_from_location(file, format, input_source, location):
# Fix for Windows problem https://github.com/RDFLib/rdflib/issues/145
if os.path.exists(location):
location = pathname2url(location)

base = urljoin("file:", "%s/" % pathname2url(os.getcwd()))

absolute_location = URIRef(location, base=base)

if absolute_location.startswith("file:///"):
filename = url2pathname(absolute_location.replace("file:///", "/"))
file = open(filename, "rb")
else:
input_source = URLInputSource(absolute_location, format)

auto_close = True
# publicID = publicID or absolute_location # Further to fix
# for issue 130

return absolute_location, auto_close, file, input_source
25 changes: 25 additions & 0 deletions test/test_create_input_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from rdflib.parser import create_input_source


class ParserTestCase(unittest.TestCase):
def test_empty_arguments(self):
"""create_input_source() function must receive exactly one argument."""
self.assertRaises(
ValueError,
create_input_source,
)

def test_too_many_arguments(self):
"""create_input_source() function has a few conflicting arguments."""
self.assertRaises(
ValueError,
create_input_source,
source='a',
location='b',
)


if __name__ == "__main__":
unittest.main()
32 changes: 32 additions & 0 deletions test/test_issue1160.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
from unittest import mock

import rdflib

from rdflib import ConjunctiveGraph
from rdflib.parser import URLInputSource

QUERY = '''
SELECT DISTINCT ?g
FROM NAMED <http://ns.example.com/named#>
WHERE {
GRAPH ?g {
?s ?p ?o .
}
}
'''


class NamedGraphWithFragmentTest(unittest.TestCase):
def test_named_graph_with_fragment(self):
"""Test that fragment part of the URL is not erased."""
graph = ConjunctiveGraph()

with mock.patch('rdflib.parser.URLInputSource') as load_mock:
# We have to expect an exception here.
self.assertRaises(Exception, graph.query, QUERY)

load_mock.assert_called_with(
rdflib.URIRef('http://ns.example.com/named#'),
'nt',
)

0 comments on commit 63f46e4

Please sign in to comment.