a
    ”˜ëjý  ã                   @   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   úb/mnt/bigstor/www/usecases/.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   	!