comparison sat/plugins/plugin_exp_pubsub_schema.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 68d423f4fb55
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
16 16
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from collections import Iterable 20 from collections import Iterable
21 import copy
22 import itertools 21 import itertools
23 from zope.interface import implementer 22 from zope.interface import implementer
24 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
25 from twisted.words.protocols.jabber.xmlstream import XMPPHandler 24 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
25 from twisted.words.xish import domish
26 from twisted.internet import defer 26 from twisted.internet import defer
27 from wokkel import disco, iwokkel 27 from wokkel import disco, iwokkel
28 from wokkel import data_form 28 from wokkel import data_form
29 from wokkel import generic 29 from wokkel import generic
30 from sat.core.i18n import _ 30 from sat.core.i18n import _
172 "no schema specified, and this node has no schema either, we can't construct the data form" 172 "no schema specified, and this node has no schema either, we can't construct the data form"
173 ) 173 )
174 ) 174 )
175 elif isinstance(schema, data_form.Form): 175 elif isinstance(schema, data_form.Form):
176 if copy_form: 176 if copy_form:
177 schema = copy.deepcopy(schema) 177 # XXX: we don't use deepcopy as it will do an infinite loop if a
178 # domish.Element is present in the form fields (happens for
179 # XEP-0315 data forms XML Element)
180 schema = data_form.Form(
181 formType = schema.formType,
182 title = schema.title,
183 instructions = schema.instructions[:],
184 formNamespace = schema.formNamespace,
185 fields = schema.fieldList,
186 )
178 defer.returnValue(schema) 187 defer.returnValue(schema)
179 188
180 try: 189 try:
181 form = data_form.Form.fromElement(schema) 190 form = data_form.Form.fromElement(schema)
182 except data_form.Error as e: 191 except data_form.Error as e:
392 allowed_values = [o.value for o in field.options] 401 allowed_values = [o.value for o in field.options]
393 values_list = [v for v in values_list if v in allowed_values] 402 values_list = [v for v in values_list if v in allowed_values]
394 if not values_list: 403 if not values_list:
395 # if values don't map to allowed values, we use default ones 404 # if values don't map to allowed values, we use default ones
396 values_list = field.values 405 values_list = field.values
406 elif field.ext_type == 'xml':
407 # FIXME: XML elements are not handled correctly, we need to know if we
408 # actual XML/XHTML, or text to escape
409 for idx, value in enumerate(values_list[:]):
410 if not isinstance(value, domish.Element):
411 if field.value and field.value.uri == C.NS_XHTML:
412 div_elt = domish.Element((C.NS_XHTML, 'div'))
413 div_elt.addContent(str(value))
414 values_list[idx] = div_elt
415 else:
416 raise NotImplementedError
417
397 field.values = values_list 418 field.values = values_list
398 419
399 yield self._p.sendItem( 420 yield self._p.sendItem(
400 client, service, nodeIdentifier, form.toElement(), item_id, extra 421 client, service, nodeIdentifier, form.toElement(), item_id, extra
401 ) 422 )
544 else: 565 else:
545 # and parse it 566 # and parse it
546 form = data_form.findForm(item_elt, form_ns) 567 form = data_form.findForm(item_elt, form_ns)
547 if form is None: 568 if form is None:
548 log.warning( 569 log.warning(
549 _( 570 _("Can't parse previous item, update ignored: data form not found")
550 "Can't parse previous item, update ignored: data form not found"
551 ).format(reason=e)
552 ) 571 )
553 else: 572 else:
554 for name, field in form.fields.items(): 573 for name, field in form.fields.items():
555 if name not in values: 574 if name not in values:
556 values[name] = "\n".join(str(v) for v in field.values) 575 values[name] = "\n".join(str(v) for v in field.values)