<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import division <EOL> from __future__ import absolute_import <EOL> from future . builtins import super <EOL> from future . builtins import str <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> import re <EOL> import sys <EOL> import time <EOL> import random <EOL> import warnings <EOL> from io import StringIO , BytesIO <EOL> from future . backports . email . _policybase import compat32 <EOL> from future . backports . email . header import Header <EOL> from future . backports . email . utils import _has_surrogates <EOL> import future . backports . email . charset as _charset <EOL> UNDERSCORE = '<STR_LIT:_>' <EOL> NL = '<STR_LIT:\n>' <EOL> fcre = re . compile ( r'<STR_LIT>' , re . MULTILINE ) <EOL> class Generator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outfp , mangle_from_ = True , maxheaderlen = None , ** _3to2kwargs ) : <EOL> if '<STR_LIT>' in _3to2kwargs : policy = _3to2kwargs [ '<STR_LIT>' ] ; del _3to2kwargs [ '<STR_LIT>' ] <EOL> else : policy = None <EOL> """<STR_LIT>""" <EOL> self . _fp = outfp <EOL> self . _mangle_from_ = mangle_from_ <EOL> self . maxheaderlen = maxheaderlen <EOL> self . policy = policy <EOL> def write ( self , s ) : <EOL> self . _fp . write ( s ) <EOL> def flatten ( self , msg , unixfrom = False , linesep = None ) : <EOL> r"""<STR_LIT>""" <EOL> policy = msg . policy if self . policy is None else self . policy <EOL> if linesep is not None : <EOL> policy = policy . clone ( linesep = linesep ) <EOL> if self . maxheaderlen is not None : <EOL> policy = policy . clone ( max_line_length = self . maxheaderlen ) <EOL> self . _NL = policy . linesep <EOL> self . _encoded_NL = self . _encode ( self . _NL ) <EOL> self . _EMPTY = '<STR_LIT>' <EOL> self . _encoded_EMTPY = self . _encode ( '<STR_LIT>' ) <EOL> old_gen_policy = self . policy <EOL> old_msg_policy = msg . policy <EOL> try : <EOL> self . policy = policy <EOL> msg . policy = policy <EOL> if unixfrom : <EOL> ufrom = msg . get_unixfrom ( ) <EOL> if not ufrom : <EOL> ufrom = '<STR_LIT>' + time . ctime ( time . time ( ) ) <EOL> self . write ( ufrom + self . _NL ) <EOL> self . _write ( msg ) <EOL> finally : <EOL> self . policy = old_gen_policy <EOL> msg . policy = old_msg_policy <EOL> def clone ( self , fp ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( fp , <EOL> self . _mangle_from_ , <EOL> None , <EOL> policy = self . policy ) <EOL> _encoded_EMPTY = '<STR_LIT>' <EOL> def _new_buffer ( self ) : <EOL> return StringIO ( ) <EOL> def _encode ( self , s ) : <EOL> return s <EOL> def _write_lines ( self , lines ) : <EOL> if not lines : <EOL> return <EOL> lines = lines . splitlines ( True ) <EOL> for line in lines [ : - <NUM_LIT:1> ] : <EOL> self . write ( line . rstrip ( '<STR_LIT:\r\n>' ) ) <EOL> self . write ( self . _NL ) <EOL> laststripped = lines [ - <NUM_LIT:1> ] . rstrip ( '<STR_LIT:\r\n>' ) <EOL> self . write ( laststripped ) <EOL> if len ( lines [ - <NUM_LIT:1> ] ) != len ( laststripped ) : <EOL> self . write ( self . _NL ) <EOL> def _write ( self , msg ) : <EOL> oldfp = self . _fp <EOL> try : <EOL> self . _fp = sfp = self . _new_buffer ( ) <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 . raw_items ( ) : <EOL> self . write ( self . policy . fold ( h , v ) ) <EOL> self . write ( self . _NL ) <EOL> def _handle_text ( self , msg ) : <EOL> payload = msg . get_payload ( ) <EOL> if payload is None : <EOL> return <EOL> if not isinstance ( payload , str ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( payload ) ) <EOL> if _has_surrogates ( msg . _payload ) : <EOL> charset = msg . get_param ( '<STR_LIT>' ) <EOL> if charset is not None : <EOL> del msg [ '<STR_LIT>' ] <EOL> msg . set_payload ( payload , charset ) <EOL> payload = msg . get_payload ( ) <EOL> if self . _mangle_from_ : <EOL> payload = fcre . sub ( '<STR_LIT>' , payload ) <EOL> self . _write_lines ( 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 , str ) : <EOL> self . write ( subparts ) <EOL> return <EOL> elif not isinstance ( subparts , list ) : <EOL> subparts = [ subparts ] <EOL> for part in subparts : <EOL> s = self . _new_buffer ( ) <EOL> g = self . clone ( s ) <EOL> g . flatten ( part , unixfrom = False , linesep = self . _NL ) <EOL> msgtexts . append ( s . getvalue ( ) ) <EOL> boundary = msg . get_boundary ( ) <EOL> if not boundary : <EOL> alltext = self . _encoded_NL . join ( msgtexts ) <EOL> boundary = self . _make_boundary ( alltext ) <EOL> msg . set_boundary ( boundary ) <EOL> if msg . preamble is not None : <EOL> if self . _mangle_from_ : <EOL> preamble = fcre . sub ( '<STR_LIT>' , msg . preamble ) <EOL> else : <EOL> preamble = msg . preamble <EOL> self . _write_lines ( preamble ) <EOL> self . write ( self . _NL ) <EOL> self . write ( '<STR_LIT>' + boundary + self . _NL ) <EOL> if msgtexts : <EOL> self . _fp . write ( msgtexts . pop ( <NUM_LIT:0> ) ) <EOL> for body_part in msgtexts : <EOL> self . write ( self . _NL + '<STR_LIT>' + boundary + self . _NL ) <EOL> self . _fp . write ( body_part ) <EOL> self . write ( self . _NL + '<STR_LIT>' + boundary + '<STR_LIT>' ) <EOL> if msg . epilogue is not None : <EOL> self . write ( self . _NL ) <EOL> if self . _mangle_from_ : <EOL> epilogue = fcre . sub ( '<STR_LIT>' , msg . epilogue ) <EOL> else : <EOL> epilogue = msg . epilogue <EOL> self . _write_lines ( epilogue ) <EOL> def _handle_multipart_signed ( self , msg ) : <EOL> p = self . policy <EOL> self . policy = p . clone ( max_line_length = <NUM_LIT:0> ) <EOL> try : <EOL> self . _handle_multipart ( msg ) <EOL> finally : <EOL> self . policy = p <EOL> def _handle_message_delivery_status ( self , msg ) : <EOL> blocks = [ ] <EOL> for part in msg . get_payload ( ) : <EOL> s = self . _new_buffer ( ) <EOL> g = self . clone ( s ) <EOL> g . flatten ( part , unixfrom = False , linesep = self . _NL ) <EOL> text = s . getvalue ( ) <EOL> lines = text . split ( self . _encoded_NL ) <EOL> if lines and lines [ - <NUM_LIT:1> ] == self . _encoded_EMPTY : <EOL> blocks . append ( self . _encoded_NL . join ( lines [ : - <NUM_LIT:1> ] ) ) <EOL> else : <EOL> blocks . append ( text ) <EOL> self . _fp . write ( self . _encoded_NL . join ( blocks ) ) <EOL> def _handle_message ( self , msg ) : <EOL> s = self . _new_buffer ( ) <EOL> g = self . clone ( s ) <EOL> payload = msg . _payload <EOL> if isinstance ( payload , list ) : <EOL> g . flatten ( msg . get_payload ( <NUM_LIT:0> ) , unixfrom = False , linesep = self . _NL ) <EOL> payload = s . getvalue ( ) <EOL> else : <EOL> payload = self . _encode ( payload ) <EOL> self . _fp . write ( payload ) <EOL> @ classmethod <EOL> def _make_boundary ( cls , text = None ) : <EOL> token = random . randrange ( sys . maxsize ) <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 = cls . _compile_re ( '<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 <EOL> @ classmethod <EOL> def _compile_re ( cls , s , flags ) : <EOL> return re . compile ( s , flags ) <EOL> class BytesGenerator ( Generator ) : <EOL> """<STR_LIT>""" <EOL> _encoded_EMPTY = b'<STR_LIT>' <EOL> def write ( self , s ) : <EOL> self . _fp . write ( str ( s ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) ) <EOL> def _new_buffer ( self ) : <EOL> return BytesIO ( ) <EOL> def _encode ( self , s ) : <EOL> return s . encode ( '<STR_LIT:ascii>' ) <EOL> def _write_headers ( self , msg ) : <EOL> for h , v in msg . raw_items ( ) : <EOL> self . _fp . write ( self . policy . fold_binary ( h , v ) ) <EOL> self . write ( self . _NL ) <EOL> def _handle_text ( self , msg ) : <EOL> if msg . _payload is None : <EOL> return <EOL> if _has_surrogates ( msg . _payload ) and not self . policy . cte_type == '<STR_LIT>' : <EOL> if self . _mangle_from_ : <EOL> msg . _payload = fcre . sub ( "<STR_LIT>" , msg . _payload ) <EOL> self . _write_lines ( msg . _payload ) <EOL> else : <EOL> super ( BytesGenerator , self ) . _handle_text ( msg ) <EOL> _writeBody = _handle_text <EOL> @ classmethod <EOL> def _compile_re ( cls , s , flags ) : <EOL> return re . compile ( s . encode ( '<STR_LIT:ascii>' ) , flags ) <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 ( part . get_payload ( decode = False ) , file = self ) <EOL> elif maintype == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> print ( 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> } , file = self ) <EOL> _width = len ( repr ( sys . maxsize - <NUM_LIT:1> ) ) <EOL> _fmt = '<STR_LIT>' % _width <EOL> _make_boundary = Generator . _make_boundary </s>
