comparison src/memory/cache.py @ 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 516bf5309517
children cd7a53c31eb6
comparison
equal deleted inserted replaced
2508:4e5cc45e2be7 2509:d485e9416493
25 import cPickle as pickle 25 import cPickle as pickle
26 import mimetypes 26 import mimetypes
27 import os.path 27 import os.path
28 import time 28 import time
29 29
30 DEFAULT_EXT = '.raw'
31
30 32
31 class Cache(object): 33 class Cache(object):
32 """generic file caching""" 34 """generic file caching"""
33 35
34 def __init__(self, host, profile=None): 36 def __init__(self, host, profile):
37 """
38 @param profile(unicode, None): ame of the profile to set the cache for
39 if None, the cache will be common for all profiles
40 """
35 self.profile = profile 41 self.profile = profile
36 self.cache_dir = os.path.join( 42 path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR]
37 host.memory.getConfig('', 'local_dir'), 43 if profile:
38 C.CACHE_DIR, 44 path_elts.extend([u'profiles',regex.pathEscape(profile)])
39 regex.pathEscape(profile or '')) 45 else:
46 path_elts.append(u'common')
47 self.cache_dir = os.path.join(*path_elts)
48
40 if not os.path.exists(self.cache_dir): 49 if not os.path.exists(self.cache_dir):
41 os.makedirs(self.cache_dir) 50 os.makedirs(self.cache_dir)
42 51
43 def getPath(self, filename): 52 def getPath(self, filename):
44 """return cached file URL 53 """return cached file URL
57 @return (unicode, None): absolute path to cached file 66 @return (unicode, None): absolute path to cached file
58 None if file is not in cache (or cache is invalid) 67 None if file is not in cache (or cache is invalid)
59 """ 68 """
60 uid = uid.strip() 69 uid = uid.strip()
61 if not uid: 70 if not uid:
62 return None 71 raise exceptions.InternalError(u"uid must not be empty")
63 cache_url = self.getPath(uid) 72 cache_url = self.getPath(uid)
64 if not os.path.exists(cache_url): 73 if not os.path.exists(cache_url):
65 return None 74 return None
66 75
67 try: 76 try:
84 time.time() - eol)) 93 time.time() - eol))
85 return None 94 return None
86 95
87 return self.getPath(cache_data['filename']) 96 return self.getPath(cache_data['filename'])
88 97
89 def cacheData(self, source, uid, mime_type=u'', max_age=None, filename=None): 98 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
90 """create cache metadata and file object to use for actual data 99 """create cache metadata and file object to use for actual data
91 100
92 @param source(unicode): source of the cache (should be plugin's import_name) 101 @param source(unicode): source of the cache (should be plugin's import_name)
93 @param uid(unicode): an identifier of the file which must be unique 102 @param uid(unicode): an identifier of the file which must be unique
94 @param mime_type(unicode): MIME type of the file to cache 103 @param mime_type(unicode): MIME type of the file to cache
100 @param filename: if not None, will be used as filename 109 @param filename: if not None, will be used as filename
101 else one will be generated from uid and guessed extension 110 else one will be generated from uid and guessed extension
102 @return(file): file object opened in write mode 111 @return(file): file object opened in write mode
103 you have to close it yourself (hint: use with statement) 112 you have to close it yourself (hint: use with statement)
104 """ 113 """
105 # FIXME: is it needed to use a separate thread?
106 # probably not with the little data expected with BoB
107 cache_url = self.getPath(uid) 114 cache_url = self.getPath(uid)
108 ext = mimetypes.guess_extension(mime_type, strict=False)
109 if ext is None:
110 log.warning(u"can't find extension for MIME type {}".format(mime_type))
111 ext = '.dump'
112 if filename is None: 115 if filename is None:
116 if mime_type:
117 ext = mimetypes.guess_extension(mime_type, strict=False)
118 if ext is None:
119 log.warning(u"can't find extension for MIME type {}".format(mime_type))
120 ext = DEFAULT_EXT
121 elif ext == u'.jpe':
122 ext = u'.jpg'
123 else:
124 ext = DEFAULT_EXT
125 mime_type = None
113 filename = uid + ext 126 filename = uid + ext
114 if max_age is None: 127 if max_age is None:
115 max_age = C.DEFAULT_MAX_AGE 128 max_age = C.DEFAULT_MAX_AGE
116 cache_data = {'source': source, 129 cache_data = {u'source': source,
117 'filename': filename, 130 u'filename': filename,
118 'eol': int(time.time()) + max_age, 131 u'eol': int(time.time()) + max_age,
119 'mime_type': mime_type, 132 u'mime_type': mime_type,
120 } 133 }
121 file_path = self.getPath(filename) 134 file_path = self.getPath(filename)
122 135
123 with open(cache_url, 'wb') as f: 136 with open(cache_url, 'wb') as f:
124 pickle.dump(cache_data, f, protocol=2) 137 pickle.dump(cache_data, f, protocol=2)