def ensure_file(name, url=None, force=False, logger=logging.getLogger(), postprocess=None):
"""
Ensures that the file requested exists in the cache, downloading it if it does not exist.
Args:
name (str): name of the file.
url (str): url to download the file from, if it doesn't exist.
force (bool): whether to force the download, regardless of the existence of the file.
logger (logging.Logger): logger to log results.
postprocess (function): a function that, if given, will be applied after the file is downloaded. The function has the signature ``f(fname)``
Returns:
str: file name of the downloaded file.
"""
fname = Embedding.path(name)
if not path.isfile(fname) or force:
if url:
logger.critical('Downloading from {} to {}'.format(url, fname))
Embedding.download_file(url, fname)
if postprocess:
postprocess(fname)
else:
raise Exception('{} does not exist!'.format(fname))
return fname
评论列表
文章目录