o ,]X@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_closedselfrC/usr/lib/python3/dist-packages/future/backports/email/feedparser.py__init__9s zBufferedSubFile.__init__cCs|j|dSN)rappend)rpredrrrpush_eof_matcherCsz BufferedSubFile.push_eof_matchercCs |jSr)rpoprrrrpop_eof_matcherF zBufferedSubFile.pop_eof_matchercCs|j|jd|_d|_dS)NrT)rrrrrrrrcloseIs zBufferedSubFile.closecCsR|js |jrdStS|j}|jdddD]}||r&|j|dSq|S)Nr)rr NeedMoreDatarrr)rlineateofrrrreadlineOs  zBufferedSubFile.readlinecCs|tusJ|j|dSr)r$rrrr%rrr unreadlineas zBufferedSubFile.unreadlinecCs|j|d}|_t|}||_|js)|r)|ddr)|d||_g}tt|dD]}|||d||ddq3||dS)z$Push some new data into this object.rr# N) r NLCRE_cracksplitrendswithrlenr pushlines)rdatapartslinesirrrpushfs  $zBufferedSubFile.pushcCs|ddd|jdd<dS)Nr#r)r)rr5rrrr2}szBufferedSubFile.pushlinescCs|Srrrrrr__iter__szBufferedSubFile.__iter__cCs|}|dkr t|S)Nr)r' StopIterationr(rrr__next__szBufferedSubFile.__next__N)__name__ __module__ __qualname____doc__rrr r"r'r)r7r2r8r:rrrrr1s  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_Wnty2dd_Ynwt_g_trC j _ n j _ d_ d_d_dS)Npolicyr?cs djiS)Nr?r@rrrrs z%FeedParser.__init__..cSsiSrrrrrrrAsF)r_factoryr? _factory_kwds TypeErrorr_input _msgstackr _parsegenr:_parsenext_cur_last _headersonly)rrB _3to2kwargsr?rrrrs&    zFeedParser.__init__cCs d|_dS)NT)rLrrrr_set_headersonlyr!zFeedParser._set_headersonlycCs|j||dS)zPush more data into the parser.N)rEr7 _call_parserr3rrrfeeds  zFeedParser.feedcCs$z|WdStyYdSwr)rHr9rrrrrOs  zFeedParser._call_parsecCsR|j||}|jrJ|dkr'|s't}|j |||S)z|!d rpd} |!d} n| r| r| d}t"#|}|r|dt$|!d | d<t| |j _%d } |j |qD |j}|turtVq| |}|s|j |nq|j| j|D] }|turtVq|j&dkr|j&j'}|dkrd|j&_'n@|dur t"#|}|r t$|!d}|d| |j&_'n"|j&j(}t)|t*r0t"#|}|r0|dt$|!d }||j&_(|j||j |_&n | sCJ| |qE| r~t+}|j |j ||j t| g}|jD] }|turstVqgqgt||j _'dS| st,}|j |j |dS| rdg}ng}|jD]}|turtVq| |q|r|d}t-|}|r|t$|!dd|d<t||j _'dSg}|jD]}|turtVq| |q|j t|dS)NTrzmessage/delivery-statusr rRzcontent-transfer-encoding8bit)7bitr`binaryz--z(?Pz4)(?P--)?(?P[ \t]*)(?P\r\n|\r|\n)?$Fendlinesepr#r).r^rEr$headerREmatchNLCREr MissingHeaderBodySeparatorDefectr?rWrJr)r_parse_headersrLr' set_payload EMPTYSTRINGjoinrZrrGrSr rT get_boundaryNoBoundaryInMultipartDefectgetlower-InvalidMultipartContentTransferEncodingDefectrecompileescapegroup NLCRE_eolsearchr1preamblerKepilogue_payload isinstancestrStartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect NLCRE_bol)rheadersr%rYr5r_r]boundary separator boundaryrecapturing_preamblerxrdclose_boundary_seenmolastlineeolmoryrcpayload firstlinebolmorrrrGs|             !                               S       zFeedParser._parsegenc CsBd}g}t|D]\}}|ddvr(|s"t|}|j|j|q||q|r9|jj|j|dg}}| drz|dkr\t |}|rU|dt | d }|j|q|t |dkrm|j|dSt|}|jj|q|d}|dksJd|d|}|g}q|r|jj|j|dSdS)Nrrz zFrom r-:z3_parse_headers fed line with no : and no leading WS) enumerater #FirstHeaderLineIsContinuationDefectr?rWrJrset_rawheader_source_parse startswithrvrwr1ru set_unixfromrEr)MisplacedEnvelopeHeaderDefectdefectsfind) rr5 lastheader lastvaluelinenor%rYrr6rrrrisB           zFeedParser._parse_headersN)r;r<r=r>r MessagerrNrQrOr"r^rSrGrirrrrr s   ~cs eZdZdZfddZZS)r z(Like FeedParser, but feed accepts bytes.cst|dddS)Nasciisurrogateescape)rrQdecoderP __class__rrrQ szBytesFeedParser.feed)r;r<r=r>rQ __classcell__rrrrr s)r> __future__rrrfuture.builtinsrrr future.utilsrr __all__rrfuture.backports.emailr r "future.backports.email._policybaserrsrgrrvr.rerkNLr$rr r rrrrs2           Z