changeset 3309:71761e9fb984

plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation: those methods now return data serialised with `data_format.serialise`
author Goffi <goffi@goffi.org>
date Thu, 16 Jul 2020 09:07:26 +0200 (2020-07-16)
parents 384283adcce1
children e4121a1f2399
files sat/plugins/plugin_exp_pubsub_schema.py sat/plugins/plugin_misc_merge_requests.py sat/plugins/plugin_misc_tickets.py sat_frontends/jp/cmd_merge_request.py sat_frontends/jp/cmd_ticket.py
diffstat 5 files changed, 71 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_pubsub_schema.py	Thu Jul 16 09:07:20 2020 +0200
+++ b/sat/plugins/plugin_exp_pubsub_schema.py	Thu Jul 16 09:07:26 2020 +0200
@@ -1,6 +1,5 @@
 #!/usr/bin/env python3
 
-
 # SAT plugin for Pubsub Schemas
 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
 
@@ -535,6 +534,7 @@
             filters=filters,
         )
         d.addCallback(self._p.transItemsData)
+        d.addCallback(lambda data: data_format.serialise(data))
         return d
 
     def prepareBridgeSet(self, service, node, schema, item_id, extra, profile_key):
@@ -601,7 +601,7 @@
         client, service, node, schema, item_id, extra = self.prepareBridgeSet(
             service, node, schema, item_id, extra
         )
-        d = self.set(
+        d = defer.ensureDeferred(self.set(
             client,
             service,
             node,
@@ -613,12 +613,12 @@
             form_ns=form_ns,
             default_node=default_node,
             fill_author=fill_author,
-        )
+        ))
         d.addCallback(lambda ret: ret or "")
         return d
 
