PageRenderTime 47ms CodeModel.GetById 30ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/mimeparse.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 184 lines | 174 code | 0 blank | 10 comment | 0 complexity | 0931fbc228b7a1a2e8c4ebd104c22ae2 MD5 | raw file
  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
  9Contents:
 10    - parse_mime_type():   Parses a mime-type into its component parts.
 11    - parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q' quality parameter.
 12    - quality():           Determines the quality ('q') of a mime-type when compared against a list of media-ranges.
 13    - quality_parsed():    Just like quality() except the second parameter must be pre-parsed.
 14    - best_match():        Choose the mime-type with the highest quality ('q') from a list of candidates.
 15"""
 16
 17__version__ = "0.1.2"
 18__author__ = 'Joe Gregorio'
 19__email__ = "joe@bitworking.org"
 20__credits__ = ""
 21
 22def parse_mime_type(mime_type):
 23    """Carves up a mime-type and returns a tuple of the
 24       (type, subtype, params) where 'params' is a dictionary
 25       of all the parameters for the media range.
 26       For example, the media range 'application/xhtml;q=0.5' would
 27       get parsed into:
 28
 29       ('application', 'xhtml', {'q', '0.5'})
 30       """
 31    parts = mime_type.split(";")
 32    params = dict([tuple([s.strip() for s in param.split("=")])\
 33            for param in parts[1:] ])
 34    full_type = parts[0].strip()
 35    # Java URLConnection class sends an Accept header that includes a single "*"
 36    # Turn it into a legal wildcard.
 37    if full_type == '*': full_type = '*/*'
 38    (type, subtype) = full_type.split("/")
 39    return (type.strip(), subtype.strip(), params)
 40
 41def parse_media_range(range):
 42    r"""
 43    Carves up a media range and returns a tuple of the
 44    (type, subtype, params) where 'params' is a dictionary
 45    of all the parameters for the media range.
 46    For example, the media range 'application/*;q=0.5' would
 47    get parsed into:
 48
 49    .. raw:: text
 50
 51        ('application', '*', {'q', '0.5'})
 52
 53    In addition this function also guarantees that there
 54    is a value for 'q' in the params dictionary, filling it
 55    in with a proper default if necessary.
 56    """
 57    (type, subtype, params) = parse_mime_type(range)
 58    if not params.has_key('q') or not params['q'] or \
 59            not float(params['q']) or float(params['q']) > 1\
 60            or float(params['q']) < 0:
 61        params['q'] = '1'
 62    return (type, subtype, params)
 63
 64def fitness_and_quality_parsed(mime_type, parsed_ranges):
 65    """Find the best match for a given mime-type against
 66       a list of media_ranges that have already been
 67       parsed by parse_media_range(). Returns a tuple of
 68       the fitness value and the value of the 'q' quality
 69       parameter of the best match, or (-1, 0) if no match
 70       was found. Just as for quality_parsed(), 'parsed_ranges'
 71       must be a list of parsed media ranges. """
 72    best_fitness = -1
 73    best_fit_q = 0
 74    (target_type, target_subtype, target_params) =\
 75            parse_media_range(mime_type)
 76    for (type, subtype, params) in parsed_ranges:
 77        if (type == target_type or type == '*' or target_type == '*') and \
 78                (subtype == target_subtype or subtype == '*' or target_subtype == '*'):
 79            param_matches = reduce(lambda x, y: x+y, [1 for (key, value) in \
 80                    target_params.iteritems() if key != 'q' and \
 81                    params.has_key(key) and value == params[key]], 0)
 82            fitness = (type == target_type) and 100 or 0
 83            fitness += (subtype == target_subtype) and 10 or 0
 84            fitness += param_matches
 85            if fitness > best_fitness:
 86                best_fitness = fitness
 87                best_fit_q = params['q']
 88
 89    return best_fitness, float(best_fit_q)
 90
 91def quality_parsed(mime_type, parsed_ranges):
 92    """Find the best match for a given mime-type against
 93    a list of media_ranges that have already been
 94    parsed by parse_media_range(). Returns the
 95    'q' quality parameter of the best match, 0 if no
 96    match was found. This function bahaves the same as quality()
 97    except that 'parsed_ranges' must be a list of
 98    parsed media ranges. """
 99    return fitness_and_quality_parsed(mime_type, parsed_ranges)[1]
100
101def quality(mime_type, ranges):
102    """Returns the quality 'q' of a mime-type when compared
103    against the media-ranges in ranges. For example:
104
105    >>> 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')
106    0.7
107
108    """
109    parsed_ranges = [parse_media_range(r) for r in ranges.split(",")]
110    return quality_parsed(mime_type, parsed_ranges)
111
112def best_match(supported, header):
113    """Takes a list of supported mime-types and finds the best
114    match for all the media-ranges listed in header. The value of
115    header must be a string that conforms to the format of the
116    HTTP Accept: header. The value of 'supported' is a list of
117    mime-types.
118
119    >>> best_match(['application/xbel+xml', 'text/xml'], 'text/*;q=0.5,*/*; q=0.1')
120    'text/xml'
121    """
122    parsed_header = [parse_media_range(r) for r in header.split(",")]
123    weighted_matches = [(fitness_and_quality_parsed(mime_type, parsed_header), mime_type)\
124            for mime_type in supported]
125    weighted_matches.sort()
126    return weighted_matches[-1][0][1] and weighted_matches[-1][1] or ''
127
128if __name__ == "__main__":
129    import unittest
130
131    class TestMimeParsing(unittest.TestCase):
132
133        def test_parse_media_range(self):
134            self.assert_(('application', 'xml', {'q': '1'}) == parse_media_range('application/xml;q=1'))
135            self.assertEqual(('application', 'xml', {'q': '1'}), parse_media_range('application/xml'))
136            self.assertEqual(('application', 'xml', {'q': '1'}), parse_media_range('application/xml;q='))
137            self.assertEqual(('application', 'xml', {'q': '1'}), parse_media_range('application/xml ; q='))
138            self.assertEqual(('application', 'xml', {'q': '1', 'b': 'other'}), parse_media_range('application/xml ; q=1;b=other'))
139            self.assertEqual(('application', 'xml', {'q': '1', 'b': 'other'}), parse_media_range('application/xml ; q=2;b=other'))
140            # Java URLConnection class sends an Accept header that includes a single *
141            self.assertEqual(('*', '*', {'q': '.2'}), parse_media_range(" *; q=.2"))
142
143        def test_rfc_2616_example(self):
144            accept = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"
145            self.assertEqual(1, quality("text/html;level=1", accept))
146            self.assertEqual(0.7, quality("text/html", accept))
147            self.assertEqual(0.3, quality("text/plain", accept))
148            self.assertEqual(0.5, quality("image/jpeg", accept))
149            self.assertEqual(0.4, quality("text/html;level=2", accept))
150            self.assertEqual(0.7, quality("text/html;level=3", accept))
151
152        def test_best_match(self):
153            mime_types_supported = ['application/xbel+xml', 'application/xml']
154            # direct match
155            self.assertEqual(best_match(mime_types_supported, 'application/xbel+xml'), 'application/xbel+xml')
156            # direct match with a q parameter
157            self.assertEqual(best_match(mime_types_supported, 'application/xbel+xml; q=1'), 'application/xbel+xml')
158            # direct match of our second choice with a q parameter
159            self.assertEqual(best_match(mime_types_supported, 'application/xml; q=1'), 'application/xml')
160            # match using a subtype wildcard
161            self.assertEqual(best_match(mime_types_supported, 'application/*; q=1'), 'application/xml')
162            # match using a type wildcard
163            self.assertEqual(best_match(mime_types_supported, '*/*'), 'application/xml')
164
165            mime_types_supported = ['application/xbel+xml', 'text/xml']
166            # match using a type versus a lower weighted subtype
167            self.assertEqual(best_match(mime_types_supported, 'text/*;q=0.5,*/*; q=0.1'), 'text/xml')
168            # fail to match anything
169            self.assertEqual(best_match(mime_types_supported, 'text/html,application/atom+xml; q=0.9'), '')
170
171            # common AJAX scenario
172            mime_types_supported = ['application/json', 'text/html']
173            self.assertEqual(best_match(mime_types_supported, 'application/json, text/javascript, */*'), 'application/json')
174            # verify fitness ordering
175            self.assertEqual(best_match(mime_types_supported, 'application/json, text/html;q=0.9'), 'application/json')
176
177        def test_support_wildcards(self):
178            mime_types_supported = ['image/*', 'application/xml']
179            # match using a type wildcard
180            self.assertEqual(best_match(mime_types_supported, 'image/png'), 'image/*')
181            # match using a wildcard for both requested and supported
182            self.assertEqual(best_match(mime_types_supported, 'image/*'), 'image/*')
183
184    unittest.main()