Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/rdflib/plugins/serializers/trig.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 Trig RDF graph serializer for RDFLib. | |
3 See <http://www.w3.org/TR/trig/> for syntax specification. | |
4 """ | |
5 | |
6 from collections import defaultdict | |
7 | |
8 from rdflib.plugins.serializers.turtle import TurtleSerializer, _GEN_QNAME_FOR_DT, VERB | |
9 | |
10 from rdflib.term import BNode, Literal | |
11 | |
12 __all__ = ['TrigSerializer'] | |
13 | |
14 | |
15 class TrigSerializer(TurtleSerializer): | |
16 | |
17 short_name = "trig" | |
18 indentString = 4 * ' ' | |
19 | |
20 def __init__(self, store): | |
21 if store.context_aware: | |
22 self.contexts = list(store.contexts()) | |
23 self.default_context = store.default_context.identifier | |
24 if store.default_context: | |
25 self.contexts.append(store.default_context) | |
26 else: | |
27 self.contexts = [store] | |
28 self.default_context = None | |
29 | |
30 super(TrigSerializer, self).__init__(store) | |
31 | |
32 def preprocess(self): | |
33 for context in self.contexts: | |
34 self.store = context | |
35 self.getQName(context.identifier) | |
36 self._references = defaultdict(int) | |
37 self._subjects = {} | |
38 | |
39 for triple in context: | |
40 self.preprocessTriple(triple) | |
41 | |
42 self._contexts[context]=(self.orderSubjects(), self._subjects, self._references) | |
43 | |
44 def reset(self): | |
45 super(TrigSerializer, self).reset() | |
46 self._contexts = {} | |
47 | |
48 def serialize(self, stream, base=None, encoding=None, | |
49 spacious=None, **args): | |
50 self.reset() | |
51 self.stream = stream | |
52 self.base = base | |
53 | |
54 if spacious is not None: | |
55 self._spacious = spacious | |
56 | |
57 self.preprocess() | |
58 | |
59 self.startDocument() | |
60 | |
61 firstTime = True | |
62 for store, (ordered_subjects, subjects, ref) in list(self._contexts.items()): | |
63 if not ordered_subjects: continue | |
64 | |
65 self._references = ref | |
66 self._serialized = {} | |
67 self.store = store | |
68 self._subjects = subjects | |
69 | |
70 if self.default_context and store.identifier==self.default_context: | |
71 self.write(self.indent() + '\n{') | |
72 else: | |
73 if isinstance(store.identifier, BNode): | |
74 iri = store.identifier.n3() | |
75 else: | |
76 iri = self.getQName(store.identifier) | |
77 if iri is None: | |
78 iri = store.identifier.n3() | |
79 self.write(self.indent() + '\n%s {' % iri) | |
80 | |
81 self.depth += 1 | |
82 for subject in ordered_subjects: | |
83 if self.isDone(subject): | |
84 continue | |
85 if firstTime: | |
86 firstTime = False | |
87 if self.statement(subject) and not firstTime: | |
88 self.write('\n') | |
89 self.depth -= 1 | |
90 self.write('}\n') | |
91 | |
92 self.endDocument() | |
93 stream.write("\n".encode('ascii')) |