
    |h                         d Z ddlZddlZddlmZ ddlmZ d Z G d de	      Z
 G d	 d
e	      Zd Z G d de      Z G d de      Zy)z

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

This holds all the implementation details of the MultipartDecoder

    N   encode_with)CaseInsensitiveDictc                 P    | j                  |      }| d | | |t        |      z   d  fS N)findlen)contentboundpoints      b/var/www/html/test/engine/venv/lib/python3.12/site-packages/requests_toolbelt/multipart/decoder.py_split_on_findr      s2    LLE6E?GECJ$6$7888    c                       e Zd Zy) ImproperBodyPartContentExceptionN__name__
__module____qualname__ r   r   r   r          r   r   c                       e Zd Zy) NonMultipartContentTypeExceptionNr   r   r   r   r   r      r   r   r   c                     t         j                  d   }|dk(  r| j                        } t        j                  j                         j                  |       j                         }fd|D        S )Nr      c              3   T   K   | ]  \  }}t        |      t        |      f ! y wr   r   ).0kvencodings      r   	<genexpr>z!_header_parser.<locals>.<genexpr>#   s0      Aq 
Q	!;q(#;<s   %()sysversion_infodecodeemailparserHeaderParserparsestritems)stringr!   majorheaderss    `  r   _header_parserr.      s`    QEzx(ll'')226:@@BG r   c                   &    e Zd ZdZd Zed        Zy)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                     || _         i }d|v r4t        |d      \  }| _        |dk7  r&t        |j	                         |      }nt        d      t        |      | _        y )Ns   

r   z$content does not contain CR-LF-CR-LF)r!   r   r   r.   lstripr   r   r-   )selfr   r!   r-   firsts        r   __init__zBodyPart.__init__6   s`     '!"0+"FE4<|(B26  +73r   c                 L    | j                   j                  | j                        S )z'Content of the ``BodyPart`` in unicode.)r   r%   r!   )r3   s    r   textzBodyPart.textD   s     ||""4==11r   N)r   r   r   __doc__r5   propertyr7   r   r   r   r0   r0   )   s     
4 2 2r   r0   c                   F    e Zd ZdZddZd Zed        Zd Ze	dd       Z
y)	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'``).

    c                     || _         || _        t               | _        | j	                          | j                  |       y r   )content_typer!   tupleparts_find_boundary_parse_body)r3   r   r=   r!   s       r   r5   zMultipartDecoder.__init__g   s4    ( W
!r   c                    t        d | j                  j                  d      D              }|d   }|j                  d      d   j                         dk7  rt	        dj                  |            |dd  D ]O  }t        |d      \  }}|j                         d	k(  s&t        |j                  d
      | j                        | _
        Q y )Nc              3   <   K   | ]  }|j                           y wr   )strip)r   xs     r   r"   z2MultipartDecoder._find_boundary.<locals>.<genexpr>r   s     Ha	Hs   ;r   /	multipartz)Unexpected mimetype in content-type: '{}'r   =boundary")r>   r=   splitlowerr   formatr   r   rD   r!   rJ   )r3   ct_infomimetypeitemattrvalues         r   r@   zMultipartDecoder._find_boundaryq   s    H4+<+<+B+B3+GHH1:>>#q!'')[82;BB8L  ABK 	MD(KD% zz|z) +EKK,<dmm L	Mr   c                 6    t        |      }|| d | k(  r| |d  S | S r   )r
   )partboundary_markerbm_lens      r   _fix_first_partz MultipartDecoder._fix_first_part   s+    _%d7Fm+= Kr   c                      dj                  d j                  f       fdd |j                  dj                  df            }t        fd|D               _        y )Nr      --c                 \    t         j                  |       }t        |j                        S r   )r;   rX   r0   r!   )rU   fixedrJ   r3   s     r   	body_partz/MultipartDecoder._parse_body.<locals>.body_part   s&    $44T8DEE4==11r   c                 <    | dk7  xr | dk7  xr | d d dk7  xr | dk7  S )Nr      
   s   --
rZ   r   )rU   s    r   	test_partz/MultipartDecoder._parse_body.<locals>.test_part   s;    CK "GO"!H	)" EM#r   r_   c              3   @   K   | ]  } |      s |        y wr   r   )r   rE   r]   ra   s     r   r"   z/MultipartDecoder._parse_body.<locals>.<genexpr>   s     GA)A,9Q<Gs   )joinrJ   rL   r>   r?   )r3   r   r?   r]   rJ   ra   s   `  @@@r   rA   zMultipartDecoder._parse_body   sT    88UDMM23	2	# chh':;<GGG
r   c                 f    |j                   }|j                  j                  dd       } | |||      S )Nzcontent-type)r   r-   get)clsresponser!   r   r=   s        r   from_responsezMultipartDecoder.from_response   s4    ""''++NDA7L(33r   N)zutf-8)r   r   r   r8   r5   r@   staticmethodrX   rA   classmethodrh   r   r   r   r;   r;   J   s@    8"M  H  4 4r   r;   )r8   r#   email.parserr&   encoderr   requests.structuresr   r   	Exceptionr   r   r.   objectr0   r;   r   r   r   <module>rp      sU        39
	y 		y 	2v 2BR4v R4r   