<s> """<STR_LIT>""" <EOL> import mimetools <EOL> __all__ = [ "<STR_LIT>" ] <EOL> class MimeWriter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fp ) : <EOL> self . _fp = fp <EOL> self . _headers = [ ] <EOL> def addheader ( self , key , value , prefix = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> lines = value . split ( "<STR_LIT:\n>" ) <EOL> while lines and not lines [ - <NUM_LIT:1> ] : del lines [ - <NUM_LIT:1> ] <EOL> while lines and not lines [ <NUM_LIT:0> ] : del lines [ <NUM_LIT:0> ] <EOL> for i in range ( <NUM_LIT:1> , len ( lines ) ) : <EOL> lines [ i ] = "<STR_LIT:U+0020>" + lines [ i ] . strip ( ) <EOL> value = "<STR_LIT:\n>" . join ( lines ) + "<STR_LIT:\n>" <EOL> line = key + "<STR_LIT>" + value <EOL> if prefix : <EOL> self . _headers . insert ( <NUM_LIT:0> , line ) <EOL> else : <EOL> self . _headers . append ( line ) <EOL> def flushheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fp . writelines ( self . _headers ) <EOL> self . _headers = [ ] <EOL> def startbody ( self , ctype , plist = [ ] , prefix = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> for name , value in plist : <EOL> ctype = ctype + '<STR_LIT>' % ( name , value ) <EOL> self . addheader ( "<STR_LIT:Content-Type>" , ctype , prefix = prefix ) <EOL> self . flushheaders ( ) <EOL> self . _fp . write ( "<STR_LIT:\n>" ) <EOL> return self . _fp <EOL> def startmultipartbody ( self , subtype , boundary = None , plist = [ ] , prefix = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . _boundary = boundary or mimetools . choose_boundary ( ) <EOL> return self . startbody ( "<STR_LIT>" + subtype , <EOL> [ ( "<STR_LIT>" , self . _boundary ) ] + plist , <EOL> prefix = prefix ) <EOL> def nextpart ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fp . write ( "<STR_LIT>" + self . _boundary + "<STR_LIT:\n>" ) <EOL> return self . __class__ ( self . _fp ) <EOL> def lastpart ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fp . write ( "<STR_LIT>" + self . _boundary + "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import test . test_MimeWriter </s>
