comparison planemo/lib/python3.7/site-packages/rdflib/plugin.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 Plugin support for rdf.
3
4 There are a number of plugin points for rdf: parser, serializer,
5 store, query processor, and query result. Plugins can be registered
6 either through setuptools entry_points or by calling
7 rdf.plugin.register directly.
8
9 If you have a package that uses a setuptools based setup.py you can add the
10 following to your setup::
11
12 entry_points = {
13 'rdf.plugins.parser': [
14 'nt = rdf.plugins.parsers.nt:NTParser',
15 ],
16 'rdf.plugins.serializer': [
17 'nt = rdf.plugins.serializers.NTSerializer:NTSerializer',
18 ],
19 }
20
21 See the `setuptools dynamic discovery of services and plugins`__ for more
22 information.
23
24 .. __: http://peak.telecommunity.com/DevCenter/setuptools#dynamic-discovery-of-services-and-plugins
25
26 """
27
28 from rdflib.store import Store
29 from rdflib.parser import Parser
30 from rdflib.serializer import Serializer
31 from rdflib.query import ResultParser, ResultSerializer, \
32 Processor, Result, UpdateProcessor
33 from rdflib.exceptions import Error
34
35 __all__ = [
36 'register', 'get', 'plugins', 'PluginException', 'Plugin', 'PKGPlugin']
37
38 entry_points = {'rdf.plugins.store': Store,
39 'rdf.plugins.serializer': Serializer,
40 'rdf.plugins.parser': Parser,
41 'rdf.plugins.resultparser': ResultParser,
42 'rdf.plugins.resultserializer': ResultSerializer,
43 'rdf.plugins.queryprocessor': Processor,
44 'rdf.plugins.queryresult': Result,
45 'rdf.plugins.updateprocessor': UpdateProcessor
46 }
47
48 _plugins = {}
49
50
51 class PluginException(Error):
52 pass
53
54
55 class Plugin(object):
56
57 def __init__(self, name, kind, module_path, class_name):
58 self.name = name
59 self.kind = kind
60 self.module_path = module_path
61 self.class_name = class_name
62 self._class = None
63
64 def getClass(self):
65 if self._class is None:
66 module = __import__(self.module_path, globals(), locals(), [""])
67 self._class = getattr(module, self.class_name)
68 return self._class
69
70
71 class PKGPlugin(Plugin):
72
73 def __init__(self, name, kind, ep):
74 self.name = name
75 self.kind = kind
76 self.ep = ep
77 self._class = None
78
79 def getClass(self):
80 if self._class is None:
81 self._class = self.ep.load()
82 return self._class
83
84
85 def register(name, kind, module_path, class_name):
86 """
87 Register the plugin for (name, kind). The module_path and
88 class_name should be the path to a plugin class.
89 """
90 p = Plugin(name, kind, module_path, class_name)
91 _plugins[(name, kind)] = p
92
93
94 def get(name, kind):
95 """
96 Return the class for the specified (name, kind). Raises a
97 PluginException if unable to do so.
98 """
99 try:
100 p = _plugins[(name, kind)]
101 except KeyError:
102 raise PluginException(
103 "No plugin registered for (%s, %s)" % (name, kind))
104 return p.getClass()
105
106
107 try:
108 from pkg_resources import iter_entry_points
109 except ImportError:
110 pass # TODO: log a message
111 else:
112 # add the plugins specified via pkg_resources' EntryPoints.
113 for entry_point, kind in entry_points.items():
114 for ep in iter_entry_points(entry_point):
115 _plugins[(ep.name, kind)] = PKGPlugin(ep.name, kind, ep)
116
117
118 def plugins(name=None, kind=None):
119 """
120 A generator of the plugins.
121
122 Pass in name and kind to filter... else leave None to match all.
123 """
124 for p in list(_plugins.values()):
125 if (name is None or name == p.name) and (
126 kind is None or kind == p.kind):
127 yield p
128
129 register(
130 'default', Store,
131 'rdflib.plugins.memory', 'IOMemory')
132 register(
133 'IOMemory', Store,
134 'rdflib.plugins.memory', 'IOMemory')
135 register(
136 'Auditable', Store,
137 'rdflib.plugins.stores.auditable', 'AuditableStore')
138 register(
139 'Concurrent', Store,
140 'rdflib.plugins.stores.concurrent', 'ConcurrentStore')
141 register(
142 'Sleepycat', Store,
143 'rdflib.plugins.sleepycat', 'Sleepycat')
144 register(
145 'SPARQLStore', Store,
146 'rdflib.plugins.stores.sparqlstore', 'SPARQLStore')
147 register(
148 'SPARQLUpdateStore', Store,
149 'rdflib.plugins.stores.sparqlstore', 'SPARQLUpdateStore')
150
151 register(
152 'application/rdf+xml', Serializer,
153 'rdflib.plugins.serializers.rdfxml', 'XMLSerializer')
154 register(
155 'xml', Serializer,
156 'rdflib.plugins.serializers.rdfxml', 'XMLSerializer')
157 register(
158 'text/n3', Serializer,
159 'rdflib.plugins.serializers.n3', 'N3Serializer')
160 register(
161 'n3', Serializer,
162 'rdflib.plugins.serializers.n3', 'N3Serializer')
163 register(
164 'text/turtle', Serializer,
165 'rdflib.plugins.serializers.turtle', 'TurtleSerializer')
166 register(
167 'turtle', Serializer,
168 'rdflib.plugins.serializers.turtle', 'TurtleSerializer')
169 register(
170 'ttl', Serializer,
171 'rdflib.plugins.serializers.turtle', 'TurtleSerializer')
172 register(
173 'trig', Serializer,
174 'rdflib.plugins.serializers.trig', 'TrigSerializer')
175 register(
176 'application/n-triples', Serializer,
177 'rdflib.plugins.serializers.nt', 'NTSerializer')
178 register(
179 'ntriples', Serializer,
180 'rdflib.plugins.serializers.nt', 'NTSerializer')
181 register(
182 'nt', Serializer,
183 'rdflib.plugins.serializers.nt', 'NTSerializer')
184 register(
185 'nt11', Serializer,
186 'rdflib.plugins.serializers.nt', 'NT11Serializer')
187
188 register(
189 'pretty-xml', Serializer,
190 'rdflib.plugins.serializers.rdfxml', 'PrettyXMLSerializer')
191 register(
192 'trix', Serializer,
193 'rdflib.plugins.serializers.trix', 'TriXSerializer')
194 register(
195 'application/trix', Serializer,
196 'rdflib.plugins.serializers.trix', 'TriXSerializer')
197 register(
198 'application/n-quads', Serializer,
199 'rdflib.plugins.serializers.nquads', 'NQuadsSerializer')
200 register(
201 'nquads', Serializer,
202 'rdflib.plugins.serializers.nquads', 'NQuadsSerializer')
203
204 register(
205 'application/rdf+xml', Parser,
206 'rdflib.plugins.parsers.rdfxml', 'RDFXMLParser')
207 register(
208 'xml', Parser,
209 'rdflib.plugins.parsers.rdfxml', 'RDFXMLParser')
210 register(
211 'text/n3', Parser,
212 'rdflib.plugins.parsers.notation3', 'N3Parser')
213 register(
214 'n3', Parser,
215 'rdflib.plugins.parsers.notation3', 'N3Parser')
216 register(
217 'text/turtle', Parser,
218 'rdflib.plugins.parsers.notation3', 'TurtleParser')
219 register(
220 'turtle', Parser,
221 'rdflib.plugins.parsers.notation3', 'TurtleParser')
222 register(
223 'ttl', Parser,
224 'rdflib.plugins.parsers.notation3', 'TurtleParser')
225 register(
226 'application/n-triples', Parser,
227 'rdflib.plugins.parsers.nt', 'NTParser')
228 register(
229 'ntriples', Parser,
230 'rdflib.plugins.parsers.nt', 'NTParser')
231 register(
232 'nt', Parser,
233 'rdflib.plugins.parsers.nt', 'NTParser')
234 register(
235 'nt11', Parser,
236 'rdflib.plugins.parsers.nt', 'NTParser')
237 register(
238 'application/n-quads', Parser,
239 'rdflib.plugins.parsers.nquads', 'NQuadsParser')
240 register(
241 'nquads', Parser,
242 'rdflib.plugins.parsers.nquads', 'NQuadsParser')
243 register(
244 'application/trix', Parser,
245 'rdflib.plugins.parsers.trix', 'TriXParser')
246 register(
247 'trix', Parser,
248 'rdflib.plugins.parsers.trix', 'TriXParser')
249 register(
250 'trig', Parser,
251 'rdflib.plugins.parsers.trig', 'TrigParser')
252
253 # The basic parsers: RDFa (by default, 1.1),
254 # microdata, and embedded turtle (a.k.a. hturtle)
255 register(
256 'hturtle', Parser,
257 'rdflib.plugins.parsers.hturtle', 'HTurtleParser')
258 register(
259 'rdfa', Parser,
260 'rdflib.plugins.parsers.structureddata', 'RDFaParser')
261 register(
262 'mdata', Parser,
263 'rdflib.plugins.parsers.structureddata', 'MicrodataParser')
264 register(
265 'microdata', Parser,
266 'rdflib.plugins.parsers.structureddata', 'MicrodataParser')
267 # A convenience to use the RDFa 1.0 syntax (although the parse method can
268 # be invoked with an rdfa_version keyword, too)
269 register(
270 'rdfa1.0', Parser,
271 'rdflib.plugins.parsers.structureddata', 'RDFa10Parser')
272 # Just for the completeness, if the user uses this
273 register(
274 'rdfa1.1', Parser,
275 'rdflib.plugins.parsers.structureddata', 'RDFaParser')
276 # An HTML file may contain both microdata, rdfa, or turtle. If the user
277 # wants them all, the parser below simply invokes all:
278 register(
279 'html', Parser,
280 'rdflib.plugins.parsers.structureddata', 'StructuredDataParser')
281 # Some media types are also bound to RDFa
282 register(
283 'application/svg+xml', Parser,
284 'rdflib.plugins.parsers.structureddata', 'RDFaParser')
285 register(
286 'application/xhtml+xml', Parser,
287 'rdflib.plugins.parsers.structureddata', 'RDFaParser')
288 # 'text/html' media type should be equivalent to html:
289 register(
290 'text/html', Parser,
291 'rdflib.plugins.parsers.structureddata', 'StructuredDataParser')
292
293
294 register(
295 'sparql', Result,
296 'rdflib.plugins.sparql.processor', 'SPARQLResult')
297 register(
298 'sparql', Processor,
299 'rdflib.plugins.sparql.processor', 'SPARQLProcessor')
300
301 register(
302 'sparql', UpdateProcessor,
303 'rdflib.plugins.sparql.processor', 'SPARQLUpdateProcessor')
304
305
306 register(
307 'xml', ResultSerializer,
308 'rdflib.plugins.sparql.results.xmlresults', 'XMLResultSerializer')
309 register(
310 'txt', ResultSerializer,
311 'rdflib.plugins.sparql.results.txtresults', 'TXTResultSerializer')
312 register(
313 'json', ResultSerializer,
314 'rdflib.plugins.sparql.results.jsonresults', 'JSONResultSerializer')
315 register(
316 'csv', ResultSerializer,
317 'rdflib.plugins.sparql.results.csvresults', 'CSVResultSerializer')
318
319 register(
320 'xml', ResultParser,
321 'rdflib.plugins.sparql.results.xmlresults', 'XMLResultParser')
322 register(
323 'json', ResultParser,
324 'rdflib.plugins.sparql.results.jsonresults', 'JSONResultParser')
325 register(
326 'csv', ResultParser,
327 'rdflib.plugins.sparql.results.csvresults', 'CSVResultParser')
328 register(
329 'tsv', ResultParser,
330 'rdflib.plugins.sparql.results.tsvresults', 'TSVResultParser')