From 5d9a7b05b223a08028d0a7899789b5259741cb5a Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Sun, 29 Mar 2015 00:58:26 -0700 Subject: trig serializer wasn't including a @prefix line for the first graph's prefix --- rdflib/plugins/serializers/trig.py | 17 +---------------- test/test_trig.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index 479aae4d..a151827c 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 = {} diff --git a/test/test_trig.py b/test/test_trig.py index abcbe1b9..0f800af3 100644 --- a/test/test_trig.py +++ b/test/test_trig.py @@ -46,3 +46,18 @@ 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((rdflib.URIRef("http://example.com/s"), + rdflib.RDFS.label, + rdflib.Literal("example 1"), + 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.assertIn('@prefix ns1: .', second_out) + self.assertIn('@prefix ns1: .', first_out) + + print first_out -- cgit v1.2.1 From bd57db75f14e20aa7330624b671fb2160587dbd8 Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Sun, 29 Mar 2015 01:24:21 -0700 Subject: trig serializer corrections to graph label output Before: " {...}" and " {...}" After: "short:name {...}" and " {...}" --- rdflib/plugins/serializers/trig.py | 5 ++++- test/test_trig.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index a151827c..b30d10c8 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -68,7 +68,10 @@ 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)) + iri = self.getQName(store.identifier) + if iri is None: + iri = '<%s>' % store.identifier + self.write(self.indent() + '\n%s {' % iri) self.depth += 1 for subject in ordered_subjects: diff --git a/test/test_trig.py b/test/test_trig.py index 0f800af3..862bff12 100644 --- a/test/test_trig.py +++ b/test/test_trig.py @@ -61,3 +61,23 @@ class TestTrig(unittest.TestCase): self.assertIn('@prefix ns1: .', 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"))) + out = g.serialize(format='trig') + self.assertIn('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."))) + out = g.serialize(format='trig') + self.assertIn(' {', out) -- cgit v1.2.1 From b93f3ddcf5df16273ce57f0ce77e365ddc676b70 Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Tue, 31 Mar 2015 22:00:59 -0700 Subject: fix string types for py3 --- test/test_trig.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_trig.py b/test/test_trig.py index 862bff12..742e3281 100644 --- a/test/test_trig.py +++ b/test/test_trig.py @@ -57,8 +57,8 @@ class TestTrig(unittest.TestCase): # prefix for the graph but later serialize() calls would work. first_out = g.serialize(format='trig') second_out = g.serialize(format='trig') - self.assertIn('@prefix ns1: .', second_out) - self.assertIn('@prefix ns1: .', first_out) + self.assertIn(b'@prefix ns1: .', second_out) + self.assertIn(b'@prefix ns1: .', first_out) print first_out @@ -69,7 +69,7 @@ class TestTrig(unittest.TestCase): rdflib.Literal("example 1"), rdflib.URIRef("http://example.com/graph1"))) out = g.serialize(format='trig') - self.assertIn('ns1:graph1 {', out) + self.assertIn(b'ns1:graph1 {', out) def testGraphUriSyntax(self): g = rdflib.ConjunctiveGraph() @@ -80,4 +80,4 @@ class TestTrig(unittest.TestCase): # out as a '<...>' term. rdflib.URIRef("http://example.com/foo."))) out = g.serialize(format='trig') - self.assertIn(' {', out) + self.assertIn(b' {', out) -- cgit v1.2.1 From aff5b2525154932e7162327139a1d445b02a65bf Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Tue, 31 Mar 2015 22:01:42 -0700 Subject: run_tests_py3.sh shouldn't lose cmdline args --- run_tests_py3.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 $@ -- cgit v1.2.1 From f167ecf880ea48490c0ecc3e61103e784c42438a Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Tue, 31 Mar 2015 22:19:30 -0700 Subject: serialize bnode graph ids in trig correctly --- rdflib/plugins/serializers/trig.py | 9 ++++++--- test/test_trig.py | 33 +++++++++++++++++---------------- 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: .', second_out) self.assertIn(b'@prefix ns1: .', 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' {', 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)) + -- cgit v1.2.1 From 76075d9939f024402a1447b71781a0d6df5cea87 Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Tue, 31 Mar 2015 22:25:41 -0700 Subject: don't use assertIn; not supported in py2.6 --- test/test_trig.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_trig.py b/test/test_trig.py index c985e002..e05f483e 100644 --- a/test/test_trig.py +++ b/test/test_trig.py @@ -58,14 +58,14 @@ class TestTrig(unittest.TestCase): # prefix for the graph but later serialize() calls would work. first_out = g.serialize(format='trig') second_out = g.serialize(format='trig') - self.assertIn(b'@prefix ns1: .', second_out) - self.assertIn(b'@prefix ns1: .', first_out) + self.assert_(b'@prefix ns1: .' in second_out) + self.assert_(b'@prefix ns1: .' in first_out) def testGraphQnameSyntax(self): g = rdflib.ConjunctiveGraph() g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),)) out = g.serialize(format='trig') - self.assertIn(b'ns1:graph1 {', out) + self.assert_(b'ns1:graph1 {' in out) def testGraphUriSyntax(self): g = rdflib.ConjunctiveGraph() @@ -73,7 +73,7 @@ class TestTrig(unittest.TestCase): # a '<...>' term. g.add(TRIPLE + (rdflib.URIRef("http://example.com/foo."),)) out = g.serialize(format='trig') - self.assertIn(b' {', out) + self.assert_(b' {' in out) def testBlankGraphIdentifier(self): g = rdflib.ConjunctiveGraph() -- cgit v1.2.1