Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0346.py @ 3473:cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 27 Feb 2021 18:37:43 +0100 |
parents | d4a71a1dac88 |
children | be6d91572633 |
comparison
equal
deleted
inserted
replaced
3472:e12e9e1535d3 | 3473:cc065c13052c |
---|---|
16 # You should have received a copy of the GNU Affero General Public License | 16 # You should have received a copy of the GNU Affero General Public License |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
19 from collections import Iterable | 19 from collections import Iterable |
20 import itertools | 20 import itertools |
21 from typing import Optional | |
21 from zope.interface import implementer | 22 from zope.interface import implementer |
22 from twisted.words.protocols.jabber import jid | 23 from twisted.words.protocols.jabber import jid |
23 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | 24 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
24 from twisted.words.xish import domish | 25 from twisted.words.xish import domish |
25 from twisted.internet import defer | 26 from twisted.internet import defer |
27 from wokkel import data_form | 28 from wokkel import data_form |
28 from wokkel import generic | 29 from wokkel import generic |
29 from sat.core.i18n import _ | 30 from sat.core.i18n import _ |
30 from sat.core import exceptions | 31 from sat.core import exceptions |
31 from sat.core.constants import Const as C | 32 from sat.core.constants import Const as C |
33 from sat.core.xmpp import SatXMPPEntity | |
32 from sat.tools import xml_tools | 34 from sat.tools import xml_tools |
33 from sat.tools import utils | 35 from sat.tools import utils |
34 from sat.tools.common import date_utils | 36 from sat.tools.common import date_utils |
35 from sat.tools.common import data_format | 37 from sat.tools.common import data_format |
36 from sat.core.log import getLogger | 38 from sat.core.log import getLogger |
80 ".plugin", | 82 ".plugin", |
81 in_sign="sss", | 83 in_sign="sss", |
82 out_sign="s", | 84 out_sign="s", |
83 method=lambda service, nodeIdentifier, profile_key: self._getUISchema( | 85 method=lambda service, nodeIdentifier, profile_key: self._getUISchema( |
84 service, nodeIdentifier, default_node=None, profile_key=profile_key), | 86 service, nodeIdentifier, default_node=None, profile_key=profile_key), |
87 async_=True, | |
88 ) | |
89 host.bridge.addMethod( | |
90 "psSchemaDictGet", | |
91 ".plugin", | |
92 in_sign="sss", | |
93 out_sign="s", | |
94 method=self._getSchemaDict, | |
85 async_=True, | 95 async_=True, |
86 ) | 96 ) |
87 host.bridge.addMethod( | 97 host.bridge.addMethod( |
88 "psItemsFormGet", | 98 "psItemsFormGet", |
89 ".plugin", | 99 ".plugin", |
244 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, | 254 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, |
245 self._p.OPT_PUBLISH_MODEL: self._p.PUBLISH_MODEL_PUBLISHERS, | 255 self._p.OPT_PUBLISH_MODEL: self._p.PUBLISH_MODEL_PUBLISHERS, |
246 } | 256 } |
247 await self._p.createIfNewNode(client, service, node_id, node_options) | 257 await self._p.createIfNewNode(client, service, node_id, node_options) |
248 await self._p.sendItem(client, service, node_id, schema, self._p.ID_SINGLETON) | 258 await self._p.sendItem(client, service, node_id, schema, self._p.ID_SINGLETON) |
259 | |
260 def _getSchemaDict(self, service, nodeIdentifier, profile): | |
261 service = None if not service else jid.JID(service) | |
262 client = self.host.getClient(profile) | |
263 d = defer.ensureDeferred(self.getSchemaDict(client, service, nodeIdentifier)) | |
264 d.addCallback(data_format.serialise) | |
265 return d | |
266 | |
267 async def getSchemaDict( | |
268 self, | |
269 client: SatXMPPEntity, | |
270 service: Optional[jid.JID], | |
271 nodeIdentifier: str) -> dict: | |
272 """Retrieve a node schema and format it a simple dictionary | |
273 | |
274 The dictionary is made so it can be easily serialisable | |
275 """ | |
276 schema_form = await self.getSchemaForm(client, service, nodeIdentifier) | |
277 return xml_tools.dataForm2dataDict(schema_form) | |
249 | 278 |
250 def _getDataFormItems(self, form_ns="", service="", node="", schema="", max_items=10, | 279 def _getDataFormItems(self, form_ns="", service="", node="", schema="", max_items=10, |
251 item_ids=None, sub_id=None, extra_dict=None, | 280 item_ids=None, sub_id=None, extra_dict=None, |
252 profile_key=C.PROF_KEY_NONE): | 281 profile_key=C.PROF_KEY_NONE): |
253 client = self.host.getClient(profile_key) | 282 client = self.host.getClient(profile_key) |