U
    Q+|jœQ  ã                   @   sò   d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ G d
d„ deƒZG dd„ dƒZG dd„ deƒZG dd„ deeƒZG dd„ deƒZeƒ ZdS )é    N)ÚunquoteÚ	urldefragÚurlsplitÚ
urlunsplit)ÚSTATICFILES_STORAGE_ALIASÚsettings)Úcheck_settingsÚmatches_patterns)ÚImproperlyConfigured)ÚContentFile)ÚFileSystemStorageÚstorages)Úmd5)Ú
LazyObjectc                       s.   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡  ZS )ÚStaticFilesStoragez˜
    Standard file system storage for static files.

    The defaults for ``location`` and ``base_url`` are
    ``STATIC_ROOT`` and ``STATIC_URL``.
    Nc                    sN   |d krt j}|d krt j}t|ƒ tƒ j||f|ž|Ž |sJd | _d | _d S ©N)r   ZSTATIC_ROOTÚ
STATIC_URLr   ÚsuperÚ__init__Zbase_locationÚlocation)Úselfr   Úbase_urlÚargsÚkwargs©Ú	__class__© újc:\users\snmko\desktop\web_content\django_cbt\venv\Lib\site-packages\django/contrib/staticfiles/storage.pyr      s    zStaticFilesStorage.__init__c                    s   | j stdƒ‚tƒ  |¡S )NzaYou're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.)r   r
   r   Úpath©r   Únamer   r   r   r   %   s
    ÿzStaticFilesStorage.path)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   r   r   r      s   r   c                       sœ   e Zd ZdZdZdZdZdZdZ‡ fdd„Z	d d
d„Z
d!dd„Zd"‡ fdd„	Zd#dd„Zd$dd„Zd%dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )&ÚHashedFilesMixinzurl("%(url)s")é   F)ú*.js))zU(?P<matched>import(?s:(?P<import>[\s\{].*?))\s*from\s*['"](?P<url>[\.\/].*?)["']\s*;)z import%(import)s from "%(url)s";)zV(?P<matched>export(?s:(?P<exports>[\s\{].*?))\s*from\s*["'](?P<url>[\.\/].*?)["']\s*;)z!export%(exports)s from "%(url)s";)z4(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*;)zimport"%(url)s";)z+(?P<matched>import\(["'](?P<url>.*?)["']\))zimport("%(url)s")))z*.css)z5(?P<matched>url\(['"]{0,1}\s*(?P<url>.*?)["']{0,1}\)))z.(?P<matched>@import\s*["']\s*(?P<url>.*?)["'])z@import url("%(url)s"))zI(?m)(?P<matched>)^(/\*#[ \t](?-i:sourceMappingURL)=(?P<url>.*)[ \t]*\*/)$z/*# sourceMappingURL=%(url)s */)r(   ))z;(?m)(?P<matched>)^(//# (?-i:sourceMappingURL)=(?P<url>.*))$z//# sourceMappingURL=%(url)sTc                    s”   | j r|  j| jf7  _tƒ j||Ž i | _i | _| jD ]V\}}|D ]H}t|tt	fƒr`|\}}n| j
}t |tj¡}| j |g ¡ ||f¡ qDq8d S r   )Ú$support_js_module_import_aggregationÚpatternsÚ&_js_module_import_aggregation_patternsr   r   Ú	_patternsÚhashed_filesÚ
isinstanceÚtupleÚlistÚdefault_templateÚreÚcompileÚ
IGNORECASEÚ
setdefaultÚappend)r   r   r   Ú	extensionr*   ÚpatternÚtemplateZcompiledr   r   r   r   l   s    
zHashedFilesMixin.__init__Nc                 C   s>   |dkrdS t dd�}| ¡ D ]}| |¡ q| ¡ dd… S )zU
        Return a hash of the file with the given name and optional content.
        NF)Zusedforsecurityé   )r   ÚchunksÚupdateÚ	hexdigest)r   r    ÚcontentZhasherÚchunkr   r   r   Ú	file_hash{   s    
zHashedFilesMixin.file_hashc                 C   s"  t t|ƒƒ}|j ¡ }|r,t t|ƒƒj ¡ p.|}|d k}|r~|  |¡sVtd|| f ƒ‚z|  |¡}W n tk
r|   | Y S X z|  	||¡}W 5 |rœ| ¡  X t
j |¡\}}t
j |¡\}	}
|rÊd| nd}t
j |d|	||
f ¡}t|ƒ}||d< d|k�r|d �s|d  d7  < t|ƒS )	Nz)The file '%s' could not be found with %r.z.%sÚ z%s%s%sé   ú?#é   ú?)r   r   r   ÚstripÚexistsÚ
ValueErrorÚopenÚOSErrorÚcloser@   ÚosÚsplitÚsplitextÚjoinr0   r   )r   r    r>   ÚfilenameÚparsed_nameÚ
clean_nameZopenedr@   r   ÚrootÚextÚhashed_nameÚunparsed_namer   r   r   rU   †   s4    


