code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -21,6 +21,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.internet import threads
from twisted.python.failure import Failure
@ -31,13 +32,17 @@ from wokkel import disco, iwokkel
from sat.core import exceptions
import hashlib
try:
from PIL import Image
except:
raise exceptions.MissingModule(u"Missing module pillow, please download/install it from https://python-pillow.github.io")
raise exceptions.MissingModule(
u"Missing module pillow, please download/install it from https://python-pillow.github.io"
)
# cf. https://stackoverflow.com/a/23575424
#  cf. https://stackoverflow.com/a/23575424
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
@ -46,10 +51,10 @@ except ImportError:
from wokkel.subprotocols import XMPPHandler
MIME_TYPE = u'image/jpeg'
SAVE_FORMAT = u'JPEG' # (cf. Pillow documentation)
MIME_TYPE = u"image/jpeg"
SAVE_FORMAT = u"JPEG" # (cf. Pillow documentation)
NS_THUMBS = 'urn:xmpp:thumbs:1'
NS_THUMBS = "urn:xmpp:thumbs:1"
PLUGIN_INFO = {
C.PI_NAME: "XEP-0264",
@ -60,7 +65,7 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0234"],
C.PI_MAIN: "XEP_0264",
C.PI_HANDLER: "yes",
C.PI_DESCRIPTION: _("""Thumbnails handling""")
C.PI_DESCRIPTION: _("""Thumbnails handling"""),
}
@ -81,39 +86,39 @@ class XEP_0264(object):
def _addFileThumbnails(self, file_elt, extra_args):
try:
thumbnails = extra_args[u'extra'][C.KEY_THUMBNAILS]
thumbnails = extra_args[u"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['media-type'] = MIME_TYPE
width, height = thumbnail['size']
thumbnail_elt['width'] = unicode(width)
thumbnail_elt['height'] = unicode(height)
thumbnail_elt = file_elt.addElement((NS_THUMBS, u"thumbnail"))
thumbnail_elt["uri"] = u"cid:" + thumbnail["id"]
thumbnail_elt["media-type"] = MIME_TYPE
width, height = thumbnail["size"]
thumbnail_elt["width"] = unicode(width)
thumbnail_elt["height"] = unicode(height)
return True
def _getFileThumbnails(self, file_elt, file_data):
thumbnails = []
for thumbnail_elt in file_elt.elements(NS_THUMBS, u'thumbnail'):
uri = thumbnail_elt['uri']
if uri.startswith ('cid:'):
thumbnail = {'id': uri[4:]}
width = thumbnail_elt.getAttribute('width')
height = thumbnail_elt.getAttribute('height')
for thumbnail_elt in file_elt.elements(NS_THUMBS, u"thumbnail"):
uri = thumbnail_elt["uri"]
if uri.startswith("cid:"):
thumbnail = {"id": uri[4:]}
width = thumbnail_elt.getAttribute("width")
height = thumbnail_elt.getAttribute("height")
if width and height:
try:
thumbnail['size'] = int(width), int(height)
thumbnail["size"] = int(width), int(height)
except ValueError:
pass
try:
thumbnail['mime_type'] = thumbnail_elt['media-type']
thumbnail["mime_type"] = thumbnail_elt["media-type"]
except KeyError:
pass
thumbnails.append(thumbnail)
if thumbnails:
file_data.setdefault('extra', {})[C.KEY_THUMBNAILS] = thumbnails
file_data.setdefault("extra", {})[C.KEY_THUMBNAILS] = thumbnails
return True
## thumbnails generation ##
@ -145,11 +150,8 @@ class XEP_0264(object):
uid = self.getThumbId(image_uid or source_path, size)
with self.host.common_cache.cacheData(
PLUGIN_INFO[C.PI_IMPORT_NAME],
uid,
MIME_TYPE,
max_age,
) as f:
PLUGIN_INFO[C.PI_IMPORT_NAME], uid, MIME_TYPE, max_age
) as f:
img.save(f, SAVE_FORMAT)
return img.size, uid
@ -171,17 +173,19 @@ class XEP_0264(object):
"""
d = threads.deferToThread(
self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid)
d.addErrback(lambda failure_:
log.error(u"thumbnail generation error: {}".format(failure_)))
self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid
)
d.addErrback(
lambda failure_: log.error(u"thumbnail generation error: {}".format(failure_))
)
return d
class XEP_0264_handler(XMPPHandler):
implements(iwokkel.IDisco)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
return [disco.DiscoFeature(NS_THUMBS)]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
def getDiscoItems(self, requestor, target, nodeIdentifier=""):
return []