diff twisted/plugins/pubsub.py @ 414:ccb2a22ea0fc

Python 3 port: /!\ Python 3.6+ is now needed to use SàT Pubsub /!\ instability may occur and features may not be working anymore, this will improve with time The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs for details). Python minimal version has been updated in setup.py
author Goffi <goffi@goffi.org>
date Fri, 16 Aug 2019 12:53:33 +0200
parents a58610ab2983
children 412d26a9b2c2
line wrap: on
line diff
--- a/twisted/plugins/pubsub.py	Fri Aug 16 12:48:34 2019 +0200
+++ b/twisted/plugins/pubsub.py	Fri Aug 16 12:53:33 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #-*- coding: utf-8 -*-
 
 # Copyright (c) 2012-2019 Jérôme Poisson
@@ -67,7 +67,7 @@
 from wokkel import pubsub
 from wokkel import rsm
 from wokkel import mam
-from zope.interface import implements
+from zope.interface import implementer
 
 from sat_pubsub import const
 from sat_pubsub import mam as pubsub_mam
@@ -77,19 +77,19 @@
 from sat_pubsub.privilege import PrivilegesHandler
 from sat_pubsub.delegation import DelegationsHandler
 from os.path import expanduser, realpath
-import ConfigParser
+import configparser
 
 
 def coerceListType(value):
-    return csv.reader(
+    return next(csv.reader(
         [value], delimiter=",", quotechar='"', skipinitialspace=True
-    ).next()
+    ))
 
 
 def coerceJidListType(value):
     values = [JID(v) for v in coerceListType(value)]
     if any((j.resource for j in values)):
-        raise ValueError(u"you must use bare jids")
+        raise ValueError("you must use bare jids")
     return values
 
 
@@ -112,7 +112,7 @@
      coerceJidListType]
     ]
 
-CONFIG_FILENAME = u'sat'
+CONFIG_FILENAME = 'sat'
 # List of the configuration filenames sorted by ascending priority
 CONFIG_FILES = [realpath(expanduser(path) + CONFIG_FILENAME + '.conf') for path in (
     '/etc/', '/etc/{}/'.format(CONFIG_FILENAME),
@@ -142,23 +142,21 @@
         # if the  options values are the hard-coded ones or if they have been passed on the command line.
 
         # FIXME: must be refactored + code can be factorised with backend
-        config_parser = ConfigParser.SafeConfigParser()
+        config_parser = configparser.ConfigParser()
         config_parser.read(CONFIG_FILES)
         for param in self.optParameters + OPT_PARAMETERS_CFG:
             name = param[0]
             try:
                 value = config_parser.get(CONFIG_SECTION, name)
-                if isinstance(value, unicode):
-                    value = value.encode('utf-8')
                 try:
                     param[2] = param[4](value)
                 except IndexError: # the coerce method is optional
                     param[2] = value
                 except Exception as e:
-                    log.err(u'Invalid value for setting "{name}": {msg}'.format(
+                    log.err('Invalid value for setting "{name}": {msg}'.format(
                         name=name, msg=e))
                     sys.exit(1)
-            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
+            except (configparser.NoSectionError, configparser.NoOptionError):
                 pass
         usage.Options.__init__(self)
         for opt_data in OPT_PARAMETERS_CFG:
@@ -173,10 +171,10 @@
         self['jid'] = JID(self['jid']) if self['jid'] else None
 
 
+@implementer(IServiceMaker, IPlugin)
 class SatPubsubMaker(object):
-    implements(IServiceMaker, IPlugin)
     tapname = "sat-pubsub"
-    description = u"Salut à Toi Publish-Subscribe Service Component".encode('utf-8')
+    description = "Salut à Toi Publish-Subscribe Service Component"
     options = Options
 
     def makeService(self, config):
@@ -198,7 +196,7 @@
                 'db_port': 'port',
             }
             kwargs = {}
-            for config_k, k in keys_map.iteritems():
+            for config_k, k in keys_map.items():
                 v = config.get(config_k)
                 if v is None:
                     continue
@@ -230,7 +228,7 @@
             cs.logTraffic = True
 
         FallbackHandler().setHandlerParent(cs)
-        VersionHandler(u'SàT Pubsub', sat_pubsub.__version__).setHandlerParent(cs)
+        VersionHandler('SàT Pubsub', sat_pubsub.__version__).setHandlerParent(cs)
         DiscoHandler().setHandlerParent(cs)
 
         ph = PrivilegesHandler(config['jid'])