a
    ~g                     @   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)contentboundZpoint r   h/var/www/html/emsaiapi.evdpl.com/venv/lib/python3.9/site-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parserHeaderParser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 headers,
    ``content`` to access bytes, ``text`` to access unicode, and ``encoding``
    to access the unicode codec.

    c                 C   sP   || _ i }d|v 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 )MultipartDecoderaj  

    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 = requests.get(url)
        decoder = MultipartDecoder.from_response(response)
        for part in decoder.parts:
            print(part.headers['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.headers['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   )content_typer   tupleparts_find_boundary_parse_body)r*   r	   r2   r   r   r   r   r,   g   s
    zMultipartDecoder.__init__c                 C   s   t dd | jdD }|d }|dd  dkrFtd||dd  D ]2}t|d	\}}| d
krRt|d| j	| _
qRd S )Nc                 s   s   | ]}|  V  qd S r   )stripr   xr   r   r   r   r   r(   z2MultipartDecoder._find_boundary.<locals>.<genexpr>;r   /	multipartz)Unexpected mimetype in content-type: '{}'r   =boundary")r3   r2   splitlowerr   formatr   r   r7   r   r>   )r*   Zct_infomimetypeitemattrvaluer   r   r   r5   q   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                    sX   d djffdd dd |d df}t fdd	|D _d S )
Nr(      --c                    s   t |  }t|jS r   )r0   rH   r'   r   )rG   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   --
rI   r   )rG   r   r   r   	test_part   s    z/MultipartDecoder._parse_body.<locals>.test_partrL   c                 3   s   | ]}|r |V  qd S r   r   r8   )rK   rN   r   r   r      r(   z/MultipartDecoder._parse_body.<locals>.<genexpr>)joinr>   r@   r3   r4   )r*   r	   r4   r   )rK   r>   r*   rN   r   r6      s
    zMultipartDecoder._parse_bodyc                 C   s    |j }|jdd }| |||S )Nzcontent-type)r	   r%   get)clsresponser   r	   r2   r   r   r   from_response   s    zMultipartDecoder.from_responseN)r1   )r1   )r   r   r   r.   r,   r5   staticmethodrH   r6   classmethodrS   r   r   r   r   r0   J   s   


r0   )r.   r   email.parserr   encoderr   Zrequests.structuresr   r   	Exceptionr   r   r&   objectr'   r0   r   r   r   r   <module>   s   	!