summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-03-30 22:29:30 +0200
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2018-03-31 00:47:24 +0200
commita2ad4af79a1435648c96b99308edeb04fd112da1 (patch)
treef3bf5b7aa4cf99d89caaea323667cbe3ad74f172
parentb03a92e7eab6e0b7eb84a6a526913a449a36d603 (diff)
downloadpoezio-a2ad4af79a1435648c96b99308edeb04fd112da1.tar.gz
poezio-a2ad4af79a1435648c96b99308edeb04fd112da1.tar.bz2
poezio-a2ad4af79a1435648c96b99308edeb04fd112da1.tar.xz
poezio-a2ad4af79a1435648c96b99308edeb04fd112da1.zip
Use slixmpp’s new cache module for avatars too.
-rw-r--r--poezio/core/core.py4
-rw-r--r--poezio/core/handlers.py42
2 files changed, 15 insertions, 31 deletions
diff --git a/poezio/core/core.py b/poezio/core/core.py
index 8089408c..7c6ad886 100644
--- a/poezio/core/core.py
+++ b/poezio/core/core.py
@@ -18,6 +18,7 @@ import sys
import time
from slixmpp.xmlstream.handler import Callback
+from slixmpp.util import FileSystemPerJidCache
from poezio import connection
from poezio import decorators
@@ -30,7 +31,7 @@ from poezio import windows
from poezio.bookmarks import BookmarkList
from poezio.common import safeJID
-from poezio.config import config, firstrun
+from poezio.config import config, firstrun, CACHE_DIR
from poezio.contact import Contact, Resource
from poezio.daemon import Executor
from poezio.fifo import Fifo
@@ -76,6 +77,7 @@ class Core(object):
self.bookmarks = BookmarkList()
self.debug = False
self.remote_fifo = None
+ self.avatar_cache = FileSystemPerJidCache(CACHE_DIR, 'avatars', binary=True)
# a unique buffer used to store global information
# that are displayed in almost all tabs, in an
# information window.
diff --git a/poezio/core/handlers.py b/poezio/core/handlers.py
index 8c799b58..c73fad1f 100644
--- a/poezio/core/handlers.py
+++ b/poezio/core/handlers.py
@@ -395,19 +395,15 @@ class HandlerCore:
except Exception:
log.debug('Failed getting metadata from 0084:', exc_info=True)
return
- cache_dir = path.join(CACHE_DIR, 'avatars', jid)
for info in metadata:
avatar_hash = info['id']
# First check whether we have it in cache.
- cached_path = path.join(cache_dir, avatar_hash)
- try:
- with open(cached_path, 'rb') as avatar_file:
- contact.avatar = avatar_file.read()
- log.debug('Using cached avatar')
+ cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash)
+ if cached_avatar:
+ contact.avatar = cached_avatar
+ log.debug('Using cached avatar for %s', jid)
return
- except OSError:
- pass
# If we didn’t have any, query the data instead.
if not info['url']:
@@ -429,16 +425,11 @@ class HandlerCore:
log.debug('Received %s avatar: %s', jid, info['type'])
# Now we save the data on the file system to not have to request it again.
- try:
- makedirs(cache_dir, exist_ok=True)
- with open(cached_path, 'wb') as avatar_file:
- avatar_file.write(contact.avatar)
- except OSError:
+ if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar):
log.debug(
- 'Failed writing %s avatar to cache:',
+ 'Failed writing %s’s avatar to cache:',
jid,
exc_info=True)
- pass
return
@asyncio.coroutine
@@ -451,15 +442,11 @@ class HandlerCore:
log.debug('Received vCard avatar update from %s: %s', jid, avatar_hash)
# First check whether we have it in cache.
- cache_dir = path.join(CACHE_DIR, 'avatars', jid)
- cached_path = path.join(cache_dir, avatar_hash)
- try:
- with open(cached_path, 'rb') as avatar_file:
- contact.avatar = avatar_file.read()
- log.debug('Using cached avatar')
+ cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash)
+ if cached_avatar:
+ contact.avatar = cached_avatar
+ log.debug('Using cached avatar for %s', jid)
return
- except OSError:
- pass
# If we didn’t have any, query the vCard instead.
try:
@@ -476,13 +463,8 @@ class HandlerCore:
log.debug('Received %s avatar: %s', jid, avatar['TYPE'])
# Now we save the data on the file system to not have to request it again.
- try:
- makedirs(cache_dir, exist_ok=True)
- with open(cached_path, 'wb') as avatar_file:
- avatar_file.write(contact.avatar)
- except OSError:
- log.debug('Failed writing %s avatar to cache:', jid, exc_info=True)
- pass
+ if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar):
+ log.debug('Failed writing %s’s avatar to cache:', jid, exc_info=True)
def on_nick_received(self, message):
"""