diff 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
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0346.py	Sat Feb 27 18:36:38 2021 +0100
+++ b/sat/plugins/plugin_xep_0346.py	Sat Feb 27 18:37:43 2021 +0100
@@ -18,6 +18,7 @@
 
 from collections import Iterable
 import itertools
+from typing import Optional
 from zope.interface import implementer
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
@@ -29,6 +30,7 @@
 from sat.core.i18n import _
 from sat.core import exceptions
 from sat.core.constants import Const as C
+from sat.core.xmpp import SatXMPPEntity
 from sat.tools import xml_tools
 from sat.tools import utils
 from sat.tools.common import date_utils
@@ -85,6 +87,14 @@
             async_=True,
         )
         host.bridge.addMethod(
+            "psSchemaDictGet",
+            ".plugin",
+            in_sign="sss",
+            out_sign="s",
+            method=self._getSchemaDict,
+            async_=True,
+        )
+        host.bridge.addMethod(
             "psItemsFormGet",
             ".plugin",
             in_sign="ssssiassa{ss}s",
@@ -247,6 +257,25 @@
         await self._p.createIfNewNode(client, service, node_id, node_options)
         await self._p.sendItem(client, service, node_id, schema, self._p.ID_SINGLETON)
 
+    def _getSchemaDict(self, service, nodeIdentifier, profile):
+        service = None if not service else jid.JID(service)
+        client = self.host.getClient(profile)
+        d = defer.ensureDeferred(self.getSchemaDict(client, service, nodeIdentifier))
+        d.addCallback(data_format.serialise)
+        return d
+
+    async def getSchemaDict(
+        self,
+        client: SatXMPPEntity,
+        service: Optional[jid.JID],
+        nodeIdentifier: str) -> dict:
+        """Retrieve a node schema and format it a simple dictionary
+
+        The dictionary is made so it can be easily serialisable
+        """
+        schema_form = await self.getSchemaForm(client, service, nodeIdentifier)
+        return xml_tools.dataForm2dataDict(schema_form)
+
     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):