<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import re <EOL> import sys <EOL> import time <EOL> import random <EOL> import warnings <EOL> from cStringIO import StringIO <EOL> from email . header import Header <EOL> UNDERSCORE = '<STR_LIT:_>' <EOL> NL = '<STR_LIT:\n>' <EOL> fcre = re . compile ( r'<STR_LIT>' , re . MULTILINE ) <EOL> def _is8bitstring ( s ) : <EOL> if isinstance ( s , str ) : <EOL> try : <EOL> unicode ( s , '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> return True <EOL> return False <EOL> class Generator : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outfp , mangle_from_ = True , maxheaderlen = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . _fp = outfp <EOL> self . _mangle_from_ = mangle_from_ <EOL> self . _maxheaderlen = maxheaderlen <EOL> def write ( self , s ) : <EOL> self . _fp . write ( s ) <EOL> def flatten ( self , msg , unixfrom = False ) : <EOL> """<STR_LIT>""" <EOL> if unixfrom : <EOL> ufrom = msg . get_unixfrom ( ) <EOL> if not ufrom : <EOL> ufrom = '<STR_LIT>' + time . ctime ( time . time ( ) ) <EOL> print >> self . _fp , ufrom <EOL> self . _write ( msg ) <EOL> def clone ( self , fp ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( fp , self . _mangle_from_ , self . _maxheaderlen ) <EOL> def _write ( self , msg ) : <EOL> oldfp = self . _fp <EOL> try : <EOL> self . _fp = sfp = StringIO ( ) <EOL> self . _dispatch ( msg ) <EOL> finally : <EOL> self . _fp = oldfp <EOL> meth = getattr ( msg , '<STR_LIT>' , None ) <EOL> if meth is None : <EOL> self . _write_headers ( msg ) <EOL> else : <EOL> meth ( self ) <EOL> self . _fp . write ( sfp . getvalue ( ) ) <EOL> def _dispatch ( self , msg ) : <EOL> main = msg . get_content_maintype ( ) <EOL> sub = msg . get_content_subtype ( ) <EOL> specific = UNDERSCORE . join ( ( main , sub ) ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> meth = getattr ( self , '<STR_LIT>' + specific , None ) <EOL> if meth is None : <EOL> generic = main . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> meth = getattr ( self , '<STR_LIT>' + generic , None ) <EOL> if meth is None : <EOL> meth = self . _writeBody <EOL> meth ( msg ) <EOL> def _write_headers ( self , msg ) : <EOL> for h , v in msg . items ( ) : <EOL> print >> self . _fp , '<STR_LIT>' % h , <EOL> if self . _maxheaderlen == <NUM_LIT:0> : <EOL> print >> self . _fp , v <EOL> elif isinstance ( v , Header ) : <EOL> print >> self . _fp , v . encode ( ) <EOL> elif _is8bitstring ( v ) : <EOL> print >> self . _fp , v <EOL> else : <EOL> print >> self . _fp , Header ( <EOL> v , maxlinelen = self . _maxheaderlen , <EOL> header_name = h , continuation_ws = '<STR_LIT:\t>' ) . encode ( ) <EOL> print >> self . _fp <EOL> def _handle_text ( self , msg ) : <EOL> payload = msg . get_payload ( ) <EOL> if payload is None : <EOL> return <EOL> if not isinstance ( payload , basestring ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( payload ) ) <EOL> if self . _mangle_from_ : <EOL> payload = fcre . sub ( '<STR_LIT>' , payload ) <EOL> self . _fp . write ( payload ) <EOL> _writeBody = _handle_text <EOL> def _handle_multipart ( self , msg ) : <EOL> msgtexts = [ ] <EOL> subparts = msg . get_payload ( ) <EOL> if subparts is None : <EOL> subparts = [ ] <EOL> elif isinstance ( subparts , basestring ) : <EOL> self . _fp . write ( subparts ) <EOL> return <EOL> elif not isinstance ( subparts , list ) : <EOL> subparts = [ subparts ] <EOL> for part in subparts : <EOL> s = StringIO ( ) <EOL> g = self . clone ( s ) <EOL> g . flatten ( part , unixfrom = False ) <EOL> msgtexts . append ( s . getvalue ( ) ) <EOL> alltext = NL . join ( msgtexts ) <EOL> boundary = msg . get_boundary ( failobj = _make_boundary ( alltext ) ) <EOL> if msg . get_boundary ( ) < > boundary : <EOL> msg . set_boundary ( boundary ) <EOL> if msg . preamble is not None : <EOL> print >> self . _fp , msg . preamble <EOL> print >> self . _fp , '<STR_LIT>' + boundary <EOL> if msgtexts : <EOL> self . _fp . write ( msgtexts . pop ( <NUM_LIT:0> ) ) <EOL> for body_part in msgtexts : <EOL> print >> self . _fp , '<STR_LIT>' + boundary <EOL> self . _fp . write ( body_part ) <EOL> self . _fp . write ( '<STR_LIT>' + boundary + '<STR_LIT>' ) <EOL> if msg . epilogue is not None : <EOL> print >> self . _fp <EOL> self . _fp . write ( msg . epilogue ) <EOL> def _handle_message_delivery_status ( self , msg ) : <EOL> blocks = [ ] <EOL> for part in msg . get_payload ( ) : <EOL> s = StringIO ( ) <EOL> g = self . clone ( s ) <EOL> g . flatten ( part , unixfrom = False ) <EOL> text = s . getvalue ( ) <EOL> lines = text . split ( '<STR_LIT:\n>' ) <EOL> if lines and lines [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> blocks . append ( NL . join ( lines [ : - <NUM_LIT:1> ] ) ) <EOL> else : <EOL> blocks . append ( text ) <EOL> self . _fp . write ( NL . join ( blocks ) ) <EOL> def _handle_message ( self , msg ) : <EOL> s = StringIO ( ) <EOL> g = self . clone ( s ) <EOL> g . flatten ( msg . get_payload ( <NUM_LIT:0> ) , unixfrom = False ) <EOL> self . _fp . write ( s . getvalue ( ) ) <EOL> _FMT = '<STR_LIT>' <EOL> class DecodedGenerator ( Generator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outfp , mangle_from_ = True , maxheaderlen = <NUM_LIT> , fmt = None ) : <EOL> """<STR_LIT>""" <EOL> Generator . __init__ ( self , outfp , mangle_from_ , maxheaderlen ) <EOL> if fmt is None : <EOL> self . _fmt = _FMT <EOL> else : <EOL> self . _fmt = fmt <EOL> def _dispatch ( self , msg ) : <EOL> for part in msg . walk ( ) : <EOL> maintype = part . get_content_maintype ( ) <EOL> if maintype == '<STR_LIT:text>' : <EOL> print >> self , part . get_payload ( decode = True ) <EOL> elif maintype == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> print >> self , self . _fmt % { <EOL> '<STR_LIT:type>' : part . get_content_type ( ) , <EOL> '<STR_LIT>' : part . get_content_maintype ( ) , <EOL> '<STR_LIT>' : part . get_content_subtype ( ) , <EOL> '<STR_LIT:filename>' : part . get_filename ( '<STR_LIT>' ) , <EOL> '<STR_LIT:description>' : part . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : part . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> } <EOL> _width = len ( repr ( sys . maxint - <NUM_LIT:1> ) ) <EOL> _fmt = '<STR_LIT>' % _width <EOL> def _make_boundary ( text = None ) : <EOL> token = random . randrange ( sys . maxint ) <EOL> boundary = ( '<STR_LIT:=>' * <NUM_LIT:15> ) + ( _fmt % token ) + '<STR_LIT>' <EOL> if text is None : <EOL> return boundary <EOL> b = boundary <EOL> counter = <NUM_LIT:0> <EOL> while True : <EOL> cre = re . compile ( '<STR_LIT>' + re . escape ( b ) + '<STR_LIT>' , re . MULTILINE ) <EOL> if not cre . search ( text ) : <EOL> break <EOL> b = boundary + '<STR_LIT:.>' + str ( counter ) <EOL> counter += <NUM_LIT:1> <EOL> return b </s>
