<s> from webob . multidict import MultiDict <EOL> from flanker . mime . message . headers import encodedword <EOL> from flanker . mime . message . headers . parsing import normalize , parse_stream <EOL> from flanker . mime . message . headers . encoding import to_mime <EOL> from flanker . mime . message . errors import EncodingError <EOL> class MimeHeaders ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , items = ( ) ) : <EOL> self . _v = MultiDict ( [ ( normalize ( key ) , remove_newlines ( val ) ) <EOL> for ( key , val ) in items ] ) <EOL> self . changed = False <EOL> self . num_prepends = <NUM_LIT:0> <EOL> def __getitem__ ( self , key ) : <EOL> v = self . _v . get ( normalize ( key ) , None ) <EOL> if v is not None : <EOL> return encodedword . decode ( v ) <EOL> return None <EOL> def __len__ ( self ) : <EOL> return len ( self . _v ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _v ) <EOL> def __contains__ ( self , key ) : <EOL> return normalize ( key ) in self . _v <EOL> def __setitem__ ( self , key , value ) : <EOL> key = normalize ( key ) <EOL> if key in self . _v : <EOL> self . _v [ key ] = remove_newlines ( value ) <EOL> self . changed = True <EOL> else : <EOL> self . prepend ( key , remove_newlines ( value ) ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . _v [ normalize ( key ) ] <EOL> self . changed = True <EOL> def __nonzero__ ( self ) : <EOL> return len ( self . _v ) > <NUM_LIT:0> <EOL> def prepend ( self , key , value ) : <EOL> self . _v . _items . insert ( <NUM_LIT:0> , ( normalize ( key ) , remove_newlines ( value ) ) ) <EOL> self . num_prepends += <NUM_LIT:1> <EOL> def add ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . prepend ( key , value ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _v . keys ( ) <EOL> def transform ( self , fn , decode = False ) : <EOL> """<STR_LIT>""" <EOL> changed = [ False ] <EOL> def tracking_fn ( key , val ) : <EOL> new_key , new_val = fn ( key , val ) <EOL> if new_val != val or new_key != key : <EOL> changed [ <NUM_LIT:0> ] = True <EOL> return new_key , new_val <EOL> v = MultiDict ( tracking_fn ( key , val ) for key , val in self . iteritems ( raw = not decode ) ) <EOL> if changed [ <NUM_LIT:0> ] : <EOL> self . _v = v <EOL> self . changed = True <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . iteritems ( ) ) <EOL> def iteritems ( self , raw = False ) : <EOL> """<STR_LIT>""" <EOL> if raw : <EOL> return self . _v . iteritems ( ) <EOL> return iter ( [ ( x [ <NUM_LIT:0> ] , encodedword . decode ( x [ <NUM_LIT:1> ] ) ) <EOL> for x in self . _v . iteritems ( ) ] ) <EOL> def get ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> v = self . _v . get ( normalize ( key ) , default ) <EOL> if v is not None : <EOL> return encodedword . decode ( v ) <EOL> return None <EOL> def getraw ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _v . get ( normalize ( key ) , default ) <EOL> def getall ( self , key ) : <EOL> """<STR_LIT>""" <EOL> v = self . _v . getall ( normalize ( key ) ) <EOL> return [ encodedword . decode ( x ) for x in v ] <EOL> def have_changed ( self , ignore_prepends = False ) : <EOL> """<STR_LIT>""" <EOL> return self . changed or ( self . num_prepends > <NUM_LIT:0> and not ignore_prepends ) <EOL> def __str__ ( self ) : <EOL> return str ( self . _v ) <EOL> @ classmethod <EOL> def from_stream ( cls , stream ) : <EOL> """<STR_LIT>""" <EOL> return cls ( parse_stream ( stream ) ) <EOL> def to_stream ( self , stream , prepends_only = False ) : <EOL> """<STR_LIT>""" <EOL> i = <NUM_LIT:0> <EOL> for h , v in self . iteritems ( raw = True ) : <EOL> if prepends_only and i == self . num_prepends : <EOL> break <EOL> i += <NUM_LIT:1> <EOL> try : <EOL> h = h . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeDecodeError : <EOL> raise EncodingError ( "<STR_LIT>" ) <EOL> stream . write ( "<STR_LIT>" . format ( h , to_mime ( h , v ) ) ) <EOL> def remove_newlines ( value ) : <EOL> if not value : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( value , ( str , unicode ) ) : <EOL> return value . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> else : <EOL> return value </s>
