Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0264.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0264.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_xep_0264.py Wed Jun 27 20:14:46 2018 +0200 @@ -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 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ """ 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 []