diff sat/plugins/plugin_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 fee60f17ebac
line wrap: on
line diff
--- a/sat/plugins/plugin_import.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_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 generic data import handling
@@ -38,7 +38,7 @@
     C.PI_DEPENDENCIES: [],
     C.PI_MAIN: "ImportPlugin",
     C.PI_HANDLER: "no",
-    C.PI_DESCRIPTION: _(u"""Generic import plugin, base for specialized importers"""),
+    C.PI_DESCRIPTION: _("""Generic import plugin, base for specialized importers"""),
 }
 
 Importer = collections.namedtuple("Importer", ("callback", "short_desc", "long_desc"))
@@ -64,7 +64,7 @@
         @param name(unicode): import handler name
         """
         assert name == name.lower().strip()
-        log.info(_(u"initializing {name} import handler").format(name=name))
+        log.info(_("initializing {name} import handler").format(name=name))
         import_handler.name = name
         import_handler.register = partial(self.register, import_handler)
         import_handler.unregister = partial(self.unregister, import_handler)
@@ -93,7 +93,7 @@
             in_sign="ssa{ss}sss",
             out_sign="s",
             method=_import,
-            async=True,
+            async_=True,
         )
         self.host.bridge.addMethod(
             name + "ImportList",
@@ -115,7 +115,7 @@
         return client._import[import_handler.name][progress_id]
 
     def listImporters(self, import_handler):
-        importers = import_handler.importers.keys()
+        importers = list(import_handler.importers.keys())
         importers.sort()
         return [
             (name, import_handler.importers[name].short_desc)
@@ -132,7 +132,7 @@
             importer = import_handler.importers[name]
         except KeyError:
             raise exceptions.NotFound(
-                u"{handler_name} importer not found [{name}]".format(
+                "{handler_name} importer not found [{name}]".format(
                     handler_name=import_handler.name, name=name
                 )
             )
@@ -150,7 +150,7 @@
         profile=C.PROF_KEY_NONE,
     ):
         client = self.host.getClient(profile)
-        options = {key: unicode(value) for key, value in options.iteritems()}
+        options = {key: str(value) for key, value in options.items()}
         for option in import_handler.BOOL_OPTIONS:
             try:
                 options[option] = C.bool(options[option])
@@ -161,14 +161,14 @@
                 options[option] = json.loads(options[option])
             except ValueError:
                 raise exceptions.DataError(
-                    _(u"invalid json option: {name}").format(name=option)
+                    _("invalid json option: {name}").format(name=option)
                 )
         pubsub_service = jid.JID(pubsub_service) if pubsub_service else None
         return self.doImport(
             client,
             import_handler,
-            unicode(name),
-            unicode(location),
+            str(name),
+            str(location),
             options,
             pubsub_service,
             pubsub_node or None,
@@ -202,7 +202,7 @@
         if options is None:
             options = {}
         else:
-            for opt_name, opt_default in import_handler.OPT_DEFAULTS.iteritems():
+            for opt_name, opt_default in import_handler.OPT_DEFAULTS.items():
                 # we want a filled options dict, with all empty or False values removed
                 try:
                     value = options[opt_name]
@@ -216,21 +216,21 @@
         try:
             importer = import_handler.importers[name]
         except KeyError:
-            raise exceptions.NotFound(u"Importer [{}] not found".format(name))
+            raise exceptions.NotFound("Importer [{}] not found".format(name))
         items_import_data, items_count = yield importer.callback(
             client, location, options
         )
-        progress_id = unicode(uuid.uuid4())
+        progress_id = str(uuid.uuid4())
         try:
             _import = client._import
         except AttributeError:
             _import = client._import = {}
         progress_data = _import.setdefault(import_handler.name, {})
-        progress_data[progress_id] = {u"position": "0"}
+        progress_data[progress_id] = {"position": "0"}
         if items_count is not None:
-            progress_data[progress_id]["size"] = unicode(items_count)
+            progress_data[progress_id]["size"] = str(items_count)
         metadata = {
-            "name": u"{}: {}".format(name, location),
+            "name": "{}: {}".format(name, location),
             "direction": "out",
             "type": import_handler.name.upper() + "_IMPORT",
         }
@@ -242,8 +242,8 @@
         )
         self.host.bridge.progressStarted(progress_id, metadata, client.profile)
         session = {  #  session data, can be used by importers
-            u"root_service": pubsub_service,
-            u"root_node": pubsub_node,
+            "root_service": pubsub_service,
+            "root_node": pubsub_node,
         }
         self.recursiveImport(
             client,
@@ -306,11 +306,11 @@
                 recurse_kwargs.setdefault("options", options)
                 recurse_kwargs["return_data"] = return_data
                 recurse_kwargs["depth"] = depth + 1
-                log.debug(_(u"uploading subitems"))
+                log.debug(_("uploading subitems"))
                 yield self.recursiveImport(**recurse_kwargs)
 
             if depth == 0:
-                client._import[import_handler.name][progress_id]["position"] = unicode(
+                client._import[import_handler.name][progress_id]["position"] = str(
                     idx + 1
                 )
 
@@ -338,7 +338,7 @@
         if name in import_handler.importers:
             raise exceptions.ConflictError(
                 _(
-                    u"An {handler_name} importer with the name {name} already exist"
+                    "An {handler_name} importer with the name {name} already exist"
                 ).format(handler_name=import_handler.name, name=name)
             )
         import_handler.importers[name] = Importer(callback, short_desc, long_desc)