diff options
author | Jörn Hees <joernhees@users.noreply.github.com> | 2015-04-01 12:50:13 +0200 |
---|---|---|
committer | Jörn Hees <joernhees@users.noreply.github.com> | 2015-04-01 12:50:13 +0200 |
commit | f81e0b23d61e3cfda053bf33a7665bdea8039df4 (patch) | |
tree | cdc9934c078c0d0f71a97886a88b4b42e921385e | |
parent | 395a40101fe133d97f454ee61da0fc748a93b007 (diff) | |
parent | 76075d9939f024402a1447b71781a0d6df5cea87 (diff) | |
download | rdflib-f81e0b23d61e3cfda053bf33a7665bdea8039df4.tar.gz |
Merge pull request #480 from drewp/master
trig output fixes
-rw-r--r-- | rdflib/plugins/serializers/trig.py | 25 | ||||
-rwxr-xr-x | run_tests_py3.sh | 2 | ||||
-rw-r--r-- | test/test_trig.py | 36 |
3 files changed, 45 insertions, 18 deletions
diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index 479aae4d..f8bd789d 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -32,6 +32,7 @@ class TrigSerializer(TurtleSerializer): def preprocess(self): for context in self.contexts: self.store = context + self.getQName(context.identifier) self._references = defaultdict(int) self._subjects = {} @@ -40,22 +41,6 @@ class TrigSerializer(TurtleSerializer): self._contexts[context]=(self.orderSubjects(), self._subjects, self._references) - - def preprocessTriple(self, triple): - s, p, o = triple - self._references[o]+=1 - self._subjects[s] = True - for i, node in enumerate(triple): - if node in self.keywords: - continue - # Don't use generated prefixes for subjects and objects - self.getQName(node, gen_prefix=(i == VERB)) - if isinstance(node, Literal) and node.datatype: - self.getQName(node.datatype, gen_prefix=_GEN_QNAME_FOR_DT) - p = triple[1] - if isinstance(p, BNode): - self._references[p]+=1 - def reset(self): super(TrigSerializer, self).reset() self._contexts = {} @@ -83,7 +68,13 @@ class TrigSerializer(TurtleSerializer): if self.default_context and store.identifier==self.default_context: self.write(self.indent() + '\n{') else: - self.write(self.indent() + '\n<%s> {' % self.getQName(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 for subject in ordered_subjects: diff --git a/run_tests_py3.sh b/run_tests_py3.sh index 30fc770e..aa681ef9 100755 --- a/run_tests_py3.sh +++ b/run_tests_py3.sh @@ -16,4 +16,4 @@ cd build/py3_testing 2to3 -wn --no-diffs test 2to3 -wn --no-diffs run_tests.py -python3 run_tests.py +python3 run_tests.py $@ diff --git a/test/test_trig.py b/test/test_trig.py index abcbe1b9..e05f483e 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): @@ -46,3 +50,35 @@ class TestTrig(unittest.TestCase): self.assertEqual(len(re.findall(b("p2"), s)), 1) self.assert_(b('{}') not in s) # no empty graphs! + + def testRememberNamespace(self): + g = rdflib.ConjunctiveGraph() + 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') + second_out = g.serialize(format='trig') + self.assert_(b'@prefix ns1: <http://example.com/> .' in second_out) + self.assert_(b'@prefix ns1: <http://example.com/> .' in first_out) + + def testGraphQnameSyntax(self): + g = rdflib.ConjunctiveGraph() + g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),)) + out = g.serialize(format='trig') + self.assert_(b'ns1:graph1 {' in out) + + def testGraphUriSyntax(self): + g = rdflib.ConjunctiveGraph() + # 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.assert_(b'<http://example.com/foo.> {' in 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)) + |