Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/rdflib/plugins/parsers/trig.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author | shellac |
---|---|
date | Thu, 14 May 2020 14:56:58 -0400 |
parents | 26e78fe6e8c4 |
children |
comparison
equal
deleted
inserted
replaced
1:75ca89e9b81c | 2:6af9afd405e9 |
---|---|
1 from rdflib import ConjunctiveGraph | |
2 from rdflib.parser import Parser | |
3 from .notation3 import SinkParser, RDFSink | |
4 | |
5 | |
6 def becauseSubGraph(*args, **kwargs): pass | |
7 | |
8 | |
9 class TrigSinkParser(SinkParser): | |
10 | |
11 def directiveOrStatement(self, argstr, h): | |
12 | |
13 #import pdb; pdb.set_trace() | |
14 | |
15 i = self.skipSpace(argstr, h) | |
16 if i < 0: | |
17 return i # EOF | |
18 | |
19 j = self.graph(argstr, i) | |
20 if j >= 0: | |
21 return j | |
22 | |
23 j = self.sparqlDirective(argstr, i) | |
24 if j >= 0: | |
25 return j | |
26 | |
27 j = self.directive(argstr, i) | |
28 if j >= 0: | |
29 return self.checkDot(argstr, j) | |
30 | |
31 j = self.statement(argstr, i) | |
32 if j >= 0: | |
33 return self.checkDot(argstr, j) | |
34 | |
35 | |
36 return j | |
37 | |
38 def labelOrSubject(self, argstr, i, res): | |
39 j = self.skipSpace(argstr, i) | |
40 if j < 0: | |
41 return j # eof | |
42 i = j | |
43 | |
44 j = self.uri_ref2(argstr, i, res) | |
45 if j >= 0: | |
46 return j | |
47 | |
48 if argstr[i] == '[': | |
49 j = self.skipSpace(argstr, i+1) | |
50 if j < 0: | |
51 self.BadSyntax(argstr, i, | |
52 "Expected ] got EOF") | |
53 if argstr[j] == ']': | |
54 res.append(self.blankNode()) | |
55 return j+1 | |
56 return -1 | |
57 | |
58 def graph(self, argstr, i): | |
59 """ | |
60 Parse trig graph, i.e. | |
61 | |
62 <urn:graphname> = { .. triples .. } | |
63 | |
64 return -1 if it doesn't look like a graph-decl | |
65 raise Exception if it looks like a graph, but isn't. | |
66 """ | |
67 | |
68 #import pdb; pdb.set_trace() | |
69 j = self.sparqlTok('GRAPH', argstr, i) # optional GRAPH keyword | |
70 if j >= 0: i = j | |
71 | |
72 r = [] | |
73 j = self.labelOrSubject(argstr, i, r) | |
74 if j >= 0: | |
75 graph = r[0] | |
76 i = j | |
77 else: | |
78 graph = self._store.graph.identifier # hack | |
79 | |
80 | |
81 j = self.skipSpace(argstr, i) | |
82 if j < 0: | |
83 self.BadSyntax(argstr, i, | |
84 "EOF found when expected graph") | |
85 | |
86 if argstr[j:j + 1] == "=": # optional = for legacy support | |
87 | |
88 i = self.skipSpace(argstr, j + 1) | |
89 if i < 0: | |
90 self.BadSyntax(argstr, i, "EOF found when expecting '{'") | |
91 else: | |
92 i = j | |
93 | |
94 if argstr[i:i+1] != "{": | |
95 return -1 # the node wasn't part of a graph | |
96 | |
97 | |
98 j = i+1 | |
99 | |
100 oldParentContext = self._parentContext | |
101 self._parentContext = self._context | |
102 reason2 = self._reason2 | |
103 self._reason2 = becauseSubGraph | |
104 self._context = self._store.newGraph(graph) | |
105 | |
106 while 1: | |
107 i = self.skipSpace(argstr, j) | |
108 if i < 0: | |
109 self.BadSyntax( | |
110 argstr, i, "needed '}', found end.") | |
111 | |
112 if argstr[i:i + 1] == "}": | |
113 j = i + 1 | |
114 break | |
115 | |
116 j = self.directiveOrStatement(argstr, i) | |
117 if j < 0: | |
118 self.BadSyntax( | |
119 argstr, i, "expected statement or '}'") | |
120 | |
121 self._context = self._parentContext | |
122 self._reason2 = reason2 | |
123 self._parentContext = oldParentContext | |
124 #res.append(subj.close()) # No use until closed | |
125 return j | |
126 | |
127 | |
128 | |
129 | |
130 class TrigParser(Parser): | |
131 """ | |
132 An RDFLib parser for TriG | |
133 | |
134 """ | |
135 | |
136 def __init__(self): | |
137 pass | |
138 | |
139 def parse(self, source, graph, encoding="utf-8"): | |
140 | |
141 if encoding not in [None, "utf-8"]: | |
142 raise Exception( | |
143 ("TriG files are always utf-8 encoded, ", | |
144 "I was passed: %s") % encoding) | |
145 | |
146 # we're currently being handed a Graph, not a ConjunctiveGraph | |
147 assert graph.store.context_aware, "TriG Parser needs a context-aware store!" | |
148 | |
149 conj_graph = ConjunctiveGraph(store=graph.store, identifier=graph.identifier) | |
150 conj_graph.default_context = graph # TODO: CG __init__ should have a | |
151 # default_context arg | |
152 # TODO: update N3Processor so that it can use conj_graph as the sink | |
153 conj_graph.namespace_manager = graph.namespace_manager | |
154 | |
155 sink = RDFSink(conj_graph) | |
156 | |
157 baseURI = conj_graph.absolutize( | |
158 source.getPublicId() or source.getSystemId() or "") | |
159 p = TrigSinkParser(sink, baseURI=baseURI, turtle=True) | |
160 | |
161 p.loadStream(source.getByteStream()) | |
162 | |
163 for prefix, namespace in list(p._bindings.items()): | |
164 conj_graph.bind(prefix, namespace) | |
165 | |
166 # return ??? |