<commit_before>from cStringIO import StringIO
from PIL import Image
import urllib2 as urllib

from django.conf import settings
from django.core.files.storage import get_storage_class

from utils.viewer_tools import get_path


def get_dimensions((width, height), longest_side):
    try:
        if width > height:
            width = longest_side
            height = (height/width) * longest_side
        elif height >width:
            height = longest_side
            width = (width/height) * longest_side
        else:
            height = longest_side
            width = longest_side
        return (width, height)
    except ZeroDivisionError:
        return (longest_side, longest_side)


def _save_thumbnails(image, path, size, suffix, filename=None):
    # If filename is present, resize on s3 fs
    if filename:
        default_storage = get_storage_class()()
        fs = get_storage_class('django.core.files.storage.FileSystemStorage')()
        image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS)
        image.save(get_path(path, suffix))

        default_storage.save(get_path(filename, suffix),
                                fs.open(get_path(path, suffix)))
    else:
        image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS)
        image.save(get_path(path, suffix))


def resize(filename):
    default_storage = get_storage_class()()
    path = default_storage.url(filename)
    img_file = urllib.urlopen(path)
    im = StringIO(img_file.read())
    img_file.close()
    image = Image.open(im)
    conf = settings.THUMB_CONF

    fs = get_storage_class('django.core.files.storage.FileSystemStorage')()
    loc_path = fs.path(filename)

    [_save_thumbnails(image, loc_path, conf[key]['size'], conf[key]['suffix'],
                                    filename=filename) for key in conf.keys()]
<commit_msg>Added dummy files to write thumbnails to.<commit_after>from cStringIO import StringIO
from PIL import Image
import urllib2 as urllib

from django.conf import settings
from django.core.files.storage import get_storage_class

from utils.viewer_tools import get_path


def get_dimensions((width, height), longest_side):
    try:
        if width > height:
            width = longest_side
            height = (height/width) * longest_side
        elif height >width:
            height = longest_side
            width = (width/height) * longest_side
        else:
            height = longest_side
            width = longest_side
        return (width, height)
    except ZeroDivisionError:
        return (longest_side, longest_side)


def _save_thumbnails(image, path, size, suffix, filename=None):
    # If filename is present, resize on s3 fs
    if filename:
        default_storage = get_storage_class()()
        fs = get_storage_class('django.core.files.storage.FileSystemStorage')()
        image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS)
        image.save(get_path(path, suffix))

        default_storage.save(get_path(filename, suffix),
                                fs.open(get_path(path, suffix)))
    else:
        image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS)
        image.save(get_path(path, suffix))


def resize(filename):
    default_storage = get_storage_class()()
    path = default_storage.url(filename)
    img_file = urllib.urlopen(path)
    im = StringIO(img_file.read())
    img_file.close()
    image = Image.open(im)
    conf = settings.THUMB_CONF

    fs = get_storage_class('django.core.files.storage.FileSystemStorage')()
    loc_path = fs.path('dummy.jpg')

    [_save_thumbnails(image, loc_path, conf[key]['size'], conf[key]['suffix'],
                                    filename=filename) for key in conf.keys()]

{'fraction_non_alphanumeric': 0.09858779642952305, 'fraction_numerical': 0.0010658140154543032, 'mean_word_length': 2.866117404737384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7517767', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1100, 'n_words': 248}