U
    beE                     @   s   d Z ddlZddlZddlZddlZddlZzddlZddl	ZW n e
k
rX   dZY nX eeZeeefZdZdZdZdZdd	 Zd
d ZddeeefddZdd ZG dd deZG dd dejZG dd dejZdS )zPImplements file-like objects for reading and writing to/from Azure Blob Storage.    NTazurei   i  @    c                 C   sP   t j| }|jtkst|j}|jd}|s:d}|}n|}|}t	t||dS )N/z$root)schemecontainer_idblob_id)

smart_openutilsZsafe_urlsplitr   SCHEMEAssertionErrornetlocpathlstripdict)Zuri_as_stringsrfirstsecondr   r    r   R/var/www/html/services/stratfitenv/lib/python3.8/site-packages/smart_open/azure.py	parse_uri,   s    r   c                 C   s.   t | }tjt|}t|d |d |f|S )Nr   r   )r   r   r	   Zcheck_kwargsopen)urimodeZtransport_paramsZ
parsed_urikwargsr   r   r   open_uri=   s    r   c                 C   s^   |st d|tjjkr0t| |||tjj|dS |tjjkrNt| ||||dS td| dS )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 implementedN)	
ValueErrorr   	constantsZREAD_BINARYReaderBINARY_NEWLINEZWRITE_BINARYWriterNotImplementedError)r   r   r   clientr   r   r   r   r   r   r   r   C   s(    !r   c                 C   sR   t | dr| |} t | dr:| j|kr:td| j|f t | drN| |} | 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 %r doesn't match container %rget_blob_client)hasattrr'   r(   r    r)   )r&   	containerblobr   r   r   _get_blob_client|   s    



r-   c                   @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )
_RawReaderz Read an Azure Blob Storage file.c                 C   s   || _ || _d| _|| _d S )Nr   )_blob_size	_position_concurrency)selfr,   sizeZconcurrencyr   r   r   __init__   s    z_RawReader.__init__c                 C   s   || _ | 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.
        r1   )r3   positionr   r   r   seek   s    z_RawReader.seekc                 C   s0   | j | jkrdS | |}|  j t|7  _ |S )N    )r1   r0   _download_blob_chunklen)r3   r4   binaryr   r   r   read   s
    
z_RawReader.readc                 C   sx   | j | jkrdS |dkr.| jj| j| jd}n| jj| j| j|d}td| j t|tj	j
jrl| }n| }|S )Nr:   r9   )offsetr   )r?   r   lengthz$reading with a max concurrency of %d)r0   r1   r/   Zdownload_blobr2   loggingdebug
isinstancer   storager,   ZStorageStreamDownloaderreadallr>   )r3   r4   streamr=   r   r   r   r;      s      
z_RawReader._download_blob_chunkN)r9   )__name__
__module____qualname____doc__r5   r8   r>   r;   r   r   r   r   r.      s
   

r.   c                   @   s   e Zd ZdZeejjefddZ	dd Z
dd Zdd	 Zd
d ZejjfddZdd Zd(ddZd)ddZd*ddZdd Zd+ddZd,ddZd-ddZd d! Zd"d# Zd$d% Zd&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                 C   s   || _ t|||| _| jd kr4tjjd||f z| j d | _W n t	k
rb   d| _Y nX t
| j| j|| _d| _tj|| _|| _d | _d S )Nzblob %s not found in %sr4   r   )_container_namer-   r/   r   core
exceptionsZResourceNotFoundErrorZget_blob_propertiesr0   KeyErrorr.   _raw_readerr1   r   
bytebufferZ
ByteBuffer_current_part_line_terminatorraw)r3   r+   r,   r&   r   r   r   r   r   r   r5      s    	

