<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Base class to allow inheriting objects to download multiple files from remote
locations at once.
"""
import os
import sys
import logging
import tempfile
from concurrent.futures import ThreadPoolExecutor, as_completed

import requests

logger = logging.getLogger(__name__)


class MultiFileDownloader():

    TMP_DIR = tempfile.gettempdir()

    def __init__(self, tgt_dir):
        if not os.path.isdir(tgt_dir):
            try:
                os.makedirs(tgt_dir)
            except:
                print("Couldn't create target directory '%s'..." % tgt_dir)
                sys.exit(1)
        self.tgt_dir = tgt_dir
        self.files_to_download = list()
        self.downloaded_files = list()
        self.rejected_urls = list()
        self.zip_downloaded_files = False

    def toggle_zipping(self, zip_downloaded_files):
        if zip_downloaded_files is True:
            self.zip_downloaded_files = True
        else:
            self.zip_downloaded_files = False

    def find_files_to_download(self):
        raise NotImplementedError

    def download_files(self, workers):

        tgt_dir = self.tgt_dir

        self.files_to_download = sorted(self.files_to_download)

        if workers > 1:
            with ThreadPoolExecutor(max_workers=workers) as download_threads:
                tasks = {download_threads.submit(
                    self.download_task,
                    url, tgt_dir): url for url in self.files_to_download}
                for completed_task in as_completed(tasks):
                    try:
                        completed_task.result()
                    except Exception as e:
                        print()
                        print("Task generated an exception: %s" % e)
        else:
            for url in self.files_to_download:
                print("Downloading %s" % url)
                self.download_task(url, tgt_dir)
<commit_msg>Allow for overriding download target directory and files to be downloaded<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Base class to allow inheriting objects to download multiple files from remote
locations at once.
"""
import os
import logging
import tempfile
from concurrent.futures import ThreadPoolExecutor, as_completed

import requests

logger = logging.getLogger(__name__)


class MultiFileDownloader():

    TMP_DIR = tempfile.gettempdir()

    def __init__(self, tgt_dir):
        if not os.path.isdir(tgt_dir):
            try:
                os.makedirs(tgt_dir)
            except:
                logger.warn(
                    "Couldn't create target directory '%s', using " % tgt_dir +
                    "system temporary directory %s instead" % self.TMP_DIR)
                tgt_dir = self.TMP_DIR
        self.tgt_dir = tgt_dir
        self.files_to_download = list()
        self.downloaded_files = list()
        self.rejected_urls = list()
        self.zip_downloaded_files = False

    def toggle_zipping(self, zip_downloaded_files):
        if zip_downloaded_files is True:
            self.zip_downloaded_files = True
        else:
            self.zip_downloaded_files = False

    def find_files_to_download(self):
        raise NotImplementedError

    def download_files(self, workers, tgt_dir=None, files_to_download=None):

        if tgt_dir is None:
            tgt_dir = self.tgt_dir
        if files_to_download is None:
            files_to_download = self.files_to_download

        files_to_download = sorted(files_to_download)

        if workers > 1:
            with ThreadPoolExecutor(max_workers=workers) as download_threads:
                tasks = {download_threads.submit(
                    self.download_task,
                    url, tgt_dir): url for url in files_to_download}
                for completed_task in as_completed(tasks):
                    try:
                        completed_task.result()
                    except Exception as e:
                        print()
                        print("Task generated an exception: %s" % e)
        else:
            for url in files_to_download:
                print("Downloading %s" % url)
                self.download_task(url, tgt_dir)

{'fraction_non_alphanumeric': 0.07402168788307402, 'fraction_numerical': 0.0011786892975011788, 'mean_word_length': 1.9733707077785565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88831', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1147, 'n_words': 295}