U
    a                     @   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k
rL   dZY nX 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
f ee 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)ZjpgZjpegZpngZgifZwebpzipgztgzbz2tbzxzbrZswfZflvZwoffZwoff2NTFc                 C   s8   |d kr| j }| || _|| _|o&t| _|s4|| _d S N)SKIP_COMPRESS_EXTENSIONSget_extension_reextension_reuse_gzipbrotli_installed
use_brotlilog)self
extensionsr   r   r   quiet r   /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/whitenoise/compress.py__init__)   s    
zCompressor.__init__c              	   C   s4   | st dS t ddtt j| t jS d 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 5 Q R X t|}| jrl| |}| d|||rh| 	||d|V  nd S | j
r| |}| d|||r| 	||d|V  d S )NrbZBrotliz.brZ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   s8   t  }tjdd|ddd}||  W 5 Q R X | S )N wb	   r   )r"   modefileobjcompresslevelmtime)r   gzipGzipFilewritegetvalue)r3   outputZgz_filer   r   r   r/   T   s        zCompressor.compress_gzipc                 C   s
   t | S r   )brotlir6   )r3   r   r   r   r,   _   s    zCompressor.compress_brotlic              	   C   s`   t |}|dkrd}n|| }|dk}|rJ| d|||d |d  n| d|| |S )Nr   Fgffffff?z!{0} compressed {1} ({2}K -> {3}K)i   z,Skipping {0} ({1} compression not effective))r+   r   r   )r   Zencoding_namer0   Z	orig_sizer3   Zcompressed_sizeZis_effectiveZratior   r   r   r-   c   s*        z$Compressor.is_compressed_effectivelyc              	   C   s@   || }t |d}|| W 5 Q R X t||j|jf |S )Nr8   )r&   r@   r'   utimest_atimest_mtime)r   r0   r3   suffixr2   r"   r1   r   r   r   r.   x   s
    zCompressor.write_data)__name__
__module____qualname__r   printr   staticmethodr   r#   r   r6   r/   r,   r-   r.   r   r   r   r   r      s$       




r   c           	      K   sV   t f |}t| D ]<\}}}|D ],}||r"tj||}||D ]}qHq"qd S r   )r   r'   walkr#   r0   r   r6   )	rootkwargsZ
compressordirpathdirsfilesr"   r0   r5   r   r   r   main   s    

rS   __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   )rW   rX   destz--no-brotliz Don't produce brotli '.br' filesr   rN   z(Path root from which to search for files)rW   r   *z9File extensions to exclude from compression (default: {})z, )nargsrW   default)r>   r'   r   ior   rC   r   ImportErrorobjectr   rS   rH   argparseArgumentParserparseradd_argumentr   r   r   
parse_argsargsvarsr   r   r   r   <module>   sX   
q
   
