<s> """<STR_LIT>""" <EOL> import re <EOL> from webob . util import rfc_reference <EOL> from webob . headers import _trans_name as header_to_key <EOL> part_re = re . compile ( <EOL> r'<STR_LIT>' ) <EOL> def parse_accept ( value ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for match in part_re . finditer ( '<STR_LIT:U+002C>' + value ) : <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> if name == '<STR_LIT:q>' : <EOL> continue <EOL> quality = match . group ( <NUM_LIT:2> ) or '<STR_LIT>' <EOL> if not quality : <EOL> quality = <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> quality = max ( min ( float ( quality ) , <NUM_LIT:1> ) , <NUM_LIT:0> ) <EOL> except ValueError : <EOL> quality = <NUM_LIT:1> <EOL> result . append ( ( name , quality ) ) <EOL> return result <EOL> class Accept ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , header_name , header_value ) : <EOL> self . header_name = header_name <EOL> self . header_value = header_value <EOL> self . _parsed = parse_accept ( header_value ) <EOL> if header_name == '<STR_LIT>' : <EOL> for k , v in self . _parsed : <EOL> if k == '<STR_LIT:*>' or k == '<STR_LIT>' : <EOL> break <EOL> else : <EOL> self . _parsed . append ( ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> elif header_name == '<STR_LIT>' : <EOL> self . _match = self . _match_lang <EOL> self . _parsed_nonzero = [ ( m , q ) for ( m , q ) in self . _parsed if q ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> abs ( id ( self ) ) , <EOL> self . header_name , str ( self ) ) <EOL> def __str__ ( self ) : <EOL> result = [ ] <EOL> for mask , quality in self . _parsed : <EOL> if quality != <NUM_LIT:1> : <EOL> mask = '<STR_LIT>' % ( mask , quality ) <EOL> result . append ( mask ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( result ) <EOL> def __add__ ( self , other , reversed = False ) : <EOL> if isinstance ( other , Accept ) : <EOL> other = other . header_value <EOL> if hasattr ( other , '<STR_LIT>' ) : <EOL> other = sorted ( other . items ( ) , key = lambda item : - item [ <NUM_LIT:1> ] ) <EOL> if isinstance ( other , ( list , tuple ) ) : <EOL> result = [ ] <EOL> for item in other : <EOL> if isinstance ( item , ( list , tuple ) ) : <EOL> name , quality = item <EOL> result . append ( '<STR_LIT>' % ( name , quality ) ) <EOL> else : <EOL> result . append ( item ) <EOL> other = '<STR_LIT:U+002CU+0020>' . join ( result ) <EOL> other = str ( other ) <EOL> my_value = self . header_value <EOL> if reversed : <EOL> other , my_value = my_value , other <EOL> if not other : <EOL> new_value = my_value <EOL> elif not my_value : <EOL> new_value = other <EOL> else : <EOL> new_value = my_value + '<STR_LIT:U+002CU+0020>' + other <EOL> return self . __class__ ( self . header_name , new_value ) <EOL> def __radd__ ( self , other ) : <EOL> return self . __add__ ( other , True ) <EOL> def __contains__ ( self , offer ) : <EOL> """<STR_LIT>""" <EOL> for mask , quality in self . _parsed_nonzero : <EOL> if self . _match ( mask , offer ) : <EOL> return True <EOL> def quality ( self , offer , modifier = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> for mask , quality in self . _parsed : <EOL> if self . _match ( mask , offer ) : <EOL> return quality * modifier <EOL> return None <EOL> def first_match ( self , offers ) : <EOL> """<STR_LIT>""" <EOL> if not offers : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> for offer in offers : <EOL> if offer is None : <EOL> return None <EOL> for mask , quality in self . _parsed_nonzero : <EOL> if self . _match ( mask , offer ) : <EOL> return offer <EOL> return offers [ <NUM_LIT:0> ] <EOL> def best_match ( self , offers , default_match = None ) : <EOL> """<STR_LIT>""" <EOL> best_quality = - <NUM_LIT:1> <EOL> best_offer = default_match <EOL> matched_by = '<STR_LIT>' <EOL> for offer in offers : <EOL> if isinstance ( offer , ( tuple , list ) ) : <EOL> offer , server_quality = offer <EOL> else : <EOL> server_quality = <NUM_LIT:1> <EOL> for mask , quality in self . _parsed_nonzero : <EOL> possible_quality = server_quality * quality <EOL> if possible_quality < best_quality : <EOL> continue <EOL> elif possible_quality == best_quality : <EOL> if matched_by . count ( '<STR_LIT:*>' ) <= mask . count ( '<STR_LIT:*>' ) : <EOL> continue <EOL> if self . _match ( mask , offer ) : <EOL> best_quality = possible_quality <EOL> best_offer = offer <EOL> matched_by = mask <EOL> return best_offer <EOL> def best_matches ( self , fallback = None ) : <EOL> """<STR_LIT>""" <EOL> items = [ i for i , q in sorted ( self . _parsed , key = lambda iq : - iq [ <NUM_LIT:1> ] ) ] <EOL> if fallback : <EOL> for index , item in enumerate ( items ) : <EOL> if self . _match ( item , fallback ) : <EOL> items [ index : ] = [ fallback ] <EOL> break <EOL> else : <EOL> items . append ( fallback ) <EOL> return items <EOL> def _match ( self , mask , offer ) : <EOL> _check_offer ( offer ) <EOL> return mask == '<STR_LIT:*>' or offer . lower ( ) == mask . lower ( ) <EOL> def _match_lang ( self , mask , item ) : <EOL> return ( mask == '<STR_LIT:*>' <EOL> or item . lower ( ) == mask . lower ( ) <EOL> or item . lower ( ) . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] == mask . lower ( ) <EOL> ) <EOL> class NilAccept ( object ) : <EOL> """<STR_LIT>""" <EOL> MasterClass = Accept <EOL> def __init__ ( self , header_name ) : <EOL> self . header_name = header_name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , self . header_name , self . MasterClass ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> def __add__ ( self , item ) : <EOL> if isinstance ( item , self . MasterClass ) : <EOL> return item <EOL> else : <EOL> return self . MasterClass ( self . header_name , '<STR_LIT>' ) + item <EOL> def __radd__ ( self , item ) : <EOL> if isinstance ( item , self . MasterClass ) : <EOL> return item <EOL> else : <EOL> return item + self . MasterClass ( self . header_name , '<STR_LIT>' ) <EOL> def __contains__ ( self , item ) : <EOL> _check_offer ( item ) <EOL> return True <EOL> def quality ( self , offer , default_quality = <NUM_LIT:1> ) : <EOL> return <NUM_LIT:0> <EOL> def first_match ( self , offers ) : <EOL> return offers [ <NUM_LIT:0> ] <EOL> def best_match ( self , offers , default_match = None ) : <EOL> best_quality = - <NUM_LIT:1> <EOL> best_match = default_match <EOL> for offer in offers : <EOL> _check_offer ( offer ) <EOL> if isinstance ( offer , ( list , tuple ) ) : <EOL> offer , quality = offer <EOL> else : <EOL> quality = <NUM_LIT:1> <EOL> if quality > best_quality : <EOL> best_offer = offer <EOL> best_quality = quality <EOL> return best_offer <EOL> def best_matches ( self , fallback = None ) : <EOL> if fallback : <EOL> return [ fallback ] <EOL> else : <EOL> return [ ] <EOL> class NoAccept ( NilAccept ) : <EOL> def __contains__ ( self , item ) : <EOL> return False <EOL> class MIMEAccept ( Accept ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , header_name , header_value ) : <EOL> Accept . __init__ ( self , header_name , header_value ) <EOL> parsed = [ ] <EOL> for mask , q in self . _parsed : <EOL> try : <EOL> mask_major , mask_minor = mask . split ( '<STR_LIT:/>' ) <EOL> except ValueError : <EOL> continue <EOL> if mask_major == '<STR_LIT:*>' and mask_minor != '<STR_LIT:*>' : <EOL> continue <EOL> parsed . append ( ( mask , q ) ) <EOL> self . _parsed = parsed <EOL> def accept_html ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( '<STR_LIT>' in self <EOL> or '<STR_LIT>' in self <EOL> or '<STR_LIT>' in self <EOL> or '<STR_LIT>' in self ) <EOL> accepts_html = property ( accept_html ) <EOL> def _match ( self , mask , offer ) : <EOL> """<STR_LIT>""" <EOL> _check_offer ( offer ) <EOL> if '<STR_LIT:*>' not in mask : <EOL> return offer == mask <EOL> elif mask == '<STR_LIT>' : <EOL> return True <EOL> else : <EOL> assert mask . endswith ( '<STR_LIT>' ) <EOL> mask_major = mask [ : - <NUM_LIT:2> ] <EOL> offer_major = offer . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> return offer_major == mask_major <EOL> class MIMENilAccept ( NilAccept ) : <EOL> MasterClass = MIMEAccept <EOL> def _check_offer ( offer ) : <EOL> if '<STR_LIT:*>' in offer : <EOL> raise ValueError ( "<STR_LIT>" % offer ) <EOL> def accept_property ( header , rfc_section , <EOL> AcceptClass = Accept , NilClass = NilAccept , convert_name = '<STR_LIT>' <EOL> ) : <EOL> key = header_to_key ( header ) <EOL> doc = "<STR_LIT>" % key <EOL> doc += rfc_reference ( key , rfc_section ) <EOL> doc += "<STR_LIT>" % convert_name <EOL> def fget ( req ) : <EOL> value = req . environ . get ( key ) <EOL> if not value : <EOL> return NilClass ( header ) <EOL> return AcceptClass ( header , value ) <EOL> def fset ( req , val ) : <EOL> if val : <EOL> if isinstance ( val , ( list , tuple , dict ) ) : <EOL> val = AcceptClass ( header , '<STR_LIT>' ) + val <EOL> val = str ( val ) <EOL> req . environ [ key ] = val or None <EOL> def fdel ( req ) : <EOL> del req . environ [ key ] <EOL> return property ( fget , fset , fdel , doc ) </s>
