
    #HJj2                    |   d Z ddlmZ ddlZddlZddlZddlZddlm	Z	m
Z
 	 ddlZddlZddlmZmZ e	rddlmZ ddlmZ d	Zd
Z ej0                  e      Z G d de
      ZddiZ	 ddZddZdddddddef	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ G d dej@                        Z! G d de!      Z"y# e$ r dZY w xY w)z3Implements file-like objects for reading from http.    )annotationsN)TYPE_CHECKING	TypedDictT)
bytebuffer	constants)WriteableBuffer)TransportParamsi   )httphttpsc                  "    e Zd ZU ded<   ded<   y)_HTTPUristrschemeuri_pathN)__name__
__module____qualname____annotations__     Y/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/smart_open/http.pyr   r   $   s    KMr   r   zAccept-Encodingidentityc                    t         j                  j                  |       }|j                  t        v sJ |j
                  |j                  z   }d|j                  d      z   }|j                  |dS )zAParse an ``http://`` or ``https://`` URI into its path component./)r   r   )urllibparseurlsplitr   SCHEMESnetlocpathlstrip)uri_as_string	split_urir   s      r   	parse_urir$   2   sb    %%m4Iw&&&)..0HX__S))H&&H==r   BufferedInputBasec                d    t         j                  j                  t        |      }t        | |fi |S )zAOpen an HTTP/HTTPS URI using the given mode and transport params.)
smart_openutilscheck_kwargsopen)urimodetransport_paramskwargss       r   open_urir/   <   s-    **41ABFT$V$$r   Fc
                    |t         j                  k(  rXt        | ||	|||||||
      }
t        j                  t
        j                  j                  |       j                        |
_	        |
S d|d}t        |      )a  Implement streamed reader from a web site.

    Supports Kerberos and Basic HTTP authentication.

    Args:
        uri: The URL to open.
        mode: The mode to open using.
        kerberos: If True, will attempt to use the local Kerberos credentials.
        user: The username for authenticating over HTTP.
        password: The password for authenticating over HTTP.
        cert: If a string, path to ssl client cert file (``.pem``).
            If a tuple, ``('cert', 'key')``.
        headers: Any headers to send in the request. If ``None``, the default headers
            are sent: ``{'Accept-Encoding': 'identity'}``. To use no headers at all,
            set this variable to an empty dict, ``{}``.
        timeout: Request timeout in seconds.
        session: The ``requests.Session`` object to use with HTTP GET requests.
            Can be used for OAuth2 clients.
        buffer_size: The buffer size to use when performing I/O.

    Returns:
        A file-like object opened for reading.

    Raises:
        NotImplementedError: If ``mode`` is anything other than ``"rb"``.

    Note:
        If neither ``kerberos`` nor ``(user, password)`` are set, will connect
        unauthenticated, unless set separately in headers.
    )buffer_sizekerberosuserpasswordcertheaderssessiontimeoutzhttp support for mode z not implemented)r   READ_BINARYSeekableBufferedInputBase	posixpathbasenamer   r   urlparser    nameNotImplementedError)r+   r,   r2   r3   r4   r5   r6   r8   r7   r1   fobjmsgs               r   r*   r*   B   s    T y$$$(#
 &&v||'<'<S'A'F'FG	"4(*:
;C
c
""r   c            	          e Zd ZU dZded<   dZded<   dedddddddf		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zdd
Ze	dd       Z
ddZddZddZdddZdddZddZy)r%   zDBuffered HTTP reader implementing the `io.BufferedIOBase` interface.r   r>   Nzrequests.Response | NoneresponserFc                b   || _         || _        |	xs t        | _        |rdd l}|j                         | _        n||
||f| _        nd | _        || _        || _        |t        j                         | _        n|| _        |
| _        | j                  j                  | j                   | j                  | j                  d| j                  | j                        | _        | j                  j                  s| j                  j!                          t#        j$                  |      | _        d| _        y )Nr   T)authr5   streamr6   r8   )urlr5   requestsr7   requests_kerberosHTTPKerberosAuthrF   r1   r,   _HEADERScopyr6   r8   getrC   okraise_for_statusr   
ByteBuffer_read_buffer_current_pos)selfrH   r,   r1   r2   r3   r4   r5   r6   r7   r8   rJ   s               r   __init__zBufferedInputBase.__init__   s     	*($)::<DI("6x(DIDI&	?#==?DL"DL((HHLLLL ) 
 }}MM**,:D:O:OP[:\r   c                d    t         j                  d       | j                  sd| _        d| _        yy)zFlush and close this stream.zclose: calledN)loggerdebugclosedrC   rR   rT   s    r   closezBufferedInputBase.close   s)    _%{{ DM $D r   c                    | j                   du S )z$Return True if the stream is closed.N)rC   rZ   s    r   rY   zBufferedInputBase.closed   s     }}$$r   c                     y)z+Return True if the stream can be read from.Tr   rZ   s    r   readablezBufferedInputBase.readable   s    r   c                     y)z<Return False; the base HTTP reader does not support seeking.Fr   rZ   s    r   seekablezBufferedInputBase.seekable   s    r   c                "    t         j                  zUnsupported.ioUnsupportedOperationrZ   s    r   detachzBufferedInputBase.detach       %%%r   c                ,   |d}|dk  rd| }t        |      t        j                  d|       | j                  | j                  }}|||dk(  ry|dk(  rRt        |      r,|j                         |j                  j                         z   }nh|j                  j                         }nMt        |      |k  r.|j                  |j                        dk(  rnt        |      |k  r.|j                  |      }| xj                  t        |      z  c_	        |S )z+Mimic the read call to a filehandle object.zsize must be >= -1, got zreading with size: %dr   r   )

