annotate cagou/core/image.py @ 439:12d188cb1206

core: use of new profileConnected method: bookmarks cache is now retrieved in profileConnected, as getting it in ProfilePlugged could result in an exception is client was not fully connected (if plugins were not all initialised).
author Goffi <goffi@goffi.org>
date Sat, 07 Mar 2020 00:05:49 +0100
parents aa204c813f07
children b5e8e470f7f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
379
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
1 #!/usr/bin/env python3
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
2
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
3
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
378
4d660b252487 dates update
Goffi <goffi@goffi.org>
parents: 312
diff changeset
5 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
6
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
11
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
16
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
19
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
20
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import log as logging
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = logging.getLogger(__name__)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from kivy.uix import image as kivy_img
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from kivy.core.image import Image as CoreImage
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.resources import resource_find
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import io
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import PIL
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
28
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
29
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class Image(kivy_img.Image):
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
31 """Image widget which accept source without extension"""
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
32
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def texture_update(self, *largs):
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
34 if not self.source:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.texture = None
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
36 else:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
37 filename = resource_find(self.source)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self._loops = 0
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if filename is None:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
40 return log.error('Image: Error reading file {filename}'.
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
41 format(filename=self.source))
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
42 mipmap = self.mipmap
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
43 if self._coreimage is not None:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self._coreimage.unbind(on_texture=self._on_tex_change)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
45 try:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self._coreimage = ci = CoreImage(filename, mipmap=mipmap,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
47 anim_delay=self.anim_delay,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
48 keep_data=self.keep_data,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
49 nocache=self.nocache)
433
aa204c813f07 chat: attachment preview:
Goffi <goffi@goffi.org>
parents: 379
diff changeset
50 except Exception:
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
51 # loading failed probably because of unmanaged extention,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
52 # we try our luck with with PIL
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
53 try:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
54 im = PIL.Image.open(filename)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
55 ext = im.format.lower()
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
56 del im
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
57 # we can't use im.tobytes as it would use the
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
58 # internal decompressed representation from pillow
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
59 # and im.save would need processing to handle format
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
60 data = io.BytesIO(open(filename, "rb").read())
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
61 cache_filename = "{}.{}".format(filename,ext) # needed for kivy's Image to use cache
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self._coreimage = ci = CoreImage(data, ext=ext,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
63 filename=cache_filename, mipmap=mipmap,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
64 anim_delay=self.anim_delay,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
65 keep_data=self.keep_data,
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
66 nocache=self.nocache)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
67 except Exception as e:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
68 log.warning("Can't load image: {}".format(e))
8
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self._coreimage = ci = None
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
70
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if ci:
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
72 ci.bind(on_texture=self._on_tex_change)
d9095d1dd7ae added an Image class which accept source without extention
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.texture = ci.texture
101
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
74
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
75
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
76 class AsyncImage(kivy_img.AsyncImage):
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
77 """AsyncImage which accept file:// schema"""
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
78
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
79 def _load_source(self, *args):
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
80 if self.source.startswith('file://'):
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
81 self.source = self.source[7:]
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
82 else:
d5ede9281e4c core (image): AsyncImage now handle file:// schema in source
Goffi <goffi@goffi.org>
parents: 15
diff changeset
83 super(AsyncImage, self)._load_source(*args)