
    \jU                     \   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 ej                  e      ZeeefZ	 dZ	 dZ	 dZ	 dZ	 d Zd	 Zddeeefd
Zd Z G d de      Z G d dej:                        Z G d dej:                        Z G d dej:                        Z y# e
$ r dZY w xY w)zPImplements file-like objects for reading and writing to/from Azure Blob Storage.    NTazurei   i  @    c                     t         j                  j                  |       }|j                  t        k(  sJ |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         :/root/env/lib/python3.12/site-packages/smart_open/azure.py	parse_urir   ,   sm    				'	'	6B99IIEWW^^C F vL'JJ    c                     t        |       }t        j                  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!   =   sB    3J**41ABF
>*Jy,A4R6RRr   c                 x   |st        d      |t        j                  j                  k(  r)t	        | |||t        j                  j
                  |      S |t        j                  j                  k(  rt        | ||||      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", "wb", or "ab".
    client: azure.storage.blob.BlobServiceClient, ContainerClient, or BlobClient
        The Azure Blob Storage client to use when working with azure-storage-blob.
    blob_kwargs: dict, optional
        Additional parameters to pass to `BlobClient.commit_block_list` (for "wb")
        or `BlobClient.upload_blob` (for "ab"). For writing only.
    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)blob_kwargsmin_part_sizez6Azure Blob Storage support for mode %r not implemented)
ValueErrorr
   	constantsREAD_BINARYReaderBINARY_NEWLINEWRITE_BINARYWriterAPPEND_BINARYAppendWriterNotImplementedError)r   r	   r   clientr&   r#   r'   r%   s           r   r   r   C   s    B JKKz##///#&00??+
 	
 
%%22	2#'
 	
 
%%33	3#'
 	
 ""Z]a"abbr   c                     t        | d      r| j                  |      } t        | d      r*| j                  |k7  rt        d| j                  d|      t        | d      r| j	                  |      } | S )z
    Return an Azure BlobClient starting with any of BlobServiceClient,
    ContainerClient, or BlobClient plus container name and blob name.
    get_container_clientcontainer_namezClient for z doesn't match container get_blob_client)hasattrr4   r5   r(   r6   )r2   	containerblobs      r   _get_blob_clientr:      sw     v-.,,Y7v'(V-B-Bi-O$33Y@
 	

 v()''-Mr   c                   *    e Zd ZdZd Zd ZddZd Zy)
_RawReaderz Read an Azure Blob Storage file.c                 <    || _         || _        d| _        || _        y Nr   )_blob_size	_position_concurrency)selfr9   sizeconcurrencys       r   __init__z_RawReader.__init__   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.
        rA   )rC   positions     r   seekz_RawReader.seek   s     "~~r   c                     | j                   | j                  k\  ry| j                  |      }| xj                   t        |      z  c_         |S )Nr   )rA   r@   _download_blob_chunklen)rC   rD   binarys      r   readz_RawReader.read   s<    >>TZZ'**40#f+%r   c                    | j                   | j                  k(  ry|dk(  r2| j                  j                  | j                  | j                        }n2| j                  j                  | j                  | j                  |      }t        j                  d| j                         t        |t        j                  j                  j                        r|j                         }|S |j                         }|S )Nr   )offsetr%   )rR   r%   lengthz$reading with a max concurrency of %d)r@   rA   r?   download_blobrB   loggingdebug
isinstancer   storager9   StorageStreamDownloaderreadallrO   )rC   rD   streamrN   s       r   rL   z_RawReader._download_blob_chunk   s    ::'
 RZZZ--T^^UYUfUf-gFZZ--~~t7H7HQU . WF<d>O>OPfemm00HHI^^%F  [[]Fr   NrQ   )__name__
__module____qualname____doc__rF   rJ   rO   rL    r   r   r<   r<      s    *(r   r<   c                       e Zd ZdZdZeej                  j                  e	fdZ
d Ze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y)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.
    Nc                    || _         || _        t        |||      | _        | j                  /t        j
                  j                  j                  d|d|      	 | j                  j                         d   | _	        t        | j                  | j                  |      | _        d| _        t        j                  j!                  |      | _        || _        y # t        $ r
 d| _	        Y kw xY w)Nzblob z not found in rD   r   )_container_name
_blob_namer:   r?   r   core
exceptionsResourceNotFoundErrorget_blob_propertiesr@   KeyErrorr<   _raw_readerrA   r
   
