diff options
-rwxr-xr-x | mimeparse.py | 10 | ||||
-rw-r--r-- | testdata.json | 1 |
2 files changed, 6 insertions, 5 deletions
diff --git a/mimeparse.py b/mimeparse.py index 181e01f..e258016 100755 --- a/mimeparse.py +++ b/mimeparse.py @@ -72,7 +72,7 @@ def parse_media_range(range): return (type, subtype, params) -def fitness_and_quality_parsed(mime_type, parsed_ranges): +def quality_and_fitness_parsed(mime_type, parsed_ranges): """Find the best match for a mime-type amongst parsed media-ranges. Find the best match for a given mime-type against a list of media_ranges @@ -103,7 +103,7 @@ def fitness_and_quality_parsed(mime_type, parsed_ranges): best_fitness = fitness best_fit_q = params['q'] - return best_fitness, float(best_fit_q) + return float(best_fit_q), best_fitness def quality_parsed(mime_type, parsed_ranges): @@ -115,7 +115,7 @@ def quality_parsed(mime_type, parsed_ranges): bahaves the same as quality() except that 'parsed_ranges' must be a list of parsed media ranges. """ - return fitness_and_quality_parsed(mime_type, parsed_ranges)[1] + return quality_and_fitness_parsed(mime_type, parsed_ranges)[0] def quality(mime_type, ranges): @@ -153,12 +153,12 @@ def best_match(supported, header): weighted_matches = [] pos = 0 for mime_type in supported: - weighted_matches.append((fitness_and_quality_parsed(mime_type, + weighted_matches.append((quality_and_fitness_parsed(mime_type, parsed_header), pos, mime_type)) pos += 1 weighted_matches.sort() - return weighted_matches[-1][0][1] and weighted_matches[-1][2] or '' + return weighted_matches[-1][0][0] and weighted_matches[-1][2] or '' def _filter_blank(i): diff --git a/testdata.json b/testdata.json index 330a7fb..7a84b39 100644 --- a/testdata.json +++ b/testdata.json @@ -32,6 +32,7 @@ [[["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/jpeg", "text/plain"], "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"], "image/jpeg", "media type with highest associated quality factor should win, not necessarily most specific"], [[["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"] ], |