
    .Ph                         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dS )z

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

This holds all the implementation details of the MultipartDecoder

    N   encode_with)CaseInsensitiveDictc                 t    |                      |          }| d |         | |t          |          z   d          fS N)findlen)contentboundpoints      c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/requests_toolbelt/multipart/decoder.py_split_on_findr      s<    LLE6E6?GECJJ$6$7$7888    c                       e Zd ZdS ) ImproperBodyPartContentExceptionN__name__
__module____qualname__ r   r   r   r              Dr   r   c                       e Zd ZdS ) NonMultipartContentTypeExceptionNr   r   r   r   r   r      r   r   r   c                     t           j        d         }|dk    r|                               } t          j                                                            |                                           }fd|D             S )Nr      c              3   ^   K   | ]'\  }}t          |          t          |          fV  (d S r   r   ).0kvencodings      r   	<genexpr>z!_header_parser.<locals>.<genexpr>#   sT        Aq 
Q	!	!;q(#;#;<     r   )sysversion_infodecodeemailparserHeaderParserparsestritems)stringr!   majorheaderss    `  r   _header_parserr.      s    QEzzx((l''))226::@@BBG      r   c                   .    e Zd ZdZd Ze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                     || _         i }d|v rAt          |d          \  }| _        |dk    r"t          |                                |          }nt          d          t          |          | _        d S )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   sz     '!!"0+"F"FE4<||(BB26   +733r   c                 @    | j                             | j                  S )z'Content of the ``BodyPart`` in unicode.)r   r%   r!   )r3   s    r   textzBodyPart.textD   s     |""4=111r   N)r   r   r   __doc__r5   propertyr7   r   r   r   r0   r0   )   sH        
 
4 4 4 2 2 X2 2 2r   r0   c                   T    e Zd ZdZd	dZd Zed             Zd Ze	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                     || _         || _        t                      | _        |                                  |                     |           d S r   )content_typer!   tupleparts_find_boundary_parse_body)r3   r   r>   r!   s       r   r5   zMultipartDecoder.__init__g   sH    ( WW
!!!!!r   c                    t          d | j                            d          D                       }|d         }|                    d          d                                         dk    r"t	          d                    |                    |dd          D ]Z}t          |d          \  }}|                                d	k    r-t          |                    d
          | j	                  | _
        [d S )Nc              3   >   K   | ]}|                                 V  d S r   )strip)r   xs     r   r"   z2MultipartDecoder._find_boundary.<locals>.<genexpr>r   s*      HHa		HHHHHHr   ;r   /	multipartz)Unexpected mimetype in content-type: '{}'r   =boundary")r?   r>   splitlowerr   formatr   r   rE   r!   rK   )r3   ct_infomimetypeitemattrvalues         r   rA   zMultipartDecoder._find_boundaryq   s    HH4+<+B+B3+G+GHHHHH1:>>#q!''))[882;BB8LL   ABBK 	M 	MD( KD% zz||z)) +EKK,<,<dm L L	M 	Mr   c                 T    t          |          }|| d |         k    r
| |d          S | S r   )r
   )partboundary_markerbm_lens      r   _fix_first_partz MultipartDecoder._fix_first_part   s5    _%%d7F7m++= Kr   c                      d                     d j        f           fdd |                    d                     df                    }t          fd|D                        _        d S )Nr      --c                 d    t                               |           }t          |j                  S r   )r;   rY   r0   r!   )rV   fixedrK   r3   s     r   	body_partz/MultipartDecoder._parse_body.<locals>.body_part   s*    $44T8DDEE4=111r   c                 B    | dk    o| dk    o| d d         dk    o| dk    S )Nr      
   s   --
r[   r   )rV   s    r   	test_partz/MultipartDecoder._parse_body.<locals>.test_part   s=    CK "GO"!H	)" EM#r   r`   c              3   D   K   | ]} |           |          V  d S r   r   )r   rF   r^   rb   s     r   r"   z/MultipartDecoder._parse_body.<locals>.<genexpr>   s8      GGA))A,,G99Q<<GGGGGGr   )joinrK   rM   r?   r@   )r3   r   r@   r^   rK   rb   s   `  @@@r   rB   zMultipartDecoder._parse_body   s    88UDM233	2 	2 	2 	2 	2 	2	# 	# 	# chh':;;<<GGGGGGGGGG


r   c                 `    |j         }|j                            dd           } | |||          S )Nzcontent-type)r   r-   get)clsresponser!   r   r>   s        r   from_responsezMultipartDecoder.from_response   s6    "'++NDAAs7L(333r   N)r<   )r   r   r   r8   r5   rA   staticmethodrY   rB   classmethodri   r   r   r   r;   r;   J   s         8" " " "M M M   \H H H  4 4 4 [4 4 4r   r;   )r8   r#   email.parserr&   encoderr   requests.structuresr   r   	Exceptionr   r   r.   objectr0   r;   r   r   r   <module>rq      s%    


                 3 3 3 3 3 39 9 9
	 	 	 	 	y 	 	 		 	 	 	 	y 	 	 	  2 2 2 2 2v 2 2 2BR4 R4 R4 R4 R4v R4 R4 R4 R4 R4r   