bytebuffer
ByteBuffer_current_part_line_terminator)rC   r8   r9   r2   r#   r$   r%   s          r   rF   zReader.__init__   s      ) &fi>
::**''==-19= 	779&ADJ &djj$**oN'22==kJ /  	DJ	s   "C C+*C+c                 d    t         j                  d       | j                  sd| _        d| _        yy)zFlush and close this stream.close: calledN)loggerrV   closedr?   rk   rC   s    r   closezReader.close   s)    _%{{DJ#D r   c                     | j                   d u S Nr?   rt   s    r   rs   zReader.closed       zzT!!r   c                      y)z+Return True if the stream can be read from.Tra   rt   s    r   readablezReader.readable       r   c                      y)zyIf False, seek(), tell() and truncate() will raise IOError.

        We offer only seek support, and no truncate support.Tra   rt   s    r   seekablezReader.seekable        r   c                 "    t         j                  zUnsupported.ioUnsupportedOperationrt   s    r   detachzReader.detach	      %%%r   c                 @   t         j                  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                  j                  |       t         j                  d| j                         | 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)rr   rV   r
   r)   WHENCE_CHOICESr(   WHENCE_STARTWHENCE_CURRENTrA   r@   rk   rJ   rn   empty)rC   rR   whencenew_positions       r   rJ   zReader.seek  s     	7H--<<<DGQG[G[GjGjHl l m m Z))666!Lz++:::>>F2L::.L%l+&7  "~~r   c                     | j                   S )z,Return the current position within the file.rH   rt   s    r   tellzReader.tell&  s    ~~r   c                 "    t         j                  r   r   rC   rD   s     r   truncatezReader.truncate*  r   r   c                 x   |dk(  ry|dk  r<| j                   | _        | j                         | j                  j	                         z   S t        | j                        |k\  r| j                  |      S | j                  | j                   k(  r| j                         S | j                  |       | j                  |      S )z6Read up to size bytes from the object and return them.r   r   )r@   rA   _read_from_bufferrk   rO   rM   rn   _fill_bufferr   s     r   rO   zReader.read.  s    19AX!ZZDN))+d.>.>.C.C.EEE
 t!!"d*))$//>>TZZ'))++$%%d++r   c                 &    | j                  |      S )zThis is the same as read().)rD   )rO   r   s     r   read1zReader.read1B  s    yydy##r   c                 n    | j                  t        |            }|sy||dt        |       t        |      S )zDRead up to len(b) bytes into b, and return the number of bytes read.r   N)rO   rM   )rC   bdatas      r   readintozReader.readintoF  s3    yyQ *3t94yr   c                 R   |dk7  rt        d      t        j                         }| j                  | j                  k(  rt        | j                        dk(  s| j                  j                  | j                        }|j                  |       | xj                  t        |      z  c_        |j                  | j                        r	 |j                         S | j                          | j                  | j                  k(  st        | j                        dk(  s|j                         S )zCRead up to and including the next newline.  Returns the bytes read.rQ   z(limits other than -1 not implemented yetr   )r1   r   BytesIOrA   r@   rM   rn   readlinero   writeendswithr   getvalue)rC   limitline	line_parts       r   r   zReader.readlineN  s    B;%&PQQ
 zz|>>TZZ/C8J8J4Kq4P**33D4I4IJIJJy!NNc)n,N!!$"7"78 }} !!# >>TZZ/C8J8J4Kq4P }}r   c                     |dk\  r|nt        | j                        }| j                  j                  |      }| xj                  t        |      z  c_        |S )z:Remove at most size bytes from our buffer and return them.r   )rM   rn   rO   rA   )rC   rD   parts      r   r   zReader._read_from_bufferf  sI     qytc$*<*<&=!!&&t,#d)#r   c                    t        || j                  j                        }t        | j                        |k  r| j                  | j
                  k(  st| j                  j                  | j                        }|dk(  rt        j                  d       yt        | j                        |k  r| j                  | j
                  k(  sry y y y )Nr   z reached EOF while filling bufferT)
maxrn   _chunk_sizerM   rA   r@   fillrk   rr   rV   )rC   rD   
bytes_reads      r   r   zReader._fill_buffero  s    4++778$$$%,T^^tzz5Q++001A1ABJQ?@	 $$$%,T^^tzz5Q,5Q,r   c                     | S rw   ra   rt   s    r   	__enter__zReader.__enter__w      r   c                 $    | j                          y rw   )ru   rC   exc_typeexc_valexc_tbs       r   __exit__zReader.__exit__z  s    

r   c                 j    d| j                   j                  d| j                  d| j                  dS N(z, )	__class__r]   rd   re   rt   s    r   __str__zReader.__str__}  )    NN##  OO
 	
