OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 Joe Gregorio |
| 2 # |
| 3 # Licensed under the MIT License |
| 4 |
| 5 """MIME-Type Parser |
| 6 |
| 7 This module provides basic functions for handling mime-types. It can handle |
| 8 matching mime-types against a list of media-ranges. See section 14.1 of the |
| 9 HTTP specification [RFC 2616] for a complete explanation. |
| 10 |
| 11 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 |
| 12 |
| 13 Contents: |
| 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' |
| 16 quality parameter. |
| 17 - quality(): Determines the quality ('q') of a mime-type when |
| 18 compared against a list of media-ranges. |
| 19 - quality_parsed(): Just like quality() except the second parameter must be |
| 20 pre-parsed. |
| 21 - best_match(): Choose the mime-type with the highest quality ('q') |
| 22 from a list of candidates. |
| 23 """ |
| 24 |
| 25 __version__ = '0.1.3' |
| 26 __author__ = 'Joe Gregorio' |
| 27 __email__ = 'joe@bitworking.org' |
| 28 __license__ = 'MIT License' |
| 29 __credits__ = '' |
| 30 |
| 31 |
| 32 def parse_mime_type(mime_type): |
| 33 """Parses a mime-type into its component parts. |
| 34 |
| 35 Carves up a mime-type and returns a tuple of the (type, subtype, params) |
| 36 where 'params' is a dictionary of all the parameters for the media range. |
| 37 For example, the media range 'application/xhtml;q=0.5' would get parsed |
| 38 into: |
| 39 |
| 40 ('application', 'xhtml', {'q', '0.5'}) |
| 41 """ |
| 42 parts = mime_type.split(';') |
| 43 params = dict([tuple([s.strip() for s in param.split('=', 1)])\ |
| 44 for param in parts[1:] |
| 45 ]) |
| 46 full_type = parts[0].strip() |
| 47 # Java URLConnection class sends an Accept header that includes a |
| 48 # single '*'. Turn it into a legal wildcard. |
| 49 if full_type == '*': |
| 50 full_type = '*/*' |
| 51 (type, subtype) = full_type.split('/') |
| 52 |
| 53 return (type.strip(), subtype.strip(), params) |
| 54 |
| 55 |
| 56 def parse_media_range(range): |
| 57 """Parse a media-range into its component parts. |
| 58 |
| 59 Carves up a media range and returns a tuple of the (type, subtype, |
| 60 params) where 'params' is a dictionary of all the parameters for the media |
| 61 range. For example, the media range 'application/*;q=0.5' would get parsed |
| 62 into: |
| 63 |
| 64 ('application', '*', {'q', '0.5'}) |
| 65 |
| 66 In addition this function also guarantees that there is a value for 'q' |
| 67 in the params dictionary, filling it in with a proper default if |
| 68 necessary. |
| 69 """ |
| 70 (type, subtype, params) = parse_mime_type(range) |
| 71 if not params.has_key('q') or not params['q'] or \ |
| 72 not float(params['q']) or float(params['q']) > 1\ |
| 73 or float(params['q']) < 0: |
| 74 params['q'] = '1' |
| 75 |
| 76 return (type, subtype, params) |
| 77 |
| 78 |
| 79 def fitness_and_quality_parsed(mime_type, parsed_ranges): |
| 80 """Find the best match for a mime-type amongst parsed media-ranges. |
| 81 |
| 82 Find the best match for a given mime-type against a list of media_ranges |
| 83 that have already been parsed by parse_media_range(). Returns a tuple of |
| 84 the fitness value and the value of the 'q' quality parameter of the best |
| 85 match, or (-1, 0) if no match was found. Just as for quality_parsed(), |
| 86 'parsed_ranges' must be a list of parsed media ranges. |
| 87 """ |
| 88 best_fitness = -1 |
| 89 best_fit_q = 0 |
| 90 (target_type, target_subtype, target_params) =\ |
| 91 parse_media_range(mime_type) |
| 92 for (type, subtype, params) in parsed_ranges: |
| 93 type_match = (type == target_type or\ |
| 94 type == '*' or\ |
| 95 target_type == '*') |
| 96 subtype_match = (subtype == target_subtype or\ |
| 97 subtype == '*' or\ |
| 98 target_subtype == '*') |
| 99 if type_match and subtype_match: |
| 100 param_matches = reduce(lambda x, y: x + y, [1 for (key, value) in \ |
| 101 target_params.iteritems() if key != 'q' and \ |
| 102 params.has_key(key) and value == params[key]], 0) |
| 103 fitness = (type == target_type) and 100 or 0 |
| 104 fitness += (subtype == target_subtype) and 10 or 0 |
| 105 fitness += param_matches |
| 106 if fitness > best_fitness: |
| 107 best_fitness = fitness |
| 108 best_fit_q = params['q'] |
| 109 |
| 110 return best_fitness, float(best_fit_q) |
| 111 |
| 112 |
| 113 def quality_parsed(mime_type, parsed_ranges): |
| 114 """Find the best match for a mime-type amongst parsed media-ranges. |
| 115 |
| 116 Find the best match for a given mime-type against a list of media_ranges |
| 117 that have already been parsed by parse_media_range(). Returns the 'q' |
| 118 quality parameter of the best match, 0 if no match was found. This function |
| 119 bahaves the same as quality() except that 'parsed_ranges' must be a list of |
| 120 parsed media ranges. |
| 121 """ |
| 122 |
| 123 return fitness_and_quality_parsed(mime_type, parsed_ranges)[1] |
| 124 |
| 125 |
| 126 def quality(mime_type, ranges): |
| 127 """Return the quality ('q') of a mime-type against a list of media-ranges. |
| 128 |
| 129 Returns the quality 'q' of a mime-type when compared against the |
| 130 media-ranges in ranges. For example: |
| 131 |
| 132 >>> quality('text/html','text/*;q=0.3, text/html;q=0.7, |
| 133 text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5') |
| 134 0.7 |
| 135 |
| 136 """ |
| 137 parsed_ranges = [parse_media_range(r) for r in ranges.split(',')] |
| 138 |
| 139 return quality_parsed(mime_type, parsed_ranges) |
| 140 |
| 141 |
| 142 def best_match(supported, header): |
| 143 """Return mime-type with the highest quality ('q') from list of candidates. |
| 144 |
| 145 Takes a list of supported mime-types and finds the best match for all the |
| 146 media-ranges listed in header. The value of header must be a string that |
| 147 conforms to the format of the HTTP Accept: header. The value of 'supported' |
| 148 is a list of mime-types. The list of supported mime-types should be sorted |
| 149 in order of increasing desirability, in case of a situation where there is |
| 150 a tie. |
| 151 |
| 152 >>> best_match(['application/xbel+xml', 'text/xml'], |
| 153 'text/*;q=0.5,*/*; q=0.1') |
| 154 'text/xml' |
| 155 """ |
| 156 split_header = _filter_blank(header.split(',')) |
| 157 parsed_header = [parse_media_range(r) for r in split_header] |
| 158 weighted_matches = [] |
| 159 pos = 0 |
| 160 for mime_type in supported: |
| 161 weighted_matches.append((fitness_and_quality_parsed(mime_type, |
| 162 parsed_header), pos, mime_type)) |
| 163 pos += 1 |
| 164 weighted_matches.sort() |
| 165 |
| 166 return weighted_matches[-1][0][1] and weighted_matches[-1][2] or '' |
| 167 |
| 168 |
| 169 def _filter_blank(i): |
| 170 for s in i: |
| 171 if s.strip(): |
| 172 yield s |
OLD | NEW |