ó
	Q˜[c           @` sV  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z m Z m Z d d l	 m
 Z
 m Z d d g Z d d	 l Z d d
 l m Z d d l m Z d d l m Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z d Z d Z e ƒ  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 S(   u  FeedParser - 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.
i    (   t   unicode_literals(   t   division(   t   absolute_import(   t   objectt   ranget   super(   t   implements_iteratort   PY3u
   FeedParseru   BytesFeedParserN(   t   errors(   t   message(   t   compat32u   
||
u   (
||
)u
   (
||
)\Zu(   ^(From |[\041-\071\073-\176]{1,}:|[\t ])u    u   
t   BufferedSubFilec           B` sh   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   uk  A 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.
    c         C` s(   d |  _  g  |  _ g  |  _ t |  _ d  S(   Nu    (   t   _partialt   _linest	   _eofstackt   Falset   _closed(   t   self(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   __init__9   s    			c         C` s   |  j  j | ƒ d  S(   N(   R   t   append(   R   t   pred(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   push_eof_matcherC   s    c         C` s   |  j  j ƒ  S(   N(   R   t   pop(   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   pop_eof_matcherF   s    c         C` s)   |  j  j |  j ƒ d |  _ t |  _ d  S(   Nu    (   R   R   R   t   TrueR   (   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   closeI   s    	c         C` sn   |  j  s |  j r d St S|  j  j ƒ  } x> |  j d  d  d … D]& } | | ƒ r@ |  j  j | ƒ d Sq@ W| S(   Nu    iÿÿÿÿ(   R   R   t   NeedMoreDataR   R   R   (   R   t   linet   ateof(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   readlineO   s    		c         C` s&   | t  k	 s t ‚ |  j j | ƒ d  S(   N(   R   t   AssertionErrorR   R   (   R   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt
   unreadlinea   s    c         C` sÔ   |  j  | d } |  _  t j | ƒ } | j ƒ  |  _  |  j  rw | rw | d j d ƒ rw | j d ƒ | j ƒ  |  _  n  g  } xC t t | ƒ d ƒ D]+ } | j | | d | | d d ƒ q” W|  j | ƒ d S(   u$   Push some new data into this object.u    iÿÿÿÿu   iþÿÿÿi   i   N(	   R   t   NLCRE_crackt   splitR   t   endswithR   t   lenR   t	   pushlines(   R   t   datat   partst   linest   i(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   pushf   s    #)c         C` s   | d  d  d … |  j  d *d  S(   Niÿÿÿÿi    (   R   (   R   R'   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR$   }   s    c         C` s   |  S(   N(    (   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   __iter__   s    c         C` s%   |  j  ƒ  } | d k r! t ‚ n  | S(   Nu    (   R   t   StopIteration(   R   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   __next__„   s    	(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R)   R$   R*   R,   (    (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR   1   s   	
								t
   FeedParserc           B` se   e  Z d  Z e j d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z RS(
   u   A feed-style parser of email.c         ` sß   d | k r  | d } | d =n t  } | ˆ  _ | ˆ  _ y& | d ˆ  j ƒ ‡  f d †  ˆ  _ Wn t k
 r} d „  ˆ  _ n Xt ƒ  ˆ  _ g  ˆ  _ t r® ˆ  j	 ƒ  j
 ˆ  _ n ˆ  j	 ƒ  j ˆ  _ d  ˆ  _ d  ˆ  _ t ˆ  _ d  S(   Nu   policyt   policyc           ` s   i ˆ  j  d 6S(   Nu   policy(   R1   (    (   R   (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   <lambda>œ   s    c           S` s   i  S(   N(    (    (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR2   Ÿ   s    (   R
   t   _factoryR1   t   _factory_kwdst	   TypeErrorR   t   _inputt	   _msgstackR   t	   _parsegenR,   t   _parset   nextt   Nonet   _curt   _lastR   t   _headersonly(   R   R3   t   _3to2kwargsR1   (    (   R   s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR   Ž   s&     
 
					c         C` s   t  |  _ d  S(   N(   R   R>   (   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   _set_headersonly«   s    c         C` s   |  j  j | ƒ |  j ƒ  d S(   u   Push more data into the parser.N(   R6   R)   t   _call_parse(   R   R%   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   feed®   s    c         C` s&   y |  j  ƒ  Wn t k
 r! n Xd  S(   N(   R9   R+   (   R   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyRA   ³   s    c         C` sx   |  j  j ƒ  |  j ƒ  |  j ƒ  } |  j s3 t ‚ | j ƒ  d k rt | j ƒ  rt t j	 ƒ  } |  j
 j | | ƒ n  | S(   u<   Parse all remaining data and return the root message object.u	   multipart(   R6   R   RA   t   _pop_messageR7   R   t   get_content_maintypet   is_multipartR   t!   MultipartInvariantViolationDefectR1   t   handle_defect(   R   t   roott   defect(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR   ¹   s    
c         C` s‰   |  j  |  j ƒ    } |  j rC |  j j ƒ  d k rC | j d ƒ n  |  j rc |  j d j | ƒ n  |  j j | ƒ | |  _ | |  _ d  S(   Nu   multipart/digestu   message/rfc822iÿÿÿÿ(	   R3   R4   R<   t   get_content_typet   set_default_typeR7   t   attachR   R=   (   R   t   msg(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   _new_messageÆ   s    		c         C` s8   |  j  j ƒ  } |  j  r+ |  j  d |  _ n	 d  |  _ | S(   Niÿÿÿÿ(   R7   R   R<   R;   (   R   t   retval(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyRC   Ð   s
    		c         c` s@  |  j  ƒ  g  } xŒ |  j D] } | t k r7 t Vq n  t j | ƒ sŽ t j | ƒ sŠ t j ƒ  } |  j j	 |  j
 | ƒ |  j j | ƒ n  Pn  | j | ƒ q W|  j | ƒ |  j r(g  } xM t r
|  j j ƒ  } | t k rê t Vq¾ n  | d k rú Pn  | j | ƒ q¾ W|  j
 j t j | ƒ ƒ d  S|  j
 j ƒ  d k r1xí t r,|  j j t j ƒ x, |  j ƒ  D] } | t k rƒt Vqfn  PqfW|  j ƒ  } |  j j ƒ  x1 t rÔ|  j j ƒ  } | t k rÐt Vq¤n  Pq¤Wx1 t r|  j j ƒ  } | t k rt VqØn  PqØW| d k rPn  |  j j | ƒ q@Wd  S|  j
 j ƒ  d k rƒx, |  j ƒ  D] } | t k rpt VqSn  PqSW|  j ƒ  d  S|  j
 j ƒ  d k rå|  j
 j ƒ  } | d  k r0t j ƒ  } |  j j	 |  j
 | ƒ g  } x5 |  j D]* } | t k rt Vqån  | j | ƒ qåW|  j
 j t j | ƒ ƒ d  S|  j
 j d d ƒ j ƒ  d k rvt j ƒ  } |  j j	 |  j
 | ƒ n  d	 | } t  j! d
 t  j" | ƒ d ƒ }	 t }
 g  } t# } t# } xÂt r||  j j ƒ  } | t k rçt Vq»n  | d k r÷Pn  |	 j | ƒ } | r`| j$ d ƒ r4t } | j$ d ƒ } Pn  |
 r´| r˜| d } t% j& | ƒ } | r€| t' | j$ d ƒ ƒ  | d <n  t j | ƒ |  j
 _( n  t# }
 |  j j | ƒ q»n  xY t r|  j j ƒ  } | t k rãt Vq·n  |	 j | ƒ } | s·|  j j | ƒ Pq·q·W|  j j |	 j ƒ x, |  j ƒ  D] } | t k rMt Vq0n  Pq0W|  j) j ƒ  d k rÞ|  j) j* } | d k rŽd  |  j) _* q:| d  k	 r:t% j& | ƒ } | rÛt' | j$ d ƒ ƒ } | |  |  j) _* qÛq:n\ |  j) j+ } t, | t- ƒ r:t% j& | ƒ } | r:| t' | j$ d ƒ ƒ  } | |  j) _+ q:n  |  j j ƒ  |  j ƒ  |  j
 |  _) q»|
 slt. ‚ | j | ƒ q»W|
 rt j/ ƒ  } |  j j	 |  j
 | ƒ |  j
 j t j | ƒ ƒ g  } x( |  j D] } | t k rÎt VqÎqÎqÎWt j | ƒ |  j
 _* d  S| s4t j0 ƒ  } |  j j	 |  j
 | ƒ d  S| rFd g } n g  } x5 |  j D]* } | t k rst VqVn  | j | ƒ qVW| rÌ| d } t1 j | ƒ } | rÌ| t' | j$ d ƒ ƒ | d <qÌn  t j | ƒ |  j
 _* d  Sg  } x5 |  j D]* } | t k rt Vqõn  | j | ƒ qõW|  j
 j t j | ƒ ƒ d  S(   Nu    u   message/delivery-statusu   messageu	   multipartu   content-transfer-encodingu   8bitu   7bitu   binaryu   --u   (?P<sep>u4   )(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$u   endu   linesepiÿÿÿÿi    (   u   7bitu   8bitu   binary(2   RN   R6   R   t   headerREt   matcht   NLCRER   t    MissingHeaderBodySeparatorDefectR1   RG   R<   R   R   t   _parse_headersR>   R   R   t   set_payloadt   EMPTYSTRINGt   joinRJ   R   R8   RC   R   RD   t   get_boundaryR;   t   NoBoundaryInMultipartDefectt   gett   lowert-   InvalidMultipartContentTransferEncodingDefectt   ret   compilet   escapeR   t   groupt	   NLCRE_eolt   searchR#   t   preambleR=   t   epiloguet   _payloadt
   isinstancet   strR   t   StartBoundaryNotFoundDefectt   CloseBoundaryNotFoundDefectt	   NLCRE_bol(   R   t   headersR   RI   R'   RO   RM   t   boundaryt	   separatort
   boundaryret   capturing_preambleRc   t   linesept   close_boundary_seent   mot   lastlinet   eolmoRd   t   endt   payloadt	   firstlinet   bolmo(    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR8   Ø   sh   
					
	

	
!	


#c   	      C` sÇ  d } g  } xt  | ƒ D]\ } } | d d k ry | sf t j | ƒ } |  j j |  j | ƒ q n  | j | ƒ q n  | r« |  j j |  j j | ƒ Œ  d g  } } n  | j	 d ƒ r`| d k rt
 j | ƒ } | rø | t | j d ƒ ƒ  } n  |  j j | ƒ q q`| t | ƒ d k r8|  j j | ƒ d  St j | ƒ } |  j j j | ƒ q n  | j d ƒ } | d k s‡t d ƒ ‚ | |  } | g } q W| rÃ|  j j |  j j | ƒ Œ  n  d  S(   Nu    i    u    	u   From i   u   :u3   _parse_headers fed line with no : and no leading WS(   t	   enumerateR   t#   FirstHeaderLineIsContinuationDefectR1   RG   R<   R   t   set_rawt   header_source_parset
   startswithRa   Rb   R#   R`   t   set_unixfromR6   R   t   MisplacedEnvelopeHeaderDefectt   defectst   findR   (	   R   R'   t
   lastheadert	   lastvaluet   linenoR   RI   Rr   R(   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyRT   Õ  s@    
(   R-   R.   R/   R	   t   MessageR   R@   RB   RA   R   RN   RC   R8   RT   (    (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR0   ‹   s   					
		ýt   BytesFeedParserc           B` s   e  Z d  Z d „  Z RS(   u(   Like FeedParser, but feed accepts bytes.c         C` s    t  ƒ  j | j d d ƒ ƒ d  S(   Nu   asciiu   surrogateescape(   R   RB   t   decode(   R   R%   (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyRB     s    (   R-   R.   R/   RB   (    (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyR†   	  s   (   R/   t
   __future__R    R   R   t   future.builtinsR   R   R   t   future.utilsR   R   t   __all__R]   t   future.backports.emailR   R	   t"   future.backports.email._policybaseR
   R^   RR   Rj   Ra   R    RP   RV   t   NLR   R   R0   R†   (    (    (    s@   lib/python2.7/site-packages/future/backports/email/feedparser.pyt   <module>   s,   	Zÿ 