diff options
| author | Ed Summers <ed.summers@gmail.com> | 2010-09-07 12:15:28 -0700 |
|---|---|---|
| committer | David Tsai <dbtsai@dbtsai.com> | 2012-08-22 14:41:23 -0700 |
| commit | 8bfb5ebf9d6cef561f5ab6462e51291127cdf1ad (patch) | |
| tree | 96b281dae2037b755c5c25d0c47f81009ee14a13 /mimeparse.py | |
| parent | 7d9fecd71d687ee61bbc055d5b5026a2d36d02d5 (diff) | |
| download | python-mimeparse-8bfb5ebf9d6cef561f5ab6462e51291127cdf1ad.tar.gz | |
Respect order of supported in the event of a tie.
Diffstat (limited to 'mimeparse.py')
| -rw-r--r-- | mimeparse.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/mimeparse.py b/mimeparse.py index 0fd91e7..64f2fff 100644 --- a/mimeparse.py +++ b/mimeparse.py @@ -14,7 +14,7 @@ Contents: - best_match(): Choose the mime-type with the highest quality ('q') from a list of candidates. """ -__version__ = "0.1.2" +__version__ = "0.1.3" __author__ = 'Joe Gregorio' __email__ = "joe@bitworking.org" __credits__ = "" @@ -111,13 +111,24 @@ def best_match(supported, header): match for all the media-ranges listed in header. The value of header must be a string that conforms to the format of the HTTP Accept: header. The value of 'supported' is a list of - mime-types. + mime-types. The list of supported mime-types should be sorted + in order of increasing desirability, in case of a situation + where there is a tie >>> best_match(['application/xbel+xml', 'text/xml'], 'text/*;q=0.5,*/*; q=0.1') 'text/xml' """ - parsed_header = [parse_media_range(r) for r in header.split(",")] - weighted_matches = [(fitness_and_quality_parsed(mime_type, parsed_header), mime_type)\ - for mime_type in supported] + parsed_header = [parse_media_range(r) for r in _filter_blank(header.split(","))] + weighted_matches = [] + pos = 0 + for mime_type in supported: + weighted_matches.append((fitness_and_quality_parsed(mime_type, + parsed_header), pos, mime_type)) + pos += 1 weighted_matches.sort() - return weighted_matches[-1][0][1] and weighted_matches[-1][1] or '' + return weighted_matches[-1][0][1] and weighted_matches[-1][2] or '' + +def _filter_blank(i): + for s in i: + if s.strip(): + yield s |
