diff libervia/backend/plugins/plugin_misc_merge_requests.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_misc_merge_requests.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/plugins/plugin_misc_merge_requests.py	Wed Jun 19 18:44:57 2024 +0200
@@ -29,7 +29,7 @@
 
 log = getLogger(__name__)
 
-APP_NS_MERGE_REQUESTS = 'org.salut-a-toi.merge_requests:0'
+APP_NS_MERGE_REQUESTS = "org.salut-a-toi.merge_requests:0"
 
 PLUGIN_INFO = {
     C.PI_NAME: _("Merge requests management"),
@@ -39,69 +39,86 @@
     C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0346", "LISTS", "TEXT_SYNTAXES"],
     C.PI_MAIN: "MergeRequests",
     C.PI_HANDLER: "no",
-    C.PI_DESCRIPTION: _("""Merge requests management plugin""")
+    C.PI_DESCRIPTION: _("""Merge requests management plugin"""),
 }
 
-FIELD_DATA_TYPE = 'type'
-FIELD_DATA = 'request_data'
+FIELD_DATA_TYPE = "type"
+FIELD_DATA = "request_data"
 
 
-MergeRequestHandler = namedtuple("MergeRequestHandler", ['name',
-                                                         'handler',
-                                                         'data_types',
-                                                         'short_desc',
-                                                         'priority'])
+MergeRequestHandler = namedtuple(
+    "MergeRequestHandler", ["name", "handler", "data_types", "short_desc", "priority"]
+)
 
 
 class MergeRequests(object):
-    META_AUTHOR = 'author'
-    META_EMAIL = 'email'
-    META_TIMESTAMP = 'timestamp'
-    META_HASH = 'hash'
-    META_PARENT_HASH = 'parent_hash'
-    META_COMMIT_MSG = 'commit_msg'
-    META_DIFF = 'diff'
+    META_AUTHOR = "author"
+    META_EMAIL = "email"
+    META_TIMESTAMP = "timestamp"
+    META_HASH = "hash"
+    META_PARENT_HASH = "parent_hash"
+    META_COMMIT_MSG = "commit_msg"
+    META_DIFF = "diff"
     # index of the diff in the whole data
     # needed to retrieve comments location
-    META_DIFF_IDX = 'diff_idx'
+    META_DIFF_IDX = "diff_idx"
 
     def __init__(self, host):
         log.info(_("Merge requests plugin initialization"))
         self.host = host
         self._s = self.host.plugins["XEP-0346"]
         self.namespace = self._s.get_submitted_ns(APP_NS_MERGE_REQUESTS)
-        host.register_namespace('merge_requests', self.namespace)
+        host.register_namespace("merge_requests", self.namespace)
         self._p = self.host.plugins["XEP-0060"]
         self._t = self.host.plugins["LISTS"]
         self._handlers = {}
-        self._handlers_list = []  # handlers sorted by priority
+        self._handlers_list = []  # handlers sorted by priority
         self._type_handlers = {}  # data type => handler map
-        host.bridge.add_method("merge_requests_get", ".plugin",
-                              in_sign='ssiassss', out_sign='s',
-                              method=self._get,
-                              async_=True
-                              )
-        host.bridge.add_method("merge_request_set", ".plugin",
-                              in_sign='ssssa{sas}ssss', out_sign='s',
-                              method=self._set,
-                              async_=True)
-        host.bridge.add_method("merge_requests_schema_get", ".plugin",
-                              in_sign='sss', out_sign='s',
-                              method=lambda service, nodeIdentifier, profile_key:
-                                self._s._get_ui_schema(service,
-                                                     nodeIdentifier,
-                                                     default_node=self.namespace,
-                                                     profile_key=profile_key),
-                              async_=True)
-        host.bridge.add_method("merge_request_parse_data", ".plugin",
-                              in_sign='ss', out_sign='aa{ss}',
-                              method=self._parse_data,
-                              async_=True)
-        host.bridge.add_method("merge_requests_import", ".plugin",
-                              in_sign='ssssa{ss}s', out_sign='',
-                              method=self._import,
-                              async_=True
-                              )
+        host.bridge.add_method(
+            "merge_requests_get",
+            ".plugin",
+            in_sign="ssiassss",
+            out_sign="s",
+            method=self._get,
+            async_=True,
+        )
+        host.bridge.add_method(
+            "merge_request_set",
+            ".plugin",
+            in_sign="ssssa{sas}ssss",
+            out_sign="s",
+            method=self._set,
+            async_=True,
+        )
+        host.bridge.add_method(
+            "merge_requests_schema_get",
+            ".plugin",
+            in_sign="sss",
+            out_sign="s",
+            method=lambda service, nodeIdentifier, profile_key: self._s._get_ui_schema(
+                service,
+                nodeIdentifier,
+                default_node=self.namespace,
+                profile_key=profile_key,
+            ),
+            async_=True,
+        )
+        host.bridge.add_method(
+            "merge_request_parse_data",
+            ".plugin",
+            in_sign="ss",
+            out_sign="aa{ss}",
+            method=self._parse_data,
+            async_=True,
+        )
+        host.bridge.add_method(
+            "merge_requests_import",
+            ".plugin",
+            in_sign="ssssa{ss}s",
+            out_sign="",
+            method=self._import,
+            async_=True,
+        )
 
     def register(self, name, handler, data_types, short_desc, priority=0):
         """register an merge request handler
@@ -118,49 +135,81 @@
         @aram data_types(list[unicode]): data types that his handler can generate or parse
         """
         if name in self._handlers:
