# Path: oggm/exceptions.py
# class InvalidParamsError(ValueError):
#     pass
# 
# class NoInternetException(Exception):
#     pass
# 
# class DownloadVerificationFailedException(Exception):
#     def __init__(self, msg=None, path=None):
#         self.msg = msg
#         self.path = path
# 
# class DownloadCredentialsMissingException(Exception):
#     pass
# 
# class HttpDownloadError(Exception):
#     def __init__(self, code, url):
#         self.code = code
#         self.url = url
# 
# class HttpContentTooShortError(Exception):
#     pass
# 
# class InvalidDEMError(RuntimeError):
#     pass
# 
# class FTPSDownloadError(Exception):
#     def __init__(self, orgerr):
#         self.orgerr = orgerr


import glob
import os
import gzip
import bz2
import hashlib
import shutil
import zipfile
import sys
import math
import logging
import time
import fnmatch
import urllib.request
import urllib.error
import socket
import multiprocessing
import ftplib
import ssl
import tarfile
import pandas as pd
import numpy as np
import shapely.geometry as shpg
import requests
    import geopandas as gpd
    import salem
    import rasterio
import oggm.cfg as cfg
from functools import partial, wraps
from urllib.parse import urlparse
from netrc import netrc
    from salem import wgs84
        from rasterio.merge import merge as merge_tool
        from rasterio.tools.merge import merge as merge_tool
from oggm.exceptions import (InvalidParamsError, NoInternetException,
                             DownloadVerificationFailedException,
                             DownloadCredentialsMissingException,
                             HttpDownloadError, HttpContentTooShortError,
                             InvalidDEMError, FTPSDownloadError)
        from progressbar import DataTransferBar, UnknownLength
        from oggm.utils import robust_tar_extract
    from oggm.utils import tolist
    from oggm.utils import tolist


def file_downloader(www_path, retry_max=5, cache_name=None,
                    reset=False, auth=None, timeout=None):
    """A slightly better downloader: it tries more than once."""

    local_path = None
    retry_counter = 0
    while retry_counter <= retry_max:
        # Try to download
        try:
            retry_counter += 1
            local_path = _progress_urlretrieve(www_path, cache_name=cache_name,
                                               reset=reset, auth=auth,
                                               timeout=timeout)
            # if no error, exit
            break
        except HttpDownloadError as err:
            # This works well for py3
            if err.code == 404 or err.code == 300:
                # Ok so this *should* be an ocean tile
                return None
            elif err.code >= 500 and err.code < 600:
                logger.info("Downloading %s failed with HTTP error %s, "
                            "retrying in 10 seconds... %s/%s" %
                            (www_path, err.code, retry_counter, retry_max))
                time.sleep(10)
                continue
            else:
                raise

# Path: oggm/exceptions.py
# class InvalidParamsError(ValueError):
#     pass
# 
# class NoInternetException(Exception):
#     pass
# 
# class DownloadVerificationFailedException(Exception):
#     def __init__(self, msg=None, path=None):
#         self.msg = msg
#         self.path = path
# 
# class DownloadCredentialsMissingException(Exception):
#     pass
# 
# class HttpDownloadError(Exception):
#     def __init__(self, code, url):
#         self.code = code
#         self.url = url
# 
# class HttpContentTooShortError(Exception):
#     pass
# 
# class InvalidDEMError(RuntimeError):
#     pass
# 
# class FTPSDownloadError(Exception):
#     def __init__(self, orgerr):
#         self.orgerr = orgerr


# Path: oggm/utils/_downloads.py
import glob
import os
import gzip
import bz2
import hashlib
import shutil
import zipfile
import sys
import math
import logging
import time
import fnmatch
import urllib.request
import urllib.error
import socket
import multiprocessing
import ftplib
import ssl
import tarfile
import pandas as pd
import numpy as np
import shapely.geometry as shpg
import requests
    import geopandas as gpd
    import salem
    import rasterio
import oggm.cfg as cfg
from functools import partial, wraps
from urllib.parse import urlparse
from netrc import netrc
    from salem import wgs84
        from rasterio.merge import merge as merge_tool
        from rasterio.tools.merge import merge as merge_tool
from oggm.exceptions import (InvalidParamsError, NoInternetException,
                             DownloadVerificationFailedException,
                             DownloadCredentialsMissingException,
                             HttpDownloadError, HttpContentTooShortError,
                             InvalidDEMError, FTPSDownloadError)
        from progressbar import DataTransferBar, UnknownLength
        from oggm.utils import robust_tar_extract
    from oggm.utils import tolist
    from oggm.utils import tolist



def file_downloader(www_path, retry_max=5, cache_name=None,
                    reset=False, auth=None, timeout=None):
    """A slightly better downloader: it tries more than once."""

    local_path = None
    retry_counter = 0
    while retry_counter <= retry_max:
        # Try to download
        try:
            retry_counter += 1
            local_path = _progress_urlretrieve(www_path, cache_name=cache_name,
                                               reset=reset, auth=auth,
                                               timeout=timeout)
            # if no error, exit
            break
        except HttpDownloadError as err:
            # This works well for py3
            if err.code == 404 or err.code == 300:
                # Ok so this *should* be an ocean tile
                return None
            elif err.code >= 500 and err.code < 600:
                logger.info("Downloading %s failed with HTTP error %s, "
                            "retrying in 10 seconds... %s/%s" %
                            (www_path, err.code, retry_counter, retry_max))
                time.sleep(10)
                continue
            else:
                raise

        except HttpContentTooShortError as err: