1"""MIME-Type Parser
2
3This module provides basic functions for handling mime-types. It can handle
4matching mime-types against a list of media-ranges. See section 14.1 of
5the HTTP specification [RFC 2616] for a complete explanation.
6
7   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
8
9Based on mimeparse 0.1.2 by Joe Gregorio:
10
11    http://code.google.com/p/mimeparse/
12
13Contents:
14    - parse_mime_type():   Parses a mime-type into its component parts.
15    - parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q' quality parameter.
16    - quality():           Determines the quality ('q') of a mime-type when compared against a list of media-ranges.
17    - quality_parsed():    Just like quality() except the second parameter must be pre-parsed.
18    - best_match():        Choose the mime-type with the highest quality ('q') from a list of candidates.
19    - desired_matches():   Filter against a list of desired mime-types in the order the server prefers.
20
21"""
22
23
24def parse_mime_type(mime_type):
25    """Carves up a mime-type and returns a tuple of the
26       (type, subtype, params) where 'params' is a dictionary
27       of all the parameters for the media range.
28       For example, the media range 'application/xhtml;q=0.5' would
29       get parsed into:
30
31       ('application', 'xhtml', {'q', '0.5'})
32       """
33    type = mime_type.split(';')
34    type, plist = type[0], type[1:]
35    try:
36        type, subtype = type.split('/', 1)
37    except ValueError:
38        type, subtype = type.strip() or '*', '*'
39    else:
40        type = type.strip() or '*'
41        subtype = subtype.strip() or '*'
42    params = {}
43    for param in plist:
44        param = param.split('=', 1)
45        if len(param) == 2:
46            key, value = param[0].strip(), param[1].strip()
47            if key and value:
48                params[key] = value
49    return type, subtype, params
50
51def parse_media_range(range):
52    """Carves up a media range and returns a tuple of the
53       (type, subtype, params) where 'params' is a dictionary
54       of all the parameters for the media range.
55       For example, the media range 'application/*;q=0.5' would
56       get parsed into:
57
58       ('application', '*', {'q', '0.5'})
59
60       In addition this function also guarantees that there
61       is a value for 'q' in the params dictionary, filling it
62       in with a proper default if necessary.
63       """
64    type, subtype, params = parse_mime_type(range)
65    try:
66        if not 0 <= float(params['q']) <= 1:
67            raise ValueError
68    except (KeyError, ValueError):
69        params['q'] = '1'
70    return type, subtype, params
71
72def fitness_and_quality_parsed(mime_type, parsed_ranges):
73    """Find the best match for a given mime-type against
74       a list of media_ranges that have already been
75       parsed by parse_media_range(). Returns a tuple of
76       the fitness value and the value of the 'q' quality
77       parameter of the best match, or (-1, 0) if no match
78       was found. Just as for quality_parsed(), 'parsed_ranges'
79       must be a list of parsed media ranges."""
80    best_fitness, best_fit_q = -1, 0
81    target_type, target_subtype, target_params = parse_media_range(mime_type)
82    for type, subtype, params in parsed_ranges:
83        if (type == target_type
84                    or type == '*' or target_type == '*') and (
85                subtype == target_subtype
86                    or subtype == '*' or target_subtype == '*'):
87            fitness = 0
88            if type == target_type:
89                fitness += 100
90            if subtype == target_subtype:
91                fitness += 10
92            for key in target_params:
93                if key != 'q' and key in params:
94                    if params[key] == target_params[key]:
95                        fitness += 1
96            if fitness > best_fitness:
97                best_fitness = fitness
98                best_fit_q = params['q']
99    return best_fitness, float(best_fit_q)
100
101def quality_parsed(mime_type, parsed_ranges):
102    """Find the best match for a given mime-type against
103    a list of media_ranges that have already been
104    parsed by parse_media_range(). Returns the
105    'q' quality parameter of the best match, 0 if no
106    match was found. This function behaves the same as quality()
107    except that 'parsed_ranges' must be a list of
108    parsed media ranges."""
109    return fitness_and_quality_parsed(mime_type, parsed_ranges)[1]
110
111def quality(mime_type, ranges):
112    """Returns the quality 'q' of a mime-type when compared
113    against the media-ranges in ranges. For example:
114
115    >>> quality('text/html','text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5')
116    0.7
117
118    """
119    parsed_ranges = map(parse_media_range, ranges.split(','))
120    return quality_parsed(mime_type, parsed_ranges)
121
122def best_match(supported, header):
123    """Takes a list of supported mime-types and finds the best
124    match for all the media-ranges listed in header. In case of
125    ambiguity, whatever comes first in the list will be chosen.
126    The value of header must be a string that conforms to the format
127    of the HTTP Accept: header. The value of 'supported' is a list
128    of mime-types.
129
130    >>> best_match(['application/xbel+xml', 'text/xml'], 'text/*;q=0.5,*/*; q=0.1')
131    'text/xml'
132    """
133    if not supported:
134        return ''
135    parsed_header = list(map(parse_media_range, header.split(',')))
136    best_type = max([
137            (fitness_and_quality_parsed(mime_type, parsed_header), -n)
138            for n, mime_type in enumerate(supported)])
139    return best_type[0][1] and supported[-best_type[1]] or ''
140
141def desired_matches(desired, header):
142    """Takes a list of desired mime-types in the order the server prefers to
143    send them regardless of the browsers preference.
144
145    Browsers (such as Firefox) technically want XML over HTML depending on how
146    one reads the specification. This function is provided for a server to
147    declare a set of desired mime-types it supports, and returns a subset of
148    the desired list in the same order should each one be Accepted by the
149    browser.
150
151    >>> desired_matches(['text/html', 'application/xml'], \
152    ...     'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png')
153    ['text/html', 'application/xml']
154    >>> desired_matches(['text/html', 'application/xml'], 'application/xml,application/json')
155    ['application/xml']
156    """
157    parsed_ranges = list(map(parse_media_range, header.split(',')))
158    return [mimetype for mimetype in desired
159        if quality_parsed(mimetype, parsed_ranges)]
160
161