comparison env/lib/python3.7/site-packages/rdflib/plugins/serializers/nquads.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 import warnings
2
3 from rdflib.term import Literal
4 from rdflib.serializer import Serializer
5 from rdflib.py3compat import b
6
7 from rdflib.plugins.serializers.nt import _quoteLiteral
8
9 __all__ = ['NQuadsSerializer']
10
11
12 class NQuadsSerializer(Serializer):
13
14 def __init__(self, store):
15 if not store.context_aware:
16 raise Exception(
17 "NQuads serialization only makes "
18 "sense for context-aware stores!")
19
20 super(NQuadsSerializer, self).__init__(store)
21
22 def serialize(self, stream, base=None, encoding=None, **args):
23 if base is not None:
24 warnings.warn("NQuadsSerializer does not support base.")
25 if encoding is not None:
26 warnings.warn("NQuadsSerializer does not use custom encoding.")
27 encoding = self.encoding
28 for context in self.store.contexts():
29 for triple in context:
30 stream.write(_nq_row(
31 triple, context.identifier).encode(encoding, "replace"))
32 stream.write(b("\n"))
33
34
35 def _nq_row(triple, context):
36 if isinstance(triple[2], Literal):
37 return "%s %s %s %s .\n" % (triple[0].n3(),
38 triple[1].n3(),
39 _quoteLiteral(triple[2]),
40 context.n3())
41 else:
42 return "%s %s %s %s .\n" % (triple[0].n3(),
43 triple[1].n3(),
44 triple[2].n3(),
45 context.n3())