"""Downloaders are responsible for downloading evaluation repositories from various sources (e.g. IOM, UNHCR)."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_downloaders.ipynb.

# %% auto 0
__all__ = ['download_eval', 'download_evals']

# %% ../nbs/02_downloaders.ipynb 2
from pathlib import Path
from fastcore.all import *
from urllib.parse import urlparse
from functools import partial

from .readers import load_evals, default_config, Evaluation

# %% ../nbs/02_downloaders.ipynb 3
def download_eval(
    eval:Evaluation, # Evaluation object 
    dst:str='files/test/eval_reports', # Destination path to save files
    cfg:dict=default_config # Configuration dictionary
    ):
    "Download all documents for an evaluation to dst/eval_id/"
    eval_dir = Path(dst)/getattr(eval, cfg.id)
    eval_dir.mkdir(parents=True, exist_ok=True)
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
    for doc in getattr(eval, cfg.docs):
        url = doc[cfg.url]
        fname = Path(urlparse(url).path).name
        dest = eval_dir/fname
        urlsave(url, dest, headers=headers)
    return eval_dir

# %% ../nbs/02_downloaders.ipynb 7
def download_evals(
    evals:L, # list of evaluation records 
    dst:str='files/test/eval_reports', # destination path to save documents
    cfg:dict=default_config, # config dictionary
    n_workers:int=4 # number of workers to use
    ):
    "Download all documents for multiple evaluations in parallel"
    if not isinstance(evals, L): evals = L(evals) if isinstance(evals, list) else L([evals])
    return parallel(partial(download_eval, dst=dst, cfg=cfg), evals, n_workers=n_workers, progress=True)
