diff options
author | Jörn Hees <joernhees@users.noreply.github.com> | 2015-07-28 09:47:44 +0200 |
---|---|---|
committer | Jörn Hees <joernhees@users.noreply.github.com> | 2015-07-28 09:47:44 +0200 |
commit | dd3c44ce19b8c9a6f66b16dce5651b5cb49c85be (patch) | |
tree | b1e89e446ba92748c1a0add8192d9acc013e422a | |
parent | 4e257e45a5bc55e1d3c4be15ed6d448a5e68b04e (diff) | |
parent | 693e947f4e51deecde7aa357529475fe6e194d0e (diff) | |
download | rdflib-dd3c44ce19b8c9a6f66b16dce5651b5cb49c85be.tar.gz |
Merge pull request #503 from joernhees/fix_from_n3
fix util.from_n3() parsing Literals with datatypes and Namespace support, fixes #502
-rw-r--r-- | rdflib/util.py | 34 | ||||
-rw-r--r-- | test/test_util.py | 13 |
2 files changed, 33 insertions, 14 deletions
diff --git a/rdflib/util.py b/rdflib/util.py index b7171255..fd38b008 100644 --- a/rdflib/util.py +++ b/rdflib/util.py @@ -46,6 +46,8 @@ from rdflib.exceptions import PredicateTypeError from rdflib.exceptions import SubjectTypeError from rdflib.graph import Graph from rdflib.graph import QuotedGraph +from rdflib.namespace import Namespace +from rdflib.namespace import NamespaceManager from rdflib.term import BNode from rdflib.term import Literal from rdflib.term import URIRef @@ -122,7 +124,7 @@ def to_term(s, default=None): raise Exception(msg) -def from_n3(s, default=None, backend=None): +def from_n3(s, default=None, backend=None, nsm=None): r''' Creates the Identifier corresponding to the given n3 string. @@ -135,10 +137,20 @@ def from_n3(s, default=None, backend=None): True >>> from_n3('42') == Literal(42) True + >>> from_n3(Literal(42).n3()) == Literal(42) + True + >>> from_n3('"42"^^xsd:integer') == Literal(42) + True + >>> from rdflib import RDFS + >>> from_n3('rdfs:label') == RDFS['label'] + True + >>> nsm = NamespaceManager(Graph()) + >>> nsm.bind('dbpedia', 'http://dbpedia.org/resource/') + >>> berlin = URIRef('http://dbpedia.org/resource/Berlin') + >>> from_n3('dbpedia:Berlin', nsm=nsm) == berlin + True ''' - # TODO: should be able to handle prefixes given as opt. argument maybe: - # from_n3('rdfs:label') if not s: return default if s.startswith('<'): @@ -160,7 +172,7 @@ def from_n3(s, default=None, backend=None): # datatype has to come after lang-tag so ignore everything before # see: http://www.w3.org/TR/2011/WD-turtle-20110809/ # #prod-turtle2-RDFLiteral - datatype = rest[dtoffset + 2:] + datatype = from_n3(rest[dtoffset + 2:], default, backend, nsm) else: if rest.startswith("@"): language = rest[1:] # strip leading at sign @@ -180,11 +192,17 @@ def from_n3(s, default=None, backend=None): elif s.startswith('['): identifier = from_n3(s[1:-1]) return 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()) + prefix, last_part = s.split(':', 1) + ns = dict(nsm.namespaces())[prefix] + return Namespace(ns)[last_part] else: - if s.startswith("_:"): - return BNode(s[2:]) - else: - return BNode(s) + return BNode(s) def check_context(c): diff --git a/test/test_util.py b/test/test_util.py index a6606ac9..546167de 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -7,6 +7,7 @@ from rdflib.term import BNode from rdflib.term import Literal from rdflib.term import URIRef from rdflib import util +from rdflib import XSD from rdflib.exceptions import SubjectTypeError from rdflib.exceptions import PredicateTypeError from rdflib.exceptions import ObjectTypeError @@ -193,13 +194,13 @@ class TestUtilTermConvert(unittest.TestCase): res = util.from_n3(s, default=None, backend=None) self.assert_(isinstance(res, Literal)) self.assertEqual(res, Literal('michel', - datatype=URIRef('xsd:fr'))) - + datatype=XSD['fr'])) + def test_util_from_n3_expectliteralanddtype(self): s = '"true"^^xsd:boolean' res = util.from_n3(s, default=None, backend=None) - self.assertTrue(res.eq(Literal('true', datatype='xsd:boolean'))) - + self.assertTrue(res.eq(Literal('true', datatype=XSD['boolean']))) + def test_util_from_n3_expectliteralwithdatatypefromint(self): s = '42' res = util.from_n3(s) @@ -263,12 +264,12 @@ class TestUtilTermConvert(unittest.TestCase): def test_util_from_n3_expectquotedgraph(self): - s = '{http://example.com/schema}' + s = '{<http://example.com/schema>}' res = util.from_n3(s, default=None, backend="IOMemory") self.assert_(isinstance(res, QuotedGraph)) def test_util_from_n3_expectgraph(self): - s = '[http://example.com/schema]' + s = '[<http://example.com/schema>]' res = util.from_n3(s, default=None, backend="IOMemory") self.assert_(isinstance(res, Graph)) |