U
    a[                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ ddl	m
Z
 G dd deZG dd	 d	eeZG d
d deZG dd deZG dd deeZdS )    N)settings)ManifestStaticFilesStorageStaticFilesStorage   
Compressorc                       s:   e Zd ZdZ fddZdddZdd Zd	d
 Z  ZS )CompressedStaticFilesMixinzF
    Wraps a StaticFilesStorage instance to compress output files
    c                    s6   t tt| d| j}|||}|ds2| |}|S )Npost_processdry_run)getattrsuperr   fallback_post_processgetpost_process_with_compression)selfargskwargsZsuper_post_processfiles	__class__ /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/whitenoise/storage.pyr	      s    


z'CompressedStaticFilesMixin.post_processFc                 k   s   |s|D ]}|d dfV  qd S )NFr   )r   pathsr
   optionspathr   r   r   r   !   s    z0CompressedStaticFilesMixin.fallback_post_processc                 K   s
   t f |S Nr   r   r   r   r   r   create_compressor&   s    z,CompressedStaticFilesMixin.create_compressorc                 c   s   t tdd }| j|dd}|D ]\}}}|||fV  t|tr@qttd ||g}|D ]P}||rV| |}t	|t	| }	|
|D ]}
|
|	d  }||dfV  qqVqd S NZ#WHITENOISE_SKIP_COMPRESS_EXTENSIONST)
extensionsquiet)r   r   r   
isinstance	Exceptionsetfiltershould_compressr   lencompress)r   r   r   
compressornamehashed_name	processedZunique_namesr   
prefix_lencompressed_pathcompressed_namer   r   r   r   )   s    


z8CompressedStaticFilesMixin.post_process_with_compression)F)	__name__
__module____qualname____doc__r	   r   r   r   __classcell__r   r   r   r   r      s
   
r   c                   @   s   e Zd ZdS )CompressedStaticFilesStorageNr/   r0   r1   r   r   r   r   r4   :   s   r4   c                       s<   e Zd ZdZedZedZ	 fddZ
dd Z  ZS )HelpfulExceptionMixina]  
    If a CSS file contains references to images, fonts etc that can't be found
    then Django's `post_process` blows up with a not particularly helpful
    ValueError that leads people to think WhiteNoise is broken.

    Here we attempt to intercept such errors and reformat them to be more
    helpful in revealing the source of the problem.
    z#^The file '(.+)' could not be founda          {orig_message}

        The {ext} file '{filename}' references a file which could not be found:
          {missing}

        Please check the URL references in this {ext} file, particularly any
        relative paths which might be pointing to the wrong location.
        c                 /   sH   t t| j||}|D ],\}}}t|tr6| ||}|||fV  qd S r   )r   r6   r	   r!   r"   make_helpful_exception)r   r   r   r   r)   r*   r+   r   r   r   r	   V   s
    
z"HelpfulExceptionMixin.post_processc                 C   sr   t |trnt|jr|jd nd}| j|}|rntj|d 	d
 }| jj|||d|d}t|}|S )Nr    r   .)Zorig_messagefilenamemissingext)r!   
ValueErrorr&   r   ERROR_MSG_REsearchosr   splitextlstripupper	ERROR_MSGformatgroupMissingFileError)r   	exceptionr)   messagematch	extensionr   r   r   r7   ]   s    
z,HelpfulExceptionMixin.make_helpful_exception)r/   r0   r1   r2   recompiler>   textwrapdedentrD   r	   r7   r3   r   r   r   r   r6   >   s   	
r6   c                   @   s   e Zd ZdS )rG   Nr5   r   r   r   r   rG   n   s   rG   c                       sx   e Zd ZdZdZ fddZ fddZdd Z fd	d
Zdd Z	dd Z
edd Zdd Zdd Zdd Z  ZS )$CompressedManifestStaticFilesStoragez
    Extends ManifestStaticFilesStorage instance to create compressed versions
    of its output files and, optionally, to delete the non-hashed files (i.e.
    those without the hash in their name)
    Nc                    s,   t tdd }|d k	r|| _t j|| d S )NZWHITENOISE_MANIFEST_STRICT)r   r   manifest_strictr   __init__)r   r   r   rQ   r   r   r   rR   }   s    z-CompressedManifestStaticFilesStorage.__init__c                    s*   t t| j||}|ds&| |}|S )Nr
   )r   rP   r	   r   r   )r   r   r   r   r   r   r   r	      s    
 

z1CompressedManifestStaticFilesStorage.post_processc                 c   s   i }t  }| | |D ]2\}}}|r>t|ts>||| |< |||fV  q|   t | }t | }| jr||B | }	|}
nt  }	||B }
| 	|	 | 
|
D ]\}}||dfV  qd S )NT)r#   start_tracking_new_filesr!   r"   
clean_namestop_tracking_new_fileskeysvalueskeep_only_hashed_filesdelete_filescompress_files)r   r   Zhashed_names	new_filesr)   r*   r+   Zoriginal_filesZhashed_filesfiles_to_deleteZfiles_to_compressr.   r   r   r   r      s$    

zBCompressedManifestStaticFilesStorage.post_process_with_compressionc                    s2   t t| j||}| jd k	r.| j| | |S r   )r   rP   r*   
_new_filesaddrT   )r   r   r   r)   r   r   r   r*      s    
 
z0CompressedManifestStaticFilesStorage.hashed_namec                 C   s
   || _ d S r   r]   )r   r[   r   r   r   rS      s    z=CompressedManifestStaticFilesStorage.start_tracking_new_filesc                 C   s
   d | _ d S r   r_   r   r   r   r   rU      s    z<CompressedManifestStaticFilesStorage.stop_tracking_new_filesc                 C   s   t tddS )NZ!WHITENOISE_KEEP_ONLY_HASHED_FILESF)r   r   r`   r   r   r   rX      s    z;CompressedManifestStaticFilesStorage.keep_only_hashed_filesc                 C   sT   |D ]J}zt | | W q tk
rL } z|jtjkr< W 5 d }~X Y qX qd S r   )r@   unlinkr   OSErrorerrnoENOENT)r   r\   r)   er   r   r   rY      s    z1CompressedManifestStaticFilesStorage.delete_filesc                 K   s
   t f |S r   r   r   r   r   r   r      s    z6CompressedManifestStaticFilesStorage.create_compressorc           	      c   sr   t tdd }| j|dd}|D ]N}||r| |}t|t| }||D ]}||d  }||fV  qPqd S r   )r   r   r   r%   r   r&   r'   )	r   namesr   r(   r)   r   r,   r-   r.   r   r   r   rZ      s    

z3CompressedManifestStaticFilesStorage.compress_files)r/   r0   r1   r2   r]   rR   r	   r   r*   rS   rU   propertyrX   rY   r   rZ   r3   r   r   r   r   rP   r   s   
rP   )rc   r@   rL   rN   Zdjango.confr   Z"django.contrib.staticfiles.storager   r   r'   r   objectr   r4   r6   r=   rG   rP   r   r   r   r   <module>   s   +0
 