o
    RDi                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ dd	lmZmZ e
eZdddZG dd dZdS )    )annotationsN)BytesIO)	getLogger)IO)default_backend)hashes   )UTF8kilobytereturnintc                 C  s   t j| |ddS )Ni  )mode)osopen)pathflags r   V/var/www/Datamplify/venv/lib/python3.10/site-packages/snowflake/connector/file_util.pyowner_rw_opener   s   r   c                   @  s`   e Zd ZedddZedd	d
ZedddZedddZedddZedddZ	dS )SnowflakeFileUtilsrc	IO[bytes]r   tuple[str, int]c                 C  sr   dt  }t }t }t||}	 | |}|dkrn|| qt|	 
t}|  }| d ||fS )zGets stream digest and size.

        Args:
            src: The input stream.

        Returns:
            Tuple of src's digest and src's size in bytes.
        @   T    r   )r
   r   r   SHA256Hashreadupdatebase64standard_b64encodefinalizedecoder	   tellseek)r   
CHUNK_SIZEbackendchosen_hashhasherchunkdigestsizer   r   r   get_digest_and_size   s   



z%SnowflakeFileUtil.get_digest_and_size
src_streamtuple[IO[bytes], int]c                 C  s(   t |  }| d t|t|fS )zCompresses a stream of bytes with GZIP.

        Args:
            src_stream: bytes stream

        Returns:
            A tuple of byte stream and size.
        r   )gzipcompressr   r$   r   len)r-   compressed_datar   r   r   compress_with_gzip_from_stream3   s   

z0SnowflakeFileUtil.compress_with_gzip_from_stream	file_namestrtmp_dirc              	   C  s   t j| }t j||d }td||  t| d(}t|d}t	j
||dt d W d   n1 s7w   Y  W d   n1 sFw   Y  t| t |}||jfS )zCompresses a file with GZIP.

        Args:
            file_name: Local path to file to be compressed.
            tmp_dir: Temporary directory where an GZIP file will be created.

        Returns:
            A tuple of gzip file name and size.
        z_c.gzz gzip file: %s, original file: %srbwbr   )lengthN)r   r   basenamejoinloggerdebugr   r/   GzipFileshutilcopyfileobjr
   r   normalize_gzip_headerstatst_size)r4   r6   	base_namegzip_file_namefrfwstatinfor   r   r   compress_file_with_gzipA   s   


z)SnowflakeFileUtil.compress_file_with_gziprE   Nonec                 C  s<  t | d}|dd |d}td|d }|dd |tdd |d@ r||d	d |d@ rJ|d
}td|d }|d|  |d}|rtd|d }|dkr^n.|dd |tdd |d}|sQW d   dS W d   dS W d   dS W d   dS 1 sw   Y  dS )a  Normalizes GZIP file header.

        For consistent file digest, this removes creation timestamp and file name from the header.
        For more information see http://www.zlib.org/rfc-gzip.html#file-format

        Args:
            gzip_file_name: Local path of gzip file.
        zr+b   r   r   B   z<L   
      z<H       N)r   r$   r   structunpackwritepack)rE   f	flag_byter   
xlen_bytesxlenbytevaluer   r   r   rA   W   s8   




	"z'SnowflakeFileUtil.normalize_gzip_headerc                 C  s$   t | \}}td|| ||fS )zGets stream digest and size.

        Args:
            src_stream: The input source stream.

        Returns:
            Tuple of src_stream's digest and src_stream's size in bytes.
        z*getting digest and size for stream: %s, %s)r   r,   r<   r=   )r-   r*   r+   r   r   r   get_digest_and_size_for_stream{   s   
z0SnowflakeFileUtil.get_digest_and_size_for_streamc                 C  sX   d\}}t | d}t|\}}W d   n1 sw   Y  td|||  ||fS )zGets file digest and size.

        Args:
            file_name: Local path to a file.

        Returns:
            Tuple of file's digest and file size in bytes.
        )NNr7   Nz(getting digest and size: %s, %s, file=%s)r   r   r,   r<   r=   )r4   r*   r+   r   r   r   r   get_digest_and_size_for_file   s   
z.SnowflakeFileUtil.get_digest_and_size_for_fileN)r   r   r   r   )r-   r   r   r.   )r4   r5   r6   r5   r   r   )rE   r5   r   rJ   )r-   r   r   r   )r4   r5   r   r   )
__name__
__module____qualname__staticmethodr,   r3   rI   rA   r^   r_   r   r   r   r   r      s    #r   )r   r   )
__future__r   r   r/   r   r?   rT   ior   loggingr   typingr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   	constantsr	   r
   r`   r<   r   r   r   r   r   r   <module>   s    
