Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_pubsub_schema.py @ 2562:26edcf3a30eb
core, setup: huge cleaning:
- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention
- move twisted directory to root
- removed all hacks from setup.py, and added missing dependencies, it is now clean
- use https URL for website in setup.py
- removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed
- renamed sat.sh to sat and fixed its installation
- added python_requires to specify Python version needed
- replaced glib2reactor which use deprecated code by gtk3reactor
sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Apr 2018 19:44:50 +0200 |
parents | src/plugins/plugin_exp_pubsub_schema.py@0062d3e79d12 |
children | 5d4ac5415b40 |
comparison
equal
deleted
inserted
replaced
2561:bd30dc3ffe5a | 2562:26edcf3a30eb |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for Pubsub Schemas | |
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core import exceptions | |
22 from sat.core.constants import Const as C | |
23 from sat.tools import xml_tools | |
24 from sat.tools import utils | |
25 from twisted.words.protocols.jabber import jid | |
26 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | |
27 from twisted.internet import defer | |
28 from sat.core.log import getLogger | |
29 log = getLogger(__name__) | |
30 from wokkel import disco, iwokkel | |
31 from wokkel import data_form | |
32 from wokkel import generic | |
33 from zope.interface import implements | |
34 from collections import Iterable | |
35 import copy | |
36 import itertools | |
37 | |
38 NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0' | |
39 | |
40 PLUGIN_INFO = { | |
41 C.PI_NAME: "PubSub Schema", | |
42 C.PI_IMPORT_NAME: "PUBSUB_SCHEMA", | |
43 C.PI_TYPE: "EXP", | |
44 C.PI_PROTOCOLS: [], | |
45 C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"], | |
46 C.PI_MAIN: "PubsubSchema", | |
47 C.PI_HANDLER: "yes", | |
48 C.PI_DESCRIPTION: _("""Handle Pubsub data schemas""") | |
49 } | |
50 | |
51 | |
52 class PubsubSchema(object): | |
53 | |
54 def __init__(self, host): | |
55 log.info(_(u"PubSub Schema initialization")) | |
56 self.host = host | |
57 self._p = self.host.plugins["XEP-0060"] | |
58 self._i = self.host.plugins["IDENTITY"] | |
59 host.bridge.addMethod("psSchemaGet", ".plugin", | |
60 in_sign='sss', out_sign='s', | |
61 method=self._getSchema, | |
62 async=True | |
63 ) | |
64 host.bridge.addMethod("psSchemaSet", ".plugin", | |
65 in_sign='ssss', out_sign='', | |
66 method=self._setSchema, | |
67 async=True | |
68 ) | |
69 host.bridge.addMethod("psSchemaUIGet", ".plugin", | |
70 in_sign='sss', out_sign='s', | |
71 method=utils.partial(self._getUISchema, default_node=None), | |
72 async=True | |
73 ) | |
74 host.bridge.addMethod("psItemsFormGet", ".plugin", | |
75 in_sign='ssssiassa{ss}s', out_sign='(asa{ss})', | |
76 method=self._getDataFormItems, | |
77 async=True) | |
78 host.bridge.addMethod("psItemFormSend", ".plugin", | |
79 in_sign='ssa{sas}ssa{ss}s', out_sign='s', | |
80 method=self._sendDataFormItem, | |
81 async=True) | |
82 | |
83 def getHandler(self, client): | |
84 return SchemaHandler() | |
85 | |
86 def _getSchemaBridgeCb(self, schema_elt): | |
87 if schema_elt is None: | |
88 return u'' | |
89 return schema_elt.toXml() | |
90 | |
91 def _getSchema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE): | |
92 client = self.host.getClient(profile_key) | |
93 service = None if not service else jid.JID(service) | |
94 d = self.getSchema(client, service, nodeIdentifier) | |
95 d.addCallback(self._getSchemaBridgeCb) | |
96 return d | |
97 | |
98 def _getSchemaCb(self, iq_elt): | |
99 try: | |
100 schema_elt = next(iq_elt.elements(NS_SCHEMA, 'schema')) | |
101 except StopIteration: | |
102 raise exceptions.DataError('missing <schema> element') | |
103 try: | |
104 x_elt = next(schema_elt.elements((data_form.NS_X_DATA, 'x'))) | |
105 except StopIteration: | |
106 # there is not schema on this node | |
107 return None | |
108 return x_elt | |
109 | |
110 def getSchema(self, client, service, nodeIdentifier): | |
111 """retrieve PubSub node schema | |
112 | |
113 @param service(jid.JID, None): jid of PubSub service | |
114 None to use our PEP | |
115 @param nodeIdentifier(unicode): node to get schema from | |
116 @return (domish.Element, None): schema (<x> element) | |
117 None if not schema has been set on this node | |
118 """ | |
119 iq_elt = client.IQ(u'get') | |
120 if service is not None: | |
121 iq_elt['to'] = service.full() | |
122 pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub')) | |
123 schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema')) | |
124 schema_elt['node'] = nodeIdentifier | |
125 d = iq_elt.send() | |
126 d.addCallback(self._getSchemaCb) | |
127 return d | |
128 | |
129 @defer.inlineCallbacks | |
130 def getSchemaForm(self, client, service, nodeIdentifier, schema=None, form_type='form', copy_form=True): | |
131 """get data form from node's schema | |
132 | |
133 @param service(None, jid.JID): PubSub service | |
134 @param nodeIdentifier(unicode): node | |
135 @param schema(domish.Element, data_form.Form, None): node schema | |
136 if domish.Element, will be converted to data form | |
137 if data_form.Form it will be returned without modification | |
138 if None, it will be retrieved from node (imply one additional XMPP request) | |
139 @param form_type(unicode): type of the form | |
140 @param copy_form(bool): if True and if schema is already a data_form.Form, will deep copy it before returning | |
141 needed when the form is reused and it will be modified (e.g. in sendDataFormItem) | |
142 @return(data_form.Form): data form | |
143 the form should not be modified if copy_form is not set | |
144 """ | |
145 if schema is None: | |
146 log.debug(_(u"unspecified schema, we need to request it")) | |
147 schema = yield self.getSchema(client, service, nodeIdentifier) | |
148 if schema is None: | |
149 raise exceptions.DataError(_(u"no schema specified, and this node has no schema either, we can't construct the data form")) | |
150 elif isinstance(schema, data_form.Form): | |
151 if copy_form: | |
152 schema = copy.deepcopy(schema) | |
153 defer.returnValue(schema) | |
154 | |
155 try: | |
156 form = data_form.Form.fromElement(schema) | |
157 except data_form.Error as e: | |
158 raise exceptions.DataError(_(u"Invalid Schema: {msg}").format( | |
159 msg = e)) | |
160 form.formType = form_type | |
161 defer.returnValue(form) | |
162 | |
163 def schema2XMLUI(self, schema_elt): | |
164 form = data_form.Form.fromElement(schema_elt) | |
165 xmlui = xml_tools.dataForm2XMLUI(form, '') | |
166 return xmlui | |
167 | |
168 def _getUISchema(self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE): | |
169 if not nodeIdentifier: | |
170 if not default_node: | |
171 raise ValueError(_(u"nodeIndentifier needs to be set")) | |
172 nodeIdentifier = default_node | |
173 client = self.host.getClient(profile_key) | |
174 service = None if not service else jid.JID(service) | |
175 d = self.getUISchema(client, service, nodeIdentifier) | |
176 d.addCallback(lambda xmlui: xmlui.toXml()) | |
177 return d | |
178 | |
179 def getUISchema(self, client, service, nodeIdentifier): | |
180 d = self.getSchema(client, service, nodeIdentifier) | |
181 d.addCallback(self.schema2XMLUI) | |
182 return d | |
183 | |
184 def _setSchema(self, service, nodeIdentifier, schema, profile_key=C.PROF_KEY_NONE): | |
185 client = self.host.getClient(profile_key) | |
186 service = None if not service else jid.JID(service) | |
187 schema = generic.parseXml(schema.encode('utf-8')) | |
188 return self.setSchema(client, service, nodeIdentifier, schema) | |
189 | |
190 def setSchema(self, client, service, nodeIdentifier, schema): | |
191 """set or replace PubSub node schema | |
192 | |
193 @param schema(domish.Element, None): schema to set | |
194 None if schema need to be removed | |
195 """ | |
196 iq_elt = client.IQ() | |
197 if service is not None: | |
198 iq_elt['to'] = service.full() | |
199 pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub')) | |
200 schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema')) | |
201 schema_elt['node'] = nodeIdentifier | |
202 if schema is not None: | |
203 schema_elt.addChild(schema) | |
204 return iq_elt.send() | |
205 | |
206 def _getDataFormItems(self, form_ns='', service='', node='', schema='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, profile_key=C.PROF_KEY_NONE): | |
207 client = self.host.getClient(profile_key) | |
208 service = jid.JID(service) if service else None | |
209 if not node: | |
210 raise exceptions.DataError(_(u'empty node is not allowed')) | |
211 if schema: | |
212 schema = generic.parseXml(schema.encode('utf-8')) | |
213 else: | |
214 schema = None | |
215 max_items = None if max_items == C.NO_LIMIT else max_items | |
216 extra = self._p.parseExtra(extra_dict) | |
217 d = self.getDataFormItems(client, service, node, schema, max_items or None, item_ids, sub_id or None, extra.rsm_request, extra.extra, form_ns=form_ns or None) | |
218 d.addCallback(self._p.serItemsData) | |
219 return d | |
220 | |
221 @defer.inlineCallbacks | |
222 def getDataFormItems(self, client, service, nodeIdentifier, schema=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None, default_node=None, form_ns=None, filters=None): | |
223 """Get items known as being data forms, and convert them to XMLUI | |
224 | |
225 @param schema(domish.Element, data_form.Form, None): schema of the node if known | |
226 if None, it will be retrieved from node | |
227 @param default_node(unicode): node to use if nodeIdentifier is None or empty | |
228 @param form_ns (unicode, None): namespace of the form | |
229 None to accept everything, even if form has no namespace | |
230 @param filters(dict, None): same as for xml_tools.dataFormResult2XMLUI | |
231 other parameters as the same as for [getItems] | |
232 @return (list[unicode]): XMLUI of the forms | |
233 if an item is invalid (not corresponding to form_ns or not a data_form) | |
234 it will be skipped | |
235 @raise ValueError: one argument is invalid | |
236 """ | |
237 if not nodeIdentifier: | |
238 if not default_node: | |
239 raise ValueError(_(u"default_node must be set if nodeIdentifier is not set")) | |
240 nodeIdentifier = default_node | |
241 # we need the initial form to get options of fields when suitable | |
242 schema_form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='result', copy_form=False) | |
243 items_data = yield self._p.getItems(client, service, nodeIdentifier, max_items, item_ids, sub_id, rsm_request, extra) | |
244 items, metadata = items_data | |
245 items_xmlui = [] | |
246 for item_elt in items: | |
247 for x_elt in item_elt.elements((data_form.NS_X_DATA, u'x')): | |
248 form = data_form.Form.fromElement(x_elt) | |
249 if form_ns and form.formNamespace != form_ns: | |
250 continue | |
251 xmlui = xml_tools.dataFormResult2XMLUI( | |
252 form, | |
253 schema_form, | |
254 # FIXME: conflicts with schema (i.e. if "id" or "publisher" already exists) | |
255 # are not checked | |
256 prepend = (('label', 'id'),('text', item_elt['id'], u'id'), | |
257 ('label', 'publisher'),('text', item_elt.getAttribute('publisher',''), u'publisher')), | |
258 filters = filters, | |
259 ) | |
260 items_xmlui.append(xmlui) | |
261 break | |
262 defer.returnValue((items_xmlui, metadata)) | |
263 | |
264 | |
265 def _sendDataFormItem(self, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE): | |
266 client = self.host.getClient(profile_key) | |
267 service = None if not service else jid.JID(service) | |
268 if schema: | |
269 schema = generic.parseXml(schema.encode('utf-8')) | |
270 else: | |
271 schema = None | |
272 d = self.sendDataFormItem(client, service, nodeIdentifier, values, schema, item_id or None, extra, deserialise=True) | |
273 d.addCallback(lambda ret: ret or u'') | |
274 return d | |
275 | |
276 @defer.inlineCallbacks | |
277 def sendDataFormItem(self, client, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, deserialise=False): | |
278 """Publish an item as a dataform when we know that there is a schema | |
279 | |
280 @param values(dict[key(unicode), [iterable[object], object]]): values set for the form | |
281 if not iterable, will be put in a list | |
282 @param schema(domish.Element, data_form.Form, None): data schema | |
283 None to retrieve data schema from node (need to do a additional XMPP call) | |
284 Schema is needed to construct data form to publish | |
285 @param deserialise(bool): if True, data are list of unicode and must be deserialized according to expected type | |
286 This is done in this method and not directly in _sendDataFormItem because we need to know the data type | |
287 which is in the form, not availablable in _sendDataFormItem | |
288 other parameters as the same as for [self._p.sendItem] | |
289 @return (unicode): id of the created item | |
290 """ | |
291 form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='submit') | |
292 | |
293 for name, values_list in values.iteritems(): | |
294 try: | |
295 field = form.fields[name] | |
296 except KeyError: | |
297 log.warning(_(u"field {name} doesn't exist, ignoring it").format(name=name)) | |
298 continue | |
299 if isinstance(values_list, basestring) or not isinstance(values_list, Iterable): | |
300 values_list = [values_list] | |
301 if deserialise: | |
302 if field.fieldType == 'boolean': | |
303 values_list = [C.bool(v) for v in values_list] | |
304 elif field.fieldType == 'text-multi': | |
305 # for text-multi, lines must be put on separate values | |
306 values_list = list(itertools.chain(*[v.splitlines() for v in values_list])) | |
307 | |
308 elif 'jid' in field.fieldType: | |
309 values_list = [jid.JID(v) for v in values_list] | |
310 if 'list' in field.fieldType: | |
311 # for lists, we check that given values are allowed in form | |
312 allowed_values = [o.value for o in field.options] | |
313 values_list = [v for v in values_list if v in allowed_values] | |
314 if not values_list: | |
315 # if values don't map to allowed values, we use default ones | |
316 values_list = field.values | |
317 field.values = values_list | |
318 | |
319 yield self._p.sendItem(client, service, nodeIdentifier, form.toElement(), item_id, extra) | |
320 | |
321 ## filters ## | |
322 # filters useful for data form to XMLUI conversion # | |
323 | |
324 def valueOrPublisherFilter(self, form_xmlui, widget_type, args, kwargs): | |
325 """Replace missing value by publisher's user part""" | |
326 if not args[0]: | |
327 # value is not filled: we use user part of publisher (if we have it) | |
328 try: | |
329 publisher = jid.JID(form_xmlui.named_widgets['publisher'].value) | |
330 except (KeyError, RuntimeError): | |
331 pass | |
332 else: | |
333 args[0] = publisher.user.capitalize() | |
334 return widget_type, args, kwargs | |
335 | |
336 def textbox2ListFilter(self, form_xmlui, widget_type, args, kwargs): | |
337 """Split lines of a textbox in a list | |
338 | |
339 main use case is using a textbox for labels | |
340 """ | |
341 if widget_type != u'textbox': | |
342 return widget_type, args, kwargs | |
343 widget_type = u'list' | |
344 options = [o for o in args.pop(0).split(u'\n') if o] | |
345 kwargs = {'options': options, | |
346 'name': kwargs.get('name'), | |
347 'styles': (u'noselect', u'extensible', u'reducible')} | |
348 return widget_type, args, kwargs | |
349 | |
350 def dateFilter(self, form_xmlui, widget_type, args, kwargs): | |
351 """Convert a string with a date to a unix timestamp""" | |
352 if widget_type != u'string' or not args[0]: | |
353 return widget_type, args, kwargs | |
354 # we convert XMPP date to timestamp | |
355 try: | |
356 args[0] = unicode(utils.date_parse(args[0])) | |
357 except Exception as e: | |
358 log.warning(_(u"Can't parse date field: {msg}").format(msg=e)) | |
359 return widget_type, args, kwargs | |
360 | |
361 ## Helper methods ## | |
362 | |
363 def prepareBridgeGet(self, service, node, max_items, sub_id, extra_dict, profile_key): | |
364 """Parse arguments received from bridge *Get methods and return higher level data | |
365 | |
366 @return (tuple): (client, service, node, max_items, extra, sub_id) usable for internal methods | |
367 """ | |
368 client = self.host.getClient(profile_key) | |
369 service = jid.JID(service) if service else None | |
370 if not node: | |
371 node = None | |
372 max_items = None if max_items == C.NO_LIMIT else max_items | |
373 if not sub_id: | |
374 sub_id = None | |
375 extra = self._p.parseExtra(extra_dict) | |
376 | |
377 return client, service, node, max_items, extra, sub_id | |
378 | |
379 def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, default_node=None, form_ns=None, filters=None, profile_key=C.PROF_KEY_NONE): | |
380 """Bridge method to retrieve data from node with schema | |
381 | |
382 this method is a helper so dependant plugins can use it directly | |
383 when adding *Get methods | |
384 """ | |
385 client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(service, node, max_items, sub_id, extra_dict, profile_key) | |
386 d = self.getDataFormItems(client, service, node or None, | |
387 max_items=max_items, | |
388 item_ids=item_ids, | |
389 sub_id=sub_id, | |
390 rsm_request=extra.rsm_request, | |
391 extra=extra.extra, | |
392 default_node=default_node, | |
393 form_ns=form_ns, | |
394 filters=filters) | |
395 d.addCallback(self._p.serItemsData) | |
396 return d | |
397 | |
398 def prepareBridgeSet(self, service, node, schema, item_id, extra, profile_key): | |
399 """Parse arguments received from bridge *Set methods and return higher level data | |
400 | |
401 @return (tuple): (client, service, node, schema, item_id, extra) usable for internal methods | |
402 """ | |
403 client = self.host.getClient(profile_key) | |
404 service = None if not service else jid.JID(service) | |
405 if schema: | |
406 schema = generic.parseXml(schema.encode('utf-8')) | |
407 else: | |
408 schema = None | |
409 if extra and u'update' in extra: | |
410 extra[u'update'] = C.bool(extra[u'update']) | |
411 return client, service, node or None, schema, item_id or None, extra | |
412 | |
413 def _set(self, service, node, values, schema=None, item_id=None, extra=None, default_node=None, form_ns=None, fill_author=True, profile_key=C.PROF_KEY_NONE): | |
414 """Bridge method to set item in node with schema | |
415 | |
416 this method is a helper so dependant plugins can use it directly | |
417 when adding *Set methods | |
418 """ | |
419 client, service, node, schema, item_id, extra = self.prepareBridgeSet(service, node, schema, item_id, extra) | |
420 d = self.set(client, service, node, values, schema, item_id, extra, | |
421 deserialise=True, | |
422 form_ns=form_ns, | |
423 default_node=default_node, | |
424 fill_author=fill_author) | |
425 d.addCallback(lambda ret: ret or u'') | |
426 return d | |
427 | |
428 @defer.inlineCallbacks | |
429 def set(self, client, service, node, values, schema, item_id, extra, deserialise, form_ns, default_node=None, fill_author=True): | |
430 """Set an item in a node with a schema | |
431 | |
432 This method can be used directly by *Set methods added by dependant plugin | |
433 @param values(dict[key(unicode), [iterable[object]|object]]): values of the items | |
434 if value is not iterable, it will be put in a list | |
435 'created' and 'updated' will be forced to current time: | |
436 - 'created' is set if item_id is None, i.e. if it's a new ticket | |
437 - 'updated' is set everytime | |
438 @param extra(dict, None): same as for [XEP-0060.sendItem] with additional keys: | |
439 - update(bool): if True, get previous item data to merge with current one | |
440 if True, item_id must be None | |
441 @param form_ns (unicode, None): namespace of the form | |
442 needed when an update is done | |
443 @param default_node(unicode, None): value to use if node is not set | |
444 other arguments are same as for [self._s.sendDataFormItem] | |
445 @return (unicode): id of the created item | |
446 """ | |
447 if not node: | |
448 if default_node is None: | |
449 raise ValueError(_(u"default_node must be set if node is not set")) | |
450 node = default_node | |
451 now = utils.xmpp_date() | |
452 if not item_id: | |
453 values['created'] = now | |
454 elif extra.get(u'update', False): | |
455 if item_id is None: | |
456 raise exceptions.DataError(_(u'if extra["update"] is set, item_id must be set too')) | |
457 try: | |
458 # we get previous item | |
459 items_data = yield self._p.getItems(client, service, node, item_ids=[item_id]) | |
460 item_elt = items_data[0][0] | |
461 except Exception as e: | |
462 log.warning(_(u"Can't get previous item, update ignored: {reason}").format( | |
463 reason = e)) | |
464 else: | |
465 # and parse it | |
466 form = data_form.findForm(item_elt, form_ns) | |
467 if form is None: | |
468 log.warning(_(u"Can't parse previous item, update ignored: data form not found").format( | |
469 reason = e)) | |
470 else: | |
471 for name, field in form.fields.iteritems(): | |
472 if name not in values: | |
473 values[name] = u'\n'.join(unicode(v) for v in field.values) | |
474 | |
475 values['updated'] = now | |
476 if fill_author: | |
477 if not values.get('author'): | |
478 identity = yield self._i.getIdentity(client, client.jid) | |
479 values['author'] = identity['nick'] | |
480 if not values.get('author_jid'): | |
481 values['author_jid'] = client.jid.full() | |
482 item_id = yield self.sendDataFormItem(client, service, node, values, schema, item_id, extra, deserialise) | |
483 defer.returnValue(item_id) | |
484 | |
485 | |
486 class SchemaHandler(XMPPHandler): | |
487 implements(iwokkel.IDisco) | |
488 | |
489 def getDiscoInfo(self, requestor, service, nodeIdentifier=''): | |
490 return [disco.DiscoFeature(NS_SCHEMA)] | |
491 | |
492 def getDiscoItems(self, requestor, service, nodeIdentifier=''): | |
493 return [] |