<s> """<STR_LIT>""" <EOL> import re <EOL> from circuits . six import iteritems , u , b <EOL> tspecials = re . compile ( r'<STR_LIT>' ) <EOL> q_separator = re . compile ( r'<STR_LIT>' ) <EOL> def _formatparam ( param , value = None , quote = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if value is not None and len ( value ) > <NUM_LIT:0> : <EOL> if quote or tspecials . search ( value ) : <EOL> value = value . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , r'<STR_LIT>' ) <EOL> return '<STR_LIT>' % ( param , value ) <EOL> else : <EOL> return '<STR_LIT>' % ( param , value ) <EOL> else : <EOL> return param <EOL> def header_elements ( fieldname , fieldvalue ) : <EOL> """<STR_LIT>""" <EOL> if not fieldvalue : <EOL> return [ ] <EOL> result = [ ] <EOL> for element in fieldvalue . split ( "<STR_LIT:U+002C>" ) : <EOL> if fieldname . startswith ( "<STR_LIT>" ) or fieldname == '<STR_LIT>' : <EOL> hv = AcceptElement . from_str ( element ) <EOL> else : <EOL> hv = HeaderElement . from_str ( element ) <EOL> result . append ( hv ) <EOL> return list ( reversed ( sorted ( result ) ) ) <EOL> class HeaderElement ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , params = None ) : <EOL> self . value = value <EOL> if params is None : <EOL> params = { } <EOL> self . params = params <EOL> def __eq__ ( self , other ) : <EOL> return self . value == other . value <EOL> def __lt__ ( self , other ) : <EOL> return self . value < other . value <EOL> def __str__ ( self ) : <EOL> p = [ "<STR_LIT>" % ( k , v ) for k , v in iteritems ( self . params ) ] <EOL> return "<STR_LIT>" % ( self . value , "<STR_LIT>" . join ( p ) ) <EOL> def __bytes__ ( self ) : <EOL> return b ( self . __str__ ( ) ) <EOL> def __unicode__ ( self ) : <EOL> return u ( self . __str__ ( ) ) <EOL> def parse ( elementstr ) : <EOL> """<STR_LIT>""" <EOL> atoms = [ x . strip ( ) for x in elementstr . split ( "<STR_LIT:;>" ) if x . strip ( ) ] <EOL> if not atoms : <EOL> initial_value = '<STR_LIT>' <EOL> else : <EOL> initial_value = atoms . pop ( <NUM_LIT:0> ) . strip ( ) <EOL> params = { } <EOL> for atom in atoms : <EOL> atom = [ x . strip ( ) for x in atom . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) if x . strip ( ) ] <EOL> key = atom . pop ( <NUM_LIT:0> ) <EOL> if atom : <EOL> val = atom [ <NUM_LIT:0> ] <EOL> else : <EOL> val = "<STR_LIT>" <EOL> params [ key ] = val <EOL> return initial_value , params <EOL> parse = staticmethod ( parse ) <EOL> @ classmethod <EOL> def from_str ( cls , elementstr ) : <EOL> """<STR_LIT>""" <EOL> ival , params = cls . parse ( elementstr ) <EOL> return cls ( ival , params ) <EOL> class AcceptElement ( HeaderElement ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def from_str ( cls , elementstr ) : <EOL> qvalue = None <EOL> atoms = q_separator . split ( elementstr , <NUM_LIT:1> ) <EOL> media_range = atoms . pop ( <NUM_LIT:0> ) . strip ( ) <EOL> if atoms : <EOL> qvalue = HeaderElement . from_str ( atoms [ <NUM_LIT:0> ] . strip ( ) ) <EOL> media_type , params = cls . parse ( media_range ) <EOL> if qvalue is not None : <EOL> params [ "<STR_LIT:q>" ] = qvalue <EOL> return cls ( media_type , params ) <EOL> def qvalue ( self ) : <EOL> val = self . params . get ( "<STR_LIT:q>" , "<STR_LIT:1>" ) <EOL> if isinstance ( val , HeaderElement ) : <EOL> val = val . value <EOL> return float ( val ) <EOL> qvalue = property ( qvalue , doc = "<STR_LIT>" ) <EOL> def __eq__ ( self , other ) : <EOL> return self . qvalue == other . qvalue <EOL> def __lt__ ( self , other ) : <EOL> if self . qvalue == other . qvalue : <EOL> return str ( self ) < str ( other ) <EOL> else : <EOL> return self . qvalue < other . qvalue <EOL> class CaseInsensitiveDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> d = dict ( * args , ** kwargs ) <EOL> for key , value in iteritems ( d ) : <EOL> dict . __setitem__ ( self , str ( key ) . title ( ) , value ) <EOL> dict . __init__ ( self ) <EOL> def __getitem__ ( self , key ) : <EOL> return dict . __getitem__ ( self , str ( key ) . title ( ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> dict . __setitem__ ( self , str ( key ) . title ( ) , value ) <EOL> def __delitem__ ( self , key ) : <EOL> dict . __delitem__ ( self , str ( key ) . title ( ) ) <EOL> def __contains__ ( self , key ) : <EOL> return dict . __contains__ ( self , str ( key ) . title ( ) ) <EOL> def get ( self , key , default = None ) : <EOL> return dict . get ( self , str ( key ) . title ( ) , default ) <EOL> def update ( self , E ) : <EOL> for k in E . keys ( ) : <EOL> self [ str ( k ) . title ( ) ] = E [ k ] <EOL> @ classmethod <EOL> def fromkeys ( cls , seq , value = None ) : <EOL> newdict = cls ( ) <EOL> for k in seq : <EOL> newdict [ k ] = value <EOL> return newdict <EOL> def setdefault ( self , key , x = None ) : <EOL> key = str ( key ) . title ( ) <EOL> try : <EOL> return dict . __getitem__ ( self , key ) <EOL> except KeyError : <EOL> self [ key ] = x <EOL> return x <EOL> def pop ( self , key , default = None ) : <EOL> return dict . pop ( self , str ( key ) . title ( ) , default ) <EOL> class Headers ( CaseInsensitiveDict ) : <EOL> """<STR_LIT>""" <EOL> def elements ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return header_elements ( key , self . get ( key ) ) <EOL> def get_all ( self , name ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( name , '<STR_LIT>' ) <EOL> if isinstance ( value , list ) : <EOL> return value <EOL> return [ val . strip ( ) for val in value . split ( '<STR_LIT:U+002C>' ) ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % repr ( list ( self . items ( ) ) ) <EOL> def __str__ ( self ) : <EOL> headers = [ "<STR_LIT>" % ( k , v ) for k , v in self . items ( ) ] <EOL> return "<STR_LIT>" . join ( headers ) + '<STR_LIT:\r\n>' <EOL> def items ( self ) : <EOL> for k , v in super ( Headers , self ) . items ( ) : <EOL> if isinstance ( v , list ) : <EOL> for vv in v : <EOL> yield ( k , vv ) <EOL> else : <EOL> yield ( k , v ) <EOL> def __bytes__ ( self ) : <EOL> return str ( self ) . encode ( "<STR_LIT>" ) <EOL> def append ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if key not in self : <EOL> if key . lower ( ) == "<STR_LIT>" : <EOL> self [ key ] = [ value ] <EOL> else : <EOL> self [ key ] = value <EOL> else : <EOL> if isinstance ( self [ key ] , list ) : <EOL> self [ key ] . append ( value ) <EOL> else : <EOL> self [ key ] = "<STR_LIT:U+002CU+0020>" . join ( [ self [ key ] , value ] ) <EOL> def add_header ( self , _name , _value , ** _params ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> if _value is not None : <EOL> parts . append ( _value ) <EOL> for k , v in list ( _params . items ( ) ) : <EOL> k = k . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> if v is None : <EOL> parts . append ( k ) <EOL> else : <EOL> parts . append ( _formatparam ( k , v ) ) <EOL> self . append ( _name , "<STR_LIT>" . join ( parts ) ) </s>
