a
    W%bX                     @   s   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dZedZedZedZedZdZdZe ZG dd deZG dd deZG dd deZd	S )a  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.
    )unicode_literals)division)absolute_import)objectrangesuper)implements_iteratorPY3
FeedParserBytesFeedParserN)errors)message)compat32z
||
z(
||
)z
(
||
)\Zz(^(From |[\041-\071\073-\176]{1,}:|[\t ]) 
c                   @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )BufferedSubFileak  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 | _d| _d S )Nr   F)_partial_lines	_eofstack_closedself r   q/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/future/backports/email/feedparser.py__init__9   s    zBufferedSubFile.__init__c                 C   s   | j | d S N)r   append)r   predr   r   r   push_eof_matcherC   s    z BufferedSubFile.push_eof_matcherc                 C   s
   | j  S r   )r   popr   r   r   r   pop_eof_matcherF   s    zBufferedSubFile.pop_eof_matcherc                 C   s   | j | j d| _d| _d S )Nr   T)r   r   r   r   r   r   r   r   closeI   s    zBufferedSubFile.closec                 C   sR   | j s| jrdS tS | j  }| jd d d D ]}||r.| j |  dS q.|S )Nr   )r   r   NeedMoreDatar   r   r   )r   lineateofr   r   r   readlineO   s    
zBufferedSubFile.readlinec                 C   s   |t usJ | j| d S r   )r#   r   r   r   r$   r   r   r   
unreadlinea   s    zBufferedSubFile.unreadlinec                 C   s   | j | d }| _ t|}| | _ | j sR|rR|d drR|d|  | _ g }tt|d D ]&}|||d  ||d d    qf| | dS )z$Push some new data into this object.r   r"         N)	r   NLCRE_cracksplitr   endswithr   lenr   	pushlines)r   datapartslinesir   r   r   pushf   s    

$zBufferedSubFile.pushc                 C   s   |d d d | j d d< d S )Nr"   r   )r   )r   r4   r   r   r   r1   }   s    zBufferedSubFile.pushlinesc                 C   s   | S r   r   r   r   r   r   __iter__   s    zBufferedSubFile.__iter__c                 C   s   |   }|dkrt|S )Nr   )r&   StopIterationr'   r   r   r   __next__   s    zBufferedSubFile.__next__N)__name__
__module____qualname____doc__r   r   r    r!   r&   r(   r6   r1   r7   r9   r   r   r   r   r   1   s   
r   c                   @   s^   e Zd ZdZejfddZdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )r
   zA feed-style parser of email.c                    s   d|v r|d }|d= nt }| _| _z| jd  fdd _W n tyb   dd  _Y n0 t  _g  _tr 	 j
 _n 	 j _d  _d  _d _d S )Npolicyr>   c                      s
   d j iS )Nr>   r?   r   r   r   r   <lambda>       z%FeedParser.__init__.<locals>.<lambda>c                   S   s   i S r   r   r   r   r   r   r@      rA   F)r   _factoryr>   _factory_kwds	TypeErrorr   _input	_msgstackr	   	_parsegenr9   _parsenext_cur_last_headersonly)r   rB   Z_3to2kwargsr>   r   r   r   r      s"    zFeedParser.__init__c                 C   s
   d| _ d S )NT)rL   r   r   r   r   _set_headersonly   s    zFeedParser._set_headersonlyc                 C   s   | j | |   dS )zPush more data into the parser.N)rE   r6   _call_parser   r2   r   r   r   feed   s    zFeedParser.feedc                 C   s$   z|    W n ty   Y n0 d S r   )rH   r8   r   r   r   r   rN      s    zFeedParser._call_parsec                 C   sR   | j   |   |  }| jr$J | dkrN| sNt }| j	
|| |S )z<Parse all remaining data and return the root message object.	multipart)rE   r!   rN   _pop_messagerF   get_content_maintypeis_multipartr   !MultipartInvariantViolationDefectr>   handle_defect)r   rootdefectr   r   r   r!      s    

zFeedParser.closec                 C   sd   | j f i |  }| jr2| j dkr2|d | jrH| jd | | j| || _|| _d S )Nzmultipart/digestzmessage/rfc822r"   )	rB   rC   rJ   get_content_typeset_default_typerF   attachr   rK   )r   msgr   r   r   _new_message   s    
zFeedParser._new_messagec                 C   s(   | j  }| j r| j d | _nd | _|S )Nr"   )rF   r   rJ   )r   retvalr   r   r   rR      s
    
zFeedParser._pop_messagec                 c   s   |    g }| jD ]Z}|tu r&tV  qt|sbt|s^t }| j	| j
| | j|  qn|| q| | | jrg }| j }|tu rtV  q|dkrq|| q| j
t| d S | j
 dkr| jtj |  D ]}|tu rtV  q qq|  }| j  | j }|tu rDtV  qqDq| j }|tu rjtV  qDqjqD|dkrxq| j| qd S | j
 dkr|  D ] }|tu rtV  q qĐq|   d S | j
 dkr| j
 }|d u rRt }| j	| j
| g }| jD ]$}|tu r.tV  q|| q| j
t| d S | j
dd dvrt }| j	| j
| d| }td	t | d
 }	d}
g }d}d}| j }|tu rtV  q|dkrސq|	|}|r|!drd}|!d}q|
rn|rZ|d }t"#|}|rL|d t$|!d  |d< t|| j
_%d}
| j| q| j }|tu rtV  qn|	|}|sn| j| qqn| j|	j |  D ] }|tu rtV  q qq| j& dkrP| j&j'}|dkrd | j&_'n:|d urt"#|}|rt$|!d}|d |  | j&_'nD| j&j(}t)|t*rt"#|}|r|d t$|!d  }|| j&_(| j  |   | j
| _&n|
sJ || q|
r0t+ }| j	| j
| | j
t| g }| jD ]}|tu rtV  qqt|| j
_'d S |sRt, }| j	| j
| d S |r`dg}ng }| jD ]$}|tu rtV  qj|| qj|r|d }t-|}|r|t$|!dd  |d< t|| j
_'d S g }| jD ]$}|tu rtV  q|| q| j
t| d S )Nr   zmessage/delivery-statusr   rQ   zcontent-transfer-encoding8bit)7bitr_   binaryz--z(?P<sep>z4)(?P<end>--)?(?P<ws>[ \t]*)(?P<linesep>\r\n|\r|\n)?$TFendlinesepr"   r   ).r]   rE   r#   headerREmatchNLCREr    MissingHeaderBodySeparatorDefectr>   rV   rJ   r(   r   _parse_headersrL   r&   set_payloadEMPTYSTRINGjoinrY   r   rG   rR   r    rS   get_boundaryNoBoundaryInMultipartDefectgetlower-InvalidMultipartContentTransferEncodingDefectrecompileescapegroup	NLCRE_eolsearchr0   preamblerK   epilogue_payload
isinstancestrStartBoundaryNotFoundDefectCloseBoundaryNotFoundDefect	NLCRE_bol)r   headersr$   rX   r4   r^   r\   boundary	separator
boundaryrecapturing_preamblerw   rc   close_boundary_seenmolastlineeolmorx   rb   payload	firstlinebolmor   r   r   rG      sb   











































zFeedParser._parsegenc           	      C   sF  d}g }t |D ]\}}|d dv rR|sFt|}| j| j| q|| q|rt| jj| j|  dg  }}|	dr|dkrt
|}|r|d t|d  }| j| qn<|t|d kr| j|  d S t|}| jj| q|d}|dksJ d|d | }|g}q|rB| jj| j|  d S )Nr   r   z 	zFrom r,   :z3_parse_headers fed line with no : and no leading WS)	enumerater   #FirstHeaderLineIsContinuationDefectr>   rV   rJ   r   set_rawheader_source_parse
startswithru   rv   r0   rt   set_unixfromrE   r(   MisplacedEnvelopeHeaderDefectdefectsfind)	r   r4   
lastheader	lastvaluelinenor$   rX   r   r5   r   r   r   rh     s@    






zFeedParser._parse_headersN)r:   r;   r<   r=   r   Messager   rM   rP   rN   r!   r]   rR   rG   rh   r   r   r   r   r
      s   
 ~c                       s    e Zd ZdZ fddZ  ZS )r   z(Like FeedParser, but feed accepts bytes.c                    s   t  |dd d S )Nasciisurrogateescape)r   rP   decoderO   	__class__r   r   rP     s    zBytesFeedParser.feed)r:   r;   r<   r=   rP   __classcell__r   r   r   r   r   	  s   )r=   
__future__r   r   r   Zfuture.builtinsr   r   r   Zfuture.utilsr   r	   __all__rq   Zfuture.backports.emailr   r   Z"future.backports.email._policybaser   rr   rf   r~   ru   r-   rd   rj   NLr#   r   r
   r   r   r   r   r   <module>   s0   




Z   