<commit_before>import mimetypes
import posixpath
import urllib

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.contrib.staticfiles.views import serve as staticfiles_serve
from django.http import HttpResponse

from .asset_attributes import AssetAttributes
from .assets import Asset, StaticAsset
from .settings import environment


def build_asset(environment, path, absolute_path):
    asset_attributes = AssetAttributes(environment, path)
    if asset_attributes.get_processors():
        return Asset(asset_attributes, absolute_path)
    return StaticAsset(asset_attributes, absolute_path)


def serve(request, path, document_root=None, insecure=False, **kwargs):
    if not settings.DEBUG and not insecure:
        raise ImproperlyConfigured(
            "The gears view can only be used in debug mode or if the "
            "--insecure option of 'runserver' is used.")
    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
    if normalized_path in environment.public_assets:
        absolute_path = environment.find(normalized_path)
    else:
        absolute_path = None
    if not absolute_path:
        return staticfiles_serve(request, path, document_root=document_root,
                                 insecure=insecure, **kwargs)
    mimetype, encoding = mimetypes.guess_type(absolute_path)
    mimetype = mimetype or 'application/octet-stream'
    response = HttpResponse(
        build_asset(environment, normalized_path, absolute_path),
        mimetype=mimetype)
    if encoding:
        response['Content-Encoding'] = encoding
    return response
<commit_msg>Move some code from serve view to build_asset function<commit_after>import mimetypes
import posixpath
import urllib

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.contrib.staticfiles.views import serve as staticfiles_serve
from django.http import HttpResponse

from .asset_attributes import AssetAttributes
from .assets import Asset, StaticAsset
from .settings import environment


def build_asset(environment, path):
    if path not in environment.public_assets:
        return
    asset_attributes = AssetAttributes(environment, path)
    absolute_path = environment.find(path)
    if absolute_path:
        if asset_attributes.get_processors():
            return Asset(asset_attributes, absolute_path)
        return StaticAsset(asset_attributes, absolute_path)


def serve(request, path, **kwargs):
    if not settings.DEBUG and not kwargs.get('insecure'):
        raise ImproperlyConfigured(
            "The gears view can only be used in debug mode or if the "
            "--insecure option of 'runserver' is used.")
    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
    asset = build_asset(environment, normalized_path)
    if not asset:
        return staticfiles_serve(request, path, **kwargs)
    mimetype, encoding = mimetypes.guess_type(normalized_path)
    mimetype = mimetype or 'application/octet-stream'
    response = HttpResponse(asset, mimetype=mimetype)
    if encoding:
        response['Content-Encoding'] = encoding
    return response

{'fraction_non_alphanumeric': 0.0704666457876605, 'fraction_numerical': 0.0, 'mean_word_length': 3.967340590979782, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '567024', 'n_tokens_mistral': 859, 'n_tokens_neox': 838, 'n_words': 252}