Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | 20 |
21 from sat.core.i18n import _ | 21 from sat.core.i18n import _ |
22 from sat.core.constants import Const as C | 22 from sat.core.constants import Const as C |
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 | |
24 log = getLogger(__name__) | 25 log = getLogger(__name__) |
25 from twisted.internet import threads | 26 from twisted.internet import threads |
26 from twisted.python.failure import Failure | 27 from twisted.python.failure import Failure |
27 | 28 |
28 from zope.interface import implements | 29 from zope.interface import implements |
29 | 30 |
30 from wokkel import disco, iwokkel | 31 from wokkel import disco, iwokkel |
31 | 32 |
32 from sat.core import exceptions | 33 from sat.core import exceptions |
33 import hashlib | 34 import hashlib |
35 | |
34 try: | 36 try: |
35 from PIL import Image | 37 from PIL import Image |
36 except: | 38 except: |
37 raise exceptions.MissingModule(u"Missing module pillow, please download/install it from https://python-pillow.github.io") | 39 raise exceptions.MissingModule( |
40 u"Missing module pillow, please download/install it from https://python-pillow.github.io" | |
41 ) | |
38 | 42 |
39 # cf. https://stackoverflow.com/a/23575424 | 43 # cf. https://stackoverflow.com/a/23575424 |
40 from PIL import ImageFile | 44 from PIL import ImageFile |
45 | |
41 ImageFile.LOAD_TRUNCATED_IMAGES = True | 46 ImageFile.LOAD_TRUNCATED_IMAGES = True |
42 | 47 |
43 try: | 48 try: |
44 from twisted.words.protocols.xmlstream import XMPPHandler | 49 from twisted.words.protocols.xmlstream import XMPPHandler |
45 except ImportError: | 50 except ImportError: |
46 from wokkel.subprotocols import XMPPHandler | 51 from wokkel.subprotocols import XMPPHandler |
47 | 52 |
48 | 53 |
49 MIME_TYPE = u'image/jpeg' | 54 MIME_TYPE = u"image/jpeg" |
50 SAVE_FORMAT = u'JPEG' # (cf. Pillow documentation) | 55 SAVE_FORMAT = u"JPEG" # (cf. Pillow documentation) |
51 | 56 |
52 NS_THUMBS = 'urn:xmpp:thumbs:1' | 57 NS_THUMBS = "urn:xmpp:thumbs:1" |
53 | 58 |
54 PLUGIN_INFO = { | 59 PLUGIN_INFO = { |
55 C.PI_NAME: "XEP-0264", | 60 C.PI_NAME: "XEP-0264", |
56 C.PI_IMPORT_NAME: "XEP-0264", | 61 C.PI_IMPORT_NAME: "XEP-0264", |
57 C.PI_TYPE: "XEP", | 62 C.PI_TYPE: "XEP", |
58 C.PI_MODES: C.PLUG_MODE_BOTH, | 63 C.PI_MODES: C.PLUG_MODE_BOTH, |
59 C.PI_PROTOCOLS: ["XEP-0264"], | 64 C.PI_PROTOCOLS: ["XEP-0264"], |
60 C.PI_DEPENDENCIES: ["XEP-0234"], | 65 C.PI_DEPENDENCIES: ["XEP-0234"], |
61 C.PI_MAIN: "XEP_0264", | 66 C.PI_MAIN: "XEP_0264", |
62 C.PI_HANDLER: "yes", | 67 C.PI_HANDLER: "yes", |
63 C.PI_DESCRIPTION: _("""Thumbnails handling""") | 68 C.PI_DESCRIPTION: _("""Thumbnails handling"""), |
64 } | 69 } |
65 | 70 |
66 | 71 |
67 class XEP_0264(object): | 72 class XEP_0264(object): |
68 SIZE_SMALL = (250, 250) | 73 SIZE_SMALL = (250, 250) |
79 | 84 |
80 ## triggers ## | 85 ## triggers ## |
81 | 86 |
82 def _addFileThumbnails(self, file_elt, extra_args): | 87 def _addFileThumbnails(self, file_elt, extra_args): |
83 try: | 88 try: |
84 thumbnails = extra_args[u'extra'][C.KEY_THUMBNAILS] | 89 thumbnails = extra_args[u"extra"][C.KEY_THUMBNAILS] |
85 except KeyError: | 90 except KeyError: |
86 return | 91 return |
87 for thumbnail in thumbnails: | 92 for thumbnail in thumbnails: |
88 thumbnail_elt = file_elt.addElement((NS_THUMBS, u'thumbnail')) | 93 thumbnail_elt = file_elt.addElement((NS_THUMBS, u"thumbnail")) |
89 thumbnail_elt['uri'] = u'cid:' + thumbnail['id'] | 94 thumbnail_elt["uri"] = u"cid:" + thumbnail["id"] |
90 thumbnail_elt['media-type'] = MIME_TYPE | 95 thumbnail_elt["media-type"] = MIME_TYPE |
91 width, height = thumbnail['size'] | 96 width, height = thumbnail["size"] |
92 thumbnail_elt['width'] = unicode(width) | 97 thumbnail_elt["width"] = unicode(width) |
93 thumbnail_elt['height'] = unicode(height) | 98 thumbnail_elt["height"] = unicode(height) |
94 return True | 99 return True |
95 | 100 |
96 def _getFileThumbnails(self, file_elt, file_data): | 101 def _getFileThumbnails(self, file_elt, file_data): |
97 thumbnails = [] | 102 thumbnails = [] |
98 for thumbnail_elt in file_elt.elements(NS_THUMBS, u'thumbnail'): | 103 for thumbnail_elt in file_elt.elements(NS_THUMBS, u"thumbnail"): |
99 uri = thumbnail_elt['uri'] | 104 uri = thumbnail_elt["uri"] |
100 if uri.startswith ('cid:'): | 105 if uri.startswith("cid:"): |
101 thumbnail = {'id': uri[4:]} | 106 thumbnail = {"id": uri[4:]} |
102 width = thumbnail_elt.getAttribute('width') | 107 width = thumbnail_elt.getAttribute("width") |
103 height = thumbnail_elt.getAttribute('height') | 108 height = thumbnail_elt.getAttribute("height") |
104 if width and height: | 109 if width and height: |
105 try: | 110 try: |
106 thumbnail['size'] = int(width), int(height) | 111 thumbnail["size"] = int(width), int(height) |
107 except ValueError: | 112 except ValueError: |
108 pass | 113 pass |
109 try: | 114 try: |
110 thumbnail['mime_type'] = thumbnail_elt['media-type'] | 115 thumbnail["mime_type"] = thumbnail_elt["media-type"] |
111 except KeyError: | 116 except KeyError: |
112 pass | 117 pass |
113 thumbnails.append(thumbnail) | 118 thumbnails.append(thumbnail) |
114 | 119 |
115 if thumbnails: | 120 if thumbnails: |
116 file_data.setdefault('extra', {})[C.KEY_THUMBNAILS] = thumbnails | 121 file_data.setdefault("extra", {})[C.KEY_THUMBNAILS] = thumbnails |
117 return True | 122 return True |
118 | 123 |
119 ## thumbnails generation ## | 124 ## thumbnails generation ## |
120 | 125 |
121 def getThumbId(self, image_uid, size): | 126 def getThumbId(self, image_uid, size): |
143 | 148 |
144 img.thumbnail(size) | 149 img.thumbnail(size) |
145 uid = self.getThumbId(image_uid or source_path, size) | 150 uid = self.getThumbId(image_uid or source_path, size) |
146 | 151 |
147 with self.host.common_cache.cacheData( | 152 with self.host.common_cache.cacheData( |
148 PLUGIN_INFO[C.PI_IMPORT_NAME], | 153 PLUGIN_INFO[C.PI_IMPORT_NAME], uid, MIME_TYPE, max_age |
149 uid, | 154 ) as f: |
150 MIME_TYPE, | |
151 max_age, | |
152 ) as f: | |
153 img.save(f, SAVE_FORMAT) | 155 img.save(f, SAVE_FORMAT) |
154 | 156 |
155 return img.size, uid | 157 return img.size, uid |
156 | 158 |
157 def generateThumbnail(self, source_path, size=None, max_age=None, image_uid=None): | 159 def generateThumbnail(self, source_path, size=None, max_age=None, image_uid=None): |
169 - size of the thumbnail | 171 - size of the thumbnail |
170 - unique Id of the thumbnail | 172 - unique Id of the thumbnail |
171 """ | 173 """ |
172 | 174 |
173 d = threads.deferToThread( | 175 d = threads.deferToThread( |
174 self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid) | 176 self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid |
175 d.addErrback(lambda failure_: | 177 ) |
176 log.error(u"thumbnail generation error: {}".format(failure_))) | 178 d.addErrback( |
179 lambda failure_: log.error(u"thumbnail generation error: {}".format(failure_)) | |
180 ) | |
177 return d | 181 return d |
178 | 182 |
179 | 183 |
180 class XEP_0264_handler(XMPPHandler): | 184 class XEP_0264_handler(XMPPHandler): |
181 implements(iwokkel.IDisco) | 185 implements(iwokkel.IDisco) |
182 | 186 |
183 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | 187 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
184 return [disco.DiscoFeature(NS_THUMBS)] | 188 return [disco.DiscoFeature(NS_THUMBS)] |
185 | 189 |
186 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | 190 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
187 return [] | 191 return [] |