import os
import sys
from urllib.parse import urlparse
from urllib.request import urlretrieve

from typing import Optional


def _progress_bar(
    current: int, block_size: int, total_size: int, bar_width: int = 100
) -> str:

    proportion_downloaded = round(
        (float(current * block_size)) / total_size, 8
    )

    pbar_width = int(bar_width * proportion_downloaded)
    pbar = "#" * pbar_width

    ws_width = int((bar_width - (bar_width * proportion_downloaded)))
    ws = " " * ws_width

    pbar_line = "{}{}{:7.1f}%".format(pbar, ws, 100 * proportion_downloaded)
    return pbar_line


def progress_hook(current: int, block_size: int, total_size: int) -> None:
    """ a simple progress bar """

    sys.stderr.write(
        _progress_bar(
            current=current, block_size=block_size, total_size=total_size
        )
        + "\r"
    )


def download(
    url: str, out_path: Optional[str] = None, progress: bool = False
) -> str:
    """

    Download a file given a URL. Returns the downloaded file's local path:

    >>> download('http://i.imgur.com/ij2h06p.png')
    'ij2h06p.png'

    URL parameters are stripped out before saving:

    >>> download('http://i.imgur.com/ij2h06p.png?foo=bar')
    'ij2h06p.png'

    You can override the output path:

    >>> download('http://i.imgur.com/ij2h06p.png', out_path='computer.png')
    'computer.png'

    There's even a fancy progress bar:
    >>> download('http://i.imgur.com/ij2h06p.png', out_path='computer.png', progress=True)
    'computer.png'
    """

    parsed = urlparse(url)

    out_path = (
        os.path.join(os.getcwd(), os.path.basename(parsed.path))
        if out_path is None
        else out_path
    )

    if progress:
        urlretrieve(url, out_path, reporthook=progress_hook)
        # finish off progress bar
        sys.stderr.write("\n")
    else:
        urlretrieve(url, out_path)

    return out_path

@@ -117,16 +117,57 @@
 tional%0A%0A
+from tempfile import TemporaryDirectory%0A%0A
 %0Adef _pr
@@ -1734,24 +1734,143 @@
 path%0A    )%0A%0A
+    with TemporaryDirectory() as tmpdir:%0A        temp_out_path = os.path.join(tmpdir, os.path.basename(out_path))%0A%0A    
     if progr
@@ -1874,32 +1874,36 @@
 ogress:%0A        
+  
+  
 urlretrieve(url,
@@ -1895,32 +1895,37 @@
 rlretrieve(url, 
+temp_
 out_path, report
@@ -1952,16 +1952,20 @@
         
+    
 # finish
@@ -1986,24 +1986,28 @@
 bar%0A        
+ 
+   
 sys.stderr.w
@@ -2025,14 +2025,22 @@
     
+   
+ 
 else:%0A
+    
     
@@ -2059,16 +2059,65 @@
 eve(url,
+ temp_out_path)%0A%0A        os.rename(temp_out_path,
  out_pat

Write to temp dir then copy to out_path on completion [closes #7]