-            raise exceptions.ConflictError(_("a handler with name {name} already "
-                                             "exists!").format(name = name))
-        self._handlers[name] = MergeRequestHandler(name,
-                                                   handler,
-                                                   data_types,
-                                                   short_desc,
-                                                   priority)
+            raise exceptions.ConflictError(
+                _("a handler with name {name} already " "exists!").format(name=name)
+            )
+        self._handlers[name] = MergeRequestHandler(
+            name, handler, data_types, short_desc, priority
+        )
         self._handlers_list.append(name)
         self._handlers_list.sort(key=lambda name: self._handlers[name].priority)
         if isinstance(data_types, str):
             data_types = [data_types]
         for data_type in data_types:
             if data_type in self._type_handlers:
-                log.warning(_('merge requests of type {type} are already handled by '
-                              '{old_handler}, ignoring {new_handler}').format(
-                                type = data_type,
-                old_handler = self._type_handlers[data_type].name,
-                new_handler = name))
+                log.warning(
+                    _(
+                        "merge requests of type {type} are already handled by "
+                        "{old_handler}, ignoring {new_handler}"
+                    ).format(
+                        type=data_type,
+                        old_handler=self._type_handlers[data_type].name,
+                        new_handler=name,
+                    )
+                )
                 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.trans_items_data((tickets_xmlui, metadata))
-        return data_format.serialise({
-            "items": tickets_xmlui_s,
-            "metadata": metadata,
-            "items_patches": items_patches,
-        })
+        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="", profile_key=C.PROF_KEY_NONE):
+    def _get(
+        self,
+        service="",
+        node="",
+        max_items=10,
+        item_ids=None,
+        sub_id=None,
+        extra="",
+        profile_key=C.PROF_KEY_NONE,
+    ):
         extra = data_format.deserialise(extra)
         client, service, node, max_items, extra, sub_id = self._s.prepare_bridge_get(
-            service, node, max_items, sub_id, extra, profile_key)
-        d = self.get(client, service, node or None, max_items, item_ids, sub_id or None,
-                     extra.rsm_request, extra.extra)
+            service, node, max_items, sub_id, extra, 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(self.serialise)
         return d
 
     @defer.inlineCallbacks
