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