ValueErrorrW   rX   rR   rC   lenreadrawfillrS   )rT   sizerA   bufrC   retvals         r   rl   zBufferedInputBase.read   s    <D"9,TF3CS/!,d3))4==X;(*dai2:3xhll&7&7&99!**, c(T/88HLL)Q. c(T/ XXd^FS[(r   c                &    | j                  |      S )zThis is the same as read().)ro   )rl   rT   ro   s     r   read1zBufferedInputBase.read1   s    yydy##r   c                    t        |      j                  d      }| j                  t        |            }|sy||dt        |       t        |      S )zLRead up to ``len(b)`` bytes into ``b``, and return the number of bytes read.Br   N)
memoryviewcastrl   rk   )rT   bmvdatas       r   readintozBufferedInputBase.readinto   sG    ]$yyR!;SY4yr   rH   r   r,   r   r1   intr2   boolr3   
str | Noner4   r   r5   str | tuple[str, str] | Noner6   dict[str, str] | Noner7   requests.Session | Noner8   float | NonereturnNone)r   r   )r   r   )r   zio.RawIOBase)ri   )ro   
int | Noner   bytes)ry   r   r   r~   )r   r   r   __doc__r   rC   DEFAULT_BUFFER_SIZErU   r[   propertyrY   r^   r`   rf   rl   rt   r|   r   r   r   r%   r%      s    N
I)-H&-
 .#-1)-+/ $22 2 	2
 2 2 2 +2 '2 )2 2 
2n% % %&8$r   c            	           e Zd ZdZdedddddddf		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZdddZddZddZddd	Z	ddd
Z
 xZS )r:   a  Seekable streamed reader from a web site.

    Supports Kerberos, client certificate and Basic HTTP authentication.
    If ``kerberos`` is True, will attempt to use the local Kerberos credentials.
    If ``cert`` is set, will try to use a client certificate. Otherwise, will try
    to use "basic" HTTP authentication via username/password. If none of those are
    set, will connect unauthenticated.
    rD   FNc                2   t         |   |||||||||	|

       | j                  J t        | j                  j                  j                  dd            | _        | j                  j                  j                  dd      j                         dk7  | _        y )NzContent-Lengthri   zAccept-Ranges none)	superrU   rC   r~   r6   rN   content_lengthlower	_seekable)rT   rH   r,   r1   r2   r3   r4   r5   r6   r7   r8   	__class__s              r   rU   z"SeekableBufferedInputBase.__init__  s     	dK44QXZacjk}}(((!$--"7"7";";<Lb"QR ..22?BGMMOSYYr   c                   t         j                  d||       |t        j                  vrdt        j                  }t	        |      | j                         sd}t        |      | j                  }|d}t        |      |t        j                  k(  r|}n2|t        j                  k(  r| j                  |z   }n| j                  |z   }| j                  dk(  r"t        j                  j                  |d      }n+t        j                  j                  || j                        }| j                  |k(  r| j                  S || j                  kD  rL|| j                  z
  t        |      k  r1|j!                  || j                  z
         || _
        | j                  S t         j                  d| j                  |       || _
        || j                  k(  r#d| _        |j%                          | j                  S | j'                  |      }|j(                  r#|| _        |j%                          | j                  S d| _        | j                  S )	aU  Seek to the specified position.

        Args:
            offset: The offset in bytes.
            whence: Where the offset is from.

        Returns:
            The position after seeking.

        Raises:
            ValueError: If ``whence`` is not one of ``WHENCE_CHOICES``.
            OSError: If the stream is not seekable.
        z seeking to offset: %r whence: %rz invalid whence, expected one of zstream is not seekableNzseek on closed streamri   )maxvalz0http seeking from current_pos: %d to new_pos: %d)rW   rX   r   WHENCE_CHOICESrj   r`   OSErrorrR   WHENCE_STARTWHENCE_CURRENTrS   r   r'   r(   clamprk   rl   rC   empty_partial_requestrO   )rT   offsetwhencerA   rp   new_posrC   s          r   seekzSeekableBufferedInputBase.seek#  s    	7H1114Y5M5M4PQCS/!}}*C#,;)C#,Y+++Gy///''&0G))F2G"$ &&,,WT,BG &&,,WT=P=P,QG'$$$ T&&&7T5F5F+F#c(+RHHWt0001 'D$$$GIZIZ\cd#d))) DMIIK     ,,W5H{{ (		     !%   r   c                    | j                   S )z#Return the current stream position.)rS   rZ   s    r   tellzSeekableBufferedInputBase.telle  s       r   c                    | j                   S )zAReturn True if the server reports it accepts byte-range requests.)r   )rT   argsr.   s      r   r`   z"SeekableBufferedInputBase.seekablei  s    ~~r   c                "    t         j                  rb   rc   rs   s     r   truncatez"SeekableBufferedInputBase.truncatem  rg   r   c                   | j                   j                         }|"t        j                  j	                  |      |d<   | j
                  j                  | j                  | j                  d| j                  || j                        S )NrangeT)rF   rG   r5   r6   r8   )r6   rM   r'   r(   make_range_stringr7   rN   rH   rF   r5   r8   )rT   	start_posr6   s      r   r   z*SeekableBufferedInputBase._partial_requestq  ss    ,,##% )//AA)LGG||HHLL   
 	
r   r}   )r   )r   r~   r   r~   r   r~   )r   r~   )r   objectr.   r   r   r   )N)ro   r   r   r~   )r   r   r   zrequests.Response)r   r   r   r   r   rU   r   r   r`   r   r   __classcell__)r   s   @r   r:   r:     s     .#-1)-+/ $ZZ Z 	Z
 Z Z Z +Z 'Z )Z Z 
Z0@!D!&
 
r   r:   )r"   r   r   r   )r+   r   r,   r   r-   r	   r   r%   )r+   r   r,   r   r2   r   r3   r   r4   r   r5   r   r6   r   r8   r   r7   r   r1   r~   r   r%   )#r   
__future__r   rd   loggingr;   urllib.parser   typingr   r   rI   ImportErrorMISSING_DEPSsmart_open.utilsr'   r   r   	_typeshedr   smart_open._typingr	   r   r   	getLoggerr   rW   r   rL   r$   r/   r*   BufferedIOBaser%   r:   r   r   r   <module>r      s>   : " 	    +  ,)2  
			8	$y 
 z*>% )-%) '+*:#	:#
:# :# 	:#
 :# ':# #:# :# %:# :# :#z)) D|
 1 |
]  Ls   B1 1B;:B;