<commit_before>from cStringIO import StringIO
from gzip import GzipFile

from django.core.files.base import File
from django.contrib.staticfiles.storage import CachedFilesMixin

from storages.backends.s3boto import S3BotoStorage

from require.storage import OptimizedFilesMixin


class GzipFixS3BotoStorageMixin(object):
    
    def _open(self, name, mode="rb"):
        file = super(GzipFixS3BotoStorageMixin, self)._open(name, mode)
        if self.gzip:
            file = GzipFile(mode=mode, compresslevel=9, fileobj=file)
            file_size = len(file.read())
            file.seek(0)
            file = File(file, name=name)
            file.size = file_size
        return file
    
    # Patch for bug in django-storages with compressed content.
    def _compress_content(self, content):
        """Gzip a given string."""<commit_msg>Fixing hash generation of gzipped S3 static files.<commit_after>from cStringIO import StringIO
from gzip import GzipFile

from django.core.files.base import File
from django.contrib.staticfiles.storage import CachedFilesMixin

from storages.backends.s3boto import S3BotoStorage

from require.storage import OptimizedFilesMixin


class GzipFixS3BotoStorageMixin(object):
    
    # Decompresses previously compressed content, essential for correct hash calculation.
    def _open(self, name, mode="rb"):
        """Opens the given file."""
        file = super(GzipFixS3BotoStorageMixin, self)._open(name, mode)
        if file.key.content_encoding == "gzip":
            file = GzipFile(mode=mode, compresslevel=9, fileobj=file)
            file_size = len(file.read())
            file.seek(0)
            file = File(file, name=name)
            file.size = file_size
        return file
    
    # Patch for bug in django-storages with compressed content.
    def _compress_content(self, content):
        """Gzip a given string."""
{'fraction_non_alphanumeric': 0.08399787347155768, 'fraction_numerical': 0.006911217437533227, 'mean_word_length': 3.163716814159292, 'pattern_counts': {'":': 1, '<': 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': '617417', 'n_tokens_mistral': 559, 'n_tokens_neox': 553, 'n_words': 150}