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 from sat.core.log import getLogger
log = getLogger(__name__) log = getLogger(__name__)
from sat.core.constants import Const as C 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 trigger
from sat.tools import utils from sat.tools import utils
from sat.tools.common import dynamic_import from sat.tools.common import dynamic_import
@ -58,7 +59,7 @@ class SAT(service.Service):
self.plugins = {} self.plugins = {}
self.ns_map = {u'x-data': u'jabber:x:data'} # map for short name to whole namespace, self.ns_map = {u'x-data': u'jabber:x:data'} # map for short name to whole namespace,
# extended by plugins with registerNamespace # 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 self.trigger = trigger.TriggerManager() # trigger are used to change SàT behaviour
bridge_name = self.memory.getConfig('', 'bridge', 'dbus') bridge_name = self.memory.getConfig('', 'bridge', 'dbus')
@ -149,6 +150,7 @@ class SAT(service.Service):
def _postMemoryInit(self, ignore): def _postMemoryInit(self, ignore):
"""Method called after memory initialization is done""" """Method called after memory initialization is done"""
self.common_cache = cache.Cache(self, None)
log.info(_("Memory initialised")) log.info(_("Memory initialised"))
try: try:
self._import_plugins() 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 # but we restrict to domish.Element to make trigger treatments easier
assert isinstance(obj, domish.Element) assert isinstance(obj, domish.Element)
# XXX: this trigger is the last one before sending stanza on wire # 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 # *DO NOT* cancel (i.e. return False) without very good reason
# (out of band transmission for instance). # (out of band transmission for instance).
# e2e should have a priority of 0 here, and out of band transmission # 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 os.path
import time import time
DEFAULT_EXT = '.raw'
class Cache(object): class Cache(object):
"""generic file caching""" """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.profile = profile
self.cache_dir = os.path.join( path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR]
host.memory.getConfig('', 'local_dir'), if profile:
C.CACHE_DIR, path_elts.extend([u'profiles',regex.pathEscape(profile)])
regex.pathEscape(profile or '')) else:
path_elts.append(u'common')
self.cache_dir = os.path.join(*path_elts)
if not os.path.exists(self.cache_dir): if not os.path.exists(self.cache_dir):
os.makedirs(self.cache_dir) os.makedirs(self.cache_dir)
@ -59,7 +68,7 @@ class Cache(object):
""" """
uid = uid.strip() uid = uid.strip()
if not uid: if not uid:
return None raise exceptions.InternalError(u"uid must not be empty")
cache_url = self.getPath(uid) cache_url = self.getPath(uid)
if not os.path.exists(cache_url): if not os.path.exists(cache_url):
return None return None
@ -86,7 +95,7 @@ class Cache(object):
return self.getPath(cache_data['filename']) 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 """create cache metadata and file object to use for actual data
@param source(unicode): source of the cache (should be plugin's import_name) @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 @return(file): file object opened in write mode
you have to close it yourself (hint: use with statement) 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) 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 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 filename = uid + ext
if max_age is None: if max_age is None:
max_age = C.DEFAULT_MAX_AGE max_age = C.DEFAULT_MAX_AGE
cache_data = {'source': source, cache_data = {u'source': source,
'filename': filename, u'filename': filename,
'eol': int(time.time()) + max_age, u'eol': int(time.time()) + max_age,
'mime_type': mime_type, u'mime_type': mime_type,
} }
file_path = self.getPath(filename) file_path = self.getPath(filename)