<commit_before>import os, errno
import StringIO

from PIL import ImageOps, Image

from django.conf import settings
from django.core.cache import (
    cache as default_cache,
    get_cache,
    InvalidCacheBackendError
)
from django.core.exceptions import ImproperlyConfigured
from django.core.files.uploadedfile import InMemoryUploadedFile

FMT = 'JPEG'
EXT = 'jpg'

QUAL = getattr(settings, 'IMAGE_RESIZE_QUALITY', 70)

SIZEDIMAGEFIELD_DIRECTORY_NAME = '__sized'
SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE = getattr(settings, 'SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE', None)

if not SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE:
    USE_PLACEHOLDIT = True
else:
    USE_PLACEHOLDIT = False
    SIZEDIMAGEFIELD_PLACEHOLDER_FOLDER,
    SIZEDIMAGEFIELD_PLACEHOLDER_FILENAME = os.path.split(SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE)

try:
    cache = get_cache('sizedimagefield_cache')
except InvalidCacheBackendError:
    cache = default_cache

SIZEDIMAGEFIELD_CACHE_LENGTH = getattr(settings, 'SIZEDIMAGEFIELD_CACHE_LENGTH', 2592000)

class SizedImage(dict):

    def __init__(self, path_to_image, storage):
        self.path_to_image = path_to_image
        self.storage = storage
        if getattr(self, 'filename_key', None) is None:
            raise NotImplementedError('SizedImage subclasses MUST provide a `filename_key` attribute')

    def __setitem__(self, key, value):
        raise NotImplementedError(
            'SizedImageDict instances do not allow key assignment.'
        )

    def __getitem__(self, key):
        try:
            width, height = [int(i) for i in key.split('x')]
        except KeyError:
            raise SizedImageDictKeyError(
                "SizedImageDict keys must be in the following format: "
                "`width`x`height`' where both `width` and `height` are integers."
            )

        if not self.path_to_image and USE_PLACEHOLDIT:
            resized_url = "http://placehold.it/%dx%d" % (width, height)
        else:
            resized_url = self.get_resized_path(
                path_to_image=self.path_to_image,
                width=width,
                height=height,
                filename_key=self.get_filename_key(),
                for_url=True
            )
            if cache.get(resized_url):
                # The sized path exists in the cache so the image already exists.
                # So we `pass` to skip directly to the return statement.
                pass
            else:
                resized_image_path = self.get_resized_path(
                    path_to_image=self.path_to_image,
                    width=width,
                    height=height,
                    filename_key=self.get_filename_key()
                )
                if not self.storage.exists(resized_image_path):
                    image_created = self.create_resized_image(
                        path_to_image=self.path_to_image,
                        width=width,
                        height=height,
                        filename_key=self.get_filename_key()
                    )
                # Setting a super-long cache for a resized image (30 Days)
                cache.set(resized_url, 1, SIZEDIMAGEFIELD_CACHE_LENGTH)
        return resized_url

    def get_filename_key(self):
        return self.filename_key

    @staticmethod
    def get_resized_filename(filename, width, height, filename_key):
        """
        Returns the 'resized filename' (according to `width`, `height` and `filename_key`)
        in the following format:
        `filename`-`filename_key`-`width`x`height`.ext
        """
        try:
            image_name, ext = filename.rsplit('.', 1)
        except ValueError:
            image_name = filename
            ext = 'jpg'
        return "%(image_name)s-%(filename_key)s-%(width)dx%(height)d.%(ext)s" % ({
            'image_name':image_name,
            'filename_key':filename_key,
            'width':width,
            'height':height,
            'ext':ext
        })

    def get_resized_path(self, path_to_image, width, height, filename_key, for_url=False):
        """
        Returns the 'resized' path of `path_to_image`

        If `for_url` is `True`, settings.MEDIA_URL is prepended to the path
        """
        if not path_to_image:
            filename = SIZEDIMAGEFIELD_PLACEHOLDER_FILENAME
            containing_folder = 'GLOBAL-PLACEHOLDER'
        else:
            containing_folder, filename = os.path.split(path_to_image)

        resized_filename = self.get_resized_filename(filename, width, height, filename_key)
        path_segments = [SIZEDIMAGEFIELD_DIRECTORY_NAME, containing_folder, resized_filename]

        joined_path = os.path.join(*path_segments)

        if for_url:
            path_to_return = self.storage.url(joined_path)
        else:
            path_to_return = joined_path
        return path_to_return

    def process_image(self, image, return_image=False):
        raise NotImplementedError('SizedImageDict subclasses MUST provide a `process_image` method.')

    def create_resized_image(self, path_to_image, width, height, filename_key):
        """
        Creates a resized image.
        `path_to_image`: The path to the image with the media directory to resize.
            If `None`, the SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE will be used.
        `width`: Width of resized image (int)
        `height`: Desired height of resized image (int)
        `filename_key`: A string that will be used in the sized image filename to signify
        what operation was done to it. Examples: 'crop' or 'scale'
        """
        resized_image_path = self.get_resized_path(
            path_to_image=path_to_image,
            width=width,
            height=height,
            filename_key=filename_key<commit_msg>Code clean-up<commit_after>import os, errno
import StringIO

from PIL import ImageOps, Image

from django.conf import settings
from django.core.cache import (
    cache as default_cache,
    get_cache,
    InvalidCacheBackendError
)
from django.core.exceptions import ImproperlyConfigured
from django.core.files.uploadedfile import InMemoryUploadedFile

QUAL = getattr(settings, 'SIZEDIMAGEFIELD_RESIZE_IMAGE_QUALITY', 70)

SIZEDIMAGEFIELD_DIRECTORY_NAME = '__sized'
SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE = getattr(settings, 'SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE', None)

if not SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE:
    USE_PLACEHOLDIT = True
else:
    USE_PLACEHOLDIT = False
    SIZEDIMAGEFIELD_PLACEHOLDER_FOLDER,
    SIZEDIMAGEFIELD_PLACEHOLDER_FILENAME = os.path.split(SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE)

try:
    cache = get_cache('sizedimagefield_cache')
except InvalidCacheBackendError:
    cache = default_cache

SIZEDIMAGEFIELD_CACHE_LENGTH = getattr(settings, 'SIZEDIMAGEFIELD_CACHE_LENGTH', 2592000)

class SizedImage(dict):

    def __init__(self, path_to_image, storage):
        self.path_to_image = path_to_image
        self.storage = storage
        if getattr(self, 'filename_key', None) is None:
            raise NotImplementedError(
                "Subclasses MUST provide a `filename_key` attribute"
            )

    def __setitem__(self, key, value):
        raise NotImplementedError(
            '%s instances do not allow key assignment.' % self.__class__.__name__
        )

    def __getitem__(self, key):
        try:
            width, height = [int(i) for i in key.split('x')]
        except KeyError:
            raise SizedImageDictKeyError(
                "%s keys must be in the following format: "
                "'`width`x`height`' where both `width` and `height` are "
                "integers." % self.__class__.__name__
            )

        if not self.path_to_image and USE_PLACEHOLDIT:
            resized_url = "http://placehold.it/%dx%d" % (width, height)
        else:
            resized_url = self.get_resized_path(
                path_to_image=self.path_to_image,
                width=width,
                height=height,
                filename_key=self.get_filename_key(),
                for_url=True
            )
            if cache.get(resized_url):
                # The sized path exists in the cache so the image already exists.
                # So we `pass` to skip directly to the return statement.
                pass
            else:
                resized_image_path = self.get_resized_path(
                    path_to_image=self.path_to_image,
                    width=width,
                    height=height,
                    filename_key=self.get_filename_key()
                )
                if not self.storage.exists(resized_image_path):
                    image_created = self.create_resized_image(
                        path_to_image=self.path_to_image,
                        width=width,
                        height=height,
                        filename_key=self.get_filename_key()
                    )
                # Setting a super-long cache for a resized image (30 Days)
                cache.set(resized_url, 1, SIZEDIMAGEFIELD_CACHE_LENGTH)
        return resized_url

    def get_filename_key(self):
        return self.filename_key

    @staticmethod
    def get_resized_filename(filename, width, height, filename_key):
        """
        Returns the 'resized filename' (according to `width`, `height` and `filename_key`)
        in the following format:
        `filename`-`filename_key`-`width`x`height`.ext
        """
        try:
            image_name, ext = filename.rsplit('.', 1)
        except ValueError:
            image_name = filename
            ext = 'jpg'
        return "%(image_name)s-%(filename_key)s-%(width)dx%(height)d.%(ext)s" % ({
            'image_name':image_name,
            'filename_key':filename_key,
            'width':width,
            'height':height,
            'ext':ext
        })

    def get_resized_path(self, path_to_image, width, height, filename_key, for_url=False):
        """
        Returns the 'resized' path of `path_to_image`
        """
        if not path_to_image:
            filename = SIZEDIMAGEFIELD_PLACEHOLDER_FILENAME
            containing_folder = 'GLOBAL-PLACEHOLDER'
        else:
            containing_folder, filename = os.path.split(path_to_image)

        resized_filename = self.get_resized_filename(filename, width, height, filename_key)

        joined_path = os.path.join(*[
            SIZEDIMAGEFIELD_DIRECTORY_NAME,
            containing_folder,
            resized_filename
        ])

        if for_url:
            path_to_return = self.storage.url(joined_path)
        else:
            path_to_return = joined_path
        return path_to_return

    def process_image(self, image, return_image=False):
        raise NotImplementedError('Subclasses MUST provide a `process_image` method.')

    def create_resized_image(self, path_to_image, width, height, filename_key):
        """
        Creates a resized image.
        `path_to_image`: The path to the image with the media directory to resize.
            If `None`, the SIZEDIMAGEFIELD_PLACEHOLDER_IMAGE will be used.
        `width`: Width of resized image (int)
        `height`: Desired height of resized image (int)
        `filename_key`: A string that will be used in the sized image filename to signify
        what operation was done to it. Examples: 'crop' or 'scale'
        """
        resized_image_path = self.get_resized_path(
            path_to_image=path_to_image,
            width=width,
            height=height,
            filename_key=filename_key
{'fraction_non_alphanumeric': 0.07828964556300619, 'fraction_numerical': 0.0022642166681180876, 'mean_word_length': 2.2773972602739727, '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': '5695818', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3305, 'n_words': 762}