o
    i-                     @   s  U d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZ ejdkr7d dlmZ nd dlmZ eeedd	f Zzzd dlZW n eyY   d dlZY nw d
ZW n eyh   dZY nw zejdkrvd dlmZ nd dlmZ d
ZW n ey   dZY nw dZdZd ZdZ G dd deZ!G dd deZ"G dd deZ#G dd deddZ$G dd dZ%e%eZ&e%e'd< de#ddfdd Z(		d/d!ee) d"e*de+fd#d$Z,G d%d& d&eZ-G d'd( d(Z.G d)d* d*e-Z/G d+d, d,e-Z0G d-d. d.e-Z1dS )0    N)ABCabstractmethod)Executor)AnyFinalOptionalProtocol	TypedDictcast)      )Buffer)Unionzmemoryview[int]zmemoryview[bytes]TF)r      )ZstdDecompressori   i   c                   @   s2   e Zd ZdedefddZd
dedefddZd	S )ZLibCompressObjProtocoldatareturnc                 C      d S N selfr   r   r   S/home/kim/smarthome/.venv/lib/python3.10/site-packages/aiohttp/compression_utils.pycompress-       z ZLibCompressObjProtocol.compress.modec                C   r   r   r   r   r   r   r   r   flush.   r   zZLibCompressObjProtocol.flushN.)__name__
__module____qualname__r   bytesr   intr   r   r   r   r   r   ,   s    r   c                   @   sJ   e Zd ZddededefddZddedefdd	Zede	fd
dZ
dS )ZLibDecompressObjProtocol.r   
max_lengthr   c                 C   r   r   r   r   r   r'   r   r   r   
decompress2   r   z$ZLibDecompressObjProtocol.decompresslengthc                C   r   r   r   r   r*   r   r   r   r   3   r   zZLibDecompressObjProtocol.flushc                 C   r   r   r   r   r   r   r   eof5      zZLibDecompressObjProtocol.eofNr    )r!   r"   r#   r   r%   r$   r)   r   propertyboolr-   r   r   r   r   r&   1   s
    r&   c                   @   s   e Zd ZU eed< eed< eed< eed< eed< 						ddeded	ed
ededee defddZ	dd	edede	fddZ
	dded	ededefddZ	dd	edededefddZdS )ZLibBackendProtocol	MAX_WBITSZ_FULL_FLUSHZ_SYNC_FLUSHZ_BEST_SPEEDZ_FINISH.levelmethodwbitsmemLevelstrategyzdictr   c                 C   r   r   r   )r   r7   r8   r9   r:   r;   r<   r   r   r   compressobj@   s   zZLibBackendProtocol.compressobjc                 C   r   r   r   )r   r9   r<   r   r   r   decompressobjI      z!ZLibBackendProtocol.decompressobjr   c                C   r   r   r   )r   r   r7   r9   r   r   r   r   M   r?   zZLibBackendProtocol.compressbufsizec                C   r   r   r   )r   r   r9   r@   r   r   r   r)   P   r?   zZLibBackendProtocol.decompressN)......)..)r!   r"   r#   r%   __annotations__r   r   r   r=   r&   r>   r$   r   r)   r   r   r   r   r1   9   sn   
 



r1   c                   @   s&   e Zd ZU eed< eed< eed< dS )CompressObjArgsr9   r;   r7   N)r!   r"   r#   r%   rA   r   r   r   r   rB   U   s   
 rB   )totalc                   @   s   e Zd ZdefddZedefddZedefddZ	edefd	d
Z
edefddZedefddZedefddZdededefddZdededefddZdedededefddZdedededefddZdedefddZdS ) ZLibBackendWrapper_zlib_backendc                 C   s
   || _ d S r   )rE   )r   rE   r   r   r   __init__\      
