Mercurial > repos > shellac > guppy_basecaller
diff env/lib/python3.7/site-packages/rdflib/plugins/serializers/nquads.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author | shellac |
---|---|
date | Thu, 14 May 2020 14:56:58 -0400 |
parents | 26e78fe6e8c4 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/env/lib/python3.7/site-packages/rdflib/plugins/serializers/nquads.py Thu May 14 14:56:58 2020 -0400 @@ -0,0 +1,45 @@ +import warnings + +from rdflib.term import Literal +from rdflib.serializer import Serializer +from rdflib.py3compat import b + +from rdflib.plugins.serializers.nt import _quoteLiteral + +__all__ = ['NQuadsSerializer'] + + +class NQuadsSerializer(Serializer): + + def __init__(self, store): + if not store.context_aware: + raise Exception( + "NQuads serialization only makes " + "sense for context-aware stores!") + + super(NQuadsSerializer, self).__init__(store) + + def serialize(self, stream, base=None, encoding=None, **args): + if base is not None: + warnings.warn("NQuadsSerializer does not support base.") + if encoding is not None: + warnings.warn("NQuadsSerializer does not use custom encoding.") + encoding = self.encoding + for context in self.store.contexts(): + for triple in context: + stream.write(_nq_row( + triple, context.identifier).encode(encoding, "replace")) + stream.write(b("\n")) + + +def _nq_row(triple, context): + if isinstance(triple[2], Literal): + return "%s %s %s %s .\n" % (triple[0].n3(), + triple[1].n3(), + _quoteLiteral(triple[2]), + context.n3()) + else: + return "%s %s %s %s .\n" % (triple[0].n3(), + triple[1].n3(), + triple[2].n3(), + context.n3())