# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/10_Download-demo-data.ipynb (unless otherwise specified).

__all__ = ['download']

# Cell

import wget
import re
import os
import sys
import ssl

def _bar_progress(current, total, width=80):
    '''Create this bar_progress method which is invoked automatically from wget'''

    percent = (100 * current) // total
    progress_message = f'Downloading {total//1e6} MB: {percent}% '
    # Don't use print() as it will print in new line every time.
    sys.stdout.write("\r" + progress_message)
    sys.stdout.flush()

def download():
    '''Download demo files from cloud storage via wget and ssl.'''

    # trying to fix SSL errors with wget.download()  below
    ssl._create_default_https_context = ssl._create_unverified_context

    cwd = os.getcwd()

    bucket_url = 'https://f002.backblazeb2.com/file/crono-maxrf-data-demo/'

    fname_list = ['14200215102021-blindTest2AgedDetail.HDF5',
                  'Ink-08-aged-VIS.png']

    n_files = len(fname_list)


    print(f'Checking your current working directory: \n\'{cwd}\'\n')
    print(f'Please wait while synchronizing {n_files} files...')

    for i, fname in enumerate(fname_list):

        # do not download again if already present
        if fname in os.listdir():
            print(f'({i+1}) \'{fname}')

        # download
        else:
            url = bucket_url + fname
            fname = wget.download(url, bar=_bar_progress)
            print(f' ({i+1}) \'{fname}')

    print('Ready!')

