Skip to content

Commit

Permalink
Check and rewrite namespace prefixes starting with _ in turtle serial…
Browse files Browse the repository at this point in the history
…izer. fixes #161
  • Loading branch information
gromgull committed Apr 16, 2012
1 parent cfdc4b0 commit 7859342
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
27 changes: 26 additions & 1 deletion rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,35 @@ def __init__(self, store):
self.reset()
self.stream = None
self._spacious = _SPACIOUS_OUTPUT
self._ns_rewrite={}

def addNamespace(self, prefix, namespace):
# Turtle does not support prefix that start with _
# if they occur in the graph, rewrite to p_blah
# this is more complicated since we need to make sure p_blah
# does not already exist. And we register namespaces as we go, i.e.
# we may first see a triple with prefix _9 - rewrite it to p_9
# and then later find a triple with a "real" p_9 prefix

# so we need to keep track of ns rewrites we made so far.

if prefix[0]=='_' or self.namespaces.get(prefix, namespace)!=namespace:

if prefix not in self._ns_rewrite:
p="p"+prefix
while p in self.namespaces:
p="p"+p
self._ns_rewrite[prefix]=p

prefix=self._ns_rewrite.get(prefix,prefix)
super(TurtleSerializer, self).addNamespace(prefix, namespace)
return prefix

def reset(self):
super(TurtleSerializer, self).reset()
self._shortNames = {}
self._started = False
self._ns_rewrite={}

def serialize(self, stream, base=None, encoding=None, spacious=None, **args):
self.reset()
Expand Down Expand Up @@ -220,7 +244,8 @@ def getQName(self, uri, gen_prefix=True):
# Local parts with '.' will mess up serialization
if '.' in local:
return None
self.addNamespace(prefix, namespace)
prefix=self.addNamespace(prefix, namespace)

return u'%s:%s' % (prefix, local)

def startDocument(self):
Expand Down
14 changes: 11 additions & 3 deletions test/test_issue161.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
from unittest import TestCase
from rdflib.graph import ConjunctiveGraph
from nose.exc import SkipTest


class EntityTest(TestCase):

def test_turtle_namespace_prefixes(self):
raise SkipTest('Skipping: Turtle serializer preserves n3 prefixes (eg. "_9") that violate Turtle syntax.')

g = ConjunctiveGraph()
n3 = \
"""
@prefix _9: <http://data.linkedmdb.org/resource/movie/> .
@prefix p_9: <urn:test:> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
p_9:a p_9:b p_9:c .
<http://data.linkedmdb.org/resource/director/1> a
<http://data.linkedmdb.org/resource/movie/director>;
rdfs:label "Cecil B. DeMille (Director)";
_9:director_name "Cecil B. DeMille" ."""


g.parse(data=n3, format='n3')
turtle = g.serialize(format="turtle")

# Check round-tripping, just for kicks.
g = ConjunctiveGraph()
g.parse(data=turtle, format='turtle')
# Shouldn't have got to here
self.assert_('_9' not in g.serialize(format="turtle"))
s=g.serialize(format="turtle")

self.assert_('@prefix _9' not in s)



Expand Down

0 comments on commit 7859342

Please sign in to comment.