
    L^iw>                     :   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ g dZ	 ddlmZ n# e$ r dZY nw xY w	 ddlZd Zn# e$ r ddlZddlZd ZY nw xY werd	 Znd
 Zerd Znd Z G d de          Zd ZddZd Zd Z G d d          ZddZdS )zmultipart/form-data encoding module

This module provides functions that faciliate encoding name/value pairs
as multipart/form-data suitable for a HTTP POST or PUT request.

multipart/form-data is the standard way to upload files over HTTP    N)Header)PY3advance_iterator
quote_plusto_bytearrayto_bytes	to_string)gen_boundaryencode_and_quoteMultipartParamencode_stringencode_file_headerget_body_sizeget_headersmultipart_encode)UnsupportedOperationc                  2    t          j                    j        S )<Returns a random string to use as the boundary for a message)uuiduuid4hex     ;D:\Nbitz\venv\Lib\site-packages\cloudinary/poster/encode.pyr
   r
      s    z||r   c                      t          j        d          } t          j        t	          |                                                     S )r      )randomgetrandbitsshanewstr	hexdigest)bitss    r   r
   r
   $   s4    !#&&ws4yy!!++---r   c                 B    | d S t          t          |                     S N)r   r   datas    r   r   r   *   s     <4(4..)))r   c                 |    | dS t          | t                    r|                     d          } t          |           S )zaIf ``data`` is unicode, return quote_plus(data.encode("utf-8")) otherwise return quote_plus(data)Nutf-8)
isinstanceunicodeencoder   r&   s    r   r   r   0   s=    <4dG$$ 	(;;w''D$r   c                     | d S t          | t                    r| S 	 t          |           S # t          $ r t          t	          |                     cY S w xY wr%   )r*   bytesr   AttributeErrorr!   ss    r   _strifyr2   :   si    945!! 	(H({{"! ( ( (A'''''(s   , &AAc                 |    | dS t          | t                    r|                     d          S t          |           S )zzIf s is a unicode string, encode it to UTF-8 and return the results,
        otherwise return str(s), or None if s is NoneNr)   )r*   r+   r,   r!   r0   s    r   r2   r2   E   s=     94a!! 	%88G$$$1vvr   c                   p    e Zd ZdZ	 	 ddZd Zd Zed             Zed             Z	d Z
d	 ZddZd ZdS )r   a  Represents a single parameter in a multipart/form-data request

    ``name`` is the name of this parameter.

    If ``value`` is set, it must be a string or unicode object to use as the
    data for this parameter.

    If ``filename`` is set, it is what to say that this parameter's filename
    is.  Note that this does not have to be the actual filename any local file.

    If ``filetype`` is set, it is used as the Content-Type for this parameter.
    If unset it defaults to "text/plain; charset=utf8"

    If ``filesize`` is set, it specifies the length of the file ``fileobj``

    If ``fileobj`` is set, it must be a file-like object that supports
    .read().

    Both ``value`` and ``fileobj`` must not be set, doing so will
    raise a ValueError assertion.

    If ``fileobj`` is set, and ``filesize`` is not specified, then
    the file's size will be determined first by stat'ing ``fileobj``'s
    file descriptor, and if that fails, by seeking to the end of the file,
    recording the current position as the size, and then by seeking back to the
    beginning of the file.

    ``cb`` is a callable which will be called from iter_encode with (self,
    current, total), representing the current parameter, current amount
    transferred, and the total size.
    Nc                    t          |                                          | _        t          |          | _        |d | _        nt          r-|                    dd          }t          |          | _        d}	nGt          |t                    r|                    dd          | _        nt          |          | _        d}	| j                            |	                              t          d          t          d                    | _        t          |          | _        || _        || _        || _        | j        | j        t#          d          ||	 t%          j        |                                          j        | _        d S # t,          t.          t0          f$ rf 	 |                    dd	           |                                | _        |                    d           Y d S # t6          $ r t#          d
          w xY ww xY wd S d S )Nasciixmlcharrefreplaceunicode_escapestring_escape"z\"z-Only one of value or fileobj may be specifiedr      zCould not determine filesize)r   r,   namer2   valuefilenamer   r	   r*   r+   r!   replacer   filetypefilesizefileobjcb
