code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools.common import regex
from sat.core import exceptions
@ -27,7 +28,7 @@ import mimetypes
import os.path
import time
DEFAULT_EXT = '.raw'
DEFAULT_EXT = ".raw"
class Cache(object):
@ -39,11 +40,11 @@ class Cache(object):
if None, the cache will be common for all profiles
"""
self.profile = profile
path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR]
path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR]
if profile:
path_elts.extend([u'profiles',regex.pathEscape(profile)])
path_elts.extend([u"profiles", regex.pathEscape(profile)])
else:
path_elts.append(u'common')
path_elts.append(u"common")
self.cache_dir = os.path.join(*path_elts)
if not os.path.exists(self.cache_dir):
@ -54,8 +55,10 @@ class Cache(object):
@param filename(unicode): cached file name (cache data or actual file)
"""
if not filename or u'/' in filename:
log.error(u"invalid char found in file name, hack attempt? name:{}".format(filename))
if not filename or u"/" in filename:
log.error(
u"invalid char found in file name, hack attempt? name:{}".format(filename)
)
raise exceptions.DataError(u"Invalid char found")
return os.path.join(self.cache_dir, filename)
@ -76,26 +79,27 @@ class Cache(object):
return None
try:
with open(cache_url, 'rb') as f:
with open(cache_url, "rb") as f:
cache_data = pickle.load(f)
except IOError:
log.warning(u"can't read cache at {}".format(cache_url))
return None
except pickle.UnpicklingError:
log.warning(u'invalid cache found at {}'.format(cache_url))
log.warning(u"invalid cache found at {}".format(cache_url))
return None
try:
eol = cache_data['eol']
eol = cache_data["eol"]
except KeyError:
log.warning(u'no End Of Life found for cached file {}'.format(uid))
log.warning(u"no End Of Life found for cached file {}".format(uid))
eol = 0
if eol < time.time():
log.debug(u"removing expired cache (expired for {}s)".format(
time.time() - eol))
log.debug(
u"removing expired cache (expired for {}s)".format(time.time() - eol)
)
return None
cache_data['path'] = self.getPath(cache_data['filename'])
cache_data["path"] = self.getPath(cache_data["filename"])
return cache_data
def getFilePath(self, uid):
@ -107,7 +111,7 @@ class Cache(object):
"""
metadata = self.getMetadata(uid)
if metadata is not None:
return metadata['path']
return metadata["path"]
def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
"""create cache metadata and file object to use for actual data
@ -130,24 +134,27 @@ class Cache(object):
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))
log.warning(
u"can't find extension for MIME type {}".format(mime_type)
)
ext = DEFAULT_EXT
elif ext == u'.jpe':
ext = u'.jpg'
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 = {u'source': source,
u'filename': filename,
u'eol': int(time.time()) + max_age,
u'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)
with open(cache_url, 'wb') as f:
with open(cache_url, "wb") as f:
pickle.dump(cache_data, f, protocol=2)
return open(file_path, 'wb')
return open(file_path, "wb")