# Path: vimswitch/Settings.py
# def getSettings(app):
#     return app.get('settings', createSettings(app))
# 
# Path: vimswitch/DiskIo.py
# def getDiskIo(app):
#     return app.get('diskIo', createDiskIo(app))


import os
import re
import datetime
from .six.moves.urllib_parse import urlparse
from .six.moves.urllib_request import FancyURLopener
from .Settings import getSettings
from .DiskIo import getDiskIo
        """
        Returns the filename of the download by first parsing the header and if
        unsuccessful, then parsing the url. If both fail, then returns an empty
        string.
        """
        if 'content-disposition' in headers:
            regex = 'attachment; filename=(.*)'
            contentDisposition = headers['content-disposition']
            match = re.match(regex, contentDisposition)
            if match is not None:
                filename = match.group(1)
                return filename

        urlPath = urlparse(url).path
        filename = os.path.basename(urlPath)
        return filename

    class UrlOpener(FancyURLopener):
        "A URLOpener that raises an error when there is a http error"
        def http_error_default(self, url, fp, errcode, errmsg, headers):
            errorMessage = 'Error when accessing %s: %s %s' % (url, errcode, errmsg)
            raise IOError(errorMessage)


def getFileDownloader(app):
    return app.get('fileDownloader', createFileDownloader(app))


def createFileDownloader(app):
    settings = getSettings(app)

# Path: vimswitch/Settings.py
# def getSettings(app):
#     return app.get('settings', createSettings(app))
# 
# Path: vimswitch/DiskIo.py
# def getDiskIo(app):
#     return app.get('diskIo', createDiskIo(app))


# Path: vimswitch/FileDownloader.py
import os
import re
import datetime
from .six.moves.urllib_parse import urlparse
from .six.moves.urllib_request import FancyURLopener
from .Settings import getSettings
from .DiskIo import getDiskIo

        """
        Returns the filename of the download by first parsing the header and if
        unsuccessful, then parsing the url. If both fail, then returns an empty
        string.
        """
        if 'content-disposition' in headers:
            regex = 'attachment; filename=(.*)'
            contentDisposition = headers['content-disposition']
            match = re.match(regex, contentDisposition)
            if match is not None:
                filename = match.group(1)
                return filename

        urlPath = urlparse(url).path
        filename = os.path.basename(urlPath)
        return filename

    class UrlOpener(FancyURLopener):
        "A URLOpener that raises an error when there is a http error"
        def http_error_default(self, url, fp, errcode, errmsg, headers):
            errorMessage = 'Error when accessing %s: %s %s' % (url, errcode, errmsg)
            raise IOError(errorMessage)


def getFileDownloader(app):
    return app.get('fileDownloader', createFileDownloader(app))


def createFileDownloader(app):
    settings = getSettings(app)

    diskIo = getDiskIo(app)