summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDrew Perttula <drewp@bigasterisk.com>2015-03-31 22:19:30 -0700
committerDrew Perttula <drewp@bigasterisk.com>2015-03-31 22:19:30 -0700
commitf167ecf880ea48490c0ecc3e61103e784c42438a (patch)
tree6dc9f60b14aee3fb5eb74c460ec121bafd3b6d59
parentaff5b2525154932e7162327139a1d445b02a65bf (diff)
downloadrdflib-f167ecf880ea48490c0ecc3e61103e784c42438a.tar.gz
serialize bnode graph ids in trig correctly
-rw-r--r--rdflib/plugins/serializers/trig.py9
-rw-r--r--test/test_trig.py33
2 files changed, 23 insertions, 19 deletions
diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py
index b30d10c8..f8bd789d 100644
--- a/rdflib/plugins/serializers/trig.py
+++ b/rdflib/plugins/serializers/trig.py
@@ -68,9 +68,12 @@ class TrigSerializer(TurtleSerializer):
if self.default_context and store.identifier==self.default_context:
self.write(self.indent() + '\n{')
else:
- iri = self.getQName(store.identifier)
- if iri is None:
- iri = '<%s>' % store.identifier
+ if isinstance(store.identifier, BNode):
+ iri = store.identifier.n3()
+ else:
+ iri = self.getQName(store.identifier)
+ if iri is None:
+ iri = store.identifier.n3()
self.write(self.indent() + '\n%s {' % iri)
self.depth += 1
diff --git a/test/test_trig.py b/test/test_trig.py
index 742e3281..c985e002 100644
--- a/test/test_trig.py
+++ b/test/test_trig.py
@@ -4,6 +4,10 @@ import re
from rdflib.py3compat import b
+TRIPLE = (rdflib.URIRef("http://example.com/s"),
+ rdflib.RDFS.label,
+ rdflib.Literal("example 1"))
+
class TestTrig(unittest.TestCase):
def testEmpty(self):
@@ -49,10 +53,7 @@ class TestTrig(unittest.TestCase):
def testRememberNamespace(self):
g = rdflib.ConjunctiveGraph()
- g.add((rdflib.URIRef("http://example.com/s"),
- rdflib.RDFS.label,
- rdflib.Literal("example 1"),
- rdflib.URIRef("http://example.com/graph1")))
+ g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
# In 4.2.0 the first serialization would fail to include the
# prefix for the graph but later serialize() calls would work.
first_out = g.serialize(format='trig')
@@ -60,24 +61,24 @@ class TestTrig(unittest.TestCase):
self.assertIn(b'@prefix ns1: <http://example.com/> .', second_out)
self.assertIn(b'@prefix ns1: <http://example.com/> .', first_out)
- print first_out
-
def testGraphQnameSyntax(self):
g = rdflib.ConjunctiveGraph()
- g.add((rdflib.URIRef("http://example.com/s"),
- rdflib.RDFS.label,
- rdflib.Literal("example 1"),
- rdflib.URIRef("http://example.com/graph1")))
+ g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
out = g.serialize(format='trig')
self.assertIn(b'ns1:graph1 {', out)
def testGraphUriSyntax(self):
g = rdflib.ConjunctiveGraph()
- g.add((rdflib.URIRef("http://example.com/s"),
- rdflib.RDFS.label,
- rdflib.Literal("example 1"),
- # getQName will not abbreviate this, so it should come
- # out as a '<...>' term.
- rdflib.URIRef("http://example.com/foo.")))
+ # getQName will not abbreviate this, so it should serialize as
+ # a '<...>' term.
+ g.add(TRIPLE + (rdflib.URIRef("http://example.com/foo."),))
out = g.serialize(format='trig')
self.assertIn(b'<http://example.com/foo.> {', out)
+
+ def testBlankGraphIdentifier(self):
+ g = rdflib.ConjunctiveGraph()
+ g.add(TRIPLE + (rdflib.BNode(),))
+ out = g.serialize(format='trig')
+ graph_label_line = out.splitlines()[-4]
+ self.assert_(re.match(br'^_:[a-zA-Z0-9]+ \{', graph_label_line))
+