ÿ

zHashedFilesMixin.hashed_namec                    sÀ   t jr|s|d }}nBt|ƒ\}}t|ƒj d¡r8|}n |f}|dk	rP||f7 }||Ž }tƒ  |¡}	d|k}
|st|
r¸tt|	ƒƒ}|r”|d s”||d< |
r°|d s°|d  d7  < t	|ƒ}	t
|	ƒS )	ú:
        Return the non-hashed URL in DEBUG mode.
        rA   ú/NrC   é   rD   rB   rE   )r   ÚDEBUGr   r   r   Úendswithr   Úurlr0   r   r   )r   Zhashed_name_funcr    Úforcer-   rU   ÚfragmentrR   r   Z	final_urlZquery_fragmentÚurlpartsr   r   r   Ú_url¨   s&    

zHashedFilesMixin._urlc                 C   s   |   | j||¡S )rW   )r`   Ústored_name)r   r    r]   r   r   r   r\   Ç   s    zHashedFilesMixin.urlc                    s$   ˆdkrˆj ‰‡ ‡‡‡fdd„}|S )zJ
        Return the custom URL converter for the given file name.
        Nc           
         s(  |   ¡ }|d }|d }t d|¡r(|S | d¡rB| tj¡sB|S t|ƒ\}}|sV|S | d¡r„| tj¡spt‚|ttjƒd… }n.t	j
dkr’ˆnˆ t	j
d¡}t t |¡|¡}ˆjˆjt|ƒdˆ d�}d | d¡dd… | d¡dd…  ¡}	|�r|	d	|k�r
d	nd
| 7 }	t|	ƒ|d< ˆ| S )zÚ
            Convert the matched URL to a normalized and hashed URL.

            This requires figuring out which files the matched URL resolves
            to and calling the url() method of the storage.
            Úmatchedr\   z^[a-z]+:rX   NT)r]   r-   éÿÿÿÿrC   ú#)Ú	groupdictr2   ÚmatchÚ
startswithr   r   r   ÚAssertionErrorÚlenrL   ÚsepÚreplaceÚ	posixpathrO   Údirnamer`   Ú_stored_namer   rM   )
ZmatchobjÚmatchesrb   r\   Zurl_pathr^   Ztarget_nameZsource_nameZ
hashed_urlZtransformed_url©r-   r    r   r9   r   r   Ú	converterÔ   s8    
ü"ÿz1HashedFilesMixin.url_converter.<locals>.converter)r1   )r   r    r-   r9   rq   r   rp   r   Úurl_converterÍ   s    8zHashedFilesMixin.url_converterc                 +   s  |rdS i }‡fdd„ˆ D ƒ}i }ˆ  ˆ ||¡D ]:\}}}	}
||ksNt|	tƒr\|||	fV  q0|||	f||< q0‡ fdd„|D ƒ‰ d}tˆjƒD ]B}d}ˆ  ˆ ||¡D ]"\}}}	}|||	f||< |pÂ|}q¢|sŒ qÐqŒ|räddtdƒfV  ˆj |¡ | ¡ E dH  dS )	a  
        Post process the given dictionary of files (called from collectstatic).

        Processing is actually two separate operations:

        1. renaming files to include a hash of their content for cache-busting,
           and copying those files to the target storage.
        2. adjusting files which contain references to other files so they
           refer to the cache-busting filenames.

        If either of these are performed on a file, then that file is considered
        post-processed.
        Nc                    s   g | ]}t |ˆ jƒr|‘qS r   )r	   r,   ©Ú.0r   ©r   r   r   Ú
