Skip to content

Commit

Permalink
#1160 removed .defrag() and added a test
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoly-scherbakov committed Sep 12, 2020
1 parent 65987a3 commit 7c7f05c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
52 changes: 38 additions & 14 deletions rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ def create_input_source(
"""

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

if len(non_empty_arguments) != 1:
raise ValueError(
Expand Down Expand Up @@ -248,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)
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 @@ -282,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
4 changes: 4 additions & 0 deletions test/test_create_input_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ def test_too_many_arguments(self):
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 7c7f05c

Please sign in to comment.