Mercurial > libervia-backend
changeset 2509:d485e9416493
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
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Mar 2018 17:40:09 +0100 |
parents | 4e5cc45e2be7 |
children | 4001aa395a04 |
files | src/core/sat_main.py src/core/xmpp.py src/memory/cache.py |
diffstat | 3 files changed, 35 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/src/core/sat_main.py Fri Mar 02 17:37:41 2018 +0100 +++ b/src/core/sat_main.py Fri Mar 02 17:40:09 2018 +0100 @@ -29,7 +29,8 @@ 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 @@ 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 @@ 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()
--- a/src/core/xmpp.py Fri Mar 02 17:37:41 2018 +0100 +++ b/src/core/xmpp.py Fri Mar 02 17:40:09 2018 +0100 @@ -523,7 +523,7 @@ # 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
--- a/src/memory/cache.py Fri Mar 02 17:37:41 2018 +0100 +++ b/src/memory/cache.py Fri Mar 02 17:40:09 2018 +0100 @@ -27,16 +27,25 @@ 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 @@ """ 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 @@ 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 @@ @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)