
    ɯei                        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
Z G d d      Zy)    )annotationsN)BytesIO)	getLogger)IO)default_backend)hashes   )UTF8kilobytec                2    t        j                  | |d      S )Ni  )mode)osopen)pathflagss     _/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/connector/file_util.pyowner_rw_openerr      s    774U++    c                  x    e Zd Zedd       Zed	d       Zed
d       Zedd       Zedd       Zedd       Z	y)SnowflakeFileUtilc                   dt         z  }t               }t        j                         }t        j                  ||      }	 | j                  |      }|dk(  rn|j                  |       )t        j                  |j                               j                  t              }| j                         }| j                  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.
        @   r   r   )r   r   r   SHA256Hashreadupdatebase64standard_b64encodefinalizedecoder
   tellseek)src
CHUNK_SIZEbackendchosen_hashhasherchunkdigestsizes           r   get_digest_and_sizez%SnowflakeFileUtil.get_digest_and_size   s     (]
!#mmo['2HHZ(E|MM% 	  **6??+<=DDTJxxzt|r   c                    t        j                  | j                               }| j                  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)
src_streamcompressed_datas     r   compress_with_gzip_from_streamz0SnowflakeFileUtil.compress_with_gzip_from_stream3   s:     --
(9:'_)===r   c                   t         j                  j                  |       }t         j                  j                  ||dz         }t        j                  d||        t        | d      5 }t        j                  |d      5 }t        j                  ||dt        z         ddd       ddd       t        j                  |       t        j                  |      }||j                  fS # 1 sw Y   IxY w# 1 sw Y   MxY w)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)	file_nametmp_dir	base_namegzip_file_namefrfwstatinfos          r   compress_file_with_gzipz)SnowflakeFileUtil.compress_file_with_gzipA   s     GG$$Y/	gy7/BC7S)T" 	Ab~t4 A""2r"x-@A	A 	//?77>*x////A A	A 	As$   &C1= C%C1%C.	*C11C:c                   t        | d      5 }|j                  dd       |j                  d      }t        j                  d|      d   }|j                  dd       |j                  t        j                  dd             |dz  r|j                  d	d       |dz  r>|j                  d
      }t        j                  d|      d   }|j                  d|z          |j                  d      }|rjt        j                  d|      d   }|dk(  rnK|j                  dd       |j                  t        j                  dd             |j                  d      }|rjddd       y# 1 sw Y   yxY w)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)rD   f	flag_byter   
xlen_bytesxlenbytevalues           r   r>   z'SnowflakeFileUtil.normalize_gzip_headerW   s*    .%( 	%AFF1aLq	IMM#y1!4EFF1aLGGFKKa()qyr119!"J!==z:1=DFF6D=)vvay"MM#t4Q7EzFF2qMGGFKKT2366!9D #	% 	% 	%s   E
E!!E*c                h    t         j                  |       \  }}t        j                  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+   r9   r:   )r0   r)   r*   s      r   get_digest_and_size_for_streamz0SnowflakeFileUtil.get_digest_and_size_for_stream{   s3     )<<ZHA64Pt|r   c                    d\  }}t        | d      5 }t        j                  |      \  }}ddd       t        j	                  d|||        ||fS # 1 sw Y   %xY w)zGets file digest and size.

        Args:
            file_name: Local path to a file.

        Returns:
            Tuple of file's digest and file size in bytes.
        )NNr4   Nz(getting digest and size: %s, %s, file=%s)r   r   r+   r9   r:   )rA   r)   r*   r#   s       r   get_digest_and_size_for_filez.SnowflakeFileUtil.get_digest_and_size_for_file   sg     ")T" 	Fc,@@ELFD	F6i	
 t|	F 	Fs   AAN)r#   	IO[bytes]returntuple[str, int])r0   ra   rb   ztuple[IO[bytes], int])rA   strrB   rd   rb   rc   )rD   rd   rb   None)r0   ra   rb   rc   )rA   rd   rb   rc   )
__name__
__module____qualname__staticmethodr+   r2   rH   r>   r^   r`    r   r   r   r      sy     2 > > 0 0* !% !%F    r   r   )rb   int)
__future__r   r   r-   r   r<   rS   ior   loggingr   typingr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   	constantsr
   r   rf   r9   r   r   rj   r   r   <module>rs      sD    "   	      8 1 %	8	,A Ar   