summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mimeparse.py23
-rw-r--r--testdata.json6
2 files changed, 21 insertions, 8 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
diff --git a/testdata.json b/testdata.json
index af08397..0d8a4f3 100644
--- a/testdata.json
+++ b/testdata.json
@@ -29,11 +29,13 @@
[[["application/json", "text/html"], "application/json, text/javascript, */*"], "application/json", "common AJAX scenario"],
[[["application/json", "text/html"], "application/json, text/html;q=0.9"], "application/json", "verify fitness ordering"],
[[["image/*", "application/xml"], "image/png"], "image/*", "match using a type wildcard"],
- [[["image/*", "application/xml"], "image/*"], "image/*", "match using a wildcard for both requested and supported"]
+ [[["image/*", "application/xml"], "image/*"], "image/*", "match using a wildcard for both requested and supported"],
+ [[["text/html", "application/rdf+xml"], "text/html, application/rdf+xml"], "application/rdf+xml", "match should use highest order of supported when there is a tie"],
+ [[["application/rdf+xml", "text/html"], "text/html, application/rdf+xml"], "text/html", "match should use highest order of supported when there is a tie"]
],
"parse_mime_type": [
["application/xhtml;q=0.5", ["application", "xhtml", {"q": "0.5"}]],
["application/xhtml;q=0.5;ver=1.2", ["application", "xhtml", {"q": "0.5", "ver": "1.2"}]]
]
-} \ No newline at end of file
+}