a
    ŠxdÒD  ã                   @   sê   d dl Z 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 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 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)Úsettings)Úcheck_settingsÚmatches_patterns)ÚImproperlyConfigured)ÚContentFile)ÚFileSystemStorageÚget_storage_class)Ú
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                    sT   |d u rt j}|d u rt j}t|ƒ tƒ j||g|¢R i |¤Ž |sPd | _d | _d S ©N)r   ZSTATIC_ROOTÚ
STATIC_URLr   ÚsuperÚ__init__Zbase_locationÚlocation)Úselfr   Úbase_urlÚargsÚkwargs©Ú	__class__© úZ/var/www/html/Ranjet/env/lib/python3.9/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‡ 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("%s")é   ))z*.css)z#(url\(['"]{0,1}\s*(.*?)["']{0,1}\)))z(@import\s*["']\s*(.*?)["'])z@import url("%s")Tc                    s€   t ƒ j|i |¤Ž i | _i | _| jD ]V\}}|D ]H}t|ttfƒrL|\}}n| j}t	 
|t	j¡}| j |g ¡ ||f¡ q0q$d S r   )r   r   Ú	_patternsÚhashed_filesÚpatternsÚ
isinstanceÚtupleÚlistÚdefault_templateÚreÚcompileÚ
IGNORECASEÚ
setdefaultÚappend)r   r   r   Ú	extensionr(   ÚpatternÚtemplateZcompiledr   r   r   r   7   s    
zHashedFilesMixin.__init__Nc                 C   s<   |du rdS t  ¡ }| ¡ D ]}| |¡ q| ¡ dd… S )zU
        Return a hash of the file with the given name and optional content.
        Né   )ÚhashlibÚmd5ÚchunksÚupdateÚ	hexdigest)r   r   Úcontentr7   Úchunkr   r   r   Ú	file_hashD   s    zHashedFilesMixin.file_hashc                 C   s,  t t|ƒƒ}|j ¡ }|r,t t|ƒƒj ¡ p.|}|d u }|r||  |¡sVtd|| f ƒ‚z|  |¡}W n tyz   | Y S 0 z|  ||¡}W |r¨| 	¡  n|r¦| 	¡  0 t
j |¡\}}t
j |¡\}	}
|rÔd| nd}t
j |d|	||
f ¡}t|ƒ}||d< d|v 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ÚOSErrorr=   ÚcloseÚosÚsplitÚsplitextÚjoinr+   r   )r   r   r;   ÚfilenameÚparsed_nameÚ
clean_nameZopenedr=   r   ÚrootÚextÚhashed_nameÚunparsed_namer   r   r   rR   O   s8    



ÿ

ÿzHashedFilesMixin.hashed_nameFc                    sÀ   t jr|s|d }}nBt|ƒ\}}t|ƒj d¡r8|}n |f}|durP||f7 }||Ž }tƒ  |¡}	d|v }
|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.
        r>   ú/Nr@   é   rA   r?   rB   )r   ÚDEBUGr   r   r   Úendswithr   Úurlr+   r   r   )r   Zhashed_name_funcr   Úforcer'   rR   ÚfragmentrO   r   Z	final_urlZquery_fragmentÚurlpartsr   r   r   Ú_urlp   s&    

zHashedFilesMixin._urlc                 C   s   |   | j||¡S )rT   )r]   Ústored_name)r   r   rZ   r   r   r   rY      s    zHashedFilesMixin.urlc                    s$   ˆdu rˆj ‰‡ ‡‡‡fdd„}|S )zJ
        Return the custom URL converter for the given file name.
        Nc           	         s  |   ¡ \}}t d|¡r|S | d¡r6| tj¡s6|S t|ƒ\}}| d¡rp| tj¡s\J ‚|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|v ròdnd| 7 }ˆt|ƒ 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.
            z^[a-z]+:rU   NT)rZ   r'   éÿÿÿÿr@   ú#)Úgroupsr-   ÚmatchÚ
startswithr   r   r   ÚlenrI   ÚsepÚreplaceÚ	posixpathrL   Údirnamer]   Ú_stored_namer   rJ   )	ZmatchobjÚmatchedrY   Zurl_pathr[   Ztarget_nameZsource_nameZ
hashed_urlZtransformed_url©r'   r   r   r4   r   r   Ú	converterœ   s&    

þ*z1HashedFilesMixin.url_converter.<locals>.converter)r,   )r   r   r'   r4   rl   r   rk   r   Úurl_converter•   s    -zHashedFilesMixin.url_converterc                 +   sÈ   |rdS i }‡fdd„ˆ D ƒ}ˆ  ˆ ||¡D ]\}}}}	|||fV  q,‡ fdd„|D ƒ‰ tˆjƒD ]@}
d}ˆ  ˆ ||¡D ] \}}}}|||fV  |p–|}qx|sb q¤qb|r¸ddtdƒfV  ˆj |¡ 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   rn   )Úpathsr   r   Ú
<dictcomp>ê   ó    z1HashedFilesMixin.post_process.<locals>.<dictcomp>FZAllz!Max post-process passes exceeded.)Ú_post_processÚrangeÚmax_post_process_passesÚRuntimeErrorr'   r9   )r   rr   Údry_runÚoptionsr'   Úadjustable_pathsr   rR   Ú	processedÚ_ÚiÚsubstitutionsÚsubstr   )rr   r   r   Úpost_processË   s&    
ÿ
zHashedFilesMixin.post_processc                 c   s  dd„ }t ||ddD ]ê}d}|| \}}| |¡¸}	|  |¡}
|  |
¡}||vrd|  ||	¡}n|| }t|	dƒr€|	 d¡ |  |¡}d}||v r¦|}|	 ¡  	d¡}| j
 ¡ D ]v\}}t||fƒr´|D ]\\}}|  |||¡}z| ||¡}W qÌ ty& } z|d |dfV  W Y d }~qÌd }~0 0 qÌq´|r<|  |¡ t| ¡ ƒ}| jr\|  ||¡ |  ||¡}|  |¡r~|  |¡ |  ||¡}|  |¡}||kr¢d}d}|sÌ|sÌd}|  ||	¡}|  |¡}|||< ||||fV  W d   ƒ q1 sø0    Y  qd S )	Nc                 S   s   t |  tj¡ƒS r   )rd   rJ   rI   re   )r   r   r   r   Ú
