Skip to content

Commit

Permalink
feat(util.web.download_file): Add silent option for download.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmussig committed May 10, 2022
1 parent a2d88f5 commit b2b8fea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 3 additions & 2 deletions magna/ncbi/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ class NcbiAssemblyFileType(str, Enum):
fna = 'fna'


def download_ncbi_assembly_file_to_disk(gid: str, target: str, file: NcbiAssemblyFileType):
def download_ncbi_assembly_file_to_disk(gid: str, target: str, file: NcbiAssemblyFileType, silent: bool = False):
"""Download a file from the NCBI assembly directory to disk.
Args:
gid: The NCBI accession.
target: The target path.
file: The file type to download.
silent: True if the progress should be hidden.
Examples:
>>> download_ncbi_assembly_file_to_disk('GCA_003138775.1', '/tmp/foo.fna.gz', NcbiAssemblyFileType.fna)
Expand All @@ -118,7 +119,7 @@ def download_ncbi_assembly_file_to_disk(gid: str, target: str, file: NcbiAssembl
# Download to a temporary location and verify the md5
with tempfile.TemporaryDirectory() as tmpdir:
target_tmp = os.path.join(tmpdir, name)
download_file(url, target_tmp, md5)
download_file(url, target_tmp, md5, silent=silent)
move_file(target_tmp, target, checksum=True)


Expand Down
12 changes: 8 additions & 4 deletions magna/util/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from magna.util.disk import TqdmUpTo, md5sum


def download_file(url: str, path: str, md5: Optional[str] = None):
def download_file(url: str, path: str, md5: Optional[str] = None, silent: bool = False):
"""Downloads a file to disk, optionally validating the md5 hash.
Args:
url: The url to download from.
path: The path to save the file to.
md5: The expected md5 hash of the file.
silent: True if the progress should be supressed.
Raises:
IOError: If the md5 hash doesn't match.
Expand All @@ -21,9 +22,12 @@ def download_file(url: str, path: str, md5: Optional[str] = None):
>>> download_file('https://www.example.com/data.csv', '/tmp/data.csv')
"""
with TqdmUpTo(unit='B', unit_scale=True, unit_divisor=1024, miniters=1, desc=url.split('/')[-1]) as t:
urllib.request.urlretrieve(url, filename=path, reporthook=t.update_to, data=None)
t.total = t.n
if not silent:
with TqdmUpTo(unit='B', unit_scale=True, unit_divisor=1024, miniters=1, desc=url.split('/')[-1]) as t:
urllib.request.urlretrieve(url, filename=path, reporthook=t.update_to, data=None)
t.total = t.n
else:
urllib.request.urlretrieve(url, filename=path)

if md5 and md5 != md5sum(path):
raise IOError('Hash mismatch')

0 comments on commit b2b8fea

Please sign in to comment.