
    lc@                     (   d Z ddlZddlZddlZddlZddlZ	 ddlZddl	Zn# e
$ r dZY nw xY w ej        e          ZeeefZ	 dZ	 dZ	 dZ	 dZ	 d Zd	 Zdeeefd
Z G d de          Z G d dej                  Z G d dej                  ZdS )zPImplements file-like objects for reading and writing to/from Azure Blob Storage.    NTazurei   i  @    c                     t           j                            |           }|j        t          k    sJ |j        }|j                            d          }|sd}|}n|}|}t          t          ||          S )N/z$root)schemecontainer_idblob_id)	
smart_openutilssafe_urlsplitr   SCHEMEnetlocpathlstripdict)uri_as_stringsrfirstsecondr   r	   s         0lib/python3.11/site-packages/smart_open/azure.py	parse_urir   ,   sz    			'	'	6	6B9IEW^^C  F  vL'JJJJ    c                     t          |           }t          j                            t          |          }t	          |d         |d         |fi |S )Nr   r	   )r   r
   r   check_kwargsopen)urimodetransport_params
parsed_urikwargss        r   open_urir!   =   sJ    3J**41ABBF
>*Jy,A4RR6RRRr   c                 
   |st          d          |t          j        j        k    r$t	          | |||t          j        j        |          S |t          j        j        k    rt          | |||          S t          d|z            )a  Open an Azure Blob Storage blob for reading or writing.

    Parameters
    ----------
    container_id: str
        The name of the container this object resides in.
    blob_id: str
        The name of the blob within the bucket.
    mode: str
        The mode for opening the object.  Must be either "rb" or "wb".
    client: azure.storage.blob.BlobServiceClient
        The Azure Blob Storage client to use when working with azure-storage-blob.
    buffer_size: int, optional
        The buffer size to use when performing I/O. For reading only.
    min_part_size: int, optional
        The minimum part size for multipart uploads.  For writing only.
    max_concurrency: int, optional
        The number of parallel connections with which to download. For reading only.

    z/you must specify the client to connect to Azure)buffer_sizeline_terminatormax_concurrency)min_part_sizez6Azure Blob Storage support for mode %r not implemented)	
ValueErrorr
   	constantsREAD_BINARYReaderBINARY_NEWLINEWRITE_BINARYWriterNotImplementedError)r   r	   r   clientr#   r&   r%   s          r   r   r   C   s    :  LJKKKz#// c#&0?+
 
 
 	
 
%2	2 c'	
 
 
 	
 ""Z]a"abbbr   c                   ,    e Zd ZdZd Zd ZddZd ZdS )	
_RawReaderz Read an Azure Blob Storage file.c                 >    || _         || _        d| _        || _        d S Nr   )_blob_size	_position_concurrency)selfblobsizeconcurrencys       r   __init__z_RawReader.__init__z   s%    

'r   c                     || _         | j         S )zSeek to the specified position (byte offset) in the Azure Blob Storage blob.

        :param int position: The byte offset from the beginning of the blob.

        Returns the position after seeking.
        r6   )r8   positions     r   seekz_RawReader.seek   s     "~r   c                     | j         | j        k    rdS |                     |          }| xj         t          |          z  c_         |S )Nr   )r6   r5   _download_blob_chunklen)r8   r:   binarys      r   readz_RawReader.read   sF    >TZ' 	3**400#f++%r   c                    | j         | j        k    rdS |dk    r'| j                            | j        | j                  }n'| j                            | j        | j        |          }t          j        d| j                   t          |t          j	        j
        j                  r|                                }n|                                }|S )Nr   rA   )offsetr%   )rH   r%   lengthz$reading with a max concurrency of %d)r5   r6   r4   download_blobr7   loggingdebug
isinstancer   storager9   StorageStreamDownloaderreadallrF   )r8   r:   streamrE   s       r   rC   z_RawReader._download_blob_chunk   s    :' 
	W
 3RZ 	WZ--T^UYUf-ggFFZ--~t7HQU . W WF<d>OPPPfem0HII 	#^^%%FF[[]]Fr   NrA   )__name__