ValueErrorosfstatfilenost_sizeOSErrorr/   r   seektell	Exception)
selfr<   r=   r>   r@   rA   rB   rC   byte_filenameencodings
             r   __init__zMultipartParam.__init__o   s   4LL''))	U^^
 DMM 
+ (9L M M )- 8 8+h00 2$,OOG=P$Q$QDMM$'MMDM* M00::BB8C==RZ[`RaRabbDM)) :!dl&>LMMM8#3E ")9)9 : : B^-AB E E EELLA&&&$+LLNNDMLLOOOOOO  E E E$%CDDDEE	 #3#3s   0F
 
H"AG))HHc                 f     g d} fd|D             }fd|D             }t          ||          S )N)r<   r=   r>   r@   rA   rB   c                 0    g | ]}t          |          S r   getattr).0arM   s     r   
<listcomp>z*MultipartParam.__cmp__.<locals>.<listcomp>   s#    33374##333r   c                 0    g | ]}t          |          S r   rS   )rU   rV   others     r   rW   z*MultipartParam.__cmp__.<locals>.<listcomp>   s#    333'%##333r   )cmp)rM   rY   attrsmyattrsoattrss   ``   r   __cmp__zMultipartParam.__cmp__   sQ    PPP3333U3333333U3337F###r   c                 x    | j         | j                             d           d S | j        t          d          d S )Nr   z&Don't know how to reset this parameter)rB   rJ   r=   rD   rM   s    r   resetzMultipartParam.reset   sE    <#La     ZEFFF  r   c           
           | |t           j                            |          t          j        |          d         t           j                            |          t          |d                    S )a6  Returns a new MultipartParam object constructed from the local
        file at ``filename``.

        ``filesize`` is determined by os.path.getsize(``filename``)

        ``filetype`` is determined by mimetypes.guess_type(``filename``)[0]

        ``filename`` is set to os.path.basename(``filename``)
        r   rb)r>   r@   rA   rB   )rE   pathbasename	mimetypes
guess_typegetsizeopen)cls	paramnamer>   s      r   	from_filezMultipartParam.from_file   sb     s9rw'7'7'A'A%0::1=GOOH55$//1 1 1 	1r   c           	         t          |d          r|                                }g }|D ]}t          ||           r|                    |           (|\  }}t          ||           r#|j        |k    sJ |                    |           `t          |d          rSt          |dd          }|t          j        |          d         }nd}|                     | ||||                     |                     | ||                     |S )af  Returns a list of MultipartParam objects from a sequence of
        name, value pairs, MultipartParam instances,
        or from a mapping of names to values

        The values may be strings or file objects, or MultipartParam objects.
        MultipartParam object names must match the given names in the
        name,value pairs or mapping, if applicable.itemsreadr<   Nr   )r<   r>   r@   rB   )hasattrrn   r*   appendr<   rT   rf   rg   )rj   paramsretvalitemr<   r=   r>   r@   s           r   from_paramszMultipartParam.from_params   sL    67## 	$\\^^F 	0 	0D$$$ d###KD%%%% zT))))e$$$uf%% 0"5&$77'(3H==a@HH#Hccth+3UD D D E E E E cc$..////r   c                    t          |          }d|z  g}| j        r!d| j        dt          | j                  d}n
d| j        z  }|                    d|z             | j        rt          | j                  }nd}|                    d|z             |                    d	           |                    d	           d
                    |          S )z4Returns the header of the encoding of this parameter--%szform-data; name="z"; filename="r:   zform-data; name="%s"zContent-Disposition: %sztext/plain; charset=utf-8zContent-Type: %s 
)r   r>   r<   r	   rq   r@   join)rM   boundaryheadersdispositionr@   s        r   
encode_hdrzMultipartParam.encode_hdr   s    #H--H$%= 	= 	=			9T]33335KK 149<K0;>???= 	3 //HH2H)H4555rr{{7###r   c                 D   | j         | j                                        }n| j         }t          j        t          dt          j        |          z            |t          j                  rt          d          t          | 	                    |                    |z   dz   S )z-Returns the string encoding of this parameterNz^--%s$z boundary found in encoded strings   
)
r=   rB   ro   researchr   escapeMrD   r~   )rM   r{   r=   s      r   r,   zMultipartParam.encode   s    :L%%''EEJE9Xh8)<)<<==ubdKK 	A?@@@1122U:WDDr      c              #     K   |                      |          }d}| j        M|                     |          }|t          |          z  }|V  | j        r|                     | ||           dS dS t          |                     |                    }|t          |          z  }|V  | j        r|                     | ||           t          d          }dt          |          z  }t          j
        t          dt          j        |          z            t          j                  }	 | j                            |          }|s6|dz  }t          d          V  | j        r|                     | ||           dS ||z  }|                    |          rt!          d	          |t          t          |                     dz
  d         }|t          |          z  }|V  | j        r|                     | ||           )
