<commit_before>""" ..
"""
import abc
import mimeparse


class Parser(object):
    #! Applicable mimetypes for this parser
    mimetypes = []

    @classmethod
    def can_parse(cls, content_type_header):
        """
        Determine if this parser can deserialize an appropriate message
        specified by the CONTENT-TYPE header.
        """
        return mimeparse.best_match(cls.mimetypes, content_type_header) != ''

    @abc.abstractmethod
    def parse(cls, request):
        """
        Constructs an object dictionary from the request body according to the
        media-type specified in the `Content-Type` header.

        @param[in] request
            Django request object containing the request body (at a minimum:
            body, POST, and FILES).

        @returns
            A dictionary containing the parameters of the request.
        """
        pass


class FormData(Parser):
    #! Applicable mimetypes for this parser
    mimetypes = [
        'multipart/form-data'
    ]

    @classmethod
    def parse(cls, request):
        # Build the initial object as a copy of the POST data
        obj = dict(request.POST)

        # Iterate through this absurd multi-value-dict and multiplex the
        # values into obj
        for name in request.FILES:
            if name not in obj:
                obj[name] = []

            obj[name].extend(request.FILES.getlist(name))

        # Now flatten those in that can be flattened
        for name, item in obj.items():
            if len(item) == 1:
                obj[name] = item[0]

        # Return the final object dictionary
        return obj


# TODO: Find a more fun way to keep track of parsers
parsers = [
    FormData
]


def get(request):
    if 'CONTENT_TYPE' not in request.META:
        # No accept header provided; we can do nothing as the default
        # is application/octet-stream.
        # TODO: Perhaps devise a way to accept this ?
        return None

    content_type = request.META['CONTENT_TYPE']
    for parser in parsers:
        if parser.can_parse(content_type):
            # Parser matched against the type header; return it
            return parser

    # Nothing can be matched; return nothing<commit_msg>Added application/x-www-form-urlencoded to list of formats that Form parser can handle<commit_after>""" ..
"""
import abc
import mimeparse


class Parser(object):
    #! Applicable mimetypes for this parser
    mimetypes = []

    @classmethod
    def can_parse(cls, content_type_header):
        """
        Determine if this parser can deserialize an appropriate message
        specified by the CONTENT-TYPE header.
        """
        return mimeparse.best_match(cls.mimetypes, content_type_header) != ''

    @abc.abstractmethod
    def parse(cls, request):
        """
        Constructs an object dictionary from the request body according to the
        media-type specified in the `Content-Type` header.

        @param[in] request
            Django request object containing the request body (at a minimum:
            body, POST, and FILES).

        @returns
            A dictionary containing the parameters of the request.
        """
        pass


class Form(Parser):
    #! Applicable mimetypes for this parser
    mimetypes = [
        'multipart/form-data',
        'application/x-www-form-urlencoded',
    ]

    @classmethod
    def parse(cls, request):
        # Build the initial object as a copy of the POST data
        obj = dict(request.POST)

        # Iterate through this absurd multi-value-dict and multiplex the
        # values into obj
        for name in request.FILES:
            if name not in obj:
                obj[name] = []

            obj[name].extend(request.FILES.getlist(name))

        # Now flatten those in that can be flattened
        for name, item in obj.items():
            if len(item) == 1:
                obj[name] = item[0]

        # Return the final object dictionary
        return obj


# TODO: Find a more fun way to keep track of parsers
parsers = [
    Form
]


def get(request):
    if 'CONTENT_TYPE' not in request.META:
        # No accept header provided; we can do nothing as the default
        # is application/octet-stream.
        # TODO: Perhaps devise a way to accept this ?
        return None

    content_type = request.META['CONTENT_TYPE']
    for parser in parsers:
        if parser.can_parse(content_type):
            # Parser matched against the type header; return it
            return parser

    # Nothing can be matched; return nothing
{'fraction_non_alphanumeric': 0.07511530858774434, 'fraction_numerical': 0.0008785416209092906, 'mean_word_length': 2.1406896551724137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '736601', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1195, 'n_words': 452}