<listcomp>$  s     z1HashedFilesMixin.post_process.<locals>.<listcomp>c                    s   i | ]}|ˆ | “qS r   r   rs   )Úpathsr   r   Ú
<dictcomp>5  s      z1HashedFilesMixin.post_process.<locals>.<dictcomp>FZAllz!Max post-process passes exceeded.)	Ú_post_processr.   Ú	ExceptionÚrangeÚmax_post_process_passesÚRuntimeErrorr-   r<   Úvalues)r   rw   Údry_runÚoptionsr-   Úadjustable_pathsZprocessed_adjustable_pathsr    rU   Ú	processedÚ_ÚsubstitutionsÚiZsubstr   )rw   r   r   Úpost_process  s@    
ÿ  ÿ  ÿ
zHashedFilesMixin.post_processc                 c   sð  dd„ }t ||dd�D �]Ò}d}|| \}}| |¡��¬}	|  |¡}
|  |
¡}||krd|  ||	¡}n|| }t|	dƒr€|	 d¡ |  |¡}d}||k�r¤|}|	 ¡  	d¡}| j
 ¡ D ]t\}}t||fƒr´|D ]Z\}}|  |||¡}z| ||¡}W qÌ tk
�r$ } z|d |dfV  W 5 d }~X Y qÌX qÌq´|�r:|  |¡ t| ¡ ƒ}| j�rZ|  ||¡ |  ||¡}|  |¡�r||  |¡ |  ||¡}|  |¡}||k�r d}d}|�sÊ|�sÊd}|  ||	¡}|  |¡}|||< ||||fV  W 5 Q R X qd S )	Nc                 S   s   t |  tj¡ƒS r   )ri   rM   rL   rj   )r    r   r   r   Ú
path_levelO  s    z2HashedFilesMixin._post_process.<locals>.path_levelT)ÚkeyÚreverseÚseekr   Fzutf-8)ÚsortedrI   rR   Úhash_keyrU   ÚhasattrrŠ   rG   ÚreadÚdecoder,   Úitemsr	   rr   ÚsubrH   Údeleter   ÚencodeÚkeep_intermediate_filesÚ_save)r   rw   r�   r-   r‡   r    r„   Zstorager   Zoriginal_fileÚcleaned_namerŒ   rU   Zhashed_file_existsr‚   Zold_hashed_namer>   r7   r*   r8   r9   rq   ÚexcÚcontent_fileZ
saved_namer   r   r   ry   M  sb    





  ÿ$




zHashedFilesMixin._post_processc                 C   s   |  dd¡S )Nú\rX   )rk   r   r   r   r   rR   —  s    zHashedFilesMixin.clean_namec                 C   s   |S r   r   r   r   r   r   rŒ   š  s    zHashedFilesMixin.hash_keyc                 C   sD   t  |¡}|  |¡}|  |¡}| |¡}|d kr@|  |  |¡¡}|S r   )rl   ÚnormpathrR   rŒ   ÚgetrU   )r   r    r-   r–   rŒ   Ú
cache_namer   r   r   rn   �  s    



zHashedFilesMixin._stored_namec                 C   sˆ   |   |¡}|  |¡}| j |¡}|r(|S |}t| jd ƒD ]8}|   | j|d |d�¡}||krn|| j|< |  S |}q:td|| f ƒ‚d S )Né   )r>   rP   z*The name '%s' could not be hashed with %r.)rR   rŒ   r-   r›   r{   r|   rU   rH   )r   r    r–   rŒ   rœ   Zintermediate_namer…   r   r   r   ra   ©  s    

ÿ
zHashedFilesMixin.stored_name)N)NN)FN)F)N)F)r!   r"   r#   r1   r|   r)   r+   r*   r”   r   r@   rU   r`   r\   rr   r†   ry   rR   rŒ   rn   ra   r%   r   r   r   r   r&   /   s$   