zYields the encoding of this parameter
        If self.fileobj is set, then blocks of ``blocksize`` bytes are read and
        yielded.r   Nrx   rw   z^%s$Tr;   ry   zboundary found in file data)get_sizer=   r,   lenrC   r   r~   r   r   r   compiler   r   rB   ro   r   rD   )	rM   r{   	blocksizetotalcurrentblock
last_blockencoded_boundaryboundary_exps	            r   iter_encodezMultipartParam.iter_encode   s"      h'':!KK))Es5zz!GKKKw .gu-----. . T__X6677Es5zz!GKKKw .gu---%b))J%(8(B(BB:"),<"="==>>F FL2)))44 qLG"6*****w 6gu555Ee#
&&z22 D$%BCCC'X6F-G-G)H)H(H(J(K(KL
3u::%7 2GGD'51112r   c                     | j         | j         }nt          | j                  }t          |                     |                    dz   |z   S )z_Returns the size in bytes that this param will be when encoded
        with the given boundary.Nr;   )rA   r   r=   r~   )rM   r{   	valuesizes      r   r   zMultipartParam.get_size%  sF     =$IIDJI4??8,,--1I==r   )NNNNNN)r   )__name__
__module____qualname____doc__rP   r^   ra   classmethodrl   ru   r~   r,   r   r   r   r   r   r   r   O   s         > BF15&E &E &E &EP$ $ $G G G 1 1 [1  ! ! [!F$ $ $4
E 
E 
E%2 %2 %2 %2N> > > > >r   r   c                 H    t          ||                              |           S )zReturns ``name`` and ``value`` encoded as a multipart/form-data
    variable.  ``boundary`` is the boundary string used throughout
    a single request to separate variables.)r   r,   )r{   r<   r=   s      r   r   r   0  s"    
 $&&--h777r   c                 N    t          ||||                              |           S )aQ  Returns the leading data for a multipart/form-data field that contains
    file data.

    ``boundary`` is the boundary string used throughout a single request to
    separate variables.

    ``paramname`` is the name of the variable in this request.

    ``filesize`` is the size of the file data.

    ``filename`` if specified is the filename to give to this field.  This
    field is only useful to the server for determining the original filename.

    ``filetype`` if specified is the MIME type of this file.

    The actual file data should be sent after this header has been sent.
    )rA   r>   r@   )r   r~   )r{   rk   rA   r>   r@   s        r   r   r   8  s2    & )h#+- - --7Z-A-ABr   c                     t          fdt                              |           D                       }|t                    z   dz   S )z\Returns the number of bytes that the multipart/form-data encoding
    of ``params`` will be.c              3   B   K   | ]}|                               V  d S r%   )r   )rU   pr{   s     r   	<genexpr>z get_body_size.<locals>.<genexpr>R  s/      PPqzz(##PPPPPPr      )sumr   ru   r   )rr   r{   sizes    ` r   r   r   O  sK     PPPP^-G-G-O-OPPPPPD#h--!##r   c                 x    i }t          |          }d|z  |d<   t          t          | |                    |d<   |S )zyReturns a dictionary with Content-Type and Content-Length headers
    for the multipart/form-data encoding of ``params``.z multipart/form-data; boundary=%szContent-TypezContent-Length)r   r!   r   )rr   r{   r|   s      r   r   r   V  sG     G(##H@8KGN #M&($C$C D DGNr   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )multipart_yielderc                     || _         || _        || _        d| _        d | _        d | _        d| _        t          ||          | _        d S Nr   )	rr   r{   rC   ir   
param_iterr   r   r   )rM   rr   r{   rC   s       r   rP   zmultipart_yielder.__init__a  sH     "6844


r   c                     | S r%   r   r`   s    r   __iter__zmultipart_yielder.__iter__l  s    r   c                 *    |                                  S r%   )nextr`   s    r   __next__zmultipart_yielder.__next__o  s    yy{{r   c                    | j         	 t          | j                   }| xj        t          |          z  c_        | j        r&|                     | j        | j        | j                   |S # t          $ r d| _        d| _         Y nw xY w| j        t          | j        t          | j	                  k    rxd| _         d| _        d| _        t          d| j        z            }| xj        t          |          z  c_        | j        r&|                     | j        | j        | j                   |S | j	        | j                 | _        | j                            | j                  | _         | xj        dz  c_        t          |           S )zTgenerator function to yield multipart/form-data representation
        of parametersNz--%s--
   )r   r   r   r   rC   r   r   StopIterationr   rr   r   r{   r   )rM   r   s     r   r   zmultipart_yielder.nextr  sd    ?&'(99E

*7 >GGDFDL$*===  ' ' '"&' 6>Vs4;''''"DODFDF\DM9::ELLCJJ&LLw :dj999LTV$&,,T];;!%%%s   AA) )BBc                 ^    d| _         d| _        | j        D ]}|                                 d S r   )r   r   rr   ra   )rM   params     r   ra   zmultipart_yielder.reset  s:    [ 	 	EKKMMMM	 	r   N)r   r   r   rP   r   r   r   ra   r   r   r   r   r   `  s_        	5 	5 	5    & & &>    r   r   c                     |t                      }nt          |          }t          | |          }t                              |           } t          | ||          |fS )ah  Encode ``params`` as multipart/form-data.

    ``params`` should be a sequence of (name, value) pairs or MultipartParam
    objects, or a mapping of names to values.
    Values are either strings parameter values, or file-like objects to use as
    the parameter value.  The file-like objects must support .read() and either
    .fileno() or both .seek() and .tell().

    If ``boundary`` is set, then it as used as the MIME boundary.  Otherwise
    a randomly generated boundary will be used.  In either case, if the
    boundary string appears in the parameter values a ValueError will be
    raised.

    If ``cb`` is set, it should be a callback which will get called as blocks
    of data are encoded.  It will be called with (param, current, total),
    indicating the current parameter being encoded, the current amount encoded,
    and the total amount to encode.

    Returns a tuple of `datagen`, `headers`, where `datagen` is a
    generator that will yield blocks of data that make up the encoded
    parameters, and `headers` is a dictionary with the assoicated
    Content-Type and Content-Length headers.

    Examples:

    >>> datagen, headers = multipart_encode( [("key", "value1"), ("key", "value2")] )
    >>> s = "".join(datagen)
    >>> assert "value2" in s and "value1" in s

    >>> p = MultipartParam("key", "value2")
    >>> datagen, headers = multipart_encode( [("key", "value1"), p] )
    >>> s = "".join(datagen)
    >>> assert "value2" in s and "value1" in s

    >>> datagen, headers = multipart_encode( {"key": "value1"} )
    >>> s = "".join(datagen)
    >>> assert "value2" not in s and "value1" in s

    )r
   r   r   r   ru   r   )rr   r{   rC   r|   s       r   r   r     s\    P >>h''&(++G''//FVXr22G;;r   )NN)r   rf   rE   r   email.headerr   cloudinary.compatr   r   r   r   r   r	   __all__ior   ImportErrorr   r
   r   r   r   r2   objectr   r   r   r   r   r   r   r   r   r   <module>r      sk  E E     				 				      4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4N N N '''''''       .KKK         . . .MMMJJJ. . . . .	.   * * * *       	( 	( 	( 	(  ^> ^> ^> ^> ^>V ^> ^> ^>B8 8 8B B B B.$ $ $  5 5 5 5 5 5 5 5p0< 0< 0< 0< 0< 0<s   1 ;;A AA