Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/rdflib/plugins/serializers/nt.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 """ | |
2 N-Triples RDF graph serializer for RDFLib. | |
3 See <http://www.w3.org/TR/rdf-testcases/#ntriples> for details about the | |
4 format. | |
5 """ | |
6 from rdflib.term import Literal | |
7 from rdflib.serializer import Serializer | |
8 from rdflib.py3compat import b | |
9 | |
10 import warnings | |
11 import codecs | |
12 | |
13 __all__ = ['NTSerializer'] | |
14 | |
15 | |
16 class NTSerializer(Serializer): | |
17 """ | |
18 Serializes RDF graphs to NTriples format. | |
19 """ | |
20 | |
21 def __init__(self, store): | |
22 Serializer.__init__(self, store) | |
23 self.encoding = 'ascii' # n-triples are ascii encoded | |
24 | |
25 def serialize(self, stream, base=None, encoding=None, **args): | |
26 if base is not None: | |
27 warnings.warn("NTSerializer does not support base.") | |
28 if encoding is not None: | |
29 warnings.warn("NTSerializer does not use custom encoding.") | |
30 encoding = self.encoding | |
31 for triple in self.store: | |
32 stream.write(_nt_row(triple).encode(self.encoding, "_rdflib_nt_escape")) | |
33 stream.write(b("\n")) | |
34 | |
35 | |
36 class NT11Serializer(NTSerializer): | |
37 """ | |
38 Serializes RDF graphs to RDF 1.1 NTriples format. | |
39 | |
40 Exactly like nt - only utf8 encoded. | |
41 """ | |
42 | |
43 def __init__(self, store): | |
44 Serializer.__init__(self, store) # default to utf-8 | |
45 | |
46 | |
47 def _nt_row(triple): | |
48 if isinstance(triple[2], Literal): | |
49 return "%s %s %s .\n" % ( | |
50 triple[0].n3(), | |
51 triple[1].n3(), | |
52 _quoteLiteral(triple[2])) | |
53 else: | |
54 return "%s %s %s .\n" % (triple[0].n3(), | |
55 triple[1].n3(), | |
56 triple[2].n3()) | |
57 | |
58 | |
59 def _quoteLiteral(l): | |
60 ''' | |
61 a simpler version of term.Literal.n3() | |
62 ''' | |
63 | |
64 encoded = _quote_encode(l) | |
65 | |
66 if l.language: | |
67 if l.datatype: | |
68 raise Exception("Literal has datatype AND language!") | |
69 return '%s@%s' % (encoded, l.language) | |
70 elif l.datatype: | |
71 return '%s^^<%s>' % (encoded, l.datatype) | |
72 else: | |
73 return '%s' % encoded | |
74 | |
75 | |
76 def _quote_encode(l): | |
77 return '"%s"' % l.replace('\\', '\\\\')\ | |
78 .replace('\n', '\\n')\ | |
79 .replace('"', '\\"')\ | |
80 .replace('\r', '\\r') | |
81 | |
82 def _nt_unicode_error_resolver(err): | |
83 | |
84 """ | |
85 Do unicode char replaces as defined in https://www.w3.org/TR/2004/REC-rdf-testcases-20040210/#ntrip_strings | |
86 """ | |
87 | |
88 def _replace_single(c): | |
89 c = ord(c) | |
90 fmt = '\\u%04X' if c <= 0xFFFF else '\\U%08X' | |
91 return fmt % c | |
92 | |
93 string = err.object[err.start:err.end] | |
94 return ( "".join( _replace_single(c) for c in string ), err.end ) | |
95 | |
96 codecs.register_error('_rdflib_nt_escape', _nt_unicode_error_resolver) |