# Path: pydatajson/download.py
# def download_to_file(url, file_path, **kwargs):
#     """
#     Descarga un archivo a través del protocolo HTTP, en uno o más intentos, y
#     escribe el contenido descargado el el path especificado.
# 
#     Args:
#         url (str): URL (schema HTTP) del archivo a descargar.
#         file_path (str): Path del archivo a escribir. Si un archivo ya existe
#             en el path especificado, se sobrescribirá con nuevos contenidos.
#         kwargs: Parámetros para download().
#     """
#     content = download(url, file_path, **kwargs)


import json
import logging
import os
import re
import tempfile
from contextlib import contextmanager
from datetime import datetime
from openpyxl import load_workbook
from six import string_types, iteritems
from six.moves.urllib_parse import urlparse
from unidecode import unidecode
from pydatajson.download import download_to_file

    for sheetname in wb.sheetnames:
        if sheetname.lower() == name.lower():
            return sheetname

    raise Exception("No existe la hoja {}".format(name))


def pprint(result):
    print(text_type(json.dumps(
        result, indent=4, separators=(",", ": "),
        ensure_ascii=False
    )))


@contextmanager
def resource_files_download(catalog, distributions, download_strategy):
    resource_files = {}
    if download_strategy is not None:
        distributions = [dist for dist in distributions if
                         download_strategy(catalog, dist)]
        for dist in distributions:
            try:
                tmpdir = tempfile.mkdtemp()
                tmpfile = tempfile.NamedTemporaryFile(delete=False, dir=tmpdir)
                tmpfile.close()
                file_name = dist.get('fileName') or \
                    dist['downloadURL'].split('/')[-1]
                os.rename(tmpfile.name, os.path.join(tmpdir, file_name))
                tmpfile.name = os.path.join(tmpdir, file_name)

# Path: pydatajson/download.py
# def download_to_file(url, file_path, **kwargs):
#     """
#     Descarga un archivo a través del protocolo HTTP, en uno o más intentos, y
#     escribe el contenido descargado el el path especificado.
# 
#     Args:
#         url (str): URL (schema HTTP) del archivo a descargar.
#         file_path (str): Path del archivo a escribir. Si un archivo ya existe
#             en el path especificado, se sobrescribirá con nuevos contenidos.
#         kwargs: Parámetros para download().
#     """
#     content = download(url, file_path, **kwargs)


# Path: pydatajson/helpers.py
import json
import logging
import os
import re
import tempfile
from contextlib import contextmanager
from datetime import datetime
from openpyxl import load_workbook
from six import string_types, iteritems
from six.moves.urllib_parse import urlparse
from unidecode import unidecode
from pydatajson.download import download_to_file


    for sheetname in wb.sheetnames:
        if sheetname.lower() == name.lower():
            return sheetname

    raise Exception("No existe la hoja {}".format(name))


def pprint(result):
    print(text_type(json.dumps(
        result, indent=4, separators=(",", ": "),
        ensure_ascii=False
    )))


@contextmanager
def resource_files_download(catalog, distributions, download_strategy):
    resource_files = {}
    if download_strategy is not None:
        distributions = [dist for dist in distributions if
                         download_strategy(catalog, dist)]
        for dist in distributions:
            try:
                tmpdir = tempfile.mkdtemp()
                tmpfile = tempfile.NamedTemporaryFile(delete=False, dir=tmpdir)
                tmpfile.close()
                file_name = dist.get('fileName') or \
                    dist['downloadURL'].split('/')[-1]
                os.rename(tmpfile.name, os.path.join(tmpdir, file_name))
                tmpfile.name = os.path.join(tmpdir, file_name)

                download_to_file(dist['downloadURL'], tmpfile.name)