3
‡Œði¾  ã               @   s¸   d Z ddlmZ ddlmZ ddlmZ ddddgZdd	lZdd
lmZm	Z	 ddl
mZmZ ddlmZ ddlmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZd	S )z-A parser of RFC 2822 and MIME email messages.é    )Úunicode_literals)Údivision)Úabsolute_importÚParserÚHeaderParserÚBytesParserÚBytesHeaderParserN)ÚStringIOÚTextIOWrapper)Ú
FeedParserÚBytesFeedParser)ÚMessage)Úcompat32c               @   s,   e Zd Zefdd„Zd	dd„Zd
dd„ZdS )r   c             K   s,   d|kr|d }|d= nt }|| _|| _dS )a…  Parser of RFC 2822 and MIME email messages.

        Creates an in-memory object tree representing the email message, which
        can then be manipulated and turned over to a Generator to return the
        textual representation of the message.

        The string must be formatted as a block of RFC 2822 headers and header
        continuation lines, optionally preceded by a `Unix-from' header.  The
        header block is terminated either by the end of the string or by a
        blank line.

        _class is the class to instantiate for new message objects when they
        must be created.  This class must have a constructor that can take
        zero arguments.  Default is Message.Message.

        The policy keyword specifies a policy object that controls a number of
        aspects of the parser's operation.  The default policy maintains
        backward compatibility.

        ÚpolicyN)r   Ú_classr   )Úselfr   Z_3to2kwargsr   © r   ú?/tmp/pip-build-6m2fasei/future/future/backports/email/parser.pyÚ__init__   s      zParser.__init__Fc             C   sD   t | j| jd�}|r|jƒ  x|jdƒ}|s.P |j|ƒ qW |jƒ S )a\  Create a message structure from the data in a file.

        Reads all the data from the file and returns the root of the message
        structure.  Optional headersonly is a flag specifying whether to stop
        parsing after reading the headers or not.  The default is False,
        meaning it parses the entire contents of the file.
        )r   i    )r   r   r   Ú_set_headersonlyÚreadÚfeedÚclose)r   ÚfpÚheadersonlyÚ
feedparserÚdatar   r   r   Úparse/   s    
zParser.parsec             C   s   | j t|ƒ|d�S )a-  Create a message structure from a string.

        Returns the root of the message structure.  Optional headersonly is a
        flag specifying whether to stop parsing after reading the headers or
        not.  The default is False, meaning it parses the entire contents of
        the file.
        )r   )r   r	   )r   Útextr   r   r   r   ÚparsestrA   s    zParser.parsestrN)F)F)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r      s   
c               @   s    e Zd Zddd„Zddd„ZdS )	r   Tc             C   s   t j| |dƒS )NT)r   r   )r   r   r   r   r   r   r   N   s    zHeaderParser.parsec             C   s   t j| |dƒS )NT)r   r   )r   r   r   r   r   r   r   Q   s    zHeaderParser.parsestrN)T)T)r    r!   r"   r   r   r   r   r   r   r   M   s   
c               @   s(   e Zd Zdd„ Zd	dd„Zd
dd„ZdS )r   c             O   s   t ||Ž| _dS )aÐ  Parser of binary RFC 2822 and MIME email messages.

        Creates an in-memory object tree representing the email message, which
        can then be manipulated and turned over to a Generator to return the
        textual representation of the message.

        The input must be formatted as a block of RFC 2822 headers and header
        continuation lines, optionally preceded by a `Unix-from' header.  The
        header block is terminated either by the end of the input or by a
        blank line.

        _class is the class to instantiate for new message objects when they
        must be created.  This class must have a constructor that can take
        zero arguments.  Default is Message.Message.
        N)r   Úparser)r   ÚargsÚkwr   r   r   r   W   s    zBytesParser.__init__Fc          
   C   s,   t |ddd�}|� | jj||ƒS Q R X dS )ac  Create a message structure from the data in a binary file.

        Reads all the data from the file and returns the root of the message
        structure.  Optional headersonly is a flag specifying whether to stop
        parsing after reading the headers or not.  The default is False,
        meaning it parses the entire contents of the file.
        ÚasciiÚsurrogateescape)ÚencodingÚerrorsN)r
   r#   r   )r   r   r   r   r   r   r   i   s    zBytesParser.parsec             C   s   |j ddd�}| jj||ƒS )a2  Create a message structure from a byte string.

        Returns the root of the message structure.  Optional headersonly is a
        flag specifying whether to stop parsing after reading the headers or
        not.  The default is False, meaning it parses the entire contents of
        the file.
        ÚASCIIr'   )r)   )Údecoder#   r   )r   r   r   r   r   r   Ú
parsebytesv   s    zBytesParser.parsebytesN)F)F)r    r!   r"   r   r   r,   r   r   r   r   r   U   s   
c               @   s    e Zd Zddd„Zddd„ZdS )	r   Tc             C   s   t j| |dd�S )NT)r   )r   r   )r   r   r   r   r   r   r   ƒ   s    zBytesHeaderParser.parsec             C   s   t j| |dd�S )NT)r   )r   r,   )r   r   r   r   r   r   r,   †   s    zBytesHeaderParser.parsebytesN)T)T)r    r!   r"   r   r,   r   r   r   r   r   ‚   s   
)Ú__doc__Ú
__future__r   r   r   Ú__all__ÚwarningsÚior	   r
   Z!future.backports.email.feedparserr   r   Zfuture.backports.email.messager   Z"future.backports.email._policybaser   Úobjectr   r   r   r   r   r   r   r   Ú<module>   s   9-