__module____qualname____doc__r<   r@   rF   rC    r   r   r1   r1   w   s[        **( ( (         r   r1   c                       e Zd ZdZeej        j        efdZ	d Z
d Zd Zd Zej        j        fdZd Zdd
ZddZddZd ZddZddZddZd Zd Zd Zd Zd	S )r*   zReads bytes from Azure Blob Storage.

    Implements the io.BufferedIOBase interface of the standard library.

    :raises azure.core.exceptions.ResourceNotFoundError: Raised when the blob to read from does not exist.

    c                    |                     |          | _        | j                            |          | _        | j        *t          j        j                            d|d|          	 | j                                        d         | _	        n# t          $ r
 d| _	        Y nw xY wt          | j        | j	        |          | _        d| _        t          j                            |          | _        || _        d | _        d S )Nzblob z not found in r:   r   )get_container_client_container_clientget_blob_clientr4   r   core
exceptionsResourceNotFoundErrorget_blob_propertiesr5   KeyErrorr1   _raw_readerr6   r
   
bytebuffer
ByteBuffer_current_part_line_terminatorraw)r8   	containerr9   r/   r#   r$   r%   s          r   r<   zReader.__init__   s    "(!<!<Y!G!G +;;DAA
: 	*'===-1TT99=  	7799&ADJJ 	 	 	DJJJ	 &dj$*oNN'2==kJJ /
 s   ,$B B%$B%c                 V    t                               d           d| _        d| _        dS )zFlush and close this stream.zclose: calledN)loggerrL   r4   rb   r8   s    r   closezReader.close   s)    _%%%
r   c                     dS )z+Return True if the stream can be read from.TrW   rk   s    r   readablezReader.readable       tr   c                     dS )zyIf False, seek(), tell() and truncate() will raise IOError.

        We offer only seek support, and no truncate support.TrW   rk   s    r   seekablezReader.seekable   	     tr   c                     t           j        zUnsupported.ioUnsupportedOperationrk   s    r   detachzReader.detach       %%r   c                    t                               d||           |t          j        j        vr#t          d|t          j        j        fz            |t          j        j        k    r|}n*|t          j        j        k    r| j        |z   }n
| j	        |z   }|| _        | j
                            |           t                               d| j                   | j                                         | j        S )zSeek to the specified position.

        :param int offset: The offset in bytes.
        :param int whence: Where the offset is from.

        Returns the position after seeking.z seeking to offset: %r whence: %rz%invalid whence %i, expected one of %rzcurrent_pos: %r)rj   rL   r
   r(   WHENCE_CHOICESr'   WHENCE_STARTWHENCE_CURRENTr6   r5   rb   r@   re   empty)r8   rH   whencenew_positions       r   r@   zReader.seek   s     	7HHH-<< 	mDGQG[GjHl l m m m Z)66 	/!LLz+:: 	/>F2LL:.L%l+++&777  """~r   c                     | j         S )z,Return the current position within the file.r>   rk   s    r   tellzReader.tell  s
    ~r   Nc                     t           j        rt   ru   r8   r:   s     r   truncatezReader.truncate  ry   r   rA   c                    |dk    rdS |dk     r:| j         | _        |                                 | j                                        z   S t          | j                  |k    r|                     |          S | j        | j         k    r|                                 S |                                  |                     |          S )z6Read up to size bytes from the object and return them.r   r   )r5   r6   _read_from_bufferrb   rF   rD   re   _fill_bufferr   s     r   rF   zReader.read	  s    19 	F3AX 	F!ZDN))++d.>.C.C.E.EEE
 t!""d* 	0))$///>TZ' 	,))+++%%d+++r   c                 .    |                      |          S )zThis is the same as read().)r:   )rF   r   s     r   read1zReader.read1  s    yydy###r   c                     |                      t          |                    }|sdS ||dt          |          <   t          |          S )zDRead up to len(b) bytes into b, and return the number of bytes read.r   N)rF   rD   )r8   bdatas      r   readintozReader.readinto!  sE    yyQ   	1*3t99*4yyr   c                 `   |dk    rt          d          t          j                    }| j        | j        k    rt          | j                  dk    s| j                                        }| j        |v rF|	                    | j                  }|
                    |                     |dz                        nc|
                    |                                            |                                  | j        | j        k    t          | j                  dk    |                                S )zCRead up to and including the next newline.  Returns the bytes read.rA   z(limits other than -1 not implemented yetr   r   )r.   rv   BytesIOr6   r5   rD   re   peekrf   indexwriter   r   getvalue)r8   limitthe_lineremaining_buffernext_newlines        r   readlinezReader.readline)  s%   B; 	R%&PQQQ:<<>TZ/ 	$C8J4K4Kq4P 	$  $16688$(88 $/55d6KLLt55lQ6FGGHHHt5577888!!### >TZ/ 	$C8J4K4Kq4P 	$    """r   c                     |dk    r|nt          | j                  }| j                            |          }| xj        t          |          z  c_        |S )z:Remove at most size bytes from our buffer and return them.r   )rD   re   rF   r6   )r8   r:   parts      r   r   zReader._read_from_bufferC  sR     qy=ttc$*<&=&=!&&t,,#d))#r   c                 h   t          || j        j                  }t          | j                  |k     r{| j        | j        k    sm| j                            | j                  }|dk    rt          	                    d           dS t          | j                  |k     r| j        | j        k    id S d S d S d S )Nr   z reached EOF while filling bufferT)
maxre   _chunk_sizerD   r6   r5   fillrb   rj   rL   )r8   r:   
bytes_reads      r   r   zReader._fill_bufferL  s    4+788$$%%, 	T^tz5Q 	+001ABBJQ ?@@@t	 $$%%, 	T^tz5Q 	 	 	 	 	 	 	 	 	r   c                     | S NrW   rk   s    r   	__enter__zReader.__enter__T      r   c                 .    |                                   d S r   rl   r8   exc_typeexc_valexc_tbs       r   __exit__zReader.__exit__W      

