Skip to content

Commit

Permalink
Merge f1e4ed6 into 6197531
Browse files Browse the repository at this point in the history
  • Loading branch information
dwinston authored May 12, 2020
2 parents 6197531 + f1e4ed6 commit af1e1e7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
20 changes: 17 additions & 3 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from __future__ import division
from __future__ import print_function

from xml.sax import SAXParseException

from rdflib.term import Literal # required for doctests

assert Literal # avoid warning
from rdflib.namespace import Namespace # required for doctests

assert Namespace # avoid warning
import rdflib.util # avoid circular dependency


__doc__ = """\
Expand Down Expand Up @@ -1070,13 +1073,24 @@ def parse(
)
if format is None:
format = source.content_type
assumed_xml = False
if format is None:
# raise Exception("Could not determine format for %r. You can" + \
# "expicitly specify one with the format argument." % source)
format = "application/rdf+xml"
try:
format = rdflib.util.guess_format(source.file.name)
except (AttributeError, TypeError):
pass
if format is None:
format = "application/rdf+xml"
assumed_xml = True
parser = plugin.get(format, Parser)()
try:
parser.parse(source, self, **args)
except SAXParseException as saxpe:
if assumed_xml:
logger.warning(
"Could not guess format for %r, so assumed xml."
" You can explicitly specify format using the format argument." % source)
raise saxpe
finally:
if source.auto_close:
source.close()
Expand Down
11 changes: 5 additions & 6 deletions rdflib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
from rdflib.exceptions import ObjectTypeError
from rdflib.exceptions import PredicateTypeError
from rdflib.exceptions import SubjectTypeError
from rdflib.graph import Graph
from rdflib.graph import QuotedGraph
import rdflib.graph as graph # avoid circular dependency
from rdflib.namespace import Namespace
from rdflib.namespace import NamespaceManager
from rdflib.term import BNode
Expand Down Expand Up @@ -146,7 +145,7 @@ def from_n3(s, default=None, backend=None, nsm=None):
>>> from rdflib import RDFS
>>> from_n3('rdfs:label') == RDFS['label']
True
>>> nsm = NamespaceManager(Graph())
>>> nsm = NamespaceManager(graph.Graph())
>>> nsm.bind('dbpedia', 'http://dbpedia.org/resource/')
>>> berlin = URIRef('http://dbpedia.org/resource/Berlin')
>>> from_n3('dbpedia:Berlin', nsm=nsm) == berlin
Expand Down Expand Up @@ -192,16 +191,16 @@ def from_n3(s, default=None, backend=None, nsm=None):
return Literal(int(s))
elif s.startswith('{'):
identifier = from_n3(s[1:-1])
return QuotedGraph(backend, identifier)
return graph.QuotedGraph(backend, identifier)
elif s.startswith('['):
identifier = from_n3(s[1:-1])
return Graph(backend, identifier)
return graph.Graph(backend, identifier)
elif s.startswith("_:"):
return BNode(s[2:])
elif ':' in s:
if nsm is None:
# instantiate default NamespaceManager and rely on its defaults
nsm = NamespaceManager(Graph())
nsm = NamespaceManager(graph.Graph())
prefix, last_part = s.split(':', 1)
ns = dict(nsm.namespaces())[prefix]
return Namespace(ns)[last_part]
Expand Down
32 changes: 32 additions & 0 deletions test/test_parse_file_guess_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
from pathlib import Path
from shutil import copyfile
from tempfile import TemporaryDirectory

from xml.sax import SAXParseException

from rdflib import Graph, logger as graph_logger


class FileParserGuessFormatTest(unittest.TestCase):
def test_ttl(self):
g = Graph()
self.assertIsInstance(g.parse("test/w3c/turtle/IRI_subject.ttl"), Graph)

def test_n3(self):
g = Graph()
self.assertIsInstance(g.parse("test/n3/example-lots_of_graphs.n3"), Graph)

def test_warning(self):
g = Graph()
with TemporaryDirectory() as tmpdirname:
newpath = Path(tmpdirname).joinpath("no_file_ext")
copyfile("test/w3c/turtle/IRI_subject.ttl", newpath)
with self.assertLogs(graph_logger, "WARNING") as log_cm:
with self.assertRaises(SAXParseException):
g.parse(str(newpath))
self.assertTrue(any("Could not guess format" in msg for msg in log_cm.output))


if __name__ == '__main__':
unittest.main()

0 comments on commit af1e1e7

Please sign in to comment.