r   c                 h    | j                   j                  d| j                  d| j                  dS N(container=, blob=r   r   rt   s    r   __repr__zReader.__repr__  )    NN##  OO
 	
r   rw   r\   )r]   r^   r_   r`   r?   DEFAULT_BUFFER_SIZEr
   r)   r,   DEFAULT_MAX_CONCURRENCYrF   ru   propertyrs   r{   r~   r   r   rJ   r   r   rO   r   r   r   r   r   r   r   r   r   ra   r   r   r+   r+      s     E ,&00??30@$ " "& #-"6"6"C"C 2&,($0

r   r+   c                       e Zd ZdZdZdefdZ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y)r.   zqWrites bytes to Azure Blob Storage.

    Implements the io.BufferedIOBase interface of the standard library.
    Nc                     || _         || _        |xs i | _        || _        d| _        d| _        d| _        t        j                         | _	        g | _
        t        |||      | _        y r>   )rd   re   _blob_kwargs_min_part_size_total_size_total_parts_bytes_uploadedr   r   rn   _block_listr:   r?   rC   r8   r9   r2   r&   r'   s         r   rF   zWriter.__init__  sh      )'-2+ ZZ\ &fi>
r   c                      y rw   ra   rt   s    r   flushzWriter.flush      r   c                     t         j                  d|        | j                  sg | _        d| _        t         j                  d|        y)zDo not commit block list on abort.

        Uploaded (uncommitted) blocks will be garbage collected after 7 days.

        See also https://stackoverflow.com/a/69673084/5511061.z %s: terminating multipart uploadNz%s: terminated multipart upload)rr   rV   rs   r   r?   rt   s    r   	terminatezWriter.terminate  s8     	7>{{!DDJ6=r   c                 x   t         j                  d       | j                  st         j                  d|        | j                  j	                         dkD  r| j                           | j                  j                  | j                  fi | j                   g | _        d | _        t         j                  d|        y y )Nrq   z%s: completing multipart uploadr   z%s: completed multipart upload)
rr   rV   rs   rn   r   _upload_partr?   commit_block_listr   r   rt   s    r   ru   zWriter.close  s    _%{{LL:DA!!&&(1,!!#(DJJ(()9)9OT=N=NO!DDJLL94@ r   c                     | j                   d u S rw   rx   rt   s    r   rs   zWriter.closed  ry   r   c                      yz+Return True if the stream supports writing.Tra   rt   s    r   writablezWriter.writable  r|   r   c                      yzIf False, seek(), tell() and truncate() will raise IOError.

        We offer only tell support, and no seek or truncate support.Tra   rt   s    r   r~   zWriter.seekable  r   r   c                 "    t         j                  r   r   rC   rR   r   s      r   rJ   zWriter.seek  r   r   c                 "    t         j                  r   r   r   s     r   r   zWriter.truncate  r   r   c                     | j                   S z#Return the current stream position.r   rt   s    r   r   zWriter.tell      r   c                 ,    t        j                  d      Nzdetach() not supportedr   rt   s    r   r   zWriter.detach      %%&>??r   c                 T   t        |t              st        dt        dt        |            | j                  j                  |       | xj                  t        |      z  c_        | j                  j                         | j                  k\  r| j                          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.input must be one of , got: rW   _BINARY_TYPES	TypeErrortypern   r   r   rM   r   r   r   rC   r   s     r   r   zWriter.write  s     !]+-QUVWQXYZZ  #CF"""$(;(;;1vr   c                 ^   | j                   dz   }| j                  j                         }| j                  |z   dz
  }	 t	        |      j                  d      }t        j                  |j                               }| j                  j                  d       | j                  j                  || j                  j                  |             | j                  j                  t        j                   j"                  j%                  |             t&        j)                  d|||dz         | xj                   dz  c_         | xj                  |z  c_        t+        j,                  | j                  j                               | _        | j                  j                  dt*        j.                         y )Nr       r   )block_idz+uploading part #%i, %i bytes (total %.3fGB)g      A)r   rn   r   r   strzfillbase64	b64encodeencoderJ   r?   stage_blockrO   r   appendr   rX   r9   	BlobBlockrr   infor   r   SEEK_END)rC   part_numcontent_length
range_stopzero_padded_part_numr   s         r   r   zWriter._upload_part  sO   $$q(++002))N:Q>
	
  #8}227;##$8$?$?$AB"

x););)@)@)PQ 2 2 < <h < OP9nj;&>	

 	Q.ZZ(:(:(?(?(AB2;;/r   c                     | S rw   ra   rt   s    r   r   zWriter.__enter__  r   r   c                 J    || j                          y | j                          y rw   r   ru   r   s       r   r   zWriter.__exit__      NNJJLr   c                 j    d| j                   j                  d| j                  d| j                  dS r   r   rt   s    r   r   zWriter.__str__  r   r   c                     | j                   j                  d| j                  d| j                  d| j                  dS )Nr   r   z, min_part_size=r   )r   r]   rd   re   r   rt   s    r   r   zWriter.__repr__   s2    NN##  OO	
 	
r   rw   r]   r^   r_   r`   r?   _DEFAULT_MIN_PART_SIZErF   r   r   ru   r   rs   r   r~   r
   r)   r   rJ   r   r   r   r   r   r   r   r   r   ra   r   r   r.   r.     s     E 0?*
