#!/usr/bin/env python

"""Simple implementation of a file fetcher"""

import sys
import os
import urllib
import tarfile


def get_remote_file(url, destination=os.getcwd(), force=False, verbose=False):
    r"""Get bathymetry file located at `url`

    Will check downloaded file's suffix to see if the file needs to be extracted
    """

    try:
        file_name = os.path.basename(url)
        output_path = os.path.join(destination, file_name)
        if not os.path.exists(output_path) or force:
            if verbose:
                print "Downloading %s to %s..." % (url, output_path)
            urllib.urlretrieve(url, output_path)
            if verbose:
                print "Finished downloading."
        else:
            if verbose:
                print "Skipping %s, file already exists." % file_name

        if tarfile.is_tarfile(output_path):
            with tarfile.open(output_path, mode="r:*") as tar_file:
                tar_file.extractall(path=destination)
    except IOError as e:
        if verbose:
            print "Unsuccesfull at downloading and expanding '%s'." % url
        raise e


if __name__ == "__main__":
    # Default URLs
    base_url = "http://users.ices.utexas.edu/~kyle/bathy/"

    # Override base_url
    if len(sys.argv) > 1:
        base_url = sys.argv[1]

    urls = [os.path.join(base_url, 'gulf_caribbean.tt3'),
            os.path.join(base_url, 'NOAA_Galveston_Houston.tt3'),
            os.path.join(base_url, 'galveston_tx.asc')]

    for url in urls:
        get_remote_file(url, verbose=True)
@@ -114,16 +114,464 @@
 arfile%0A%0A
+def strip_archive_extensions(path, extensions=%5B%22tar%22, %22tgz%22, %22bz2%22, %22gz%22%5D):%0A    r%22%22%22%0A    Strip off archive extensions defined in *extensions* list.%0A%0A    Return stripped path calling this function recursively until all splitext%0A    does not provide an extension in the *extensions* list.%0A%0A    %22%22%22%0A%0A    if os.path.splitext(path)%5B-1%5D%5B1:%5D in extensions:%0A        return strip_archive_extensions(os.path.splitext(path)%5B0%5D)%0A    else:%0A        return path%0A%0A
 %0Adef get
@@ -782,21 +782,8 @@
 %22%22%0A%0A
-    try:%0A    
     
@@ -816,20 +816,16 @@
 me(url)%0A
-    
     outp
@@ -871,24 +871,88 @@
 e_name)%0A    
+unarchived_output_path = strip_archive_extensions(output_path)%0A%0A
     if not o
@@ -965,16 +965,27 @@
 .exists(
+unarchived_
 output_p
@@ -999,16 +999,60 @@
  force:%0A
+        if not os.path.exists(output_path):%0A
         
@@ -1236,16 +1236,12 @@
 nt %22
-Finished
+Done
  dow
@@ -1251,24 +1251,25 @@
 ading.%22%0A
+%0A
         
 else:%0A  
@@ -1256,28 +1256,58 @@
 .%22%0A%0A        
-else
+if tarfile.is_tarfile(output_path)
 :%0A          
@@ -1347,86 +1347,61 @@
 nt %22
-Skipping %25s, file already exists.%22 %25 file_name%0A%0A        if tarfile.is_tarfile(
+Un-archiving %25s to %25s...%22 %25 (output_path, unarchived_
 outp
@@ -1400,33 +1400,32 @@
 ved_output_path)
-:
 %0A            wit
@@ -1539,138 +1539,173 @@
     
-except IOError as e:%0A        if verbose:%0A            print %22Unsuccesfull at downloading and expanding '%25s'.%22 %25 url%0A        raise e
+        if verbose:%0A                print %22Done un-archiving.%22%0A    else:%0A        if verbose:%0A            print %22Skipping %25s because it already exists locally.%22 %25 url
 %0A%0A%0Ai
@@ -1941,24 +1941,32 @@
 aribbean.tt3
+.tar.bz2
 '),%0A        
@@ -2019,16 +2019,24 @@
 ston.tt3
+.tar.bz2
 '),%0A    
@@ -2083,16 +2083,89 @@
 n_tx.asc
+.tar.bz2'),%0A            os.path.join(base_url, 'NewOrleans_3s.tt3.tar.bz2
 ')%5D%0A%0A   

Improve get_bathy to check for local unarchived files