summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2020-09-17 15:47:25 +1000
committerGitHub <noreply@github.com>2020-09-17 15:47:25 +1000
commit63f46e476e6dd0ae3eef5b71a35e3c8b97a01c6b (patch)
treec9433c6658b67af2357f6925a2f3441dc84eb5d2
parent81c4dbb40a8c8815a0219645db06efccac74f01d (diff)
parent7c7f05c944ca71af5c52b3b321096909d0a11f6b (diff)
downloadrdflib-63f46e476e6dd0ae3eef5b71a35e3c8b97a01c6b.tar.gz
Merge pull request #1163 from anatoly-scherbakov/issue-1160-missing-url-fragment
Issue 1160 missing url fragment
-rw-r--r--rdflib/parser.py66
-rw-r--r--test/test_create_input_source.py25
-rw-r--r--test/test_issue1160.py32
3 files changed, 99 insertions, 24 deletions
diff --git a/rdflib/parser.py b/rdflib/parser.py
index fcaed5e4..6cdc0916 100644
--- a/rdflib/parser.py
+++ b/rdflib/parser.py
@@ -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
@@ -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)
@@ -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
diff --git a/test/test_create_input_source.py b/test/test_create_input_source.py
new file mode 100644
index 00000000..6fc05f9f
--- /dev/null
+++ b/test/test_create_input_source.py
@@ -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()
diff --git a/test/test_issue1160.py b/test/test_issue1160.py
new file mode 100644
index 00000000..19258be7
--- /dev/null
+++ b/test/test_issue1160.py
@@ -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',
+ )