
    hG                     6   S r SSKrSSKrSSKrSSKrSSKr SSKrSSK	r\R                  " \5      r\\\4r Sr Sr Sr Sr S rS	 rSS\\\4S
 jrS r " S S\5      r " S S\R:                  5      r " S S\R:                  5      rg! \
 a    Sr Nwf = f)zPImplements file-like objects for reading and writing to/from Azure Blob Storage.    NTazurei   i  @    c                     [         R                  R                  U 5      nUR                  [        :X  d   eUR
                  nUR                  R                  S5      nU(       d  SnUnOUnUn[        [        XES9$ )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         J/home/james-whalen/.local/lib/python3.13/site-packages/smart_open/azure.py	parse_urir   ,   sk    				'	'	6B99IIEWW^^C F vLJJ    c                     [        U 5      n[        R                  R                  [        U5      n[	        US   US   U40 UD6$ )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           	          U(       d  [        S5      eU[        R                  R                  :X  a&  [	        U UUU[        R                  R
                  US9$ U[        R                  R                  :X  a  [        U UUUUS9$ [        SU-  5      e)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, 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 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NotImplementedError)r   r	   r   clientr&   r#   r'   r%   s           r   r   r   C   s    B JKKz##///#&00??+
 	
 
%%22	2#'
 	
 ""Z]a"abbr   c                 
   [        U S5      (       a  U R                  U5      n [        U S5      (       a-  U R                  U:w  a  [        SU R                  < SU< 35      e[        U S5      (       a  U R	                  U5      n U $ )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)hasattrr2   r3   r(   r4   )r0   	containerblobs      r   _get_blob_clientr8   |   s     v-..,,Y7v'((V-B-Bi-O$33Y@
 	

 v())''-Mr   c                   4    \ rS rSrSrS rS rS	S jrS rSr	g)

_RawReader   z Read an Azure Blob Storage file.c                 6    Xl         X l        SU l        X0l        g Nr   )_blob_size	_position_concurrency)selfr7   sizeconcurrencys       r   __init___RawReader.__init__   s    

'r   c                 &    Xl         U R                   $ )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.
r@   )rB   positions     r   seek_RawReader.seek   s     "~~r   c                     U R                   U R                  :  a  gU R                  U5      nU =R                   [        U5      -  sl         U$ )Nr   )r@   r?   _download_blob_chunklen)rB   rC   binarys      r   read_RawReader.read   s<    >>TZZ'**40#f+%r   c                    U R                   U R                  :X  a  gUS:X  a/  U R                  R                  U R                  U R                  S9nO/U R                  R                  U R                  U R                  US9n[
        R                  " SU R                  5        [        U[        R                  R                  R                  5      (       a  UR                  5       nU$ UR                  5       nU$ )Nr   )offsetr%   )rT   r%   lengthz$reading with a max concurrency of %d)r?   r@   r>   download_blobrA   loggingdebug
isinstancer   storager7   StorageStreamDownloaderreadallrP   )rB   rC   streamrO   s       r   rM   _RawReader._download_blob_chunk   s    ::'
 RZZZ--T^^UYUfUf-gFZZ--~~t7H7HQU . WF<d>O>OPfemm00HHII^^%F  [[]Fr   )r>   rA   r@   r?   NrS   )
__name__
__module____qualname____firstlineno____doc__rE   rJ   rP   rM   __static_attributes__ r   r   r:   r:      s    *(r   r:   c                      \ rS rSrSrSr\\R                  R                  \
4S jrS r\S 5       rS rS rS	 r\R                  R$                  4S
 jrS rSS jrSS jrSS jrS rSS jrSS jrSS jrS rS rS rS rSr g)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                    Xl         X l        [        X1U5      U l        U R                  c1  [        R
                  R                  R                  SU< SU< 35      e U R                  R                  5       S   U l	        [        U R                  U R                  U5      U l        SU l        [        R                  R!                  U5      U l        XPl        S U l        g ! [         a
    SU l	         Nrf = f)Nzblob z not found in rC   r   )_container_name
_blob_namer8   r>   r   core
exceptionsResourceNotFoundErrorget_blob_propertiesr?   KeyErrorr:   _raw_readerr@   r
   
bytebuffer
ByteBuffer_current_part_line_terminatorraw)rB   r6   r7   r0   r#   r$   r%   s          r   rE   Reader.__init__   s      ) &f>
::**''==-19= 	779&ADJ &djj$**oN'22==kJ /
   	DJ	s   "C C21C2c                 n    [         R                  S5        U R                  (       d  SU l        SU l        gg)zFlush and close this stream.close: calledN)loggerrX   closedr>   rq   rB   s    r   closeReader.close   s)    _%{{DJ#D r   c                     U R                   S L $ Nr>   r|   s    r   r{   Reader.closed       zzT!!r   c                     g)z+Return True if the stream can be read from.Trf   r|   s    r   readableReader.readable       r   c                     g)zqIf False, seek(), tell() and truncate() will raise IOError.

We offer only seek support, and no truncate support.Trf   r|   s    r   seekableReader.seekable        r   c                 "    [         R                  ezUnsupported.ioUnsupportedOperationr|   s    r   detachReader.detach      %%%r   c                 D   [         R                  SX5        U[        R                  R                  ;  a(  [        SU[        R                  R                  4-  5      eU[        R                  R                  :X  a  UnO=U[        R                  R                  :X  a  U R                  U-   nOU R                  U-   nX0l        U R                  R                  U5        [         R                  SU R                  5        U R                  R                  5         U R                  $ )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)rz   rX   r
   r)   WHENCE_CHOICESr(   WHENCE_STARTWHENCE_CURRENTr@   r?   rq   rJ   rt   empty)rB   rT   whencenew_positions       r   rJ   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                     U R                   $ )z,Return the current position within the file.rH   r|   s    r   tellReader.tell#  s    ~~r   c                 "    [         R                  er   r   rB   rC   s     r   truncateReader.truncate'  r   r   c                    US:X  a  gUS:  a<  U R                   U l        U R                  5       U R                  R	                  5       -   $ [        U R                  5      U:  a  U R                  U5      $ U R                  U R                   :X  a  U R                  5       $ U R                  U5        U R                  U5      $ )z6Read up to size bytes from the object and return them.r   r   )r?   r@   _read_from_bufferrq   rP   rN   rt   _fill_bufferr   s     r   rP   Reader.read+  s    19AX!ZZDN))+d.>.>.C.C.EEE
 t!!"d*))$//>>TZZ'))++$%%d++r   c                      U R                  US9$ )zThis is the same as read().)rC   )rP   r   s     r   read1Reader.read1?  s    yydy##r   c                 v    U R                  [        U5      5      nU(       d  gX!S[        U5      & [        U5      $ )zDRead up to len(b) bytes into b, and return the number of bytes read.r   N)rP   rN   )rB   bdatas      r   readintoReader.readintoC  s1    yyQ *3t94yr   c                 l   US:w  a  [        S5      e[        R                  " 5       nU R                  U R                  :X  a  [        U R                  5      S:X  d  U R                  R                  U R                  5      nUR                  U5        U =R                  [        U5      -  sl        UR                  U R                  5      (       a   UR                  5       $ U R                  5         U R                  U R                  :X  d  M  [        U R                  5      S:X  d  M  UR                  5       $ )zCRead up to and including the next newline.  Returns the bytes read.rS   z(limits other than -1 not implemented yetr   )r/   r   BytesIOr@   r?   rN   rt   readlineru   writeendswithr   getvalue)rB   limitline	line_parts       r   r   Reader.readlineK  s    B;%&PQQ
 zz|>>TZZ/C8J8J4Kq4P**33D4I4IJIJJy!NNc)n,N!!$"7"788 }} !!# >>TZZ/C8J8J4Kq4P }}r   c                     US:  a  UO[        U R                  5      nU R                  R                  U5      nU =R                  [        U5      -  sl        U$ )z:Remove at most size bytes from our buffer and return them.r   )rN   rt   rP   r@   )rB   rC   parts      r   r   Reader._read_from_bufferc  sI     qytc$*<*<&=!!&&t,#d)#r   c                    [        XR                  R                  5      n[        U R                  5      U:  a  U R                  U R
                  :X  dx  U R                  R                  U R                  5      nUS:X  a  [        R                  S5        g[        U R                  5      U:  a  U R                  U R
                  :X  d  Mv  g g g g )Nr   z reached EOF while filling bufferT)
maxrt   _chunk_sizerN   r@   r?   fillrq   rz   rX   )rB   rC   
bytes_reads      r   r   Reader._fill_bufferl  s    4++778$$$%,T^^tzz5Q++001A1ABJQ?@	 $$$%,T^^tzz5Q,5Q,r   c                     U $ r   rf   r|   s    r   	__enter__Reader.__enter__t      r   c                 $    U R                  5         g r   )r}   rB   exc_typeexc_valexc_tbs       r   __exit__Reader.__exit__w  s    

r   c                 p    SU R                   R                  < SU R                  < SU R                  < S3$ N(z, )	__class__r`   rj   rk   r|   s    r   __str__Reader.__str__z  )    NN##  OO
 	
r   c                 n    U R                   R                  < SU R                  < SU R                  < S3$ )N(container=, blob=r   r   r|   s    r   __repr__Reader.__repr__  s)    NN##  OO
 	
r   )	r>   rk   rj   rt   ru   r@   rq   r?   rv   r   r_   )!r`   ra   rb   rc   rd   r>   DEFAULT_BUFFER_SIZEr
   r)   r,   DEFAULT_MAX_CONCURRENCYrE   r}   propertyr{   r   r   r   r   rJ   r   r   rP   r   r   r   r   r   r   r   r   r   re   rf   r   r   r+   r+      s     E ,&00??3 J$ " "& #-"6"6"C"C 2&,($0

r   r+   c                       \ rS rSrSrSrS\4S jrS rS r	S r
\S 5       rS	 rS
 r\R                   R"                  4S jrSS jrS rS rS rS rS rS rS rS rSrg)r.   i  ziWrites bytes to Azure Blob Storage.

Implements the io.BufferedIOBase interface of the standard library.
Nc                     Xl         X l        U=(       d    0 U l        XPl        SU l        SU l        SU l        [        R                  " 5       U l	        / U l
        [        X1U5      U l        S U l        g r=   )rj   rk   _blob_kwargs_min_part_size_total_size_total_parts_bytes_uploadedr   r   rt   _block_listr8   r>   rv   )rB   r6   r7   r0   r&   r'   s         r   rE   Writer.__init__  si      )'-2+ ZZ\ &f>

 r   c                     g r   rf   r|   s    r   flushWriter.flush  s    r   c                     [         R                  SU 5        U R                  (       d  / U l        SU l        [         R                  SU 5        g)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)rz   rX   r{   r   r>   r|   s    r   	terminateWriter.terminate  s8     	7>{{!DDJ6=r   c                    [         R                  S5        U R                  (       d  [         R                  SU 5        U R                  R	                  5       S:  a  U R                  5         U R                  R                  " U R                  40 U R                  D6  / U l        S U l        [         R                  SU 5        g g )Nry   z%s: completing multipart uploadr   z%s: completed multipart upload)
rz   rX   r{   rt   r   _upload_partr>   commit_block_listr   r   r|   s    r   r}   Writer.close  s    _%{{LL:DA!!&&(1,!!#JJ(()9)9OT=N=NO!DDJLL94@ r   c                     U R                   S L $ r   r   r|   s    r   r{   Writer.closed  r   r   c                     g)z+Return True if the stream supports writing.Trf   r|   s    r   writableWriter.writable  r   r   c                     g)zyIf False, seek(), tell() and truncate() will raise IOError.

We offer only tell support, and no seek or truncate support.Trf   r|   s    r   r   Writer.seekable  r   r   c                 "    [         R                  er   r   )rB   rT   r   s      r   rJ   Writer.seek  r   r   c                 "    [         R                  er   r   r   s     r   r   Writer.truncate  r   r   c                     U R                   $ )z#Return the current stream position.)r   r|   s    r   r   Writer.tell  s    r   c                 .    [         R                  " S5      e)Nzdetach() not supportedr   r|   s    r   r   Writer.detach  s    %%&>??r   c                 d   [        U[        5      (       d   [        S[        < S[        U5      < 35      eU R                  R                  U5        U =R                  [        U5      -  sl        U R                  R                  5       U R                  :  a  U R                  5         [        U5      $ )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: )rY   _BINARY_TYPES	TypeErrortypert   r   r   rN   r   r   r   )rB   r   s     r   r   Writer.write  s     !]++-QUVWQXYZZ  #CF"""$(;(;;1vr   c                 X   U R                   S-   nU R                  R                  5       nU R                  U-   S-
  n [	        U5      R                  S5      n[        R                  " UR                  5       5      nU R                  R                  S5        U R                  R                  XPR                  R                  U5      5        U R                  R                  [        R                   R"                  R%                  US95        [&        R)                  SXUS-  5        U =R                   S-  sl         U =R                  U-  sl        [*        R,                  " U R                  R                  5       5      U l        U R                  R                  S[*        R.                  5        g )Nr       r   )block_idz+uploading part #%i, %i bytes (total %.3fGB)g      A)r   rt   r   r   strzfillbase64	b64encodeencoderJ   r>   stage_blockrP   r   appendr   rZ   r7   	BlobBlockrz   infor   r   SEEK_END)rB   part_numcontent_length
range_stopzero_padded_part_numr  s         r   r   Writer._upload_part  sK   $$q(++002))N:Q>
	
  #8}227;##$8$?$?$AB"

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

 	Q.ZZ(:(:(?(?(AB2;;/r   c                     U $ r   rf   r|   s    r   r   Writer.__enter__  r   r   c                 L    Ub  U R                  5         g U R                  5         g r   )r   r}   r   s       r   r   Writer.__exit__  s    NNJJLr   c                 p    SU R                   R                  < SU R                  < SU R                  < S3$ r   r   r|   s    r   r   Writer.__str__  r   r   c                     U R                   R                  < SU R                  < SU R                  < SU R                  < S3$ )Nr   r   z, min_part_size=r   )r   r`   rj   rk   r   r|   s    r   r   Writer.__repr__"  s2    NN##  OO	
 	
r   )r>   r   rk   r   r   rj   rt   r   r   r   rv   r   )r`   ra   rb   rc   rd   r>   _DEFAULT_MIN_PART_SIZErE   r   r   r}   r   r{   r   r   r
   r)   r   rJ   r   r   r   r   r   r   r   r   r   re   rf   r   r   r.   r.     s     E 04
>	A " " #-"6"6"C"C && @"04

r   r.   ) rd   r
  r   rW   smart_open.bytebufferr
   smart_open.constantsazure.storage.blobr   azure.core.exceptionsImportErrorMISSING_DEPS	getLoggerr`   rz   bytes	bytearray
memoryviewr  r   r  r   r   r   r!   r   r8   objectr:   BufferedIOBaser+   r.   rf   r   r   <module>r,     s    W  	     
		8	$	:. Y	 H%  Q!    DK"S ',/6cr*, ,^F
R F
R_
R _
i  Ls   B BB