path_levelý   s    z2HashedFilesMixin._post_process.<locals>.path_levelT)ÚkeyÚreverseÚseekr   Fzutf-8)ÚsortedrF   rO   Úhash_keyrR   Úhasattrr…   rD   ÚreadÚdecoder&   Úitemsr   rm   ÚsubrE   Údeleter
   ÚencodeÚkeep_intermediate_filesÚ_save)r   rr   r{   r'   r‚   r   r   Zstorager   Zoriginal_fileÚcleaned_namer‡   rR   Zhashed_file_existsr|   Zold_hashed_namer;   r2   r(   r3   r4   rl   ÚexcÚcontent_fileZ
saved_namer   r   r   ru   û   sZ    





(




zHashedFilesMixin._post_processc                 C   s   |  dd¡S )Nú\rU   )rf   r   r   r   r   rO   C  s    zHashedFilesMixin.clean_namec                 C   s   |S r   r   r   r   r   r   r‡   F  s    zHashedFilesMixin.hash_keyc                 C   sD   t  |¡}|  |¡}|  |¡}| |¡}|d u r@|  |  |¡¡}|S r   )rg   ÚnormpathrO   r‡   ÚgetrR   )r   r   r'   r‘   r‡   Ú
cache_namer   r   r   ri   I  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;   rM   z*The name '%s' could not be hashed with %r.)rO   r‡   r'   r–   rv   rw   rR   rE   )r   r   r‘   r‡   r—   Zintermediate_namer~   r   r   r   r^   U  s    

ÿ
zHashedFilesMixin.stored_name)N)NN)FN)F)N)F)r   r    r!   r,   rw   r(   r   r   r=   rR   r]   rY   rm   r   ru   rO   r‡   ri   r^   r#   r   r   r   r   r$   ,   s    

!

6
0Hr$   c                       sX   e Zd ZdZdZdZdZ‡ fdd„Zdd„ Zd	d
„ Z	‡ fdd„Z
dd„ Zdd„ Z‡  ZS )ÚManifestFilesMixinú1.0zstaticfiles.jsonTFc                    s    t ƒ j|i |¤Ž |  ¡ | _d S r   )r   r   Úload_manifestr'   ©r   r   r   r   r   r   r   s  s    zManifestFilesMixin.__init__c                 C   sX   z>|   | j¡}| ¡  ¡ W  d   ƒ W S 1 s20    Y  W n tyR   Y d S 0 d S r   )rF   Úmanifest_namer‰   rŠ   ÚFileNotFoundError)r   Úmanifestr   r   r   Úread_manifestw  s
    0z ManifestFilesMixin.read_manifestc                 C   sn   |   ¡ }|d u ri S zt |¡}W n tjy6   Y n 0 | d¡}|dkrV| di ¡S td| j| jf ƒ‚d S )NÚversionrš   rr   z(Couldn't load manifest '%s' (version %s))r    ÚjsonÚloadsÚJSONDecodeErrorr–   rE   r   Úmanifest_version)r   r;   Zstoredr¡   r   r   r   r›   ~  s    

ÿz ManifestFilesMixin.load_manifestc                 /   s4   i | _ tƒ j|i |¤ŽE d H  | d¡s0|  ¡  d S )Nry   )r'   r   r   r–   Úsave_manifestrœ   r   r   r   r     s    
zManifestFilesMixin.post_processc                 C   sJ   | j | jdœ}|  | j¡r&|  | j¡ t |¡ ¡ }|  | jt	|ƒ¡ d S )N)rr   r¡   )
r'   r¥   rD   r   r   r¢   ÚdumpsrŽ   r   r
   )r   ÚpayloadÚcontentsr   r   r   r¦   “  s
    z ManifestFilesMixin.save_manifestc                 C   sŽ   t t|ƒƒ}|j ¡ }|  |¡}| j |¡}|d u rV| jrFtd| ƒ‚|  	|  
|¡¡}t|ƒ}||d< d|v r†|d s†|d  d7  < t|ƒS )Nz+Missing staticfiles manifest entry for '%s'r?   r@   rA   rB   )r   r   r   rC   r‡   r'   r–   Úmanifest_strictrE   rO   rR   r+   r   )r   r   rN   rO   r‡   r—   rS   r   r   r   r^   š  s    

zManifestFilesMixin.stored_name)r   r    r!   r¥   r   rª   r   r   r    r›   r   r¦   r^   r#   r   r   r   r   r™   m  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jƒƒ | _d S r   )r   r   ZSTATICFILES_STORAGEZ_wrappedrp   r   r   r   Ú_setupµ  s    zConfiguredStorage._setupN)r   r    r!   r­   r   r   r   r   r¬   ´  s   r¬   )r6   r¢   rI   rg   r-   Úurllib.parser   r   r   r   Zdjango.confr   Z django.contrib.staticfiles.utilsr   r   Zdjango.core.exceptionsr	   Zdjango.core.files.baser
   Zdjango.core.files.storager   r   Zdjango.utils.functionalr   r   r$   r™   r«   r¬   Zstaticfiles_storager   r   r   r   Ú<module>   s&     C?