-    @defer.inlineCallbacks
-    def set(self, client, service, node, values, schema, item_id, extra, deserialise,
+    async def set(
+            self, client, service, node, values, schema, item_id, extra, deserialise,
             form_ns, default_node=None, fill_author=True):
         """Set an item in a node with a schema
 
@@ -651,19 +651,19 @@
                 raise exceptions.DataError(
                     _('if extra["update"] is set, item_id must be set too')
                 )
-            yield self.copyMissingValues(client, service, node, item_id, form_ns, values)
+            await self.copyMissingValues(client, service, node, item_id, form_ns, values)
 
         values["updated"] = now
         if fill_author:
             if not values.get("author"):
-                id_data = yield self._i.getIdentity(client, None, ["nicknames"])
+                id_data = await self._i.getIdentity(client, None, ["nicknames"])
                 values["author"] = id_data['nicknames'][0]
             if not values.get("author_jid"):
                 values["author_jid"] = client.jid.full()
-        item_id = yield self.sendDataFormItem(
+        item_id = await self.sendDataFormItem(
             client, service, node, values, schema, item_id, extra, deserialise
         )
-        defer.returnValue(item_id)
+        return item_id
 
 
 @implementer(iwokkel.IDisco)
--- a/sat/plugins/plugin_misc_merge_requests.py	Thu Jul 16 09:07:20 2020 +0200
+++ b/sat/plugins/plugin_misc_merge_requests.py	Thu Jul 16 09:07:26 2020 +0200
@@ -17,14 +17,16 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from collections import namedtuple
+from twisted.internet import defer
+from twisted.words.protocols.jabber import jid
 from sat.core.i18n import _
 from sat.core.constants import Const as C
 from sat.core import exceptions
-from twisted.internet import defer
-from twisted.words.protocols.jabber import jid
-from collections import namedtuple
+from sat.tools.common import data_format
 from sat.core.log import getLogger
 
+
 log = getLogger(__name__)
 
 NS_MERGE_REQUESTS = 'org.salut-a-toi.merge_requests:0'
@@ -74,7 +76,7 @@
         self._handlers_list = []  # handlers sorted by priority
         self._type_handlers = {}  # data type => handler map
         host.bridge.addMethod("mergeRequestsGet", ".plugin",
-                              in_sign='ssiassa{ss}s', out_sign='(asa{ss}aaa{ss})',
+                              in_sign='ssiassa{ss}s', out_sign='s',
                               method=self._get,
                               async_=True
                               )
@@ -136,6 +138,15 @@
                 continue
             self._type_handlers[data_type] = self._handlers[name]
 
+    def serialise(self, get_data):
+        tickets_xmlui, metadata, items_patches = get_data
+        tickets_xmlui_s, metadata = self._p.transItemsData((tickets_xmlui, metadata))
+        return data_format.serialise({
+            "items": tickets_xmlui_s,
+            "metadata": metadata,
+            "items_patches": items_patches,
+        })
+
     def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None,
              extra_dict=None, profile_key=C.PROF_KEY_NONE):
         if extra_dict and 'parse' in extra_dict:
@@ -144,10 +155,7 @@
             service, node, max_items, sub_id, extra_dict, profile_key)
         d = self.get(client, service, node or None, max_items, item_ids, sub_id or None,
                      extra.rsm_request, extra.extra)
-        d.addCallback(lambda tickets_metadata_parsed_patches: (
-            self._p.transItemsData((tickets_metadata_parsed_patches[0], tickets_metadata_parsed_patches[1])) +
-            ([[{key: str(value) for key, value in p.items()}
-                for p in patches] for patches in tickets_metadata_parsed_patches[2]],)))
+        d.addCallback(self.serialise)
         return d
 
     @defer.inlineCallbacks
@@ -197,13 +205,16 @@
              extra="", profile_key=C.PROF_KEY_NONE):
         client, service, node, schema, item_id, extra = self._s.prepareBridgeSet(
             service, node, schema, item_id, extra, profile_key)
-        d = self.set(client, service, node, repository, method, values, schema,
-                     item_id or None, extra, deserialise=True)
+        d = defer.ensureDeferred(
+            self.set(
+                client, service, node, repository, method, values, schema,
+                item_id or None, extra, deserialise=True
+            )
+        )
         d.addCallback(lambda ret: ret or '')
         return d
 
-    @defer.inlineCallbacks
-    def set(self, client, service, node, repository, method='auto', values=None,
+    async def set(self, client, service, node, repository, method='auto', values=None,
             schema=None, item_id=None, extra=None, deserialise=False):
         """Publish a tickets
 
@@ -234,7 +245,7 @@
             if method == 'auto':
                 for name in self._handlers_list:
                     handler = self._handlers[name].handler
-                    can_handle = yield handler.check(repository)
+                    can_handle = await handler.check(repository)
                     if can_handle:
                         log.info(_("{name} handler will be used").format(name=name))
                         break
@@ -250,20 +261,20 @@
                 except KeyError:
                     raise exceptions.NotFound(_("No handler of this name found"))
 
-            data = yield handler.export(repository)
+            data = await handler.export(repository)
             if not data.strip():
                 raise exceptions.DataError(_('export data is empty, do you have any '
                                              'change to send?'))
 
             if not values.get('title') or not values.get('body'):
-                patches = yield handler.parse(data, values.get(FIELD_DATA_TYPE))
+                patches = await handler.parse(data, values.get(FIELD_DATA_TYPE))
                 commits_msg = patches[-1][self.META_COMMIT_MSG]
                 msg_lines = commits_msg.splitlines()
                 if not values.get('title'):
                     values['title'] = msg_lines[0]
                 if not values.get('body'):
                     ts = self.host.plugins['TEXT_SYNTAXES']
-                    xhtml = yield ts.convert(
+                    xhtml = await ts.convert(
                         '\n'.join(msg_lines[1:]),
                         syntax_from = ts.SYNTAX_TEXT,
                         syntax_to = ts.SYNTAX_XHTML,
@@ -273,9 +284,9 @@
 
             values[FIELD_DATA] = data
 
-        item_id = yield self._t.set(client, service, node, values, schema, item_id, extra,
+        item_id = await self._t.set(client, service, node, values, schema, item_id, extra,
                                     deserialise, form_ns=NS_MERGE_REQUESTS)
-        defer.returnValue(item_id)
+        return item_id
 
     def _parseData(self, data_type, data):
         d = self.parseData(data_type, data)
--- a/sat/plugins/plugin_misc_tickets.py	Thu Jul 16 09:07:20 2020 +0200
+++ b/sat/plugins/plugin_misc_tickets.py	Thu Jul 16 09:07:26 2020 +0200
@@ -1,6 +1,5 @@
 #!/usr/bin/env python3
 
-
 # SAT plugin for Pubsub Schemas
 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
 
@@ -52,7 +51,7 @@
             "ticketsGet",
             ".plugin",
             in_sign="ssiassa{ss}s",
-            out_sign="(asa{ss})",
+            out_sign="s",
             method=lambda service, node, max_items, items_ids, sub_id, extra, profile_key:
                 self._s._get(
                 service,
@@ -95,14 +94,13 @@
         client, service, node, schema, item_id, extra = self._s.prepareBridgeSet(
             service, node, schema, item_id, extra, profile_key
         )
-        d = self.set(
+        d = defer.ensureDeferred(self.set(
             client, service, node, values, schema, item_id, extra, deserialise=True
-        )
+        ))
         d.addCallback(lambda ret: ret or "")
         return d
 
-    @defer.inlineCallbacks
-    def set(self, client, service, node, values, schema=None, item_id=None, extra=None,
+    async def set(self, client, service, node, values, schema=None, item_id=None, extra=None,
             deserialise=False, form_ns=NS_TICKETS):
         """Publish a tickets
 
@@ -124,7 +122,7 @@
             node = NS_TICKETS
 
         if not item_id:
-            comments_service = yield self._m.getCommentsService(client, service)
+            comments_service = await self._m.getCommentsService(client, service)
 
             # we need to use uuid for comments node, because we don't know item id in
             # advance (we don't want to set it ourselves to let the server choose, so we
@@ -140,7 +138,7 @@
                 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1,
                 self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN,
             }
-            yield self._p.createNode(client, comments_service, comments_node, options)
+            await self._p.createNode(client, comments_service, comments_node, options)
             values["comments_uri"] = uri.buildXMPPUri(
                 "pubsub",
                 subtype="microblog",
@@ -148,7 +146,6 @@
                 node=comments_node,
             )
 
-        item_id = yield self._s.set(
+        return await self._s.set(
             client, service, node, values, schema, item_id, extra, deserialise, form_ns
         )
-        defer.returnValue(item_id)
--- a/sat_frontends/jp/cmd_merge_request.py	Thu Jul 16 09:07:20 2020 +0200
+++ b/sat_frontends/jp/cmd_merge_request.py	Thu Jul 16 09:07:26 2020 +0200
@@ -129,14 +129,16 @@
             self, os.getcwd(), "merge requests", meta_map={})
         extra = {}
         try:
-            requests_data = await self.host.bridge.mergeRequestsGet(
-                self.args.service,
-                self.args.node,
-                self.args.max,
-                self.args.items,
-                "",
-                extra,
-                self.profile,
+            requests_data = data_format.deserialise(
+                await self.host.bridge.mergeRequestsGet(
+                    self.args.service,
+                    self.args.node,
+                    self.args.max,
+                    self.args.items,
+                    "",
+                    extra,
+                    self.profile,
+                )
             )
         except Exception as e:
             self.disp(f"can't get merge request: {e}", error=True)
@@ -146,7 +148,7 @@
             whitelist = None
         else:
             whitelist = {"id", "title", "body"}
-        for request_xmlui in requests_data[0]:
+        for request_xmlui in requests_data['items']:
             xmlui = xmlui_manager.create(self.host, request_xmlui, whitelist=whitelist)
             await xmlui.show(values_only=True)
             self.disp("")
--- a/sat_frontends/jp/cmd_ticket.py	Thu Jul 16 09:07:20 2020 +0200
+++ b/sat_frontends/jp/cmd_ticket.py	Thu Jul 16 09:07:26 2020 +0200
@@ -18,12 +18,13 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from . import base
+import json
+import os
 from sat.core.i18n import _
+from sat.tools.common import data_format
 from sat_frontends.jp import common
 from sat_frontends.jp.constants import Const as C
-import json
-import os
+from . import base
 
 __commands__ = ["Ticket"]
 
@@ -50,14 +51,17 @@
     async def start(self):
         await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={})
         try:
-            tickets_data = await self.host.bridge.ticketsGet(
-                self.args.service,
-                self.args.node,
-                self.args.max,
-                self.args.items,
-                "",
-                self.getPubsubExtra(),
-                self.profile,
+            tickets_data = data_format.deserialise(
+                await self.host.bridge.ticketsGet(
+                    self.args.service,
+                    self.args.node,
+                    self.args.max,
+                    self.args.items,
+                    "",
+                    self.getPubsubExtra(),
+                    self.profile,
+                ),
+                type_check=list
             )
         except Exception as e:
             self.disp(f"can't get tickets: {e}", error=True)