<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from django . http . multipartparser import parse_header <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from rest_framework import HTTP_HEADER_ENCODING <EOL> def media_type_matches ( lhs , rhs ) : <EOL> """<STR_LIT>""" <EOL> lhs = _MediaType ( lhs ) <EOL> rhs = _MediaType ( rhs ) <EOL> return lhs . match ( rhs ) <EOL> def order_by_precedence ( media_type_lst ) : <EOL> """<STR_LIT>""" <EOL> ret = [ set ( ) , set ( ) , set ( ) , set ( ) ] <EOL> for media_type in media_type_lst : <EOL> precedence = _MediaType ( media_type ) . precedence <EOL> ret [ <NUM_LIT:3> - precedence ] . add ( media_type ) <EOL> return [ media_types for media_types in ret if media_types ] <EOL> @ python_2_unicode_compatible <EOL> class _MediaType ( object ) : <EOL> def __init__ ( self , media_type_str ) : <EOL> if media_type_str is None : <EOL> media_type_str = '<STR_LIT>' <EOL> self . orig = media_type_str <EOL> self . full_type , self . params = parse_header ( media_type_str . encode ( HTTP_HEADER_ENCODING ) ) <EOL> self . main_type , sep , self . sub_type = self . full_type . partition ( '<STR_LIT:/>' ) <EOL> def match ( self , other ) : <EOL> """<STR_LIT>""" <EOL> for key in self . params . keys ( ) : <EOL> if key != '<STR_LIT:q>' and other . params . get ( key , None ) != self . params . get ( key , None ) : <EOL> return False <EOL> if self . sub_type != '<STR_LIT:*>' and other . sub_type != '<STR_LIT:*>' and other . sub_type != self . sub_type : <EOL> return False <EOL> if self . main_type != '<STR_LIT:*>' and other . main_type != '<STR_LIT:*>' and other . main_type != self . main_type : <EOL> return False <EOL> return True <EOL> @ property <EOL> def precedence ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . main_type == '<STR_LIT:*>' : <EOL> return <NUM_LIT:0> <EOL> elif self . sub_type == '<STR_LIT:*>' : <EOL> return <NUM_LIT:1> <EOL> elif not self . params or list ( self . params . keys ( ) ) == [ '<STR_LIT:q>' ] : <EOL> return <NUM_LIT:2> <EOL> return <NUM_LIT:3> <EOL> def __str__ ( self ) : <EOL> ret = "<STR_LIT>" % ( self . main_type , self . sub_type ) <EOL> for key , val in self . params . items ( ) : <EOL> ret += "<STR_LIT>" % ( key , val ) <EOL> return ret </s>
