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
This commit is contained in:
Goffi 2019-08-13 19:08:41 +02:00
commit fbfe96c505

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SAT plugin for managing xep-0264
@ -26,7 +26,7 @@ log = getLogger(__name__)
from twisted.internet import threads
from twisted.python.failure import Failure
from zope.interface import implements
from zope.interface import implementer
from wokkel import disco, iwokkel
@ -37,7 +37,7 @@ try:
from PIL import Image
except:
raise exceptions.MissingModule(
u"Missing module pillow, please download/install it from https://python-pillow.github.io"
"Missing module pillow, please download/install it from https://python-pillow.github.io"
)
#  cf. https://stackoverflow.com/a/23575424
@ -51,8 +51,8 @@ except ImportError:
from wokkel.subprotocols import XMPPHandler
MIME_TYPE = u"image/jpeg"
SAVE_FORMAT = u"JPEG" # (cf. Pillow documentation)
MIME_TYPE = "image/jpeg"
SAVE_FORMAT = "JPEG" # (cf. Pillow documentation)
NS_THUMBS = "urn:xmpp:thumbs:1"
@ -74,7 +74,7 @@ class XEP_0264(object):
SIZE_MEDIUM = (1024, 1024)
def __init__(self, host):
log.info(_(u"Plugin XEP_0264 initialization"))
log.info(_("Plugin XEP_0264 initialization"))
self.host = host
host.trigger.add("XEP-0234_buildFileElement", self._addFileThumbnails)
host.trigger.add("XEP-0234_parseFileElement", self._getFileThumbnails)
@ -86,21 +86,21 @@ class XEP_0264(object):
def _addFileThumbnails(self, file_elt, extra_args):
try:
thumbnails = extra_args[u"extra"][C.KEY_THUMBNAILS]
thumbnails = extra_args["extra"][C.KEY_THUMBNAILS]
except KeyError:
return
for thumbnail in thumbnails:
thumbnail_elt = file_elt.addElement((NS_THUMBS, u"thumbnail"))
thumbnail_elt["uri"] = u"cid:" + thumbnail["id"]
thumbnail_elt = file_elt.addElement((NS_THUMBS, "thumbnail"))
thumbnail_elt["uri"] = "cid:" + thumbnail["id"]
thumbnail_elt["media-type"] = MIME_TYPE
width, height = thumbnail["size"]
thumbnail_elt["width"] = unicode(width)
thumbnail_elt["height"] = unicode(height)
thumbnail_elt["width"] = str(width)
thumbnail_elt["height"] = str(height)
return True
def _getFileThumbnails(self, file_elt, file_data):
thumbnails = []
for thumbnail_elt in file_elt.elements(NS_THUMBS, u"thumbnail"):
for thumbnail_elt in file_elt.elements(NS_THUMBS, "thumbnail"):
uri = thumbnail_elt["uri"]
if uri.startswith("cid:"):
thumbnail = {"id": uri[4:]}
@ -144,7 +144,7 @@ class XEP_0264(object):
try:
img = Image.open(source_path)
except IOError:
return Failure(exceptions.DataError(u"Can't open image"))
return Failure(exceptions.DataError("Can't open image"))
img.thumbnail(size)
uid = self.getThumbId(image_uid or source_path, size)
@ -176,13 +176,13 @@ class XEP_0264(object):
self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid
)
d.addErrback(
lambda failure_: log.error(u"thumbnail generation error: {}".format(failure_))
lambda failure_: log.error("thumbnail generation error: {}".format(failure_))
)
return d
@implementer(iwokkel.IDisco)
class XEP_0264_handler(XMPPHandler):
implements(iwokkel.IDisco)
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
return [disco.DiscoFeature(NS_THUMBS)]