>	A " " #-"6"6"C"C && @"04

r   r.   c                       e Zd ZdZdZdefdZ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y)r0   zlAppend bytes to Azure Blob Storage.

    Implements the io.BufferedIOBase interface of the standard library.Nc                     || _         || _        |xs i | _        || _        d| _        t        j                         | _        t        |||      | _	        y r>   )
rd   re   r   r   r   r   r   rn   r:   r?   r   s         r   rF   zAppendWriter.__init__/  sP      )'-2+ZZ\ &fi>
r   c                      y rw   ra   rt   s    r   r   zAppendWriter.flushA  r   r   c                 ^    | j                   s!t        j                         | _        d| _        yy)z3AppendBlob cannot be aborted, so we do nothing hereN)rs   r   r   rn   r?   rt   s    r   r   zAppendWriter.terminateD  s#    {{!#DDJ r   c                     | j                   s5| j                  j                         dkD  r| j                          d| _        yy)zCNo action needed here, as the AppendBlob is automatically committedr   N)rs   rn   r   r   r?   rt   s    r   ru   zAppendWriter.closeJ  s8    {{!!&&(1,!!#DJ r   c                     | j                   d u S rw   rx   rt   s    r   rs   zAppendWriter.closedQ  ry   r   c                      yr   ra   rt   s    r   r   zAppendWriter.writableU  r|   r   c                      yr   ra   rt   s    r   r~   zAppendWriter.seekableY  r   r   c                 "    t         j                  r   r   r   s      r   rJ   zAppendWriter.seek_  r   r   c                 "    t         j                  r   r   r   s     r   r   zAppendWriter.truncatec  r   r   c                     | j                   S r   r   rt   s    r   r   zAppendWriter.tellg  r   r   c                 ,    t        j                  d      r   r   rt   s    r   r   zAppendWriter.detachk  r   r   c                 T   t        |t              st        dt        dt        |            | j                  j                  |       | xj                  t        |      z  c_        | j                  j                         | j                  k\  r| j                          t        |      S )Nr   r   r   r   s     r   r   zAppendWriter.writen  s    !]+6CT!WM  	  #CF"""$(;(;;1vr   c                    | j                   j                         } | j                  j                  d|t        j
                  j                  j                  j                  dd| j                   t        j                         | _         y )NF)r   	blob_type	overwritera   )rn   r   r?   upload_blobr   rX   r9   BlobType
APPENDBLOBr   r   r   )rC   r   s     r   r   zAppendWriter._upload_party  sn    !!**,

 	
mm((11<<	
 		
  ZZ\r   c                     | S rw   ra   rt   s    r   r   zAppendWriter.__enter__  r   r   c                 J    || j                          y | j                          y rw   r  r   s       r   r   zAppendWriter.__exit__  r  r   c                 j    d| j                   j                  d| j                  d| j                  dS r   r   rt   s    r   r   zAppendWriter.__str__  r   r   c                 h    | j                   j                  d| j                  d| j                  dS r   r   rt   s    r   r   zAppendWriter.__repr__  r   r   rw   r  ra   r   r   r0   r0   )  s    K E ,?$ " " #-"6"6"C"C && @	*

r   r0   )!r`   r   r   rU   smart_open.bytebufferr
   smart_open.constantsazure.core.exceptionsr   azure.storage.blobImportErrorMISSING_DEPS	getLoggerr]   rr   bytes	bytearray
memoryviewr   r   r  r   r   r   r!   r   r:   objectr<   BufferedIOBaser+   r.   r0   ra   r   r   <module>r+     s    W  	     
		8	$	:. Y	 H%  Q!    DK"S ',/>cB*, ,^A
R A
HZ
R Z
zo
2$$ o
i  Ls   B! !B+*B+