diff sat/plugins/plugin_tickets_import.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_tickets_import.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_tickets_import.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SàT plugin for import external ticketss
@@ -36,14 +36,14 @@
     C.PI_MAIN: "TicketsImportPlugin",
     C.PI_HANDLER: "no",
     C.PI_DESCRIPTION: _(
-        u"""Tickets import management:
+        """Tickets import management:
 This plugin manage the different tickets importers which can register to it, and handle generic importing tasks."""
     ),
 }
 
 OPT_MAPPING = "mapping"
-FIELDS_LIST = (u"labels", u"cc_emails")  # fields which must have a list as value
-FIELDS_DATE = (u"created", u"updated")
+FIELDS_LIST = ("labels", "cc_emails")  # fields which must have a list as value
+FIELDS_DATE = ("created", "updated")
 
 NS_TICKETS = "org.salut-a-toi.tickets:0"
 
@@ -60,7 +60,7 @@
         self._p = host.plugins["XEP-0060"]
         self._m = host.plugins["XEP-0277"]
         self._s = host.plugins["PUBSUB_SCHEMA"]
-        host.plugins["IMPORT"].initialize(self, u"tickets")
+        host.plugins["IMPORT"].initialize(self, "tickets")
 
     @defer.inlineCallbacks
     def importItem(
@@ -108,21 +108,21 @@
         """
         if "comments_uri" in item_import_data:
             raise exceptions.DataError(
-                _(u"comments_uri key will be generated and must not be used by importer")
+                _("comments_uri key will be generated and must not be used by importer")
             )
         for key in FIELDS_LIST:
             if not isinstance(item_import_data.get(key, []), list):
-                raise exceptions.DataError(_(u"{key} must be a list").format(key=key))
+                raise exceptions.DataError(_("{key} must be a list").format(key=key))
         for key in FIELDS_DATE:
             try:
                 item_import_data[key] = utils.xmpp_date(item_import_data[key])
             except KeyError:
                 continue
-        if session[u"root_node"] is None:
-            session[u"root_node"] = NS_TICKETS
+        if session["root_node"] is None:
+            session["root_node"] = NS_TICKETS
         if not "schema" in session:
             session["schema"] = yield self._s.getSchemaForm(
-                client, service, node or session[u"root_node"]
+                client, service, node or session["root_node"]
             )
         defer.returnValue(item_import_data)
 
@@ -133,7 +133,7 @@
         # TODO: node access/publish model should be customisable
         comments = ticket_data.get("comments", [])
         service = yield self._m.getCommentsService(client)
-        node = self._m.getCommentsNode(session["root_node"] + u"_" + ticket_data["id"])
+        node = self._m.getCommentsNode(session["root_node"] + "_" + ticket_data["id"])
         node_options = {
             self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
             self._p.OPT_PERSIST_ITEMS: 1,
@@ -144,7 +144,7 @@
         }
         yield self._p.createIfNewNode(client, service, node, options=node_options)
         ticket_data["comments_uri"] = uri.buildXMPPUri(
-            u"pubsub", subtype="microblog", path=service.full(), node=node
+            "pubsub", subtype="microblog", path=service.full(), node=node
         )
         for comment in comments:
             if "updated" not in comment and "published" in comment:
@@ -157,7 +157,7 @@
             node = NS_TICKETS
         id_ = ticket_data.pop("id", None)
         log.debug(
-            u"uploading item [{id}]: {title}".format(
+            "uploading item [{id}]: {title}".format(
                 id=id_, title=ticket_data.get("title", "")
             )
         )
@@ -169,13 +169,13 @@
         mapping = options.get(OPT_MAPPING)
         if mapping is not None:
             if not isinstance(mapping, dict):
-                raise exceptions.DataError(_(u"mapping option must be a dictionary"))
+                raise exceptions.DataError(_("mapping option must be a dictionary"))
 
-            for source, dest in mapping.iteritems():
-                if not isinstance(source, unicode) or not isinstance(dest, unicode):
+            for source, dest in mapping.items():
+                if not isinstance(source, str) or not isinstance(dest, str):
                     raise exceptions.DataError(
                         _(
-                            u"keys and values of mapping must be sources and destinations ticket fields"
+                            "keys and values of mapping must be sources and destinations ticket fields"
                         )
                     )
                 if source in ticket_data:
@@ -185,6 +185,6 @@
                         values.append(value)
                     else:
                         if dest in ticket_data:
-                            ticket_data[dest] = ticket_data[dest] + u"\n" + value
+                            ticket_data[dest] = ticket_data[dest] + "\n" + value
                         else:
                             ticket_data[dest] = value