<commit_before>import atexit
import os
import tempfile
from importlib import import_module

from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured

from django_extra_tools.conf import settings


class LockError(Exception):
    pass


def get_locker_class(import_path=None):
    if import_path is None:
        import_path = settings.DEFAULT_LOCKER_CLASS
    try:
        module_path, class_name = import_path.rsplit('.', 1)
    except ValueError:
        raise ImproperlyConfigured("{} isn't a locker module.".format(import_path))
    try:
        module = import_module(module_path)
    except ImportError as e:
        raise ImproperlyConfigured('Error importing locker module %s: "%s"' % (module_path, e))
    try:
        return getattr(module, class_name)
    except AttributeError:
        raise ImproperlyConfigured('Locker module "%s" does not define a "%s" class.' % (module_path, class_name))


class FileLocker(object):
    """
    Create a "{name}.lock" file in temp directory.
    """
    def __call__(self, name):
        filename = os.path.join(tempfile.gettempdir(), '{}.lock'.format(name))

        try:
            fd = os.open(filename, os.O_CREAT | os.O_EXCL)

            def register():
                os.close(fd)
                os.remove(filename)

            atexit.register(register)
        except OSError:
            msg = ("Script run multiple times. If this isn't true, delete "
                   "`%s`." % filename)
            raise LockError(msg)


class CacheLocker(object):<commit_msg>Lockers: Handle errors when lock file is removed<commit_after>import atexit
import os
import tempfile
from importlib import import_module

from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured

from django_extra_tools.conf import settings


class LockError(Exception):
    pass


def get_locker_class(import_path=None):
    if import_path is None:
        import_path = settings.DEFAULT_LOCKER_CLASS
    try:
        module_path, class_name = import_path.rsplit('.', 1)
    except ValueError:
        raise ImproperlyConfigured("{} isn't a locker module.".format(import_path))
    try:
        module = import_module(module_path)
    except ImportError as e:
        raise ImproperlyConfigured('Error importing locker module %s: "%s"' % (module_path, e))
    try:
        return getattr(module, class_name)
    except AttributeError:
        raise ImproperlyConfigured('Locker module "%s" does not define a "%s" class.' % (module_path, class_name))


class FileLocker(object):
    """
    Create a "{name}.lock" file in temp directory.
    """
    def __call__(self, name):
        filename = os.path.join(tempfile.gettempdir(), '{}.lock'.format(name))

        try:
            fd = os.open(filename, os.O_CREAT | os.O_EXCL)

            def register():
                try:
                    os.close(fd)
                except OSError:
                    pass

                try:
                    os.remove(filename)
                except OSError:
                    pass

            atexit.register(register)
        except OSError:
            msg = ("Script run multiple times. If this isn't true, delete "
                   "`%s`." % filename)
            raise LockError(msg)


class CacheLocker(object):
{'fraction_non_alphanumeric': 0.095367027677497, 'fraction_numerical': 0.0006016847172081829, 'mean_word_length': 2.481675392670157, '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': '2462966', 'n_tokens_mistral': 979, 'n_tokens_neox': 929, 'n_words': 260}