<s> import re <EOL> charset_pattern = re . compile ( '<STR_LIT>' ) <EOL> def camelcase_to_slash ( name ) : <EOL> """<STR_LIT>""" <EOL> s1 = re . sub ( '<STR_LIT>' , r'<STR_LIT>' , name ) <EOL> return re . sub ( '<STR_LIT>' , r'<STR_LIT>' , s1 ) . lower ( ) <EOL> def strip_charset ( content_type ) : <EOL> """<STR_LIT>""" <EOL> return content_type . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> def extract_charset ( content_type ) : <EOL> """<STR_LIT>""" <EOL> match = charset_pattern . match ( content_type ) <EOL> return match . group ( <NUM_LIT:1> ) if match else None <EOL> def get_charset ( request ) : <EOL> """<STR_LIT>""" <EOL> content_type = request . META . get ( '<STR_LIT>' , None ) <EOL> if content_type : <EOL> return extract_charset ( content_type ) if content_type else None <EOL> else : <EOL> return None <EOL> def parse_accept_header ( accept ) : <EOL> """<STR_LIT>""" <EOL> def parse_media_range ( accept_item ) : <EOL> """<STR_LIT>""" <EOL> return accept_item . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> def comparator ( a , b ) : <EOL> """<STR_LIT>""" <EOL> result = - cmp ( a [ <NUM_LIT:2> ] , b [ <NUM_LIT:2> ] ) <EOL> if result is not <NUM_LIT:0> : <EOL> return result <EOL> mtype_a , subtype_a = parse_media_range ( a [ <NUM_LIT:0> ] ) <EOL> mtype_b , subtype_b = parse_media_range ( b [ <NUM_LIT:0> ] ) <EOL> if mtype_a == '<STR_LIT:*>' and subtype_a == '<STR_LIT:*>' : <EOL> return <NUM_LIT:1> <EOL> if mtype_b == '<STR_LIT:*>' and subtype_b == '<STR_LIT:*>' : <EOL> return - <NUM_LIT:1> <EOL> if subtype_a == '<STR_LIT:*>' : <EOL> return <NUM_LIT:1> <EOL> if subtype_b == '<STR_LIT:*>' : <EOL> return - <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> if not accept : <EOL> return [ ] <EOL> result = [ ] <EOL> for media_range in accept . split ( "<STR_LIT:U+002C>" ) : <EOL> parts = media_range . split ( "<STR_LIT:;>" ) <EOL> media_type = parts . pop ( <NUM_LIT:0> ) . strip ( ) <EOL> media_params = [ ] <EOL> q = <NUM_LIT:1.0> <EOL> for part in parts : <EOL> ( key , value ) = part . lstrip ( ) . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> if key == "<STR_LIT:q>" : <EOL> q = float ( value ) <EOL> else : <EOL> media_params . append ( ( key , value ) ) <EOL> result . append ( ( media_type , tuple ( media_params ) , q ) ) <EOL> result . sort ( comparator ) <EOL> return result </s>
