U
    6X"                     @   s   d Z ddlZddlZddlmZ ddlmZ dd ZG dd	 d	e	Z
G d
d de	Zdd ZG dd deZG dd deZdS )z

requests_toolbelt.multipart.decoder
===================================

This holds all the implementation details of the MultipartDecoder

    N   encode_with)CaseInsensitiveDictc                 C   s*   |  |}| d | | |t| d  fS N)findlen)contentZboundZpoint r
   E/usr/lib/python3/dist-packages/requests_toolbelt/multipart/decoder.py_split_on_find   s    
r   c                   @   s   e Zd ZdS ) ImproperBodyPartContentExceptionN__name__
__module____qualname__r
   r
   r
   r   r      s   r   c                   @   s   e Zd ZdS ) NonMultipartContentTypeExceptionNr   r
   r
   r
   r   r      s   r   c                    sB   t jd }|dkr|  } tj |  } fdd|D S )Nr      c                 3   s&   | ]\}}t | t | fV  qd S r   r   ).0kvencodingr
   r   	<genexpr>#   s   z!_header_parser.<locals>.<genexpr>)sysversion_infodecodeemailparserZHeaderParserZparsestritems)stringr   majorheadersr
   r   r   _header_parser   s    


r#   c                   @   s$   e Zd ZdZdd Zedd ZdS )BodyParta  

    The ``BodyPart`` object is a ``Response``-like interface to an individual
    subpart of a multipart response. It is expected that these will
    generally be created by objects of the ``MultipartDecoder`` class.

    Like ``Response``, there is a ``CaseInsensitiveDict`` object named header,
    ``content`` to access bytes, ``text`` to access unicode, and ``encoding``
    to access the unicode codec.

    c                 C   sP   || _ i }d|kr:t|d\}| _|dkrBt| |}ntdt|| _d S )Ns   

    z$content does not contain CR-LF-CR-LF)r   r   r	   r#   lstripr   r   r"   )selfr	   r   r"   firstr
   r
   r   __init__6   s    zBodyPart.__init__c                 C   s   | j | jS )z'Content of the ``BodyPart`` in unicode.)r	   r   r   )r'   r
   r
   r   textD   s    zBodyPart.textN)r   r   r   __doc__r)   propertyr*   r
   r
   r
   r   r$   )   s   r$   c                   @   sD   e Zd ZdZdddZdd Zedd Zd	d
 Ze	dddZ
dS )MultipartDecoderag  

    The ``MultipartDecoder`` object parses the multipart payload of
    a bytestring into a tuple of ``Response``-like ``BodyPart`` objects.

    The basic usage is::

        import requests
        from requests_toolbelt import MultipartDecoder

        response = request.get(url)
        decoder = MultipartDecoder.from_response(response)
        for part in decoder.parts:
            print(part.header['content-type'])

    If the multipart content is not from a response, basic usage is::

        from requests_toolbelt import MultipartDecoder

        decoder = MultipartDecoder(content, content_type)
        for part in decoder.parts:
            print(part.header['content-type'])

    For both these usages, there is an optional ``encoding`` parameter. This is
    a string, which is the name of the unicode codec to use (default is
    ``'utf-8'``).

    utf-8c                 C   s.   || _ || _|| _t | _|   |   d S r   )r	   content_typer   tupleparts_find_boundary_parse_body)r'   r	   r/   r   r
   r
   r   r)   g   s    zMultipartDecoder.__init__c                 C   s   t dd | jdD }|d }|dd dkrBtd||dd  D ]2}t|d	\}}| d
krNt|d| j	| _
qNd S )Nc                 s   s   | ]}|  V  qd S r   )stripr   xr
   r
   r   r   t   s     z2MultipartDecoder._find_boundary.<locals>.<genexpr>;r   /Z	multipartz*Unexpected mimetype in content-type: '{0}'r   =boundary")r0   r/   splitr   formatr   lowerr   r4   r   r:   )r'   Zct_infoZmimetypeitemattrvaluer
   r
   r   r2   s   s    zMultipartDecoder._find_boundaryc                 C   s,   t |}|| d | kr$| |d  S | S d S r   )r   )partZboundary_markerZbm_lenr
   r
   r   _fix_first_part   s    z MultipartDecoder._fix_first_partc                    sZ   d djffdd dd jd df}t fdd	|D _d S )
Nr%      --c                    s   t |  }t|jS r   )r-   rC   r$   r   )rB   Zfixed)r:   r'   r
   r   	body_part   s    z/MultipartDecoder._parse_body.<locals>.body_partc                 S   s(   | dko&| dko&| d d dko&| dkS )Nr%      
   s   --
rD   r
   )rB   r
   r
   r   	test_part   s    z/MultipartDecoder._parse_body.<locals>.test_partrF   c                 3   s   | ]}|r |V  qd S r   r
   r5   )rE   rH   r
   r   r      s      z/MultipartDecoder._parse_body.<locals>.<genexpr>)joinr:   r	   r<   r0   r1   )r'   r1   r
   )rE   r:   r'   rH   r   r3      s
    zMultipartDecoder._parse_bodyc                 C   s    |j }|jdd }| |||S )Nzcontent-type)r	   r"   get)clsZresponser   r	   r/   r
   r
   r   from_response   s    zMultipartDecoder.from_responseN)r.   )r.   )r   r   r   r+   r)   r2   staticmethodrC   r3   classmethodrL   r
   r
   r
   r   r-   J   s   

r-   )r+   r   Zemail.parserr   encoderr   Zrequests.structuresr   r   	Exceptionr   r   r#   objectr$   r-   r
   r
   r
   r   <module>   s   	!