comparison sat/plugins/plugin_xep_0264.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 2dab494e56fc
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
79 SIZES = (SIZE_SMALL, SIZE_MEDIUM, SIZE_BIG) 79 SIZES = (SIZE_SMALL, SIZE_MEDIUM, SIZE_BIG)
80 80
81 def __init__(self, host): 81 def __init__(self, host):
82 log.info(_("Plugin XEP_0264 initialization")) 82 log.info(_("Plugin XEP_0264 initialization"))
83 self.host = host 83 self.host = host
84 host.trigger.add("XEP-0234_buildFileElement", self._addFileThumbnails) 84 host.trigger.add("XEP-0234_buildFileElement", self._add_file_thumbnails)
85 host.trigger.add("XEP-0234_parseFileElement", self._getFileThumbnails) 85 host.trigger.add("XEP-0234_parseFileElement", self._get_file_thumbnails)
86 86
87 def getHandler(self, client): 87 def get_handler(self, client):
88 return XEP_0264_handler() 88 return XEP_0264_handler()
89 89
90 ## triggers ## 90 ## triggers ##
91 91
92 def _addFileThumbnails(self, client, file_elt, extra_args): 92 def _add_file_thumbnails(self, client, file_elt, extra_args):
93 try: 93 try:
94 thumbnails = extra_args["extra"][C.KEY_THUMBNAILS] 94 thumbnails = extra_args["extra"][C.KEY_THUMBNAILS]
95 except KeyError: 95 except KeyError:
96 return 96 return
97 for thumbnail in thumbnails: 97 for thumbnail in thumbnails:
101 width, height = thumbnail["size"] 101 width, height = thumbnail["size"]
102 thumbnail_elt["width"] = str(width) 102 thumbnail_elt["width"] = str(width)
103 thumbnail_elt["height"] = str(height) 103 thumbnail_elt["height"] = str(height)
104 return True 104 return True
105 105
106 def _getFileThumbnails(self, client, file_elt, file_data): 106 def _get_file_thumbnails(self, client, file_elt, file_data):
107 thumbnails = [] 107 thumbnails = []
108 for thumbnail_elt in file_elt.elements(NS_THUMBS, "thumbnail"): 108 for thumbnail_elt in file_elt.elements(NS_THUMBS, "thumbnail"):
109 uri = thumbnail_elt["uri"] 109 uri = thumbnail_elt["uri"]
110 if uri.startswith("cid:"): 110 if uri.startswith("cid:"):
111 thumbnail = {"id": uri[4:]} 111 thumbnail = {"id": uri[4:]}
128 file_data.setdefault("extra", {})[C.KEY_THUMBNAILS] = thumbnails 128 file_data.setdefault("extra", {})[C.KEY_THUMBNAILS] = thumbnails
129 return True 129 return True
130 130
131 ## thumbnails generation ## 131 ## thumbnails generation ##
132 132
133 def getThumbId(self, image_uid, size): 133 def get_thumb_id(self, image_uid, size):
134 """return an ID unique for image/size combination 134 """return an ID unique for image/size combination
135 135
136 @param image_uid(unicode): unique id of the image 136 @param image_uid(unicode): unique id of the image
137 can be a hash 137 can be a hash
138 @param size(tuple(int)): requested size of thumbnail 138 @param size(tuple(int)): requested size of thumbnail
139 @return (unicode): unique id for this image/size 139 @return (unicode): unique id for this image/size
140 """ 140 """
141 return hashlib.sha256(repr((image_uid, size)).encode()).hexdigest() 141 return hashlib.sha256(repr((image_uid, size)).encode()).hexdigest()
142 142
143 def _blockingGenThumb( 143 def _blocking_gen_thumb(
144 self, source_path, size=None, max_age=None, image_uid=None, 144 self, source_path, size=None, max_age=None, image_uid=None,
145 fix_orientation=True): 145 fix_orientation=True):
146 """Generate a thumbnail for image 146 """Generate a thumbnail for image
147 147
148 This is a blocking method and must be executed in a thread 148 This is a blocking method and must be executed in a thread
149 params are the same as for [generateThumbnail] 149 params are the same as for [generate_thumbnail]
150 """ 150 """
151 if size is None: 151 if size is None:
152 size = self.SIZE_SMALL 152 size = self.SIZE_SMALL
153 try: 153 try:
154 img = Image.open(source_path) 154 img = Image.open(source_path)
157 157
158 img.thumbnail(size) 158 img.thumbnail(size)
159 if fix_orientation: 159 if fix_orientation:
160 img = ImageOps.exif_transpose(img) 160 img = ImageOps.exif_transpose(img)
161 161
162 uid = self.getThumbId(image_uid or source_path, size) 162 uid = self.get_thumb_id(image_uid or source_path, size)
163 163
164 with self.host.common_cache.cacheData( 164 with self.host.common_cache.cache_data(
165 PLUGIN_INFO[C.PI_IMPORT_NAME], uid, MIME_TYPE, max_age 165 PLUGIN_INFO[C.PI_IMPORT_NAME], uid, MIME_TYPE, max_age
166 ) as f: 166 ) as f:
167 img.save(f, SAVE_FORMAT) 167 img.save(f, SAVE_FORMAT)
168 if fix_orientation: 168 if fix_orientation:
169 log.debug(f"fixed orientation for {f.name}") 169 log.debug(f"fixed orientation for {f.name}")
170 170
171 return img.size, uid 171 return img.size, uid
172 172
173 def generateThumbnail( 173 def generate_thumbnail(
174 self, source_path, size=None, max_age=None, image_uid=None, fix_orientation=True): 174 self, source_path, size=None, max_age=None, image_uid=None, fix_orientation=True):
175 """Generate a thumbnail of image 175 """Generate a thumbnail of image
176 176
177 @param source_path(unicode): absolute path to source image 177 @param source_path(unicode): absolute path to source image
178 @param size(int, None): max size of the thumbnail 178 @param size(int, None): max size of the thumbnail
179 can be one of self.SIZE_* 179 can be one of self.SIZE_*
180 None to use default value (i.e. self.SIZE_SMALL) 180 None to use default value (i.e. self.SIZE_SMALL)
181 @param max_age(int, None): same as for [memory.cache.Cache.cacheData]) 181 @param max_age(int, None): same as for [memory.cache.Cache.cache_data])
182 @param image_uid(unicode, None): unique ID to identify the image 182 @param image_uid(unicode, None): unique ID to identify the image
183 use hash whenever possible 183 use hash whenever possible
184 if None, source_path will be used 184 if None, source_path will be used
185 @param fix_orientation(bool): if True, fix orientation using EXIF data 185 @param fix_orientation(bool): if True, fix orientation using EXIF data
186 @return D(tuple[tuple[int,int], unicode]): tuple with: 186 @return D(tuple[tuple[int,int], unicode]): tuple with:
187 - size of the thumbnail 187 - size of the thumbnail
188 - unique Id of the thumbnail 188 - unique Id of the thumbnail
189 """ 189 """
190 d = threads.deferToThread( 190 d = threads.deferToThread(
191 self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid, 191 self._blocking_gen_thumb, source_path, size, max_age, image_uid=image_uid,
192 fix_orientation=fix_orientation 192 fix_orientation=fix_orientation
193 ) 193 )
194 d.addErrback( 194 d.addErrback(
195 lambda failure_: log.error("thumbnail generation error: {}".format(failure_)) 195 lambda failure_: log.error("thumbnail generation error: {}".format(failure_))
196 ) 196 )