<commit_before>import inspect
from . import settings
from .tasks import PeriodicTask


# Based on django's admin app's autodiscover.
def autodiscover():
    """
    Auto-discover INSTALLED_APPS periodictasks.py modules and fail silently
    when not present.
    """

    from django.conf import settings as project_settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for app in project_settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's periodictasks module.
        try:
            import_module('%s.periodictasks' % app)
        except:
            # Decide whether to bubble up this error. If the app just
            # doesn't have a periodictasks module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'periodictasks'):
                raise


class TaskScheduler(object):

    _backend_singletons = {} # Maps backend classes to instances.
    _backends = set()

    class BackendDoesNotExist(Exception):
        # TODO: A message that includes the backend id that doesn't exist, perhaps?
        pass

    @property
    def backends(self):
        return self._backends

    def get_backend(self, id):
        for backend in self._backends:
            if getattr(backend, 'id', None) == id:
                return backend
        raise TaskScheduler.BackendDoesNotExist

    def simple_task(self, fn, repeat_interval, task_id_suffix='',
        backend=None):
        """
        Registers a callable as a periodic task.
        """
        _task_id = '%s.%s%s' % (fn.__module__, fn.__name__, task_id_suffix)
        _repeat_interval = repeat_interval
        _backend = backend

        # Create a PeriodicTask subclass that wraps the function.
        class DecoratedTask(PeriodicTask):
            backend = _backend
            task_id = _task_id
            repeat_interval = _repeat_interval

            def run(self, *args, **kwargs):
                fn(*args, **kwargs)

        self.task(DecoratedTask)
        return DecoratedTask

    def task(self, task):
        """
        Registers a periodic task.<commit_msg>simple_task() now passes a task instance (instead of a class) to task()<commit_after>import inspect
from . import settings
from .tasks import PeriodicTask


# Based on django's admin app's autodiscover.
def autodiscover():
    """
    Auto-discover INSTALLED_APPS periodictasks.py modules and fail silently
    when not present.
    """

    from django.conf import settings as project_settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for app in project_settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's periodictasks module.
        try:
            import_module('%s.periodictasks' % app)
        except:
            # Decide whether to bubble up this error. If the app just
            # doesn't have a periodictasks module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'periodictasks'):
                raise


class TaskScheduler(object):

    _backend_singletons = {} # Maps backend classes to instances.
    _backends = set()

    class BackendDoesNotExist(Exception):
        # TODO: A message that includes the backend id that doesn't exist, perhaps?
        pass

    @property
    def backends(self):
        return self._backends

    def get_backend(self, id):
        for backend in self._backends:
            if getattr(backend, 'id', None) == id:
                return backend
        raise TaskScheduler.BackendDoesNotExist

    def simple_task(self, fn, repeat_interval, task_id_suffix='',
        backend=None):
        """
        Registers a callable as a periodic task.
        """
        _task_id = '%s.%s%s' % (fn.__module__, fn.__name__, task_id_suffix)
        _repeat_interval = repeat_interval
        _backend = backend

        # Create a PeriodicTask subclass that wraps the function.
        class DecoratedTask(PeriodicTask):
            backend = _backend
            task_id = _task_id
            repeat_interval = _repeat_interval

            def run(self, *args, **kwargs):
                fn(*args, **kwargs)

        task_instance = DecoratedTask()
        self.task(task_instance)
        return task_instance

    def task(self, task):
        """
        Registers a periodic task.
{'fraction_non_alphanumeric': 0.07319721980886186, 'fraction_numerical': 0.0, 'mean_word_length': 2.3785766691122525, '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': '205467', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1219, 'n_words': 400}