zZLibBackendWrapper.__init__r   c                 C   s   t | jddS )Nr!   Z	undefinedgetattrrE   r,   r   r   r   name_   s   zZLibBackendWrapper.namec                 C      | j jS r   )rE   r2   r,   r   r   r   r2   c      zZLibBackendWrapper.MAX_WBITSc                 C   rK   r   )rE   r3   r,   r   r   r   r3   g   rL   zZLibBackendWrapper.Z_FULL_FLUSHc                 C   rK   r   )rE   r4   r,   r   r   r   r4   k   rL   zZLibBackendWrapper.Z_SYNC_FLUSHc                 C   rK   r   )rE   r5   r,   r   r   r   r5   o   rL   zZLibBackendWrapper.Z_BEST_SPEEDc                 C   rK   r   )rE   r6   r,   r   r   r   r6   s   rL   zZLibBackendWrapper.Z_FINISHargskwargsc                 O      | j j|i |S r   )rE   r=   r   rM   rN   r   r   r   r=   w      zZLibBackendWrapper.compressobjc                 O   rO   r   )rE   r>   rP   r   r   r   r>   z   rQ   z ZLibBackendWrapper.decompressobjr   c                 O      | j j|g|R i |S r   )rE   r   r   r   rM   rN   r   r   r   r   }      zZLibBackendWrapper.compressc                 O   rR   r   )rE   r)   rS   r   r   r   r)      rT   zZLibBackendWrapper.decompressattrnamec                 C   s   t | j|S r   rH   )r   rU   r   r   r   __getattr__      zZLibBackendWrapper.__getattr__N)r!   r"   r#   r1   rF   r/   strrJ   r%   r2   r3   r4   r5   r6   r   r   r=   r&   r>   r   r$   r   r)   rV   r   r   r   r   rD   [   s&    rD   ZLibBackendnew_zlib_backendr   c                 C   s
   | t _d S r   )rY   rE   )rZ   r   r   r   set_zlib_backend   rG   r[   encodingsuppress_deflate_headerc                 C   s$   | dkr	dt j S |rt j S t jS )Ngzip   )rY   r2   )r\   r]   r   r   r   encoding_to_mode   s   
r`   c                   @   sd   e Zd Zdefdee dee fddZee	fde
dede
fd	d
Ze	fde
dede
fddZdS )DecompressionBaseHandlerNexecutormax_sync_chunk_sizec                 C   s   || _ || _dS )z&Base class for decompression handlers.N)	_executor_max_sync_chunk_sizer   rb   rc   r   r   r   rF      s   
z!DecompressionBaseHandler.__init__r   r'   r   c                 C      dS Decompress the given data.Nr   r(   r   r   r   decompress_sync   r   z(DecompressionBaseHandler.decompress_syncc                    sD   | j durt|| j krt | j| j||I dH S | ||S rh   )re   lenasyncioZget_event_looprun_in_executorrd   rj   r(   r   r   r   r)      s   

z#DecompressionBaseHandler.decompress)r!   r"   r#   MAX_SYNC_CHUNK_SIZEr   r   r%   rF   r   ZLIB_MAX_LENGTH_UNLIMITEDr$   rj   r)   r   r   r   r   ra      s2    
	ra   c                   @   s   e Zd Zddddddefdee dedee dee dee dee d	ee fd
dZ	de
de
fddZde
de
fddZddee de
fddZdS )ZLibCompressorNFr\   r]   r7   r9   r;   rb   rc   c           	      C   sv   || _ || _|d u rt||n|| _ttj| _i }| j|d< |d ur'||d< |d ur/||d< | jjdi || _d S )Nr9   r;   r7   r   )	rd   re   r`   _moderD   rY   rE   r=   _compressor)	r   r\   r]   r7   r9   r;   rb   rc   rN   r   r   r   rF      s   


zZLibCompressor.__init__r   r   c                 C   s   | j |S r   )rr   r   r   r   r   r   compress_sync   rW   zZLibCompressor.compress_syncc                    sF   | j duot|| j k}|rt | j| jj|I dH S | |S )a  Compress the data and returned the compressed bytes.

        Note that flush() must be called after the last call to compress()

        If the data size is large than the max_sync_chunk_size, the compression
        will be done in the executor. Otherwise, the compression will be done
        in the event loop.

        **WARNING: This method is NOT cancellation-safe when used with flush().**
        If this operation is cancelled, the compressor state may be corrupted.
        The connection MUST be closed after cancellation to avoid data corruption
        in subsequent compress operations.

        For cancellation-safe compression (e.g., WebSocket), the caller MUST wrap
        compress() + flush() + send operations in a shield and lock to ensure atomicity.
        N)	re   rk   rl   Zget_running_looprm   rd   rr   r   rs   )r   r   Zshould_use_executorr   r   r   r      s   


