diff sat_frontends/tools/xmlui.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 9d0df638c8b4
line wrap: on
line diff
--- a/sat_frontends/tools/xmlui.py	Wed Sep 25 08:53:38 2019 +0200
+++ b/sat_frontends/tools/xmlui.py	Wed Sep 25 08:56:41 2019 +0200
@@ -925,6 +925,67 @@
         pass
 
 
+class AIOXMLUIPanel(XMLUIPanel):
+    """Asyncio compatible version of XMLUIPanel"""
+
+    async def onFormSubmitted(self, ignore=None):
+        """An XMLUI form has been submited
+
+        call the submit action associated with this form
+        """
+        selected_values = []
+        for ctrl_name in self.ctrl_list:
+            escaped = self.escape(ctrl_name)
+            ctrl = self.ctrl_list[ctrl_name]
+            if isinstance(ctrl["control"], ListWidget):
+                selected_values.append(
+                    (escaped, "\t".join(ctrl["control"]._xmluiGetSelectedValues()))
+                )
+            else:
+                selected_values.append((escaped, ctrl["control"]._xmluiGetValue()))
+        data = dict(selected_values)
+        for key, value in self.hidden.items():
+            data[self.escape(key)] = value
+
+        if self.submit_id is not None:
+            await self.submit(data)
+        else:
+            log.warning(
+                _("The form data is not sent back, the type is not managed properly")
+            )
+            self._xmluiClose()
+
+    async def onFormCancelled(self, *__):
+        """Called when a form is cancelled"""
+        log.debug(_("Cancelling form"))
+        if self.submit_id is not None:
+            data = {C.XMLUI_DATA_CANCELLED: C.BOOL_TRUE}
+            await self.submit(data)
+        else:
+            log.warning(
+                _("The form data is not sent back, the type is not managed properly")
+            )
+        self._xmluiClose()
+
+    async def submit(self, data):
+        self._xmluiClose()
+        if self.submit_id is None:
+            raise ValueError("Can't submit is self.submit_id is not set")
+        if "session_id" in data:
+            raise ValueError(
+                "session_id must no be used in data, it is automaticaly filled with "
+                "self.session_id if present"
+            )
+        if self.session_id is not None:
+            data["session_id"] = self.session_id
+        await self._xmluiLaunchAction(self.submit_id, data)
+
+    async def _xmluiLaunchAction(self, action_id, data):
+        await self.host.launchAction(
+            action_id, data, callback=self.callback, profile=self.profile
+        )
+
+
 class XMLUIDialog(XMLUIBase):
     dialog_factory = None