<commit_before>from schemamaker.fields import BaseFieldSchema, BaseField
from schemamaker.schema_specifications import default_schema_specification as registry
from schemamaker.utils import prep_for_kwargs

import dockit

import properties

IMAGE_FORMATS = [('JPEG', '.jpg'),
                 ('PNG', '.png'),]

CROP_CHOICES = [('smart', 'Smart'),
                ('scale', 'Scale'),]

class ThumbnailFieldEntrySchema(dockit.Schema):
    format = dockit.CharField(blank=True, null=True, choices=IMAGE_FORMATS)
    quality = dockit.IntegerField(blank=True, null=True)
    width = dockit.IntegerField(blank=True, null=True)
    height = dockit.IntegerField(blank=True, null=True)
    upscale = dockit.BooleanField(default=False, help_text='Upsize the image if it doesn\'t match the width and height')
    crop = dockit.CharField(blank=True, null=True, choices=CROP_CHOICES)
    autocrop = dockit.BooleanField(default=False, help_text='Remove white space from around the image')

class ThumbnailFieldSchema(BaseFieldSchema):
    thumbnails = dockit.ListField(dockit.SchemaField(ThumbnailFieldEntrySchema))

class ThumbnailField(BaseField):
    schema = ThumbnailFieldSchema
    field = properties.ThumbnailField
    
    def create_field(self, data):
        kwargs = prep_for_kwargs(data)
        kwargs.pop('field_type', None)
        kwargs.pop('name', None)
        if kwargs.get('verbose_name', None) == '':
            del kwargs['verbose_name']
        thumbnails = kwargs.pop('thumbnails', [])
        for thumb in thumbnails:
            resize = {}
            for key in ['width', 'height', 'crop', 'upscale']:
                if key in thumb:
                    resize[key] = thumb.pop(key)
            if resize:
                thumb['resize'] = resize
        kwargs['config'] = {'thumbnails':thumbnails}
        return self.field(**kwargs)

registry.register_field('ThumbnailField', ThumbnailField)
<commit_msg>more work on thumbnails contrib<commit_after>from schemamaker.fields import BaseFieldSchema, BaseField
from schemamaker.schema_specifications import default_schema_specification as registry
from schemamaker.utils import prep_for_kwargs

import dockit

import properties

IMAGE_FORMATS = [('JPEG', '.jpg'),
                 ('PNG', '.png'),]

CROP_CHOICES = [('smart', 'Smart'),
                ('scale', 'Scale'),]

class ThumbnailFieldEntrySchema(dockit.Schema):
    key = dockit.SlugField()
    format = dockit.CharField(blank=True, null=True, choices=IMAGE_FORMATS)
    quality = dockit.IntegerField(blank=True, null=True)
    width = dockit.IntegerField(blank=True, null=True)
    height = dockit.IntegerField(blank=True, null=True)
    upscale = dockit.BooleanField(default=False, help_text='Upsize the image if it doesn\'t match the width and height')
    crop = dockit.CharField(blank=True, null=True, choices=CROP_CHOICES)
    autocrop = dockit.BooleanField(default=False, help_text='Remove white space from around the image')
    
    def __unicode__(self):
        return self.key or repr(self)

class ThumbnailFieldSchema(BaseFieldSchema):
    thumbnails = dockit.ListField(dockit.SchemaField(ThumbnailFieldEntrySchema))

class ThumbnailField(BaseField):
    schema = ThumbnailFieldSchema
    field = properties.ThumbnailField
    
    def create_field(self, data):
        kwargs = prep_for_kwargs(data)
        kwargs.pop('field_type', None)
        kwargs.pop('name', None)
        if kwargs.get('verbose_name', None) == '':
            del kwargs['verbose_name']
        thumbnails = kwargs.pop('thumbnails', list())
        config = {'thumbnails': dict()}
        for thumb in thumbnails:
            key = thumb.pop('key')
            resize = {}
            for key in ['width', 'height', 'crop', 'upscale']:
                if key in thumb:
                    resize[key] = thumb.pop(key)
            if resize:
                thumb['resize'] = resize
            config['thumbnails'][key] = thumb
        kwargs['config'] = config
        return self.field(**kwargs)

registry.register_field('ThumbnailField', ThumbnailField)

{'fraction_non_alphanumeric': 0.11490301988706113, 'fraction_numerical': 0.0, 'mean_word_length': 3.3900862068965516, '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': '4429360', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1195, 'n_words': 268}