zZLibCompressor.compressr   c                 C   s   | j |dur
|S | jjS )a	  Flush the compressor synchronously.

        **WARNING: This method is NOT cancellation-safe when called after compress().**
        The flush() operation accesses shared compressor state. If compress() was
        cancelled, calling flush() may result in corrupted data. The connection MUST
        be closed after compress() cancellation.

        For cancellation-safe compression (e.g., WebSocket), the caller MUST wrap
        compress() + flush() + send operations in a shield and lock to ensure atomicity.
        N)rr   r   rE   r6   r   r   r   r   r      s
   
zZLibCompressor.flushr   )r!   r"   r#   rn   r   rX   r0   r%   r   rF   r$   rs   r   r   r   r   r   r   rp      s4    
rp   c                
       s   e Zd Zdddefdee dedee dee f fddZ	e
fd	ed
edefddZddedefddZedefddZ  ZS )ZLibDecompressorNFr\   r]   rb   rc   c                    s>   t  j||d t||| _ttj| _| jj| jd| _d S )Nrb   rc   )r9   )	superrF   r`   rq   rD   rY   rE   r>   _decompressor)r   r\   r]   rb   rc   	__class__r   r   rF     s   zZLibDecompressor.__init__r   r'   r   c                 C   s   | j ||S r   )rw   r)   r(   r   r   r   rj     s   z ZLibDecompressor.decompress_syncr   r*   c                 C   s   |dkr
| j |S | j  S )Nr   )rw   r   r+   r   r   r   r     s
   
zZLibDecompressor.flushc                 C   rK   r   )rw   r-   r,   r   r   r   r-     rL   zZLibDecompressor.eof)r   )r!   r"   r#   rn   r   rX   r0   r   r%   rF   ro   r   r$   rj   r   r/   r-   __classcell__r   r   rx   r   rt     s2    
rt   c                       s`   e Zd Zdefdee dee ddf fddZefde	dede
fd	d
Zde
fddZ  ZS )BrotliDecompressorNrb   rc   r   c                    s*   t stdt | _t j||d dS )z)Decompress data using the Brotli library.zIThe brotli decompression is not available. Please install `Brotli` moduleru   N)
HAS_BROTLIRuntimeErrorbrotliZDecompressor_objrv   rF   rf   rx   r   r   rF   $  s   
zBrotliDecompressor.__init__r   r'   c                 C   s4   t | jdrtt| j||S tt| j||S )ri   r)   )hasattrr   r
   r$   r)   processr(   r   r   r   rj   2  s   z"BrotliDecompressor.decompress_syncc                 C   s    t | jdrtt| j S dS )zFlush the decompressor.r       )r   r   r
   r$   r   r,   r   r   r   r   :  s   zBrotliDecompressor.flush)r!   r"   r#   rn   r   r   r%   rF   ro   r   r$   rj   r   rz   r   r   rx   r   r{      s&    
r{   c                       s`   e Zd Zdefdee dee ddf fddZefde	dede	fd	d
Z
de	fddZ  ZS )ZSTDDecompressorNrb   rc   r   c                    s(   t stdt | _t j||d d S )NzOThe zstd decompression is not available. Please install `backports.zstd` moduleru   )HAS_ZSTDr}   r   r   rv   rF   rf   rx   r   r   rF   B  s   zZSTDDecompressor.__init__r   r'   c                 C   s   |t krtn|}| j||S r   )ro   ZSTD_MAX_LENGTH_UNLIMITEDr   r)   )r   r   r'   Zzstd_max_lengthr   r   r   rj   O  s
   z ZSTDDecompressor.decompress_syncc                 C   rg   )Nr   r   r,   r   r   r   r   [  r.   zZSTDDecompressor.flush)r!   r"   r#   rn   r   r   r%   rF   ro   r$   rj   r   rz   r   r   rx   r   r   A  s&    
r   )NF)2rl   syszlibabcr   r   concurrent.futuresr   typingr   r   r   r   r	   r
   version_infocollections.abcr   r   r$   	bytearrayZ
brotlicffir~   ImportErrorr|   Zcompression.zstdr   Zbackports.zstdr   rn   ZDEFAULT_MAX_DECOMPRESS_SIZEro   r   r   r&   r1   rB   rD   rY   rA   r[   rX   r0   r%   r`   ra   rp   rt   r{   r   r   r   r   r   <module>   sj   
  

-

K!