<s> """<STR_LIT>""" <EOL> from types import ListType , TupleType <EOL> import re <EOL> tspecials = 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> class Headers : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , headers ) : <EOL> if type ( headers ) is not ListType : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . _headers = headers <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _headers ) <EOL> def __setitem__ ( self , name , val ) : <EOL> """<STR_LIT>""" <EOL> del self [ name ] <EOL> self . _headers . append ( ( name , val ) ) <EOL> def __delitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> name = name . lower ( ) <EOL> self . _headers [ : ] = [ kv for kv in self . _headers if kv [ <NUM_LIT:0> ] . lower ( ) != name ] <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( name ) <EOL> def has_key ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( name ) is not None <EOL> __contains__ = has_key <EOL> def get_all ( self , name ) : <EOL> """<STR_LIT>""" <EOL> name = name . lower ( ) <EOL> return [ kv [ <NUM_LIT:1> ] for kv in self . _headers if kv [ <NUM_LIT:0> ] . lower ( ) == name ] <EOL> def get ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> name = name . lower ( ) <EOL> for k , v in self . _headers : <EOL> if k . lower ( ) == name : <EOL> return v <EOL> return default <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ k for k , v in self . _headers ] <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ v for k , v in self . _headers ] <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _headers [ : ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . _headers <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:\r\n>' . join ( [ "<STR_LIT>" % kv for kv in self . _headers ] + [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def setdefault ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> result = self . get ( name ) <EOL> if result is None : <EOL> self . _headers . append ( ( name , value ) ) <EOL> return value <EOL> else : <EOL> return result <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 _params . items ( ) : <EOL> if v is None : <EOL> parts . append ( k . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) ) <EOL> else : <EOL> parts . append ( _formatparam ( k . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) , v ) ) <EOL> self . _headers . append ( ( _name , "<STR_LIT>" . join ( parts ) ) ) </s>