zReader.__init__c                 C   s   t d d| _d| _dS )zFlush and close this stream.zclose: calledN)loggerrB   r/   rO   r3   r   r   r   close   s    
zReader.closec                 C   s   dS )z+Return True if the stream can be read from.Tr   rU   r   r   r   readable   s    zReader.readablec                 C   s   dS )zyIf False, seek(), tell() and truncate() will raise IOError.

        We offer only seek support, and no truncate support.Tr   rU   r   r   r   seekable   s    zReader.seekablec                 C   s
   t jdS zUnsupported.NioUnsupportedOperationrU   r   r   r   detach   s    zReader.detachc                 C   s   t d|| |tjjkr.td|tjjf |tjjkr@|}n"|tjjkrX| j| }n
| j	| }|| _| 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)rT   rB   r   r!   ZWHENCE_CHOICESr    WHENCE_STARTZWHENCE_CURRENTr1   r0   rO   r8   rQ   empty)r3   r?   whenceZnew_positionr   r   r   r8     s    

zReader.seekc                 C   s   | j S )z,Return the current position within the file.r6   rU   r   r   r   tell  s    zReader.tellNc                 C   s
   t jdS rY   rZ   r3   r4   r   r   r   truncate   s    zReader.truncater9   c                 C   sn   |dkrdS |dk r.| j | _|  | j  S t| j|krF| |S | j| j krZ|  S | | | |S )z6Read up to size bytes from the object and return them.r   r:   )r0   r1   _read_from_bufferrO   r>   r<   rQ   _fill_bufferrb   r   r   r   r>   $  s    

zReader.readc                 C   s   | j |dS )zThis is the same as read().)r4   )r>   rb   r   r   r   read18  s    zReader.read1c                 C   s.   |  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)r>   r<   )r3   bdatar   r   r   readinto<  s
    zReader.readintoc                 C   s   |dkrt dt }| j| jkr2t| jdks| j }| j|krj|	| j}|
| |d  qq|
|   |   q| S )zCRead up to and including the next newline.  Returns the bytes read.r9   z(limits other than -1 not implemented yetr   r   )r%   r[   BytesIOr1   r0   r<   rQ   peekrR   indexwriterd   re   getvalue)r3   limitZthe_lineZremaining_bufferZnext_newliner   r   r   readlineD  s    


zReader.readlinec                 C   s8   |dkr|nt | j}| j|}|  jt |7  _|S )z:Remove at most size bytes from our buffer and return them.r   )r<   rQ   r>   r1   )r3   r4   partr   r   r   rd   ^  s    zReader._read_from_bufferc                 C   sR   t || jj}t| j|k rN| j| jksN| j| j}|dkrt	d dS qd S )Nr   z reached EOF while filling bufferT)
maxrQ   _chunk_sizer<   r1   r0   fillrO   rT   rB   )r3   r4   Z
bytes_readr   r   r   re   g  s    
zReader._fill_bufferc                 C   s   | S Nr   rU   r   r   r   	__enter__o  s    zReader.__enter__c                 C   s   |    d S ru   rV   r3   exc_typeexc_valexc_tbr   r   r   __exit__r  s    zReader.__exit__c                 C   s   d| j j| j| jjf S Nz(%s, %r, %r)	__class__rG   rK   r/   	blob_namerU   r   r   r   __str__u  s    zReader.__str__c                 C   s   d| j j| j| jjf S )Nz%s(container=%r, blob=%r)r~   rU   r   r   r   __repr__z  s
      zReader.__repr__)N)r9   )r9   )r9   )r9   )r9   )rG   rH   rI   rJ   DEFAULT_BUFFER_SIZEr   r!   r#   DEFAULT_MAX_CONCURRENCYr5   rV   rW   rX   r]   r^   r8   ra   rc   r>   rf   ri   rp   rd   re   rv   r|   r   r   r   r   r   r   r"      s,   
$	




	
r"   c                   @   s   e Zd ZdZdefddZdd Zdd Zed	d
 Z	dd Z