r   c                 V    d| j         j        d| j        j        d| j        j        dS N(z, ))	__class__rS   
_containercontainer_namer4   	blob_namerk   s    r   __str__zReader.__str__Z  s8     !%!8!8!8!%!?!?!?!%!5!5!57 	7r   c                 T    | j         j        d| j        j        d| j        j        dS )N(container=, blob=r   r   rS   r[   r   r4   r   rk   s    r   __repr__zReader.__repr___  s5    N###T%;%J%J%JDJL`L`L`
 	
r   r   rR   )rS   rT   rU   rV   DEFAULT_BUFFER_SIZEr
   r(   r+   DEFAULT_MAX_CONCURRENCYr<   rl   rn   rq   rx   r|   r@   r   r   rF   r   r   r   r   r   r   r   r   r   rW   r   r   r*   r*      sq         ,&0?3   F         & & & #-"6"C    2  & & & &, , , ,($ $ $ $  # # # #4          7 7 7

 
 
 
 
r   r*   c                       e Zd ZdZefdZd Zd Zed             Z	d Z
d Zej        j        fdZdd
Zd Zd Zd Zd Zd Zd Zd Zd Zd	S )r-   zlWrites bytes to Azure Blob Storage.

    Implements the io.BufferedIOBase interface of the standard library.c                    || _         | j                             |          | _        | j                            |          | _        || _        d| _        d| _        d| _        t          j
                    | _        g | _        d | _        d S r3   )_clientrZ   r[   r\   r4   _min_part_size_total_size_total_parts_bytes_uploadedrv   r   re   _block_listrg   )r8   rh   r9   r/   r&   s        r   r<   zWriter.__init__j  s~     !%!B!B9!M!M+;;DAA
+ Z\\
 r   c                     d S r   rW   rk   s    r   flushzWriter.flush  s    r   c                 8   t                               d           | j        s^| j                                        dk    r|                                  | j                            | j                   g | _        d | _	        t                               d           d S )Nclosingr   zsuccessfully closed)
rj   rL   closedre   r   _upload_partr4   commit_block_listr   r   rk   s    r   rl   zWriter.close  s    Y{ 	 !&&((1, $!!###J(()9:::!DDL*+++++r   c                     | j         d u S r   )r   rk   s    r   r   zWriter.closed  s    |t##r   c                     dS )z+Return True if the stream supports writing.TrW   rk   s    r   writablezWriter.writable  ro   r   c                     dS )zIf False, seek(), tell() and truncate() will raise IOError.

        We offer only tell support, and no seek or truncate support.TrW   rk   s    r   rq   zWriter.seekable  rr   r   c                     t           j        rt   ru   )r8   rH   r   s      r   r@   zWriter.seek  ry   r   Nc                     t           j        rt   ru   r   s     r   r   zWriter.truncate  ry   r   c                     | j         S )z#Return the current stream position.)r   rk   s    r   r   zWriter.tell  s    r   c                 *    t          j        d          )Nzdetach() not supportedru   rk   s    r   rx   zWriter.detach  s    %&>???r   c                 r   t          |t                    s't          dt          dt          |                    | j                            |           | xj        t          |          z  c_        | j                                        | j	        k    r| 
                                 t          |          S )zWrite the given bytes (binary string) to the Azure Blob Storage file.

        There's buffering happening under the covers, so this may not actually
        do any HTTP transfer right away.zinput must be one of z, got: )rM   _BINARY_TYPES	TypeErrortypere   r   r   rD   r   r   r   )r8   r   s     r   r   zWriter.write  s     !]++ 	[)---QUVWQXQXQXYZZZ  ###CFF"""$$(;; 	 1vvr   c                 6   | j         dz   }| j                                        }| j        |z   dz
  }	 t	          |                              d          }t          j        |                                          }| j        	                    d           | j
                            || j                            |                     | j                            t          j        j                            |                     t&                              d|||dz             | xj         dz  c_         | xj        |z  c_        t+          j        | j                                                  | _        | j        	                    dt*          j                   d S )Nr       r   )block_idz+uploading part #%i, %i bytes (total %.3fGB)g      A)r   re   r   r   strzfillbase64	b64encodeencoder@   r4   stage_blockrF   r   appendr   rN   r9   	BlobBlockrj   inforv   r   SEEK_END)r8   part_numcontent_length
range_stopzero_padded_part_numr   s         r   r   zWriter._upload_part  sq   $q(+0022)N:Q>
	
  #8}}227;;#$8$?$?$A$ABB"""
x);)@)@)P)PQQQ 2 < <h < O OPPP9nj;&>	
 	
 	

 	Q.Z(:(?(?(A(ABB2;/////r   c                     | S r   rW   rk   s    r   r   zWriter.__enter__  r   r   c                 .    |                                   d S r   r   r   s       r   r   zWriter.__exit__  r   r   c                 V    d| j         j        d| j        j        d| j        j        dS r   r   rk   s    r   r   zWriter.__str__  s9     N###"111J   
 	
r   c                 d    | j         j        d| j        j        d| j        j        d| j        dS )Nr   r   z, min_part_size=r   )r   rS   r[   r   r4   r   r   rk   s    r   r   zWriter.__repr__  sC    N###"111J   	
 	
r   r   )rS   rT   rU   rV   _DEFAULT_MIN_PART_SIZEr<   r   rl   propertyr   r   rq   r
   r(   r|   r@   r   r   rx   r   r   r   r   r   r   rW   r   r   r-   r-   e  s>       K K 1   0  , , , $ $ X$     #-"6"C & & & && & & &     @ @ @  "0 0 04    
 
 

 
 
 
 
r   r-   )rV   r   rv   rK   smart_open.bytebufferr
   smart_open.constantsazure.storage.blobr   azure.core.exceptionsImportErrorMISSING_DEPS	getLoggerrS   rj   bytes	bytearray
memoryviewr   r   r   r   r   r   r!   r   objectr1   BufferedIOBaser*   r-   rW   r   r   <module>r      s   W V  				                 LLL 
	8	$	$	:. Y	 H%  Q!    DK K K"S S S ',/1c 1c 1c 1ch, , , , , , , ,^|
 |
 |
 |
 |
R |
 |
 |
~K
 K
 K
 K
 K
R K
 K
 K
 K
 K
s   ! ++