diff options
author | Nicholas Car <nicholas.car@csiro.au> | 2020-04-16 13:18:27 +1000 |
---|---|---|
committer | Nicholas Car <nicholas.car@csiro.au> | 2020-04-16 13:18:27 +1000 |
commit | 64075888db1aa2ef14964d756e5802215d2e99b2 (patch) | |
tree | ddd806073af45c3448903a14ee5bed511c2cdf57 | |
parent | 8dce2dd9b2162d949cd26ba958c58d23410f1a81 (diff) | |
download | rdflib-64075888db1aa2ef14964d756e5802215d2e99b2.tar.gz |
fixes Issue 1003, with tests
-rw-r--r-- | rdflib/plugins/serializers/turtle.py | 3 | ||||
-rw-r--r-- | test/test_issue1003.py | 28 |
2 files changed, 31 insertions, 0 deletions
diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py index 1c58ba1b..6d31c579 100644 --- a/rdflib/plugins/serializers/turtle.py +++ b/rdflib/plugins/serializers/turtle.py @@ -291,6 +291,9 @@ class TurtleSerializer(RecursiveSerializer): def startDocument(self): self._started = True ns_list = sorted(self.namespaces.items()) + + if self.base: + self.write(self.indent() + '@base <%s> .\n' % self.base) for prefix, uri in ns_list: self.write(self.indent() + '@prefix %s: <%s> .\n' % (prefix, uri)) if ns_list and self._spacious: diff --git a/test/test_issue1003.py b/test/test_issue1003.py new file mode 100644 index 00000000..34a194a6 --- /dev/null +++ b/test/test_issue1003.py @@ -0,0 +1,28 @@ +from rdflib import Graph, Literal, Namespace, RDF, URIRef +from rdflib.namespace import SKOS, DCTERMS + +g = Graph() + +base = Namespace("http://example.org/") + +title = Literal("Title", lang="en") +description = Literal("Test Description", lang="en") +creator = URIRef("https://creator.com") +cs = URIRef("") + +g.add((cs, RDF.type, SKOS.ConceptScheme)) +g.add((cs, DCTERMS.creator, creator)) +g.add((cs, DCTERMS.source, URIRef("nick"))) + +# Bind a few prefix, namespace pairs for more readable output +g.bind("dct", DCTERMS) +g.bind("skos", SKOS) + +# this one should not have @base in the output +assert "@base" not in g.serialize(format='turtle').decode("utf-8") +# this one should have @base in the output +assert "@base" in g.serialize(format='turtle', base=base).decode("utf-8") + +# and same for N3 (same serializer used) +assert "@base" not in g.serialize(format='n3').decode("utf-8") +assert "@base" in g.serialize(format='n3', base=base).decode("utf-8") |