summaryrefslogtreecommitdiff
path: root/rdflib
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@csiro.au>2020-04-16 14:57:47 +1000
committerNicholas Car <nicholas.car@csiro.au>2020-04-16 14:57:47 +1000
commit528a75febfed19e23731a306d57aa97dce0fc3a8 (patch)
treeaba558a5a423ca0cc029b7142999ead71157b2d6 /rdflib
parent6a49e786de6672bce0705cbb55c3be05d928055d (diff)
downloadrdflib-528a75febfed19e23731a306d57aa97dce0fc3a8.tar.gz
improved handling of base, including at Graph() init
Diffstat (limited to 'rdflib')
-rw-r--r--rdflib/graph.py14
-rw-r--r--rdflib/plugins/serializers/turtle.py7
2 files changed, 16 insertions, 5 deletions
diff --git a/rdflib/graph.py b/rdflib/graph.py
index efd58eba..a00730ce 100644
--- a/rdflib/graph.py
+++ b/rdflib/graph.py
@@ -293,8 +293,9 @@ class Graph(Node):
For more on named graphs, see: http://www.w3.org/2004/03/trix/
"""
- def __init__(self, store="default", identifier=None, namespace_manager=None):
+ def __init__(self, store="default", identifier=None, namespace_manager=None, base=None):
super(Graph, self).__init__()
+ self.base = base
self.__identifier = identifier or BNode()
if not isinstance(self.__identifier, Node):
@@ -952,14 +953,19 @@ class Graph(Node):
Format support can be extended with plugins,
but "xml", "n3", "turtle", "nt", "pretty-xml", "trix", "trig" and "nquads" are built in.
"""
+
+ # if a base is set here, use it, else if it was set at graph creation time, use that (self.base)
+ if base is not None:
+ self.base = base
+
serializer = plugin.get(format, Serializer)(self)
if destination is None:
stream = BytesIO()
- serializer.serialize(stream, base=base, encoding=encoding, **args)
+ serializer.serialize(stream, base=self.base, encoding=encoding, **args)
return stream.getvalue()
if hasattr(destination, "write"):
stream = destination
- serializer.serialize(stream, base=base, encoding=encoding, **args)
+ serializer.serialize(stream, base=self.base, encoding=encoding, **args)
else:
location = destination
scheme, netloc, path, params, _query, fragment = urlparse(location)
@@ -970,7 +976,7 @@ class Graph(Node):
return
fd, name = tempfile.mkstemp()
stream = os.fdopen(fd, "wb")
- serializer.serialize(stream, base=base, encoding=encoding, **args)
+ serializer.serialize(stream, base=self.base, encoding=encoding, **args)
stream.close()
if hasattr(shutil, "move"):
shutil.move(name, path)
diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py
index 1c58ba1b..63f9da06 100644
--- a/rdflib/plugins/serializers/turtle.py
+++ b/rdflib/plugins/serializers/turtle.py
@@ -224,7 +224,9 @@ class TurtleSerializer(RecursiveSerializer):
spacious=None, **args):
self.reset()
self.stream = stream
- self.base = base
+ # if base is set here, override previously set base if set at graph init
+ if base is not None:
+ self.base = base
if spacious is not None:
self._spacious = spacious
@@ -291,6 +293,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: