Skip to content

Commit

Permalink
Merge pull request #7 from ImageMarkup/clean-interrupt
Browse files Browse the repository at this point in the history
Cleanup on keyboard interrupts
  • Loading branch information
danlamanna authored Jun 7, 2022
2 parents d3cb62f + 286eb42 commit 5c91b2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions isic_cli/cli/image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import csv
import itertools
from pathlib import Path
Expand All @@ -16,6 +17,11 @@
from isic_cli.io.http import download_image, get_images, get_num_images


def cleanup_partially_downloaded_files(directory: Path) -> None:
for p in directory.glob('**/.isic-partial.*'):
p.unlink()


@click.group(short_help='Manage images.')
@click.pass_obj
def image(ctx):
Expand Down Expand Up @@ -73,6 +79,8 @@ def download(
"""
outdir.mkdir(exist_ok=True)

atexit.register(cleanup_partially_downloaded_files, outdir)

with Progress(console=Console(file=sys.stderr)) as progress:
archive_num_images = get_num_images(ctx.session, search, collections)
download_num_images = archive_num_images if limit == 0 else min(archive_num_images, limit)
Expand Down
8 changes: 7 additions & 1 deletion isic_cli/io/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
from pathlib import Path
import shutil
from tempfile import NamedTemporaryFile
from typing import Iterable, Optional, Union

from more_itertools import chunked
Expand Down Expand Up @@ -127,8 +129,12 @@ def download_image(image: dict, to: Path, progress, task) -> None:
r = session.get(image['urls']['full'], stream=True)
r.raise_for_status()

with open(to / f'{image["isic_id"]}.JPG', 'wb') as outfile:
temp_file_name = None
with NamedTemporaryFile(dir=to, prefix='.isic-partial.', delete=False) as outfile:
temp_file_name = outfile.name
for chunk in r.iter_content(1024 * 1024 * 5):
outfile.write(chunk)

shutil.move(temp_file_name, to / f'{image["isic_id"]}.JPG')

progress.update(task, advance=1)

0 comments on commit 5c91b2b

Please sign in to comment.