ietfparse/headers.py
def parse_accept(header_value):
    """Parse an HTTP accept-like header.

    :param str header_value: the header value to parse
    :return: a :class:`list` of :class:`.ContentType` instances
        in decreasing quality order.  Each instance is augmented
        with the associated quality as a ``float`` property
        named ``quality``.

    ``Accept`` is a class of headers that contain a list of values
    and an associated preference value.  The ever present `Accept`_
    header is a perfect example.  It is a list of content types and
    an optional parameter named ``q`` that indicates the relative
    weight of a particular type.  The most basic example is::

        Accept: audio/*;q=0.2, audio/basic

    Which states that I prefer the ``audio/basic`` content type
    but will accept other ``audio`` sub-types with an 80% mark down.

    .. _Accept: http://tools.ietf.org/html/rfc7231#section-5.3.2

    """
    next_explicit_q = decimal.ExtendedContext.next_plus(decimal.Decimal('5.0'))
    headers = [parse_content_type(header)
               for header in parse_list(header_value)]
    for header in headers:
        q = header.parameters.pop('q', None)
        if q is None:
            q = '1.0'
        elif float(q) == 1.0:
            q = float(next_explicit_q)
            next_explicit_q = next_explicit_q.next_minus()
        header.quality = float(q)

    def ordering(left, right):
        """
        Method for sorting the header values

        :param mixed left:
        :param mixed right:
        :rtype: mixed

        """
        if left.quality != right.quality:
            return right.quality - left.quality
        if left == right:
            return 0
        if left > right:
            return -1
        return 1

    return sorted(headers, key=functools.cmp_to_key(ordering))
def parse_accept(header_value):
    """Parse an HTTP accept-like header.

    :param str header_value: the header value to parse
    :return: a :class:`list` of :class:`.ContentType` instances
        in decreasing quality order.  Each instance is augmented
        with the associated quality as a ``float`` property
        named ``quality``.

    ``Accept`` is a class of headers that contain a list of values
    and an associated preference value.  The ever present `Accept`_
    header is a perfect example.  It is a list of content types and
    an optional parameter named ``q`` that indicates the relative
    weight of a particular type.  The most basic example is::

        Accept: audio/*;q=0.2, audio/basic

    Which states that I prefer the ``audio/basic`` content type
    but will accept other ``audio`` sub-types with an 80% mark down.

    .. _Accept: http://tools.ietf.org/html/rfc7231#section-5.3.2

    """
    next_explicit_q = decimal.ExtendedContext.next_plus(decimal.Decimal('5.0'))
    headers = [parse_content_type(header)
               for header in parse_list(header_value)]
    for header in headers:
        q = header.parameters.pop('q', None)
        if q is None:
            q = '1.0'
        elif float(q) == 1.0:
            q = float(next_explicit_q)
            next_explicit_q = next_explicit_q.next_minus()
        header.quality = float(q)

    def ordering(left, right):
        """
        Method for sorting the header values

        :param mixed left:
        :param mixed right:
        :rtype: mixed

        """
        if left.quality != right.quality:
            return right.quality - left.quality
        if left == right:
            return 0
        if left > right:
            return -1
        return 1

    return sorted(headers, key=functools.cmp_to_key(ordering))
Parse an HTTP accept-like header.

    :param str header_value: the header value to parse
    :return: a :class:`list` of :class:`.ContentType` instances
        in decreasing quality order.  Each instance is augmented
        with the associated quality as a ``float`` property
        named ``quality``.

    ``Accept`` is a class of headers that contain a list of values
    and an associated preference value.  The ever present `Accept`_
    header is a perfect example.  It is a list of content types and
    an optional parameter named ``q`` that indicates the relative
    weight of a particular type.  The most basic example is::

        Accept: audio/*;q=0.2, audio/basic

    Which states that I prefer the ``audio/basic`` content type
    but will accept other ``audio`` sub-types with an 80% mark down.

    .. _Accept: http://tools.ietf.org/html/rfc7231#section-5.3.2