o
    ea                     @  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 zd dlZdZ	W n e
y/   dZ	Y nw G dd dZddd	Zed
krEee dS )    )annotationsN)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)jpgjpegpnggifwebpzipgztgzbz2tbzxzbrswfflvwoffwoff23gp3gppasfavim4vmovmp4mpegmpgwebm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+   F/var/www/html/venv/lib/python3.10/site-packages/whitenoise/compress.py__init__6   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"   A   s
   
zCompressor.get_extension_rec                 C  s   | j | S r    )r#   search)r(   filenamer+   r+   r,   should_compressJ   s   zCompressor.should_compressc                 C  s   d S r    r+   )r(   messager+   r+   r,   r'   M   s   zCompressor.logc                 c  s    t |d}t| }| }W d    n1 sw   Y  t|}| jrA| |}| d|||r?| 	||d|V  nd S | j
r\| |}| d|||r^| 	||d|V  d S d S d S )NrbBrotliz.brGzipz.gz)openosfstatfilenoreadlenr&   compress_brotliis_compressed_effectively
write_datar$   compress_gzip)r(   pathfstat_resultdatasize
compressedr+   r+   r,   compressP   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   )r7   modefileobjcompresslevelmtime)r   gzipGzipFilewritegetvalue)rJ   outputgz_filer+   r+   r,   rF   a   s   

zCompressor.compress_gzipc                 C  s
   t | S r    )brotlirM   )rJ   r+   r+   r,   rC   l   s   
zCompressor.compress_brotlic              	   C  sh   t |}|dkrd}n|| }|dk}|r&| d|||d |d  |S | d| d| d |S )	Nr   Fgffffff?z{} compressed {} ({}K -> {}K)i   z	Skipping z (z compression not effective))rB   r'   r1   )r(   encoding_namerG   	orig_sizerJ   compressed_sizeis_effectiveratior+   r+   r,   rD   p   s   z$Compressor.is_compressed_effectivelyc                 C  sT   || }t |d}|| W d    n1 sw   Y  t||j|jf |S )NrO   )r=   rW   r>   utimest_atimest_mtime)r(   rG   rJ   suffixrI   r7   rH   r+   r+   r,   rE      s   zCompressor.write_data)__name__
__module____qualname__r!   printr-   staticmethodr"   r8   r'   rM   rF   rC   rD   rE   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|jD ]\}}}|D ]}||rrtj||}	||	D ]}
qoq\qUdS )NzSearch 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$   )rl   rm   destz--no-brotliz Don't produce brotli '.br' filesr&   rootz(Path root from which to search for files)rl   z, r)   *z,File extensions to exclude from compression z
(default: ))nargsrl   default)r)   r$   r&   r*   r   )argparseArgumentParseradd_argumentr2   r   r!   
parse_argsr)   r$   r&   r*   r>   walkrp   r8   rG   rM   )argvparserdefault_excludeargs
compressordirpath_dirsfilesr7   rG   _compressedr+   r+   r,   main   sX   

	
r   __main__r    )
__future__r   ru   rU   r>   r/   ior   r[   r%   ImportErrorr   r   re   
SystemExitr+   r+   r+   r,   <module>   s"    
x3
