<commit_before>from django.conf import settings
from django.db.models.fields.files import ImageFieldFile
from hashlib import md5
import pickle
from ..generatedfiles.backends import get_default_generatedfile_backend
from ..generatedfiles.strategies import StrategyWrapper
from ..processors import ProcessorPipeline
from ..utils import open_image, img_to_fobj, get_by_qname
from ..registry import generator_registry, register


class BaseImageSpec(object):
    """
    An object that defines how an new image should be generated from a source
    image.

    """

    generatedfile_storage = None
    """A Django storage system to use to save a generated file."""

    generatedfile_backend = None
    """
    An object responsible for managing the state of generated files. Defaults to
    an instance of ``IMAGEKIT_DEFAULT_GENERATEDFILE_BACKEND``

    """

    generatedfile_strategy = settings.IMAGEKIT_DEFAULT_GENERATEDFILE_STRATEGY
    """
    A dictionary containing callbacks that allow you to customize how and when
    the image file is created. Defaults to
    ``IMAGEKIT_DEFAULT_GENERATEDFILE_STRATEGY``.

    """

    def __init__(self):
        self.generatedfile_backend = self.generatedfile_backend or get_default_generatedfile_backend()
        self.generatedfile_strategy = StrategyWrapper(self.generatedfile_strategy)

    def generate(self):
        raise NotImplementedError


class ImageSpec(BaseImageSpec):
    """
    An object that defines how to generate a new image from a source file using
    PIL-based processors. (See :mod:`imagekit.processors`)

    """

    processors = None
    """A list of processors to run on the original image."""

    format = None
    """
    The format of the output file. If not provided, ImageSpecField will try to
    guess the appropriate format based on the extension of the filename and the
    format of the input image.

    """

    options = None
    """
    A dictionary that will be passed to PIL's ``Image.save()`` method as keyword
    arguments. Valid options vary between formats, but some examples include
    ``quality``, ``optimize``, and ``progressive`` for JPEGs. See the PIL
    documentation for others.

    """

    autoconvert = True
    """
    Specifies whether automatic conversion using ``prepare_image()`` should be
    performed prior to saving.

    """

    def __init__(self, source):
        self.source = source
        self.processors = self.processors or []
        super(ImageSpec, self).__init__()

    @property
    def generatedfile_name(self):
        fn = get_by_qname(settings.IMAGEKIT_SPEC_GENERATEDFILE_NAMER, 'namer')
        return fn(self)

    def __getstate__(self):
        state = self.__dict__

        # Unpickled ImageFieldFiles won't work (they're missing a storage
        # object). Since they're such a common use case, we special case them.
        if isinstance(self.source, ImageFieldFile):
            field = getattr(self.source, 'field')
            state['_field_data'] = {
                'instance': getattr(self.source, 'instance', None),
                'attname': getattr(field, 'name', None),
            }
        return state
<commit_msg>Don't assign processors, so properties will work<commit_after>from django.conf import settings
from django.db.models.fields.files import ImageFieldFile
from hashlib import md5
import pickle
from ..generatedfiles.backends import get_default_generatedfile_backend
from ..generatedfiles.strategies import StrategyWrapper
from ..processors import ProcessorPipeline
from ..utils import open_image, img_to_fobj, get_by_qname
from ..registry import generator_registry, register


class BaseImageSpec(object):
    """
    An object that defines how an new image should be generated from a source
    image.

    """

    generatedfile_storage = None
    """A Django storage system to use to save a generated file."""

    generatedfile_backend = None
    """
    An object responsible for managing the state of generated files. Defaults to
    an instance of ``IMAGEKIT_DEFAULT_GENERATEDFILE_BACKEND``

    """

    generatedfile_strategy = settings.IMAGEKIT_DEFAULT_GENERATEDFILE_STRATEGY
    """
    A dictionary containing callbacks that allow you to customize how and when
    the image file is created. Defaults to
    ``IMAGEKIT_DEFAULT_GENERATEDFILE_STRATEGY``.

    """

    def __init__(self):
        self.generatedfile_backend = self.generatedfile_backend or get_default_generatedfile_backend()
        self.generatedfile_strategy = StrategyWrapper(self.generatedfile_strategy)

    def generate(self):
        raise NotImplementedError


class ImageSpec(BaseImageSpec):
    """
    An object that defines how to generate a new image from a source file using
    PIL-based processors. (See :mod:`imagekit.processors`)

    """

    processors = []
    """A list of processors to run on the original image."""

    format = None
    """
    The format of the output file. If not provided, ImageSpecField will try to
    guess the appropriate format based on the extension of the filename and the
    format of the input image.

    """

    options = None
    """
    A dictionary that will be passed to PIL's ``Image.save()`` method as keyword
    arguments. Valid options vary between formats, but some examples include
    ``quality``, ``optimize``, and ``progressive`` for JPEGs. See the PIL
    documentation for others.

    """

    autoconvert = True
    """
    Specifies whether automatic conversion using ``prepare_image()`` should be
    performed prior to saving.

    """

    def __init__(self, source):
        self.source = source
        super(ImageSpec, self).__init__()

    @property
    def generatedfile_name(self):
        fn = get_by_qname(settings.IMAGEKIT_SPEC_GENERATEDFILE_NAMER, 'namer')
        return fn(self)

    def __getstate__(self):
        state = self.__dict__

        # Unpickled ImageFieldFiles won't work (they're missing a storage
        # object). Since they're such a common use case, we special case them.
        if isinstance(self.source, ImageFieldFile):
            field = getattr(self.source, 'field')
            state['_field_data'] = {
                'instance': getattr(self.source, 'instance', None),
                'attname': getattr(field, 'name', None),
            }
        return state

{'fraction_non_alphanumeric': 0.07918444760550024, 'fraction_numerical': 0.000316105579263474, 'mean_word_length': 3.179656538969617, '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': '2197341', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1645, 'n_words': 590}