o
    º Ži²  ã                   @  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mZmZ d dl	m
Z
 zd dlZdZW n ey7   dZY nw G dd„ dƒZdd	d
„ZedkrMeeƒ ƒ‚dS )é    )ÚannotationsN)ÚThreadPoolExecutorÚas_completed)ÚBytesIOTFc                   @  sp   e Zd ZdZdddedfdd„Zedd„ ƒZd	d
„ Zdd„ Z	dd„ Z
edd„ ƒZedd„ ƒZdd„ Zdd„ ZdS )Ú
Compressor)ZjpgZjpegZpngZgifZwebpÚzipÚgzÚtgzÚbz2ÚtbzÚxzÚbrZswfZflvZwoffZwoff2Z3gpZ3gppZasfZaviZm4vZmovZmp4ZmpegZmpgZwebmZwmvNTFc                 C  s<   |d u r| j }|  |¡| _|| _|ot| _|s|| _d S d S ©N)ÚSKIP_COMPRESS_EXTENSIONSÚget_extension_reÚextension_reÚuse_gzipÚbrotli_installedÚ
use_brotliÚlog)ÚselfÚ
extensionsr   r   r   Úquiet© r   ú]C:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\whitenoise/compress.pyÚ__init__7   s   

ÿzCompressor.__init__c              	   C  s0   | st  d¡S t  d d tt j| ƒ¡¡t j¡S )Nz^$z\.({})$ú|)ÚreÚcompileÚformatÚjoinÚmapÚescapeÚ
IGNORECASE)r   r   r   r   r   B   s
   
ÿzCompressor.get_extension_rec                 C  s   | j  |¡ S r   )r   Úsearch)r   Úfilenamer   r   r   Úshould_compressK   s   zCompressor.should_compressc                 C  s   d S r   r   )r   Úmessager   r   r   r   N   s   zCompressor.logc                 C  sÂ   g }t |dƒ�}t | ¡ ¡}| ¡ }W d   ƒ n1 sw   Y  t|ƒ}| jrD|  |¡}|  d|||¡rB| 	|  
||d|¡¡ n|S | jr_|  |¡}|  d|||¡r_| 	|  
||d|¡¡ |S )NÚrbZBrotliz.brZGzipz.gz)ÚopenÚosÚfstatÚfilenoÚreadÚlenr   Úcompress_brotliÚis_compressed_effectivelyÚappendÚ
write_datar   Úcompress_gzip)r   ÚpathÚ	filenamesÚfÚstat_resultÚdataÚsizeÚ
compressedr   r   r   ÚcompressQ   s    
þ

zCompressor.compressc                 C  sR   t ƒ }tjdd|ddd��}| | ¡ W d   ƒ | ¡ S 1 s w   Y  | ¡ S )NÚ Úwbé	   r   )r%   ÚmodeÚfileobjÚcompresslevelÚmtime)r   ÚgzipÚGzipFileÚwriteÚgetvalue)r8   ÚoutputZgz_filer   r   r   r3   d   s   
ÿ
ýüzCompressor.compress_gzipc                 C  s
   t  | ¡S r   )Úbrotlir;   )r8   r   r   r   r/   o   s   
zCompressor.compress_brotlic              
   C  st   t |ƒ}|dkrd}n|| }|dk}|r,|  |› d|› d|d › d|d › d�¡ |S |  d	|› d|› d
�¡ |S )Nr   Fgffffffî?z compressed z (i   zK -> zK)z	Skipping z compression not effective))r.   r   )r   Zencoding_namer4   Z	orig_sizer8   Zcompressed_sizeZis_effectiveZratior   r   r   r0   s   s   "ÿÿz$Compressor.is_compressed_effectivelyc                 C  sT   || }t |dƒ�}| |¡ W d   ƒ n1 sw   Y  t ||j|jf¡ |S )Nr=   )r)   rE   r*   ÚutimeÚst_atimeÚst_mtime)r   r4   r8   Úsuffixr7   r%   r6   r   r   r   r2   ‚   s   ÿzCompressor.write_data)Ú__name__Ú
__module__Ú__qualname__r   Úprintr   Ústaticmethodr   r&   r   r;   r3   r/   r0   r2   r   r   r   r   r      s    
$
ÿ



r   c                 C  s.  t jdd�}|jddddd� |jdd	d
dd� |jddd
dd� |jddd� d tj¡}|jdddd|› d� tjd� | | ¡}t|j|j|j	|j
d�}tƒ �;}g }t |j¡D ]\}}}	|	D ]}
| |
¡rx| | |jtj ||
¡¡¡ qbq[t|ƒD ]}| ¡  q~W d   ƒ dS 1 s�w   Y  dS )Nz¨Search for all files inside <root> *not* matching <extensions> and produce compressed versions with '.gz' and '.br' suffixes (as long as this results in a smaller file))Údescriptionz-qz--quietzDon't produce log outputÚ
store_true)ÚhelpÚactionz	--no-gzipzDon't produce gzip '.gz' filesÚstore_falser   )rT   rU   Údestz--no-brotliz Don't produce brotli '.br' filesr   Úrootz(Path root from which to search for files)rT   z, r   Ú*z,File extensions to exclude from compression z
(default: ú))ÚnargsrT   Údefault)r   r   r   r   r   )ÚargparseÚArgumentParserÚadd_argumentr    r   r   Ú
parse_argsr   r   r   r   r   r*   ÚwalkrX   r&   r1   Úsubmitr;   r4   r   Úresult)ÚargvÚparserZdefault_excludeÚargsZ
compressorÚexecutorÚfuturesÚdirpathÚ_dirsÚfilesr%   Úfuturer   r   r   ÚmainŠ   sl   ÿÿüü
ÿù
	ü
ÿÿ€þ
ÿ
õòrm   Ú__main__r   )Ú
__future__r   r]   rC   r*   r   Úconcurrent.futuresr   r   Úior   rH   r   ÚImportErrorr   rm   rM   Ú
SystemExitr   r   r   r   Ú<module>   s$    ÿ
x;
ÿ