<commit_before># -*- coding: utf-8 -*-
"""Implements the decoder protocol for `multipart/form-data`.
"""
from __future__ import print_function, unicode_literals
from __future__ import absolute_import, division
from StringIO import StringIO
from django.http import MultiPartParser
from .. import transcoders
from . import Decoder
from django.core.files.uploadhandler import load_handler


class Decoder(transcoders.Form, Decoder):

    def decode(self, request, fields=None):
        # Instantiate an instance of django's multi-part parser
        stream = StringIO(request.body)
        handlers =[ load_handler('django.core.files.uploadhandler.TemporaryFileUploadHandler', request) ]
        parser = MultiPartParser(request.META, stream, handlers, request.encoding)

        # Parse the form-data as data, files
        data, files = parser.parse()

        data = dict(data)

        #flatten the data dictionary if possible
        for key in data:
            if (len(data[key]) == 1):
                data[key] = data[key][0]

        data.update(dict(files))
        return data

"""        # Append values in files into the data dictionary
        obj = dict(data)
        for name in files:
            if name not in obj:
                obj[name] = []

            # This is absurd. Why can't we just do `files[name]` here?
            obj[name].extend(files.getlist([name]))

        # Normalize and flatten this as much as we can.
        for name in obj:
            if fields is None or name not in fields:
                # Field not declared; don't bother with it
                continue

            if fields[name].collection and len(obj[name]) == 1:
                # Field is really meant to be scalar
                obj[name] = obj[name][0]

        # Return the constructed object
        return obj """
<commit_msg>Touched up form decoder.<commit_after># -*- coding: utf-8 -*-
"""Implements the decoder protocol for `multipart/form-data`.
"""
from __future__ import print_function, unicode_literals
from __future__ import absolute_import, division
from StringIO import StringIO
from django.http import MultiPartParser
from .. import transcoders
from . import Decoder
from django.core.files.uploadhandler import load_handler


class Decoder(transcoders.Form, Decoder):

    def decode(self, request, fields=None):
        # Instantiate an instance of django's multi-part parser
        stream = StringIO(request.body)
        parser = MultiPartParser(request.META, stream, request.upload_handlers)

        # Parse the form-data as data, files
        data, files = parser.parse()

        # Flatten the data dictionary if possible.
        obj = dict(data)
        for key in obj:
            if fields and key in fields and fields[key].collection:
                # Field declared to be an array; don't bother.
                pass

            if len(obj[key]) == 1:
                # Field probably supposed to be scalar.
                obj[key] = obj[key][0]

        # Merge files dictionary with the data dictionary.
        obj.update(dict(files))

        # Return the decoded object.
        return obj

{'fraction_non_alphanumeric': 0.08601465434851864, 'fraction_numerical': 0.002548582351067219, 'mean_word_length': 2.390928725701944, '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': '634128', 'n_tokens_mistral': 851, 'n_tokens_neox': 806, 'n_words': 294}