<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> import re <EOL> from email import errors <EOL> from email import message <EOL> NLCRE = re . compile ( '<STR_LIT>' ) <EOL> NLCRE_bol = re . compile ( '<STR_LIT>' ) <EOL> NLCRE_eol = re . compile ( '<STR_LIT>' ) <EOL> NLCRE_crack = re . compile ( '<STR_LIT>' ) <EOL> headerRE = re . compile ( r'<STR_LIT>' ) <EOL> EMPTYSTRING = '<STR_LIT>' <EOL> NL = '<STR_LIT:\n>' <EOL> NeedMoreData = object ( ) <EOL> class BufferedSubFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _partial = '<STR_LIT>' <EOL> self . _lines = [ ] <EOL> self . _eofstack = [ ] <EOL> self . _closed = False <EOL> def push_eof_matcher ( self , pred ) : <EOL> self . _eofstack . append ( pred ) <EOL> def pop_eof_matcher ( self ) : <EOL> return self . _eofstack . pop ( ) <EOL> def close ( self ) : <EOL> self . _lines . append ( self . _partial ) <EOL> self . _partial = '<STR_LIT>' <EOL> self . _closed = True <EOL> def readline ( self ) : <EOL> if not self . _lines : <EOL> if self . _closed : <EOL> return '<STR_LIT>' <EOL> return NeedMoreData <EOL> line = self . _lines . pop ( ) <EOL> for ateof in self . _eofstack [ : : - <NUM_LIT:1> ] : <EOL> if ateof ( line ) : <EOL> self . _lines . append ( line ) <EOL> return '<STR_LIT>' <EOL> return line <EOL> def unreadline ( self , line ) : <EOL> assert line is not NeedMoreData <EOL> self . _lines . append ( line ) <EOL> def push ( self , data ) : <EOL> """<STR_LIT>""" <EOL> data , self . _partial = self . _partial + data , '<STR_LIT>' <EOL> parts = NLCRE_crack . split ( data ) <EOL> self . _partial = parts . pop ( ) <EOL> if not self . _partial and parts and parts [ - <NUM_LIT:1> ] . endswith ( '<STR_LIT:\r>' ) : <EOL> self . _partial = parts . pop ( - <NUM_LIT:2> ) + parts . pop ( ) <EOL> lines = [ ] <EOL> for i in range ( len ( parts ) // <NUM_LIT:2> ) : <EOL> lines . append ( parts [ i * <NUM_LIT:2> ] + parts [ i * <NUM_LIT:2> + <NUM_LIT:1> ] ) <EOL> self . pushlines ( lines ) <EOL> def pushlines ( self , lines ) : <EOL> self . _lines [ : <NUM_LIT:0> ] = lines [ : : - <NUM_LIT:1> ] <EOL> def is_closed ( self ) : <EOL> return self . _closed <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> line = self . readline ( ) <EOL> if line == '<STR_LIT>' : <EOL> raise StopIteration <EOL> return line <EOL> class FeedParser : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , _factory = message . Message ) : <EOL> """<STR_LIT>""" <EOL> self . _factory = _factory <EOL> self . _input = BufferedSubFile ( ) <EOL> self . _msgstack = [ ] <EOL> self . _parse = self . _parsegen ( ) . next <EOL> self . _cur = None <EOL> self . _last = None <EOL> self . _headersonly = False <EOL> def _set_headersonly ( self ) : <EOL> self . _headersonly = True <EOL> def feed ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . _input . push ( data ) <EOL> self . _call_parse ( ) <EOL> def _call_parse ( self ) : <EOL> try : <EOL> self . _parse ( ) <EOL> except StopIteration : <EOL> pass <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _input . close ( ) <EOL> self . _call_parse ( ) <EOL> root = self . _pop_message ( ) <EOL> assert not self . _msgstack <EOL> if root . get_content_maintype ( ) == '<STR_LIT>' and not root . is_multipart ( ) : <EOL> root . defects . append ( errors . MultipartInvariantViolationDefect ( ) ) <EOL> return root <EOL> def _new_message ( self ) : <EOL> msg = self . _factory ( ) <EOL> if self . _cur and self . _cur . get_content_type ( ) == '<STR_LIT>' : <EOL> msg . set_default_type ( '<STR_LIT>' ) <EOL> if self . _msgstack : <EOL> self . _msgstack [ - <NUM_LIT:1> ] . attach ( msg ) <EOL> self . _msgstack . append ( msg ) <EOL> self . _cur = msg <EOL> self . _last = msg <EOL> def _pop_message ( self ) : <EOL> retval = self . _msgstack . pop ( ) <EOL> if self . _msgstack : <EOL> self . _cur = self . _msgstack [ - <NUM_LIT:1> ] <EOL> else : <EOL> self . _cur = None <EOL> return retval <EOL> def _parsegen ( self ) : <EOL> self . _new_message ( ) <EOL> headers = [ ] <EOL> for line in self . _input : <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> if not headerRE . match ( line ) : <EOL> if not NLCRE . match ( line ) : <EOL> self . _input . unreadline ( line ) <EOL> break <EOL> headers . append ( line ) <EOL> self . _parse_headers ( headers ) <EOL> if self . _headersonly : <EOL> lines = [ ] <EOL> while True : <EOL> line = self . _input . readline ( ) <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> if line == '<STR_LIT>' : <EOL> break <EOL> lines . append ( line ) <EOL> self . _cur . set_payload ( EMPTYSTRING . join ( lines ) ) <EOL> return <EOL> if self . _cur . get_content_type ( ) == '<STR_LIT>' : <EOL> while True : <EOL> self . _input . push_eof_matcher ( NLCRE . match ) <EOL> for retval in self . _parsegen ( ) : <EOL> if retval is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> break <EOL> msg = self . _pop_message ( ) <EOL> self . _input . pop_eof_matcher ( ) <EOL> while True : <EOL> line = self . _input . readline ( ) <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> break <EOL> while True : <EOL> line = self . _input . readline ( ) <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> break <EOL> if line == '<STR_LIT>' : <EOL> break <EOL> self . _input . unreadline ( line ) <EOL> return <EOL> if self . _cur . get_content_maintype ( ) == '<STR_LIT:message>' : <EOL> for retval in self . _parsegen ( ) : <EOL> if retval is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> break <EOL> self . _pop_message ( ) <EOL> return <EOL> if self . _cur . get_content_maintype ( ) == '<STR_LIT>' : <EOL> boundary = self . _cur . get_boundary ( ) <EOL> if boundary is None : <EOL> self . _cur . defects . append ( errors . NoBoundaryInMultipartDefect ( ) ) <EOL> lines = [ ] <EOL> for line in self . _input : <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> lines . append ( line ) <EOL> self . _cur . set_payload ( EMPTYSTRING . join ( lines ) ) <EOL> return <EOL> separator = '<STR_LIT>' + boundary <EOL> boundaryre = re . compile ( <EOL> '<STR_LIT>' + re . escape ( separator ) + <EOL> r'<STR_LIT>' ) <EOL> capturing_preamble = True <EOL> preamble = [ ] <EOL> linesep = False <EOL> while True : <EOL> line = self . _input . readline ( ) <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> if line == '<STR_LIT>' : <EOL> break <EOL> mo = boundaryre . match ( line ) <EOL> if mo : <EOL> if mo . group ( '<STR_LIT:end>' ) : <EOL> linesep = mo . group ( '<STR_LIT>' ) <EOL> break <EOL> if capturing_preamble : <EOL> if preamble : <EOL> lastline = preamble [ - <NUM_LIT:1> ] <EOL> eolmo = NLCRE_eol . search ( lastline ) <EOL> if eolmo : <EOL> preamble [ - <NUM_LIT:1> ] = lastline [ : - len ( eolmo . group ( <NUM_LIT:0> ) ) ] <EOL> self . _cur . preamble = EMPTYSTRING . join ( preamble ) <EOL> capturing_preamble = False <EOL> self . _input . unreadline ( line ) <EOL> continue <EOL> while True : <EOL> line = self . _input . readline ( ) <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> mo = boundaryre . match ( line ) <EOL> if not mo : <EOL> self . _input . unreadline ( line ) <EOL> break <EOL> self . _input . push_eof_matcher ( boundaryre . match ) <EOL> for retval in self . _parsegen ( ) : <EOL> if retval is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> break <EOL> if self . _last . get_content_maintype ( ) == '<STR_LIT>' : <EOL> epilogue = self . _last . epilogue <EOL> if epilogue == '<STR_LIT>' : <EOL> self . _last . epilogue = None <EOL> elif epilogue is not None : <EOL> mo = NLCRE_eol . search ( epilogue ) <EOL> if mo : <EOL> end = len ( mo . group ( <NUM_LIT:0> ) ) <EOL> self . _last . epilogue = epilogue [ : - end ] <EOL> else : <EOL> payload = self . _last . get_payload ( ) <EOL> if isinstance ( payload , basestring ) : <EOL> mo = NLCRE_eol . search ( payload ) <EOL> if mo : <EOL> payload = payload [ : - len ( mo . group ( <NUM_LIT:0> ) ) ] <EOL> self . _last . set_payload ( payload ) <EOL> self . _input . pop_eof_matcher ( ) <EOL> self . _pop_message ( ) <EOL> self . _last = self . _cur <EOL> else : <EOL> assert capturing_preamble <EOL> preamble . append ( line ) <EOL> if capturing_preamble : <EOL> self . _cur . defects . append ( errors . StartBoundaryNotFoundDefect ( ) ) <EOL> self . _cur . set_payload ( EMPTYSTRING . join ( preamble ) ) <EOL> epilogue = [ ] <EOL> for line in self . _input : <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> self . _cur . epilogue = EMPTYSTRING . join ( epilogue ) <EOL> return <EOL> if linesep : <EOL> epilogue = [ '<STR_LIT>' ] <EOL> else : <EOL> epilogue = [ ] <EOL> for line in self . _input : <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> epilogue . append ( line ) <EOL> if epilogue : <EOL> firstline = epilogue [ <NUM_LIT:0> ] <EOL> bolmo = NLCRE_bol . match ( firstline ) <EOL> if bolmo : <EOL> epilogue [ <NUM_LIT:0> ] = firstline [ len ( bolmo . group ( <NUM_LIT:0> ) ) : ] <EOL> self . _cur . epilogue = EMPTYSTRING . join ( epilogue ) <EOL> return <EOL> lines = [ ] <EOL> for line in self . _input : <EOL> if line is NeedMoreData : <EOL> yield NeedMoreData <EOL> continue <EOL> lines . append ( line ) <EOL> self . _cur . set_payload ( EMPTYSTRING . join ( lines ) ) <EOL> def _parse_headers ( self , lines ) : <EOL> lastheader = '<STR_LIT>' <EOL> lastvalue = [ ] <EOL> for lineno , line in enumerate ( lines ) : <EOL> if line [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> if not lastheader : <EOL> defect = errors . FirstHeaderLineIsContinuationDefect ( line ) <EOL> self . _cur . defects . append ( defect ) <EOL> continue <EOL> lastvalue . append ( line ) <EOL> continue <EOL> if lastheader : <EOL> lhdr = EMPTYSTRING . join ( lastvalue ) [ : - <NUM_LIT:1> ] . rstrip ( '<STR_LIT:\r\n>' ) <EOL> self . _cur [ lastheader ] = lhdr <EOL> lastheader , lastvalue = '<STR_LIT>' , [ ] <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> if lineno == <NUM_LIT:0> : <EOL> mo = NLCRE_eol . search ( line ) <EOL> if mo : <EOL> line = line [ : - len ( mo . group ( <NUM_LIT:0> ) ) ] <EOL> self . _cur . set_unixfrom ( line ) <EOL> continue <EOL> elif lineno == len ( lines ) - <NUM_LIT:1> : <EOL> self . _input . unreadline ( line ) <EOL> return <EOL> else : <EOL> defect = errors . MisplacedEnvelopeHeaderDefect ( line ) <EOL> self . _cur . defects . append ( defect ) <EOL> continue <EOL> i = line . find ( '<STR_LIT::>' ) <EOL> if i < <NUM_LIT:0> : <EOL> defect = errors . MalformedHeaderDefect ( line ) <EOL> self . _cur . defects . append ( defect ) <EOL> continue <EOL> lastheader = line [ : i ] <EOL> lastvalue = [ line [ i + <NUM_LIT:1> : ] . lstrip ( ) ] <EOL> if lastheader : <EOL> self . _cur [ lastheader ] = EMPTYSTRING . join ( lastvalue ) . rstrip ( '<STR_LIT:\r\n>' ) </s>
