<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from functools import reduce <EOL> import six <EOL> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> __email__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> __credits__ = '<STR_LIT>' <EOL> def parse_mime_type ( mime_type ) : <EOL> """<STR_LIT>""" <EOL> parts = mime_type . split ( '<STR_LIT:;>' ) <EOL> params = dict ( [ tuple ( [ s . strip ( ) for s in param . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) ] ) for param in parts [ <NUM_LIT:1> : ] <EOL> ] ) <EOL> full_type = parts [ <NUM_LIT:0> ] . strip ( ) <EOL> if full_type == '<STR_LIT:*>' : <EOL> full_type = '<STR_LIT>' <EOL> ( type , subtype ) = full_type . split ( '<STR_LIT:/>' ) <EOL> return ( type . strip ( ) , subtype . strip ( ) , params ) <EOL> def parse_media_range ( range ) : <EOL> """<STR_LIT>""" <EOL> ( type , subtype , params ) = parse_mime_type ( range ) <EOL> if '<STR_LIT:q>' not in params or not params [ '<STR_LIT:q>' ] or not float ( params [ '<STR_LIT:q>' ] ) or float ( params [ '<STR_LIT:q>' ] ) > <NUM_LIT:1> or float ( params [ '<STR_LIT:q>' ] ) < <NUM_LIT:0> : <EOL> params [ '<STR_LIT:q>' ] = '<STR_LIT:1>' <EOL> return ( type , subtype , params ) <EOL> def fitness_and_quality_parsed ( mime_type , parsed_ranges ) : <EOL> """<STR_LIT>""" <EOL> best_fitness = - <NUM_LIT:1> <EOL> best_fit_q = <NUM_LIT:0> <EOL> ( target_type , target_subtype , target_params ) = parse_media_range ( mime_type ) <EOL> for ( type , subtype , params ) in parsed_ranges : <EOL> type_match = ( type == target_type or type == '<STR_LIT:*>' or target_type == '<STR_LIT:*>' ) <EOL> subtype_match = ( subtype == target_subtype or subtype == '<STR_LIT:*>' or target_subtype == '<STR_LIT:*>' ) <EOL> if type_match and subtype_match : <EOL> param_matches = reduce ( lambda x , y : x + y , [ <NUM_LIT:1> for ( key , value ) in six . iteritems ( target_params ) if key != '<STR_LIT:q>' and key in params and value == params [ key ] ] , <NUM_LIT:0> ) <EOL> fitness = ( type == target_type ) and <NUM_LIT:100> or <NUM_LIT:0> <EOL> fitness += ( subtype == target_subtype ) and <NUM_LIT:10> or <NUM_LIT:0> <EOL> fitness += param_matches <EOL> if fitness > best_fitness : <EOL> best_fitness = fitness <EOL> best_fit_q = params [ '<STR_LIT:q>' ] <EOL> return best_fitness , float ( best_fit_q ) <EOL> def quality_parsed ( mime_type , parsed_ranges ) : <EOL> """<STR_LIT>""" <EOL> return fitness_and_quality_parsed ( mime_type , parsed_ranges ) [ <NUM_LIT:1> ] <EOL> def quality ( mime_type , ranges ) : <EOL> """<STR_LIT>""" <EOL> parsed_ranges = [ parse_media_range ( r ) for r in ranges . split ( '<STR_LIT:U+002C>' ) ] <EOL> return quality_parsed ( mime_type , parsed_ranges ) <EOL> def best_match ( supported , header ) : <EOL> """<STR_LIT>""" <EOL> split_header = _filter_blank ( header . split ( '<STR_LIT:U+002C>' ) ) <EOL> parsed_header = [ parse_media_range ( r ) for r in split_header ] <EOL> weighted_matches = [ ] <EOL> pos = <NUM_LIT:0> <EOL> for mime_type in supported : <EOL> weighted_matches . append ( ( fitness_and_quality_parsed ( mime_type , <EOL> parsed_header ) , pos , mime_type ) ) <EOL> pos += <NUM_LIT:1> <EOL> weighted_matches . sort ( ) <EOL> return weighted_matches [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] and weighted_matches [ - <NUM_LIT:1> ] [ <NUM_LIT:2> ] or '<STR_LIT>' <EOL> def _filter_blank ( i ) : <EOL> for s in i : <EOL> if s . strip ( ) : <EOL> yield s </s>
