summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGunnar Aastrand Grimnes <gromgull@gmail.com>2017-01-24 13:59:26 +0100
committerGunnar Aastrand Grimnes <gromgull@users.noreply.github.com>2017-01-24 15:04:10 +0100
commit93c74261c4a04049026738ace46cb22902357219 (patch)
treeadc2cad50adc67c1dad1f162ccb81ff8c44aa284
parent5c2b56953850a0035e2081372c4afc756c546ea3 (diff)
downloadrdflib-93c74261c4a04049026738ace46cb22902357219.tar.gz
fixes for turtle/trig namespace handling
raise exception when trying to rebind a prefix to another ns. fix broken rebinding when generating prefixes This fixes #679 - but actually it's more like a work-around. The underlying problem is confusion about context and graph objects (#167)
-rw-r--r--rdflib/plugins/serializers/turtle.py5
-rw-r--r--test/test_trig.py11
-rw-r--r--test/test_trig_export.py23
3 files changed, 10 insertions, 29 deletions
diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py
index dbef7d49..9027ed3d 100644
--- a/rdflib/plugins/serializers/turtle.py
+++ b/rdflib/plugins/serializers/turtle.py
@@ -47,6 +47,8 @@ class RecursiveSerializer(Serializer):
self.reset()
def addNamespace(self, prefix, uri):
+ if prefix in self.namespaces and self.namespaces[prefix]!=uri:
+ raise Exception("Trying to override namespace prefix %s => %s, but it's already bound to %s"%(prefix, uri, self.namespaces[prefix]))
self.namespaces[prefix] = uri
def checkSubject(self, subject):
@@ -195,7 +197,8 @@ class TurtleSerializer(RecursiveSerializer):
p = "p" + p
self._ns_rewrite[prefix] = p
- prefix = self._ns_rewrite.get(prefix, prefix)
+ prefix = self._ns_rewrite.get(prefix, prefix)
+
super(TurtleSerializer, self).addNamespace(prefix, namespace)
return prefix
diff --git a/test/test_trig.py b/test/test_trig.py
index 90dbeb07..8b3bd68f 100644
--- a/test/test_trig.py
+++ b/test/test_trig.py
@@ -2,6 +2,7 @@ import unittest
import rdflib
import re
+from nose import SkipTest
from rdflib.py3compat import b
TRIPLE = (rdflib.URIRef("http://example.com/s"),
@@ -118,7 +119,7 @@ class TestTrig(unittest.TestCase):
def testRoundTrips(self):
- raise unittest.SkipTest('skipped until 5.0')
+ raise SkipTest('skipped until 5.0')
data = """
<http://example.com/thing#thing_a> <http://example.com/knows> <http://example.com/thing#thing_b> .
@@ -153,7 +154,7 @@ class TestTrig(unittest.TestCase):
g.parse(data=data, format='trig')
data = g.serialize(format='trig')
- self.assertTrue('None' not in data)
+ self.assertTrue(b('None') not in data)
def testPrefixes(self):
@@ -173,6 +174,6 @@ class TestTrig(unittest.TestCase):
cg.parse(data=data, format='trig')
data = cg.serialize(format='trig')
- self.assert_('ns2: <http://ex.org/docs/' in data, data)
- self.assert_('<ns2:document1>' not in data, data)
- self.assert_('ns2:document1' in data, data)
+ self.assert_(b('ns2: <http://ex.org/docs/') in data, data)
+ self.assert_(b('<ns2:document1>') not in data, data)
+ self.assert_(b('ns2:document1') in data, data)
diff --git a/test/test_trig_export.py b/test/test_trig_export.py
deleted file mode 100644
index 6cf82fde..00000000
--- a/test/test_trig_export.py
+++ /dev/null
@@ -1,23 +0,0 @@
-from rdflib import URIRef, Literal, Graph, Dataset
-
-def trig_export_test():
- graphs = [(URIRef("urn:tg1"),"A"), (URIRef("urn:tg2"), "B")]
- ds = Dataset()
- for i, n in graphs:
- g = ds.graph(i)
- a = URIRef("urn:{}#S".format(n))
- b = URIRef("urn:{}#p".format(n))
- c = Literal('c')
- g.add((a,b,c))
-
-
- # this generated two graphs, with a differnet namespace for the URIs inside.
- # this tests that the prefix for each internal ns is different
-
- data = ds.serialize(format='trig')
- print data
- assert False
-
-
-if __name__ == '__main__':
- trig_export_test()