3 jfX@sdZddlmZddlmZddlmZddlmZmZmZddl m Z m Z ddgZ dd l Z dd lmZdd lmZdd lmZe jd Ze jdZe jdZe jdZe jdZdZdZeZGdddeZGdddeZGdddeZd 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_closed)selfrC/tmp/pip-build-s1i5078x/future/future/backports/email/feedparser.py__init__9szBufferedSubFile.__init__cCs|jj|dS)N)rappend)rpredrrrpush_eof_matcherCsz BufferedSubFile.push_eof_matchercCs |jjS)N)rpop)rrrrpop_eof_matcherFszBufferedSubFile.pop_eof_matchercCs|jj|jd|_d|_dS)NrT)rrrr)rrrrcloseIszBufferedSubFile.closecCsT|js|jrdStS|jj}x0|jdddD]}||r0|jj|dSq0W|S)Nr)rr NeedMoreDatarrr)rlineateofrrrreadlineOs  zBufferedSubFile.readlinecCs|tk s t|jj|dS)N)r"AssertionErrorrr)rr#rrr unreadlineas zBufferedSubFile.unreadlinecCs|j|d}|_tj|}|j|_|j rT|rT|djdrT|jd|j|_g}x:tt|dD]&}|j||d||ddqjW|j|dS)z$Push some new data into this object.rr  Nr!) r NLCRE_cracksplitrendswithrlenr pushlines)rdatapartslinesirrrpushfs  &zBufferedSubFile.pushcCs|ddd|jdd<dS)Nr rr!)r)rr2rrrr/}szBufferedSubFile.pushlinescCs|S)Nr)rrrr__iter__szBufferedSubFile.__iter__cCs|j}|dkrt|S)Nr)r% StopIteration)rr#rrr__next__szBufferedSubFile.__next__N)__name__ __module__ __qualname____doc__rrrrr%r'r4r/r5r7rrrrr1s rc@s^eZdZdZejfddZddZddZdd Z d d Z d d Z ddZ ddZ ddZdS)r zA feed-style parser of email.c sd|kr|d}|d=nt}|_|_y|jdfdd_Wntk rddd_YnXt_g_trj j _ n j j _ d_ d_d_dS)Npolicy)r<cs djiS)Nr<)r<r)rrrsz%FeedParser.__init__..cSsiS)Nrrrrrr=sF)r_factoryr< _factory_kwds TypeErrorr_input _msgstackr _parsegenr7_parsenext_cur_last _headersonly)rr>Z _3to2kwargsr<r)rrrs&  zFeedParser.__init__cCs d|_dS)NT)rH)rrrr_set_headersonlyszFeedParser._set_headersonlycCs|jj||jdS)zPush more data into the parser.N)rAr4 _call_parse)rr0rrrfeeds zFeedParser.feedc Cs&y |jWntk r YnXdS)N)rDr6)rrrrrJs zFeedParser._call_parsecCsV|jj|j|j}|j s&t|jdkrR|j rRtj }|j j |||S)zr?rFget_content_typeset_default_typerBattachrrG)rmsgrrr _new_messages  zFeedParser._new_messagecCs(|jj}|jr|jd|_nd|_|S)Nr r!)rBrrF)rretvalrrrrMs  zFeedParser._pop_messageccs@|jg}xb|jD]X}|tkr(tVqtj|sbtj|s`tj}|jj |j ||jj |P|j |qW|j ||jrg}x2|jj}|tkrtVq|dkrP|j |qW|j jtj|dS|j jdkrx|jjtjx$|jD]}|tkrtVqPqW|j}|jjx&|jj}|tkrFtVq(Pq(Wx&|jj}|tkrntVqPPqPW|dkrP|jj |qWdS|j jdkrx(|jD]}|tkrtVqPqW|jdS|j jdkr|j j}|dkrbtj}|jj |j |g}x.|jD]$}|tkrz4)(?P--)?(?P[ \t]*)(?P\r\n|\r|\n)?$TFendlinesepr r)r[rZr\r!r!)/rXrAr"headerREmatchNLCREr MissingHeaderBodySeparatorDefectr<rQrFr'r_parse_headersrHr% set_payload EMPTYSTRINGjoinrTrrCrMrrN get_boundaryNoBoundaryInMultipartDefectgetlower-InvalidMultipartContentTransferEncodingDefectrecompileescapegroup NLCRE_eolsearchr.preamblerGepilogue_payload isinstancestrr&StartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect NLCRE_bol)rheadersr#rSr2rYrWboundary separator boundaryrecapturing_preamblerrr^close_boundary_seenmolastlineeolmorsr]payload firstlinebolmorrrrCsh                                                 zFeedParser._parsegenc CsLd}g}x"t|D]\}}|ddkrV|sJtj|}|jj|j|q|j|q|rx|jj|jj|dg}}|j dr|dkrt j |}|r|dt |j d }|jj|qn:|t |dkr|jj|dStj|}|jjj|q|jd}|dkstd|d|}|g}qW|rH|jj|jj|dS)Nrrz zFrom r :z3_parse_headers fed line with no : and no leading WS) enumerater #FirstHeaderLineIsContinuationDefectr<rQrFrset_rawheader_source_parse startswithrprqr.ro set_unixfromrAr'MisplacedEnvelopeHeaderDefectdefectsfindr&) rr2 lastheader lastvaluelinenor#rSrr3rrrrcs@            zFeedParser._parse_headersN)r8r9r:r;r MessagerrIrKrJrrXrMrCrcrrrrr s  ~cs eZdZdZfddZZS)r z(Like FeedParser, but feed accepts bytes.cstj|jdddS)Nasciisurrogateescape)rrKdecode)rr0) __class__rrrK szBytesFeedParser.feed)r8r9r:r;rK __classcell__rr)rrr s)r; __future__rrrZfuture.builtinsrrrZ future.utilsrr __all__rlZfuture.backports.emailr r Z"future.backports.email._policybaserrmraryrpr+r_reNLr"rr r rrrrs0           Z