-    def get(self, client, service=None, node=None, max_items=None, item_ids=None,
-            sub_id=None, rsm_request=None, extra=None):
+    def get(
+        self,
+        client,
+        service=None,
+        node=None,
+        max_items=None,
+        item_ids=None,
+        sub_id=None,
+        rsm_request=None,
+        extra=None,
+    ):
         """Retrieve merge requests and convert them to XMLUI
 
         @param extra(XEP-0060.parse, None): can have following keys:
@@ -177,8 +226,8 @@
             extra = {}
         # XXX: Q&D way to get list for labels when displaying them, but text when we
         #      have to modify them
-        if C.bool(extra.get('labels_as_list', C.BOOL_FALSE)):
-            filters = {'labels': self._s.textbox_2_list_filter}
+        if C.bool(extra.get("labels_as_list", C.BOOL_FALSE)):
+            filters = {"labels": self._s.textbox_2_list_filter}
         else:
             filters = {}
         tickets_xmlui, metadata = yield defer.ensureDeferred(
@@ -192,10 +241,11 @@
                 rsm_request=rsm_request,
                 extra=extra,
                 form_ns=APP_NS_MERGE_REQUESTS,
-                filters = filters)
+                filters=filters,
+            )
         )
         parsed_patches = []
-        if extra.get('parse', False):
+        if extra.get("parse", False):
             for ticket in tickets_xmlui:
                 request_type = ticket.named_widgets[FIELD_DATA_TYPE].value
                 request_data = ticket.named_widgets[FIELD_DATA].value
@@ -203,21 +253,51 @@
                 parsed_patches.append(parsed_data)
         defer.returnValue((tickets_xmlui, metadata, parsed_patches))
 
-    def _set(self, service, node, repository, method, values, schema=None, item_id=None,
-             extra="", profile_key=C.PROF_KEY_NONE):
+    def _set(
+        self,
+        service,
+        node,
+        repository,
+        method,
+        values,
+        schema=None,
+        item_id=None,
+        extra="",
+        profile_key=C.PROF_KEY_NONE,
+    ):
         client, service, node, schema, item_id, extra = self._s.prepare_bridge_set(
-            service, node, schema, item_id, extra, profile_key)
+            service, node, schema, item_id, extra, profile_key
+        )
         d = defer.ensureDeferred(
             self.set(
-                client, service, node, repository, method, values, schema,
-                item_id or None, extra, deserialise=True
+                client,
+                service,
+                node,
+                repository,
+                method,
+                values,
+                schema,
+                item_id or None,
+                extra,
+                deserialise=True,
             )
         )
-        d.addCallback(lambda ret: ret or '')
+        d.addCallback(lambda ret: ret or "")
         return d
 
-    async def set(self, client, service, node, repository, method='auto', values=None,
-            schema=None, item_id=None, extra=None, deserialise=False):
+    async def set(
+        self,
+        client,
+        service,
+        node,
+        repository,
+        method="auto",
+        values=None,
+        schema=None,
+        item_id=None,
+        extra=None,
+        deserialise=False,
+    ):
         """Publish a tickets
 
         @param service(None, jid.JID): Pubsub service to use
@@ -233,18 +313,21 @@
             node = self.namespace
         if values is None:
             values = {}
-        update = extra.get('update', False)
+        update = extra.get("update", False)
         if not repository and not update:
             # in case of update, we may re-user former patches data
             # so repository is not mandatory
             raise exceptions.DataError(_("repository must be specified"))
 
         if FIELD_DATA in values:
-            raise exceptions.DataError(_("{field} is set by backend, you must not set "
-                                         "it in frontend").format(field = FIELD_DATA))
+            raise exceptions.DataError(
+                _("{field} is set by backend, you must not set " "it in frontend").format(
+                    field=FIELD_DATA
+                )
+            )
 
         if repository:
-            if method == 'auto':
+            if method == "auto":
                 for name in self._handlers_list:
                     handler = self._handlers[name].handler
                     can_handle = await handler.check(repository)
@@ -252,11 +335,15 @@
                         log.info(_("{name} handler will be used").format(name=name))
                         break
                 else:
-                    log.warning(_("repository {path} can't be handled by any installed "
-                                  "handler").format(
-                        path = repository))
-                    raise exceptions.NotFound(_("no handler for this repository has "
-                                                "been found"))
+                    log.warning(
+                        _(
+                            "repository {path} can't be handled by any installed "
+                            "handler"
+                        ).format(path=repository)
+                    )
+                    raise exceptions.NotFound(
+                        _("no handler for this repository has " "been found")
+                    )
             else:
                 try:
                     handler = self._handlers[name].handler
@@ -265,35 +352,50 @@
 
             data = await handler.export(repository)
             if not data.strip():
-                raise exceptions.DataError(_('export data is empty, do you have any '
-                                             'change to send?'))
+                raise exceptions.DataError(
+                    _("export data is empty, do you have any " "change to send?")
+                )
 
-            if not values.get('title') or not values.get('body'):
+            if not values.get("title") or not values.get("body"):
                 patches = 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']
