o
    \"<f                     @   s  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eZ	dd Z
ed	krd dlZejd
dZejddddd ejddddd ejddddd ejddd ejdddde	je	jd  e Ze
d!i ee dS dS )"    N)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woff2NTFc                 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   J/var/www/html/kck/venv/lib/python3.10/site-packages/whitenoise/compress.py__init__)   s   

zCompressor.__init__c              	   C   s0   | st dS t ddtt j| t jS )Nz^$z\.({0})$|)recompileformatjoinmapescape
IGNORECASE)r   r   r   r    r   4   s
   
zCompressor.get_extension_rec                 C   s   | j | S r   )r   search)r   filenamer   r   r    should_compress=   s   zCompressor.should_compressc                 C   s   d S r   r   )r   messager   r   r    r   @   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    compressC   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)r>   outputgz_filer   r   r    r:   T   s   

zCompressor.compress_gzipc                 C   s
   t | S r   )brotlirA   )r>   r   r   r    r7   _   s   
zCompressor.compress_brotlic              	   C   sb   t |}|dkrd}n|| }|dk}|r&| d|||d |d  |S | d|| |S )Nr   Fgffffff?z!{0} compressed {1} ({2}K -> {3}K)i   z,Skipping {0} ({1} compression not effective))r6   r   r%   )r   encoding_namer;   	orig_sizer>   compressed_sizeis_effectiveratior   r   r    r8   c   s$   z$Compressor.is_compressed_effectivelyc                 C   sT   || }t |d}|| W d    n1 sw   Y  t||j|jf |S )NrC   )r1   rK   r2   utimest_atimest_mtime)r   r;   r>   suffixr=   r+   r<   r   r   r    r9   x   s   zCompressor.write_data)__name__
__module____qualname__r   printr!   staticmethodr   r,   r   rA   r:   r7   r8   r9   r   r   r   r    r      s    





r   c           	      K   sZ   t di |}t| D ]\}}}|D ]}||r)tj||}||D ]}q&qqd S )Nr   )r   r2   walkr,   r;   r&   rA   )	rootkwargs
compressordirpathdirsfilesr+   r;   r@   r   r   r    main   s   
re   __main__zSearch 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   )ri   rj   destz--no-brotliz Don't produce brotli '.br' filesr   r_   z(Path root from which to search for files)ri   r   *z9File extensions to exclude from compression (default: {})z, )nargsri   defaultr   )rI   r2   r#   ior   rO   r   ImportErrorobjectr   re   rY   argparseArgumentParserparseradd_argumentr%   r&   r   
parse_argsargsvarsr   r   r   r    <module>   sV    q
