Skip to content

Commit

Permalink
moved ensure_dir closer to when we actually download things
Browse files Browse the repository at this point in the history
  • Loading branch information
iskandr committed Aug 26, 2020
1 parent 92b3abc commit aebd1c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyensembl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
)
from .transcript import Transcript

__version__ = '1.8.8'
__version__ = '1.9.0'

__all__ = [
"MemoryCache",
Expand Down
25 changes: 14 additions & 11 deletions pyensembl/download_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from __future__ import print_function, division, absolute_import

from os import listdir, remove
from os.path import join, exists, split, abspath
from os.path import join, exists, split, abspath, isdir
from shutil import copy2, rmtree
import logging

Expand Down Expand Up @@ -130,7 +130,6 @@ def __init__(

@property
def cache_directory_path(self):
datacache.ensure_dir(self._cache_directory_path)
return self._cache_directory_path

def _fields(self):
Expand Down Expand Up @@ -224,6 +223,7 @@ def _download_if_necessary(self, url, download_if_missing, overwrite):
"""
Return local cached path to a remote file, download it if necessary.
"""
datacache.ensure_dir(self.cache_directory_path)
cached_path = self.cached_path(url)
missing = not exists(cached_path)
if (missing or overwrite) and download_if_missing:
Expand All @@ -240,6 +240,7 @@ def _copy_if_necessary(self, local_path, overwrite):
"""
Return cached path to local file, copying it to the cache if necessary.
"""
datacache.ensure_dir(self.cache_directory_path)
local_path = abspath(local_path)
if not exists(local_path):
raise MissingLocalFile(local_path)
Expand Down Expand Up @@ -316,14 +317,16 @@ def delete_cached_files(self, prefixes=[], suffixes=[]):
"""
Deletes any cached files matching the prefixes or suffixes given
"""
for filename in listdir(self.cache_directory_path):
delete = (
any([filename.endswith(ext) for ext in suffixes]) or
any([filename.startswith(pre) for pre in prefixes]))
if delete:
path = join(self.cache_directory_path, filename)
logger.info("Deleting %s", path)
remove(path)
if isdir(self.cache_directory_path):
for filename in listdir():
delete = (
any([filename.endswith(ext) for ext in suffixes]) or
any([filename.startswith(pre) for pre in prefixes]))
if delete:
path = join(self.cache_directory_path, filename)
logger.info("Deleting %s", path)
remove(path)

def delete_cache_directory(self):
rmtree(self.cache_directory_path)
if isdir(self.cache_directory_path):
rmtree(self.cache_directory_path)

0 comments on commit aebd1c5

Please sign in to comment.