<commit_before>from collections import namedtuple
from django.core.management.base import BaseCommand


Progress = namedtuple('Progress', ['progress', 'overall'])


class AsyncCommand(BaseCommand):
    """A management command with added convenience functions for displaying
    progress to the user.

    Rather than implementing handle() (as is for BaseCommand), subclasses, must
    implement handle_async(), which accepts the same arguments as handle().

    If ran from the command line, AsynCommand displays a progress bar to the
    user. If ran asynchronously through kolibri.tasks.schedule_command(),
    AsyncCommand sends results through the Progress class to the main Django
    process. Anyone who knows the task id for the command instance can check
    the intermediate progress by looking at the task's AsyncResult.result
    variable.

    """

    CELERY_PROGRESS_STATE_NAME = "PROGRESS"

    def _identity(*args, **kwargs):
        # heh, are we all just NoneTypes after all?
        pass

    def handle(self, *args, **options):
        self.update_state = options.pop("update_state", self._identity)

        self.handle_async(*args, **options)

    def set_progress(self, progress, overall=None, message=None):
        overall = overall or self.get_overall()
        progress = Progress(progress, overall)
        self.update_state(state=self.CELERY_PROGRESS_STATE_NAME,
                          meta=progress)

    def get_overall():
        pass

    def set_overall():
        pass<commit_msg>Flesh out AsyncCommand to add progress bars in the command line!<commit_after>from tqdm import tqdm
from collections import namedtuple
from django.core.management.base import BaseCommand


Progress = namedtuple('Progress', ['progress', 'total'])


class ProgressTracker():

    def __init__(self, total=100, update_func=None):
        if update_func:     # custom progress bar provided by programmer
            self.update_progress = update_func
            self.progressbar = None
        else:                   # standard progress bar progress tracking
            self.progressbar = tqdm(total=total)
            self.update_progress = self.progressbar.update

    def __enter__(self):
        return self.update_progress

    def __exit__(self, *exc_details):
        if self.progressbar:
            self.progressbar.close()


class AsyncCommand(BaseCommand):
    """A management command with added convenience functions for displaying
    progress to the user.

    Rather than implementing handle() (as is for BaseCommand), subclasses, must
    implement handle_async(), which accepts the same arguments as handle().

    If ran from the command line, AsynCommand displays a progress bar to the
    user. If ran asynchronously through kolibri.tasks.schedule_command(),
    AsyncCommand sends results through the Progress class to the main Django
    process. Anyone who knows the task id for the command instance can check
    the intermediate progress by looking at the task's AsyncResult.result
    variable.

    """

    CELERY_PROGRESS_STATE_NAME = "PROGRESS"

    def handle(self, *args, **options):
        self.update_progress = options.pop("update_state", None)

        self.handle_async(*args, **options)

    start_progress = ProgressTracker


def _identity(*args, **kwargs):
    # heh, are we all just NoneTypes after all?
    pass
{'fraction_non_alphanumeric': 0.07416196974191634, 'fraction_numerical': 0.0008899436369029962, 'mean_word_length': 3.312020460358056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6258992', 'n_tokens_mistral': 875, 'n_tokens_neox': 839, 'n_words': 317}