<commit_before>from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import get_module_class, tokey, serialize
from sorl.thumbnail.parsers import parse_geometry
from sorl.thumbnail.images import ImageFile


class ThumbnailBackend(object):
    """
    The main class for sorl-thumbnail, you can subclass this if you for example
    want to change the way destination filename is generated.
    """
    default_options = {
        'format': settings.THUMBNAIL_FORMAT,
        'quality': settings.THUMBNAIL_QUALITY,
        'colorspace': settings.THUMBNAIL_COLORSPACE,
        'upscale': settings.THUMBNAIL_UPSCALE,
        'crop': False,
    }

    extensions = {
        'JPEG': 'jpg',
        'PNG': 'png',
    }

    def __init__(self, engine=None, kvstore=None, storage=None):
        if engine is None:
            engine = get_module_class(settings.THUMBNAIL_ENGINE)()
        if kvstore is None:
            kvstore = get_module_class(settings.THUMBNAIL_KVSTORE)()
        if storage is None:
            storage = get_module_class(settings.THUMBNAIL_STORAGE)()
        self.engine = engine
        self.kvstore = kvstore
        self.storage = storage

    def get_thumbnail(self, file_, geometry_string, **options):
        """
        Returns thumbnail as an ImageFile instance for file with geometry and
        options given. First it will try to get it from the key value store,
        secondly it will create it.
        """
        source = ImageFile(file_)
        for key, value in self.default_options.iteritems():
            options.setdefault(key, value)
        name = self._get_thumbnail_filename(source, geometry_string, options)
        thumbnail = ImageFile(name, self.storage)
        cached = self.kvstore.get(thumbnail)
        if cached:
            return cached
        if not thumbnail.exists():
            # We have to check exists() because the Storage backend does not
            # overwrite in some implementations.
            self._create_thumbnail(source, geometry_string, options, thumbnail)
        # If the thumbnail exists we don't do anything, the other option is to
        # delete and write but this could lead to race conditions so I will
        # just leave that out for now.
        self.kvstore.set(thumbnail, source)
        return thumbnail

    def _create_thumbnail(self, source, geometry_string, options, thumbnail):
        """
        Creates the thumbnail by using self.engine and stores the source to
        kvstore if its not already there.
        """
        source_image = self.engine.get_image(source)
        source_image_ratio = self.engine.get_image_ratio(source_image)
        if not self.kvstore.get(source):
            # We store the source in kvstore if its not there already. The
            # reason for us doing that here is because we have the file open
            # and can cheaply access size.
            source_image_size = self.engine.get_image_size(source_image)
            source.set_size(source_image_size)
            self.kvstore.set(source)
        geometry = parse_geometry(geometry_string, source_image_ratio)
        thumbnail_image = self.engine.create(source_image, geometry, options)
        self.engine.write(thumbnail_image, options, thumbnail)
        # It's much cheaper to set the size here too
        thumbnail_image_size = self.engine.get_image_size(thumbnail_image)
        thumbnail.set_size(thumbnail_image_size)
<commit_msg>missed some condition on exists and kvs<commit_after>from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import get_module_class, tokey, serialize
from sorl.thumbnail.parsers import parse_geometry
from sorl.thumbnail.images import ImageFile


class ThumbnailBackend(object):
    """
    The main class for sorl-thumbnail, you can subclass this if you for example
    want to change the way destination filename is generated.
    """
    default_options = {
        'format': settings.THUMBNAIL_FORMAT,
        'quality': settings.THUMBNAIL_QUALITY,
        'colorspace': settings.THUMBNAIL_COLORSPACE,
        'upscale': settings.THUMBNAIL_UPSCALE,
        'crop': False,
    }

    extensions = {
        'JPEG': 'jpg',
        'PNG': 'png',
    }

    def __init__(self, engine=None, kvstore=None, storage=None):
        if engine is None:
            engine = get_module_class(settings.THUMBNAIL_ENGINE)()
        if kvstore is None:
            kvstore = get_module_class(settings.THUMBNAIL_KVSTORE)()
        if storage is None:
            storage = get_module_class(settings.THUMBNAIL_STORAGE)()
        self.engine = engine
        self.kvstore = kvstore
        self.storage = storage

    def get_thumbnail(self, file_, geometry_string, **options):
        """
        Returns thumbnail as an ImageFile instance for file with geometry and
        options given. First it will try to get it from the key value store,
        secondly it will create it.
        """
        source = ImageFile(file_)
        for key, value in self.default_options.iteritems():
            options.setdefault(key, value)
        name = self._get_thumbnail_filename(source, geometry_string, options)
        thumbnail = ImageFile(name, self.storage)
        cached = self.kvstore.get(thumbnail)
        if cached:
            return cached
        if not thumbnail.exists():
            # We have to check exists() because the Storage backend does not
            # overwrite in some implementations.
            source_image = self.engine.get_image(source)
            # We might as well set the size since we have the image in memory
            size = self.engine.get_image_size(source_image)
            source.set_size(size)
            self._create_thumbnail(source_image, geometry_string, options,
                                   thumbnail)
        # If the thumbnail exists we don't create it, the other option is
        # to delete and write but this could lead to race conditions so I
        # will just leave that out for now.
        self.kvstore.get_or_set(source)
        self.kvstore.set(thumbnail, source)
        return thumbnail

    def _create_thumbnail(self, source_image, geometry_string, options,
                          thumbnail):
        """
        Creates the thumbnail by using self.engine
        """
        ratio = self.engine.get_image_ratio(source_image)
        geometry = parse_geometry(geometry_string, ratio)
        image = self.engine.create(source_image, geometry, options)
        self.engine.write(image, options, thumbnail)
        # It's much cheaper to set the size here
        size = self.engine.get_image_size(image)
        thumbnail.set_size(size)

{'fraction_non_alphanumeric': 0.0735404472459853, 'fraction_numerical': 0.0, 'mean_word_length': 2.5484558040468586, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2605870', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1824, 'n_words': 551}