comparison sat/memory/cache.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.log import getLogger 20 from sat.core.log import getLogger
21
21 log = getLogger(__name__) 22 log = getLogger(__name__)
22 from sat.tools.common import regex 23 from sat.tools.common import regex
23 from sat.core import exceptions 24 from sat.core import exceptions
24 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
25 import cPickle as pickle 26 import cPickle as pickle
26 import mimetypes 27 import mimetypes
27 import os.path 28 import os.path
28 import time 29 import time
29 30
30 DEFAULT_EXT = '.raw' 31 DEFAULT_EXT = ".raw"
31 32
32 33
33 class Cache(object): 34 class Cache(object):
34 """generic file caching""" 35 """generic file caching"""
35 36
37 """ 38 """
38 @param profile(unicode, None): ame of the profile to set the cache for 39 @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 if None, the cache will be common for all profiles
40 """ 41 """
41 self.profile = profile 42 self.profile = profile
42 path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR] 43 path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR]
43 if profile: 44 if profile:
44 path_elts.extend([u'profiles',regex.pathEscape(profile)]) 45 path_elts.extend([u"profiles", regex.pathEscape(profile)])
45 else: 46 else:
46 path_elts.append(u'common') 47 path_elts.append(u"common")
47 self.cache_dir = os.path.join(*path_elts) 48 self.cache_dir = os.path.join(*path_elts)
48 49
49 if not os.path.exists(self.cache_dir): 50 if not os.path.exists(self.cache_dir):
50 os.makedirs(self.cache_dir) 51 os.makedirs(self.cache_dir)
51 52
52 def getPath(self, filename): 53 def getPath(self, filename):
53 """return cached file URL 54 """return cached file URL
54 55
55 @param filename(unicode): cached file name (cache data or actual file) 56 @param filename(unicode): cached file name (cache data or actual file)
56 """ 57 """
57 if not filename or u'/' in filename: 58 if not filename or u"/" in filename:
58 log.error(u"invalid char found in file name, hack attempt? name:{}".format(filename)) 59 log.error(
60 u"invalid char found in file name, hack attempt? name:{}".format(filename)
61 )
59 raise exceptions.DataError(u"Invalid char found") 62 raise exceptions.DataError(u"Invalid char found")
60 return os.path.join(self.cache_dir, filename) 63 return os.path.join(self.cache_dir, filename)
61 64
62 def getMetadata(self, uid): 65 def getMetadata(self, uid):
63 """retrieve metadata for cached data 66 """retrieve metadata for cached data
74 cache_url = self.getPath(uid) 77 cache_url = self.getPath(uid)
75 if not os.path.exists(cache_url): 78 if not os.path.exists(cache_url):
76 return None 79 return None
77 80
78 try: 81 try:
79 with open(cache_url, 'rb') as f: 82 with open(cache_url, "rb") as f:
80 cache_data = pickle.load(f) 83 cache_data = pickle.load(f)
81 except IOError: 84 except IOError:
82 log.warning(u"can't read cache at {}".format(cache_url)) 85 log.warning(u"can't read cache at {}".format(cache_url))
83 return None 86 return None
84 except pickle.UnpicklingError: 87 except pickle.UnpicklingError:
85 log.warning(u'invalid cache found at {}'.format(cache_url)) 88 log.warning(u"invalid cache found at {}".format(cache_url))
86 return None 89 return None
87 90
88 try: 91 try:
89 eol = cache_data['eol'] 92 eol = cache_data["eol"]
90 except KeyError: 93 except KeyError:
91 log.warning(u'no End Of Life found for cached file {}'.format(uid)) 94 log.warning(u"no End Of Life found for cached file {}".format(uid))
92 eol = 0 95 eol = 0
93 if eol < time.time(): 96 if eol < time.time():
94 log.debug(u"removing expired cache (expired for {}s)".format( 97 log.debug(
95 time.time() - eol)) 98 u"removing expired cache (expired for {}s)".format(time.time() - eol)
99 )
96 return None 100 return None
97 101
98 cache_data['path'] = self.getPath(cache_data['filename']) 102 cache_data["path"] = self.getPath(cache_data["filename"])
99 return cache_data 103 return cache_data
100 104
101 def getFilePath(self, uid): 105 def getFilePath(self, uid):
102 """retrieve absolute path to file 106 """retrieve absolute path to file
103 107
105 @return (unicode, None): absolute path to cached file 109 @return (unicode, None): absolute path to cached file
106 None if file is not in cache (or cache is invalid) 110 None if file is not in cache (or cache is invalid)
107 """ 111 """
108 metadata = self.getMetadata(uid) 112 metadata = self.getMetadata(uid)
109 if metadata is not None: 113 if metadata is not None:
110 return metadata['path'] 114 return metadata["path"]
111 115
112 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None): 116 def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
113 """create cache metadata and file object to use for actual data 117 """create cache metadata and file object to use for actual data
114 118
115 @param source(unicode): source of the cache (should be plugin's import_name) 119 @param source(unicode): source of the cache (should be plugin's import_name)
128 cache_url = self.getPath(uid) 132 cache_url = self.getPath(uid)
129 if filename is None: 133 if filename is None:
130 if mime_type: 134 if mime_type:
131 ext = mimetypes.guess_extension(mime_type, strict=False) 135 ext = mimetypes.guess_extension(mime_type, strict=False)
132 if ext is None: 136 if ext is None:
133 log.warning(u"can't find extension for MIME type {}".format(mime_type)) 137 log.warning(
138 u"can't find extension for MIME type {}".format(mime_type)
139 )
134 ext = DEFAULT_EXT 140 ext = DEFAULT_EXT
135 elif ext == u'.jpe': 141 elif ext == u".jpe":
136 ext = u'.jpg' 142 ext = u".jpg"
137 else: 143 else:
138 ext = DEFAULT_EXT 144 ext = DEFAULT_EXT
139 mime_type = None 145 mime_type = None
140 filename = uid + ext 146 filename = uid + ext
141 if max_age is None: 147 if max_age is None:
142 max_age = C.DEFAULT_MAX_AGE 148 max_age = C.DEFAULT_MAX_AGE
143 cache_data = {u'source': source, 149 cache_data = {
144 u'filename': filename, 150 u"source": source,
145 u'eol': int(time.time()) + max_age, 151 u"filename": filename,
146 u'mime_type': mime_type, 152 u"eol": int(time.time()) + max_age,
147 } 153 u"mime_type": mime_type,
154 }
148 file_path = self.getPath(filename) 155 file_path = self.getPath(filename)
149 156
150 with open(cache_url, 'wb') as f: 157 with open(cache_url, "wb") as f:
151 pickle.dump(cache_data, f, protocol=2) 158 pickle.dump(cache_data, f, protocol=2)
152 159
153 return open(file_path, 'wb') 160 return open(file_path, "wb")