a 97aX@sdZddlmZddlmZddlmZddlmZmZmZddl m Z m Z ddgZ dd l Z dd lmZdd lmZdd lmZe d Ze dZe dZe dZe dZdZdZeZGdddeZGdddeZGdddeZd S)aFeedParser - An email feed parser. The feed parser implements an interface for incrementally parsing an email message, line by line. This has advantages for certain applications, such as those reading email messages off a socket. FeedParser.feed() is the primary interface for pushing new data into the parser. It returns when there's nothing more it can do with the available data. When you have no more data to push into the parser, call .close(). This completes the parsing and returns the root message object. The other advantage of this parser is that it will never raise a parsing exception. Instead, when it finds something unexpected, it adds a 'defect' to the current message. Defects are just instances that live on the message object's .defects attribute. )unicode_literals)division)absolute_import)objectrangesuper)implements_iteratorPY3 FeedParserBytesFeedParserN)errors)message)compat32z | | z( | | )z ( | | )\Zz(^(From |[\041-\071\073-\176]{1,}:|[\t ]) c@s`eZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)BufferedSubFileakA file-ish object that can have new data loaded into it. You can also push and pop line-matching predicates onto a stack. When the current predicate matches the current line, a false EOF response (i.e. empty string) is returned instead. This lets the parser adhere to a simple abstraction -- it parses until EOF closes the current message. cCsd|_g|_g|_d|_dS)NrF)_partial_lines _eofstack_closedselfr|/private/var/folders/s6/9n5zrl012gv99k63s4q6ccsd4s6mqz/T/pip-target-f5cq3f2q/lib/python/future/backports/email/feedparser.py__init__9szBufferedSubFile.__init__cCs|j|dSN)rappend)rpredrrrpush_eof_matcherCsz BufferedSubFile.push_eof_matchercCs |jSr)rpoprrrrpop_eof_matcherFszBufferedSubFile.pop_eof_matchercCs|j|jd|_d|_dS)NrT)rrrrrrrrcloseIszBufferedSubFile.closecCsR|js|jrdStS|j}|jdddD]}||r.|j|dSq.|S)Nr)rr NeedMoreDatarrr)rlineateofrrrreadlineOs  zBufferedSubFile.readlinecCs|tus J|j|dSr)r#rrrr$rrr unreadlineas zBufferedSubFile.unreadlinecCs|j|d}|_t|}||_|jsR|rR|ddrR|d||_g}tt|dD]&}|||d||ddqf||dS)z$Push some new data into this object.rr" N) r NLCRE_cracksplitrendswithrlenr pushlines)rdatapartslinesirrrpushfs  $zBufferedSubFile.pushcCs|ddd|jdd<dS)Nr"r)r)rr4rrrr1}szBufferedSubFile.pushlinescCs|Srrrrrr__iter__szBufferedSubFile.__iter__cCs|}|dkrt|S)Nr)r& StopIterationr'rrr__next__szBufferedSubFile.__next__N)__name__ __module__ __qualname____doc__rrr r!r&r(r6r1r7r9rrrrr1s rc@s^eZdZdZejfddZddZddZdd Z d d Z d d Z ddZ ddZ ddZdS)r zA feed-style parser of email.c sd|vr|d}|d=nt}|_|_z|jdfdd_Wntybdd_Yn0t_g_tr j _ n j _ d_ d_d_dS)Npolicyr>cs djiS)Nr>r?rrrrz%FeedParser.__init__..cSsiSrrrrrrr@rAF)r_factoryr> _factory_kwds TypeErrorr_input _msgstackr _parsegenr9_parsenext_cur_last _headersonly)rrBZ _3to2kwargsr>rrrrs"   zFeedParser.__init__cCs d|_dS)NT)rLrrrr_set_headersonlyszFeedParser._set_headersonlycCs|j||dS)zPush more data into the parser.N)rEr6 _call_parserr2rrrfeeds zFeedParser.feedcCs$z |WntyYn0dSr)rHr8rrrrrNs  zFeedParser._call_parsecCsR|j||}|jr$J|dkrN|sNt}|j |||S)z handle_defect)rrootdefectrrrr!s   zFeedParser.closecCsd|jfi|}|jr2|jdkr2|d|jrH|jd||j|||_||_dS)Nzmultipart/digestzmessage/rfc822r") rBrCrJget_content_typeset_default_typerFattachrrK)rmsgrrr _new_messages  zFeedParser._new_messagecCs(|j}|jr|jd|_nd|_|S)Nr")rFrrJ)rretvalrrrrRs  zFeedParser._pop_messageccs |g}|jD]Z}|tur&tVqt|sbt|s^t}|j |j ||j |qn| |q| ||jrg}|j}|turtVq|dkrq| |q|j t|dS|j dkr|jtj|D]}|turtVqq q|}|j|j}|turDtVqqDq|j}|turjtVqDqjqD|dkrxq|j |qdS|j dkr|D] }|turtVqqĐq|dS|j dkr|j }|durRt}|j |j |g}|jD]$}|tur.tVq| |q|j t|dS|j dddvrt}|j |j |d|}td t |d } d } g} d } d } |j}|turtVq|dkrސq| |}|r|!d r d } |!d} q| rn| rZ| d}t"#|}|rL|dt$|!d | d<t| |j _%d } |j |q|j}|turtVqn| |}|sn|j |qqn|j| j|D] }|turtVqqq|j&dkrP|j&j'}|dkrd|j&_'n:|durt"#|}|rt$|!d}|d| |j&_'nD|j&j(}t)|t*rt"#|}|r|dt$|!d }||j&_(|j||j |_&n| sJ| |q| r0t+}|j |j ||j t| g}|jD]}|turtVqqt||j _'dS| sRt,}|j |j |dS| r`dg}ng}|jD]$}|turtVqj| |qj|r|d}t-|}|r|t$|!dd|d<t||j _'dSg}|jD]$}|turtVq| |q|j t|dS)Nrzmessage/delivery-statusr rQzcontent-transfer-encoding8bit)7bitr_binaryz--z(?Pz4)(?P--)?(?P[ \t]*)(?P\r\n|\r|\n)?$TFendlinesepr"r).r]rEr#headerREmatchNLCREr MissingHeaderBodySeparatorDefectr>rVrJr(r_parse_headersrLr& set_payload EMPTYSTRINGjoinrYrrGrRr rS get_boundaryNoBoundaryInMultipartDefectgetlower-InvalidMultipartContentTransferEncodingDefectrecompileescapegroup NLCRE_eolsearchr0preamblerKepilogue_payload isinstancestrStartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect NLCRE_bol)rheadersr$rXr4r^r\boundary separator boundaryrecapturing_preamblerwrcclose_boundary_seenmolastlineeolmorxrbpayload firstlinebolmorrrrGsb                                                       zFeedParser._parsegenc CsFd}g}t|D]\}}|ddvrR|sFt|}|j|j|q||q|rt|jj|j|dg}}| dr|dkrt |}|r|dt | d }|j|qn<|t |dkr|j|dSt|}|jj|q|d}|dksJd|d|}|g}q|rB|jj|j|dS)Nrrz zFrom r,:z3_parse_headers fed line with no : and no leading WS) enumerater #FirstHeaderLineIsContinuationDefectr>rVrJrset_rawheader_source_parse startswithrurvr0rt set_unixfromrEr(MisplacedEnvelopeHeaderDefectdefectsfind) rr4 lastheader lastvaluelinenor$rXrr5rrrrhs@           zFeedParser._parse_headersN)r:r;r<r=r MessagerrMrPrNr!r]rRrGrhrrrrr s  ~cs eZdZdZfddZZS)r z(Like FeedParser, but feed accepts bytes.cst|dddS)Nasciisurrogateescape)rrPdecoderO __class__rrrP szBytesFeedParser.feed)r:r;r<r=rP __classcell__rrrrr s)r= __future__rrrZfuture.builtinsrrrZ future.utilsrr __all__rqZfuture.backports.emailr r Z"future.backports.email._policybaserrrrfr~rur-rdrjNLr#rr r rrrrs0           Z