Skip to content

Commit

Permalink
fixes for turtle/trig namespace handling
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
gromgull committed Jan 24, 2017
1 parent 5c2b569 commit 93c7426
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
5 changes: 4 additions & 1 deletion rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(self, store):
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):
Expand Down Expand Up @@ -195,7 +197,8 @@ def addNamespace(self, prefix, namespace):
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

Expand Down
11 changes: 6 additions & 5 deletions test/test_trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import rdflib
import re

from nose import SkipTest
from rdflib.py3compat import b

TRIPLE = (rdflib.URIRef("http://example.com/s"),
Expand Down Expand Up @@ -118,7 +119,7 @@ def testGraphParsing(self):

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> .
Expand Down Expand Up @@ -153,7 +154,7 @@ def testDefaultGraphSerializesWithoutName(self):
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):

Expand All @@ -173,6 +174,6 @@ def testPrefixes(self):
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)
23 changes: 0 additions & 23 deletions test/test_trig_export.py

This file was deleted.

0 comments on commit 93c7426

Please sign in to comment.