core (memory/cache): common cache:

host has now a common_cache attribute for data which can be shared between profiles. client.cache is for private data
This commit is contained in:
Goffi 2018-03-02 17:40:09 +01:00
commit 7bc5b99ea1
3 changed files with 35 additions and 20 deletions

View file

@ -29,7 +29,8 @@ from sat.core import exceptions
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core.constants import Const as C
from sat.memory.memory import Memory
from sat.memory import memory
from sat.memory import cache
from sat.tools import trigger
from sat.tools import utils
from sat.tools.common import dynamic_import
@ -58,7 +59,7 @@ class SAT(service.Service):
self.plugins = {}
self.ns_map = {u'x-data': u'jabber:x:data'} # map for short name to whole namespace,
# extended by plugins with registerNamespace
self.memory = Memory(self)
self.memory = memory.Memory(self)
self.trigger = trigger.TriggerManager() # trigger are used to change SàT behaviour
bridge_name = self.memory.getConfig('', 'bridge', 'dbus')
@ -149,6 +150,7 @@ class SAT(service.Service):
def _postMemoryInit(self, ignore):
"""Method called after memory initialization is done"""
self.common_cache = cache.Cache(self, None)
log.info(_("Memory initialised"))
try:
self._import_plugins()

View file

@ -523,7 +523,7 @@ class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
# but we restrict to domish.Element to make trigger treatments easier
assert isinstance(obj, domish.Element)
# XXX: this trigger is the last one before sending stanza on wire
# is it is intended for things like end 2 end encryption.
# it is intended for things like end 2 end encryption.
# *DO NOT* cancel (i.e. return False) without very good reason
# (out of band transmission for instance).
# e2e should have a priority of 0 here, and out of band transmission

View file

@ -27,16 +27,25 @@ import mimetypes
import os.path
import time
DEFAULT_EXT = '.raw'
class Cache(object):
"""generic file caching"""
def __init__(self, host, profile=None):
def __init__(self, host, profile):
"""
@param profile(unicode, None): ame of the profile to set the cache for
if None, the cache will be common for all profiles
"""
self.profile = profile
self.cache_dir = os.path.join(
host.memory.getConfig('', 'local_dir'),
C.CACHE_DIR,
regex.pathEscape(profile or ''))
path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR]
if profile:
path_elts.extend([u'profiles',regex.pathEscape(profile)])
else:
path_elts.append(u'common')
self.cache_dir = os.path.join(*path_elts)
if not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir)
@ -59,7 +68,7 @@ class Cache(object):
"""
uid = uid.strip()
if not uid:
return None
raise exceptions.InternalError(u"uid must not be empty")
cache_url = self.getPath(uid)
if not os.path.exists(cache_url):
return None
@ -86,7 +95,7 @@ class Cache(object):
return self.getPath(cache_data['filename'])
def cacheData(self, source, uid, mime_type=u'', max_age=None, filename=None):
def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
"""create cache metadata and file object to use for actual data
@param source(unicode): source of the cache (should be plugin's import_name)
@ -102,21 +111,25 @@ class Cache(object):
@return(file): file object opened in write mode
you have to close it yourself (hint: use with statement)
"""
# FIXME: is it needed to use a separate thread?
# probably not with the little data expected with BoB
cache_url = self.getPath(uid)
ext = mimetypes.guess_extension(mime_type, strict=False)
if ext is None:
log.warning(u"can't find extension for MIME type {}".format(mime_type))
ext = '.dump'
if filename is None:
if mime_type:
ext = mimetypes.guess_extension(mime_type, strict=False)
if ext is None:
log.warning(u"can't find extension for MIME type {}".format(mime_type))
ext = DEFAULT_EXT
elif ext == u'.jpe':
ext = u'.jpg'
else:
ext = DEFAULT_EXT
mime_type = None
filename = uid + ext
if max_age is None:
max_age = C.DEFAULT_MAX_AGE
cache_data = {'source': source,
'filename': filename,
'eol': int(time.time()) + max_age,
'mime_type': mime_type,
cache_data = {u'source': source,
u'filename': filename,
u'eol': int(time.time()) + max_age,
u'mime_type': mime_type,
}
file_path = self.getPath(filename)