o
    Vh                     @   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boundpoint r   w/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/requests_toolbelt/multipart/decoder.py_split_on_find   s   
 r   c                   @      e Zd ZdS ) ImproperBodyPartContentExceptionN__name__
__module____qualname__r   r   r   r   r          r   c                   @   r   ) NonMultipartContentTypeExceptionNr   r   r   r   r   r      r   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rt| |}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/   propertyr0   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	   r5   r   r   r   r   r/   g   s
   zMultipartDecoder.__init__c                 C   s   t dd | jdD }|d }|dd  dkr#td||dd  D ]}t|d	\}}| d
krBt|d| j	| _
q)d S )Nc                 s   s    | ]}|  V  qd S r   )stripr   xr   r   r   r   r   s    z2MultipartDecoder._find_boundary.<locals>.<genexpr>;r   /	multipartz)Unexpected mimetype in content-type: '{}'r   =boundary")r6   r5   splitlowerr   formatr   r   r:   r   rA   )r-   ct_infomimetypeitemattrvaluer   r   r   r8   q   s   zMultipartDecoder._find_boundaryc                 C   s(   t |}|| d | kr| |d  S | S r   )r   )partboundary_marker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   )r3   rN   r*   r   )rK   fixed)rA   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   --
rO   r   )rK   r   r   r   	test_part   s   z/MultipartDecoder._parse_body.<locals>.test_partrR   c                 3   s     | ]}|r |V  qd S r   r   r;   )rQ   rT   r   r   r      s    z/MultipartDecoder._parse_body.<locals>.<genexpr>)joinrA   rC   r6   r7   )r-   r	   r7   r   )rQ   rA   r-   rT   r   r9      s
   zMultipartDecoder._parse_bodyc                 C   s    |j }|jdd }| |||S )Nzcontent-type)r	   r(   get)clsresponser   r	   r5   r   r   r   from_response   s   zMultipartDecoder.from_responseN)r4   )r   r   r   r1   r/   r8   staticmethodrN   r9   classmethodrY   r   r   r   r   r3   J   s    


r3   )r1   r   email.parserr!   encoderr   requests.structuresr   r   	Exceptionr   r   r)   objectr*   r3   r   r   r   r   <module>   s   	!