"

A
?Jr&   c                       s^   e Zd ZdZdZdZdZddœ‡ fdd„
Zd	d
„ Zdd„ Z	‡ fdd„Z
dd„ Zdd„ Z‡  ZS )ÚManifestFilesMixinú1.1zstaticfiles.jsonTFN)Úmanifest_storagec                   s4   t ƒ j||Ž |d kr| }|| _|  ¡ \| _| _d S r   )r   r   r    Úload_manifestr-   Úmanifest_hash)r   r    r   r   r   r   r   r   Ç  s
    zManifestFilesMixin.__init__c              
   C   sP   z4| j  | j¡�}| ¡  ¡ W  5 Q R £ W S Q R X W n tk
rJ   Y d S X d S r   )r    rI   Úmanifest_namerŽ   r�   ÚFileNotFoundError)r   Úmanifestr   r   r   Úread_manifestÎ  s
    $z ManifestFilesMixin.read_manifestc                 C   s€   |   ¡ }|d kri dfS zt |¡}W n tjk
r<   Y n,X | d¡}|dkrh| di ¡| dd¡fS td| j| jf ƒ‚d S )NrA   Úversion)z1.0rŸ   rw   Úhashz(Couldn't load manifest '%s' (version %s))r¦   ÚjsonÚloadsÚJSONDecodeErrorr›   rH   r£   Úmanifest_version)r   r>   Zstoredr§   r   r   r   r¡   Õ  s    

ÿÿz ManifestFilesMixin.load_manifestc                 /   s0   i | _ tƒ j||ŽE d H  | d¡s,|  ¡  d S )Nr   )r-   r   r†   r›   Úsave_manifest)r   r   r   r   r   r   r†   æ  s    
zManifestFilesMixin.post_processc              	   C   sz   |   d tt t| j ¡ ƒ¡ ¡ ƒ¡| _| j| j	| jdœ}| j
 | j¡rT| j
 | j¡ t |¡ ¡ }| j
 | jt|ƒ¡ d S )N)rw   r§   r¨   )r@   r   r©   Údumpsr‹   r-   r�   r“   r¢   r¬   r    rG   r£   r’   r•   )r   ÚpayloadÚcontentsr   r   r   r­   ì  s     ÿýz ManifestFilesMixin.save_manifestc                 C   sŽ   t t|ƒƒ}|j ¡ }|  |¡}| j |¡}|d krV| jrFtd| ƒ‚|  	|  
|¡¡}t|ƒ}||d< d|kr†|d s†|d  d7  < t|ƒS )Nz+Missing staticfiles manifest entry for '%s'rB   rC   rD   rE   )r   r   r   rF   rŒ   r-   r›   Úmanifest_strictrH   rR   rU   r0   r   )r   r    rQ   rR   rŒ   rœ   rV   r   r   r   ra   ú  s    

ÿzManifestFilesMixin.stored_name)r!   r"   r#   r¬   r£   r±   r”   r   r¦   r¡   r†   r­   ra   r%   r   r   r   r   rž   Á  s   rž   c                   @   s   e Zd ZdZdS )ÚManifestStaticFilesStoragezh
    A static file system storage backend which also saves
    hashed copies of the files it saves.
    N)r!   r"   r#   r$   r   r   r   r   r²     s   r²   c                   @   s   e Zd Zdd„ ZdS )ÚConfiguredStoragec                 C   s   t t | _d S r   )r   r   Z_wrappedru   r   r   r   Ú_setup  s    zConfiguredStorage._setupN)r!   r"   r#   r´   r   r   r   r   r³     s   r³   ) r©   rL   rl   r2   Úurllib.parser   r   r   r   Zdjango.confr   r   Z django.contrib.staticfiles.utilsr   r	   Zdjango.core.exceptionsr
   Zdjango.core.files.baser   Zdjango.core.files.storager   r   Zdjango.utils.cryptor   Zdjango.utils.functionalr   r   r&   rž   r²   r³   Zstaticfiles_storager   r   r   r   Ú<module>   s(      M	