diff sat/plugins/plugin_exp_pubsub_hook.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_exp_pubsub_hook.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_exp_pubsub_hook.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SAT plugin for Pubsub Hooks
@@ -43,17 +43,17 @@
 }
 
 #  python module
-HOOK_TYPE_PYTHON = u"python"
+HOOK_TYPE_PYTHON = "python"
 # python file path
-HOOK_TYPE_PYTHON_FILE = u"python_file"
+HOOK_TYPE_PYTHON_FILE = "python_file"
 # python code directly
-HOOK_TYPE_PYTHON_CODE = u"python_code"
+HOOK_TYPE_PYTHON_CODE = "python_code"
 HOOK_TYPES = (HOOK_TYPE_PYTHON, HOOK_TYPE_PYTHON_FILE, HOOK_TYPE_PYTHON_CODE)
 
 
 class PubsubHook(object):
     def __init__(self, host):
-        log.info(_(u"PubSub Hook initialization"))
+        log.info(_("PubSub Hook initialization"))
         self.host = host
         self.node_hooks = {}  # keep track of the number of hooks per node (for all profiles)
         host.bridge.addMethod(
@@ -90,7 +90,7 @@
 
     def _installNodeManager(self, client, node):
         if node in self.node_hooks:
-            log.debug(_(u"node manager already set for {node}").format(node=node))
+            log.debug(_("node manager already set for {node}").format(node=node))
             self.node_hooks[node] += 1
         else:
             # first hook on this node
@@ -98,29 +98,29 @@
                 node, items_cb=self._itemsReceived
             )
             self.node_hooks[node] = 0
-            log.info(_(u"node manager installed on {node}").format(node=node))
+            log.info(_("node manager installed on {node}").format(node=node))
 
     def _removeNodeManager(self, client, node):
         try:
             self.node_hooks[node] -= 1
         except KeyError:
-            log.error(_(u"trying to remove a {node} without hook").format(node=node))
+            log.error(_("trying to remove a {node} without hook").format(node=node))
         else:
             if self.node_hooks[node] == 0:
                 del self.node_hooks[node]
                 self.host.plugins["XEP-0060"].removeManagedNode(node, self._itemsReceived)
-                log.debug(_(u"hook removed"))
+                log.debug(_("hook removed"))
             else:
-                log.debug(_(u"node still needed for an other hook"))
+                log.debug(_("node still needed for an other hook"))
 
     def installHook(self, client, service, node, hook_type, hook_arg, persistent):
         if hook_type not in HOOK_TYPES:
             raise exceptions.DataError(
-                _(u"{hook_type} is not handled").format(hook_type=hook_type)
+                _("{hook_type} is not handled").format(hook_type=hook_type)
             )
         if hook_type != HOOK_TYPE_PYTHON_FILE:
             raise NotImplementedError(
-                _(u"{hook_type} hook type not implemented yet").format(
+                _("{hook_type} hook type not implemented yet").format(
                     hook_type=hook_type
                 )
             )
@@ -136,8 +136,8 @@
             hooks_list.append(hook_data)
 
         log.info(
-            _(u"{persistent} hook installed on {node} for {profile}").format(
-                persistent=_(u"persistent") if persistent else _(u"temporary"),
+            _("{persistent} hook installed on {node} for {profile}").format(
+                persistent=_("persistent") if persistent else _("temporary"),
                 node=node,
                 profile=client.profile,
             )
@@ -160,18 +160,18 @@
                     try:
                         if hook_type == HOOK_TYPE_PYTHON_FILE:
                             hook_globals = {}
-                            execfile(hook_data["arg"], hook_globals)
+                            exec(compile(open(hook_data["arg"], "rb").read(), hook_data["arg"], 'exec'), hook_globals)
                             callback = hook_globals["hook"]
                         else:
                             raise NotImplementedError(
-                                _(u"{hook_type} hook type not implemented yet").format(
+                                _("{hook_type} hook type not implemented yet").format(
                                     hook_type=hook_type
                                 )
                             )
                     except Exception as e:
                         log.warning(
                             _(
-                                u"Can't load Pubsub hook at node {node}, it will be removed: {reason}"
+                                "Can't load Pubsub hook at node {node}, it will be removed: {reason}"
                             ).format(node=node, reason=e)
                         )
                         hooks_list.remove(hook_data)
@@ -183,7 +183,7 @@
                     except Exception as e:
                         log.warning(
                             _(
-                                u"Error while running Pubsub hook for node {node}: {msg}"
+                                "Error while running Pubsub hook for node {node}: {msg}"
                             ).format(node=node, msg=e)
                         )
 
@@ -193,9 +193,9 @@
         return self.addHook(
             client,
             service,
-            unicode(node),
-            unicode(hook_type),
-            unicode(hook_arg),
+            str(node),
+            str(hook_type),
+            str(hook_arg),
             persistent,
         )
 
@@ -242,11 +242,11 @@
             if node in hooks:
                 for hook_data in hooks[node]:
                     if (
-                        service != hook_data[u"service"]
+                        service != hook_data["service"]
                         or hook_type is not None
-                        and hook_type != hook_data[u"type"]
+                        and hook_type != hook_data["type"]
                         or hook_arg is not None
-                        and hook_arg != hook_data[u"arg"]
+                        and hook_arg != hook_data["arg"]
                     ):
                         continue
                     hooks[node].remove(hook_data)
@@ -263,8 +263,8 @@
     def _listHooks(self, profile):
         hooks_list = self.listHooks(self.host.getClient(profile))
         for hook in hooks_list:
-            hook[u"service"] = hook[u"service"].full()
-            hook[u"persistent"] = C.boolConst(hook[u"persistent"])
+            hook["service"] = hook["service"].full()
+            hook["persistent"] = C.boolConst(hook["persistent"])
         return hooks_list
 
     def listHooks(self, client):
@@ -272,15 +272,15 @@
         hooks_list = []
         for hooks in (client._hooks, client._hooks_temporary):
             persistent = hooks is client._hooks
-            for node, hooks_data in hooks.iteritems():
+            for node, hooks_data in hooks.items():
                 for hook_data in hooks_data:
                     hooks_list.append(
                         {
-                            u"service": hook_data[u"service"],
-                            u"node": node,
-                            u"type": hook_data[u"type"],
-                            u"arg": hook_data[u"arg"],
-                            u"persistent": persistent,
+                            "service": hook_data["service"],
+                            "node": node,
+                            "type": hook_data["type"],
+                            "arg": hook_data["arg"],
+                            "persistent": persistent,
                         }
                     )
         return hooks_list