dd ZejjfddZd#ddZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$r$   zlWrites bytes to Azure Blob Storage.

    Implements the io.BufferedIOBase interface of the standard library.Nc                 C   sV   d| _ || _t|||| _|p i | _|| _d| _d| _d| _t	
 | _g | _d | _d S )NFr   )
_is_closedrK   r-   r/   _blob_kwargs_min_part_size_total_size_total_parts_bytes_uploadedr[   rj   rQ   _block_listrS   )r3   r+   r,   r&   r   r   r   r   r   r5     s    

zWriter.__init__c                 C   s   d S ru   r   rU   r   r   r   flush  s    zWriter.flushc                 C   sT   t d | jsF| j dkr&|   | jj| jf| j	 g | _d| _
t d d S )Nclosingr   Tzsuccessfully closed)rT   rB   closedrQ   ra   _upload_partr/   Zcommit_block_listr   r   r   rU   r   r   r   rV     s    
zWriter.closec                 C   s   | j S ru   )r   rU   r   r   r   r     s    zWriter.closedc                 C   s   dS )z+Return True if the stream supports writing.Tr   rU   r   r   r   writable  s    zWriter.writablec                 C   s   dS )zIf False, seek(), tell() and truncate() will raise IOError.

        We offer only tell support, and no seek or truncate support.Tr   rU   r   r   r   rX     s    zWriter.seekablec                 C   s
   t jdS rY   rZ   )r3   r?   r`   r   r   r   r8     s    zWriter.seekc                 C   s
   t jdS rY   rZ   rb   r   r   r   rc     s    zWriter.truncatec                 C   s   | j S )z#Return the current stream position.)r   rU   r   r   r   ra     s    zWriter.tellc                 C   s   t dd S )Nzdetach() not supportedrZ   rU   r   r   r   r]     s    zWriter.detachc                 C   s\   t |tstdtt|f | j| |  jt|7  _| j | j	krT| 
  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 %r, got: %r)rC   _BINARY_TYPES	TypeErrortyperQ   rm   r   r<   ra   r   r   )r3   rg   r   r   r   rm     s    
zWriter.writec                 C   s   | j d }| j }| j| d }t|d}t| }| j	d | j
|| j| | jtjjj|d td|||d  |  j d7  _ |  j|7  _t| j | _| j	dtj d S )Nr       r   )block_idz+uploading part #%i, %i bytes (total %.3fGB)g      A)r   rQ   ra   r   strzfillbase64	b64encodeencoder8   r/   Zstage_blockr>   r   appendr   rD   r,   Z	BlobBlockrT   infor[   rj   SEEK_END)r3   Zpart_numcontent_lengthZ
range_stopZzero_padded_part_numr   r   r   r   r     s$    

  zWriter._upload_partc                 C   s   | S ru   r   rU   r   r   r   rv     s    zWriter.__enter__c                 C   s   |    d S ru   rw   rx   r   r   r   r|     s    zWriter.__exit__c                 C   s   d| j j| j| jjf S r}   r~   rU   r   r   r   r     s
    zWriter.__str__c                 C   s   d| j j| j| jj| jf S )Nz+%s(container=%r, blob=%r, min_part_size=%r))r   rG   rK   r/   r   r   rU   r   r   r   r   	  s    zWriter.__repr__)N)rG   rH   rI   rJ   _DEFAULT_MIN_PART_SIZEr5   r   rV   propertyr   r   rX   r   r!   r^   r8   rc   ra   r]   rm   r   rv   r|   r   r   r   r   r   r   r$     s(   	



r$   ) rJ   r   r[   rA   Zsmart_open.bytebufferr   Zsmart_open.constantsZazure.storage.blobr   Zazure.core.exceptionsImportErrorZMISSING_DEPS	getLoggerrG   rT   bytes	bytearray
memoryviewr   r
   r   r   r   r   r   r   r-   objectr.   BufferedIOBaser"   r$   r   r   r   r   <module>	   s:   




9/ A