+                if not values.get("title"):
+                    values["title"] = msg_lines[0]
+                if not values.get("body"):
+                    ts = self.host.plugins["TEXT_SYNTAXES"]
                     xhtml = await ts.convert(
-                        '\n'.join(msg_lines[1:]),
-                        syntax_from = ts.SYNTAX_TEXT,
-                        syntax_to = ts.SYNTAX_XHTML,
-                        profile = client.profile)
-                    values['body'] = '<div xmlns="{ns}">{xhtml}</div>'.format(
-                        ns=C.NS_XHTML, xhtml=xhtml)
+                        "\n".join(msg_lines[1:]),
+                        syntax_from=ts.SYNTAX_TEXT,
+                        syntax_to=ts.SYNTAX_XHTML,
+                        profile=client.profile,
+                    )
+                    values["body"] = '<div xmlns="{ns}">{xhtml}</div>'.format(
+                        ns=C.NS_XHTML, xhtml=xhtml
+                    )
 
             values[FIELD_DATA] = data
 
-        item_id = await self._t.set(client, service, node, values, schema, item_id, extra,
-                                    deserialise, form_ns=APP_NS_MERGE_REQUESTS)
+        item_id = await self._t.set(
+            client,
+            service,
+            node,
+            values,
+            schema,
+            item_id,
+            extra,
+            deserialise,
+            form_ns=APP_NS_MERGE_REQUESTS,
+        )
         return item_id
 
     def _parse_data(self, data_type, data):
         d = self.parse_data(data_type, data)
-        d.addCallback(lambda parsed_patches:
-            {key: str(value) for key, value in parsed_patches.items()})
+        d.addCallback(
+            lambda parsed_patches: {
+                key: str(value) for key, value in parsed_patches.items()
+            }
+        )
         return d
 
     def parse_data(self, data_type, data):
@@ -308,21 +410,31 @@
         try:
             handler = self._type_handlers[data_type]
         except KeyError:
-            raise exceptions.NotFound(_('No handler can handle data type "{type}"')
-                                      .format(type=data_type))
+            raise exceptions.NotFound(
+                _('No handler can handle data type "{type}"').format(type=data_type)
+            )
         return defer.maybeDeferred(handler.handler.parse, data, data_type)
 
-    def _import(self, repository, item_id, service=None, node=None, extra=None,
-                profile_key=C.PROF_KEY_NONE):
+    def _import(
+        self,
+        repository,
+        item_id,
+        service=None,
+        node=None,
+        extra=None,
+        profile_key=C.PROF_KEY_NONE,
+    ):
         client = self.host.get_client(profile_key)
         service = jid.JID(service) if service else None
-        d = self.import_request(client, repository, item_id, service, node or None,
-                                extra=extra or None)
+        d = self.import_request(
+            client, repository, item_id, service, node or None, extra=extra or None
+        )
         return d
 
     @defer.inlineCallbacks
-    def import_request(self, client, repository, item, service=None, node=None,
-                       extra=None):
+    def import_request(
+        self, client, repository, item, service=None, node=None, extra=None
+    ):
         """import a merge request in specified directory
 
         @param repository(unicode): path to the repository where the code stands
@@ -336,7 +448,8 @@
                 node,
                 max_items=1,
                 item_ids=[item],
-                form_ns=APP_NS_MERGE_REQUESTS)
+                form_ns=APP_NS_MERGE_REQUESTS,
+            )
         )
         ticket_xmlui = tickets_xmlui[0]
         data = ticket_xmlui.named_widgets[FIELD_DATA].value
@@ -344,10 +457,14 @@
         try:
             handler = self._type_handlers[data_type]
         except KeyError:
-            raise exceptions.NotFound(_('No handler found to import {data_type}')
-                                      .format(data_type=data_type))
-        log.info(_("Importing patch [{item_id}] using {name} handler").format(
-            item_id = item,
-            name = handler.name))
-        yield handler.handler.import_(repository, data, data_type, item, service, node,
-                                      extra)
+            raise exceptions.NotFound(
+                _("No handler found to import {data_type}").format(data_type=data_type)
+            )
+        log.info(
+            _("Importing patch [{item_id}] using {name} handler").format(
+                item_id=item, name=handler.name
+            )
+        )
+        yield handler.handler.import_(
+            repository, data, data_type, item, service, node, extra
+        )