o
    uvXj¿K  ã                   @   sN  d dl Z 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 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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ƒZ%G dd„ de#ƒZ&G dd„ de&e"ƒZ'G dd„ de$e"ƒZ(G dd„ de!ƒZ)e)ƒ Z*dS )é    N)ÚOrderedDict)ÚunquoteÚ	urldefragÚurlsplitÚ
urlunsplit)Úsettings)Úcheck_settingsÚmatches_patterns)ÚInvalidCacheBackendErrorÚcacheÚcaches)ÚImproperlyConfigured)ÚContentFile)ÚFileSystemStorageÚget_storage_class)ÚRemovedInDjango31Warning)Ú
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                    sX   |d u rt j}|d u rt j}t|ƒ tƒ j||g|¢R i |¤Ž |s*d | _d | _d S d S ©N)r   ÚSTATIC_ROOTÚ
STATIC_URLr   ÚsuperÚ__init__Úbase_locationÚlocation)Úselfr   Úbase_urlÚargsÚkwargs©Ú	__class__© ú_/var/www/html/myproject/venv/lib/python3.10/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‡ 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")c                    s‚   t ƒ j|i |¤Ž tƒ | _i | _| jD ]+\}}|D ]$}t|ttfƒr'|\}}n| j	}t
 |t
j¡}| j |g ¡ ||f¡ qqd S r   )r   r   r   Ú	_patternsÚhashed_filesÚpatternsÚ
isinstanceÚtupleÚlistÚdefault_templateÚreÚcompileÚ
IGNORECASEÚ
setdefaultÚappend)r   r   r   Ú	extensionr0   ÚpatternÚtemplateÚcompiledr   r!   r"   r   <   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%   Úcontentr@   Úchunkr!   r!   r"   Ú	file_hashI   s   zHashedFilesMixin.file_hashc                 C   s*  t t|ƒƒ}|j ¡ }|rt t|ƒƒj ¡ p|}|d u }|r>|  |¡s+td|| f ƒ‚z|  |¡}W n ty=   | Y S w z|  ||¡}W |rL| 	¡  n|rT| 	¡  w w t
j |¡\}}t
j |¡\}	}
|d urmd| }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.%sz%s%s%sé   ú?#é   ú?)r   r   r#   ÚstripÚexistsÚ
ValueErrorÚopenÚIOErrorrF   ÚcloseÚosÚsplitÚsplitextÚjoinr3   r   )r   r%   rD   ÚfilenameÚparsed_nameÚ
clean_nameÚopenedrF   r#   ÚrootÚextÚhashed_nameÚunparsed_namer!   r!   r"   r[   T   s@   

þ€ÿ
ÿ
ÿzHashedFilesMixin.hashed_nameFc                    sÀ   t jr|s|d}}n!t|ƒ\}}t|ƒj d¡r|}n|f}|dur(||f7 }||Ž }tƒ  |¡}	d|v }
|s:|
r\tt|	ƒƒ}|rJ|d sJ||d< |
rX|d sX|d  d7  < t	|ƒ}	t
|	ƒS )	ú:
        Return the non-hashed URL in DEBUG mode.
        Ú ú/NrH   é   rI   rG   rJ   )r   ÚDEBUGr   r   r#   Úendswithr   Úurlr3   r   r   )r   Úhashed_name_funcr%   Úforcer/   r[   ÚfragmentrW   r   Ú	final_urlÚquery_fragmentÚurlpartsr   r!   r"   Ú_urlv   s&   

zHashedFilesMixin._urlc                 C   s   |   | j||¡S )r]   )rj   Ústored_name)r   r%   re   r!   r!   r"   rc   •   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¡r| tj¡s|S t|ƒ\}}| d¡r8| 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 ryd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]+:r_   NT)re   r/   éÿÿÿÿrH   ú#)Úgroupsr5   ÚmatchÚ
startswithr   r   r   ÚlenrQ   ÚsepÚreplaceÚ	posixpathrT   Údirnamerj   Ú_stored_namer   rR   )	ÚmatchobjÚmatchedrc   Úurl_pathrf   Útarget_nameÚsource_nameÚ
hashed_urlÚtransformed_url©r/   r%   r   r<   r!   r"   Ú	converter¢   s&   

þ*z1HashedFilesMixin.url_converter.<locals>.converter)r4   )r   r%   r/   r<   r   r!   r~   r"   Úurl_converter›   s   -zHashedFilesMixin.url_converterc                 +   sÌ   � |rdS t ƒ }‡fdd„ˆ D ƒ}ˆ ˆ ||¡D ]\}}}}	|||fV  q‡ fdd„|D ƒ‰ tˆjƒD ] }
d}ˆ ˆ ||¡D ]\}}}}|||fV  |pM|}q>|sS nq3|r^ddtdƒfV  ˆj |¡ dS )	a  
        Post process the given OrderedDict 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!   r�   )Úpathsr!   r"   Ú
<dictcomp>ð   s    z1HashedFilesMixin.post_process.<locals>.<dictcomp>FÚAllz!Max post-process passes exceeded.)r   Ú_post_processÚrangeÚmax_post_process_passesÚRuntimeErrorr/   rB   )r   r…   Údry_runÚoptionsr/   Úadjustable_pathsr%   r[   Ú	processedÚ_ÚiÚsubstitutionsÚsubstr!   )r…   r   r"   Úpost_processÑ   s*   €
ÿ
ÿzHashedFilesMixin.post_processc                 c   sî  � dd„ }t ||dd�D ]è}d}|| \}}| |¡�Ñ}	|  |¡}
|  |
¡}||vr1|  ||	¡}n|| }t|	dƒr?|	 d¡ |  |¡}d}||v rÉ|}|	 ¡  	t
j¡}| j ¡ D ]9\}}t||fƒr’|D ],\}}|  |||¡}z| ||¡}W qe ty‘ } z|d |dfV  W Y d }~qed }~ww qY|rš|  |¡ t| ¡ ƒ}|  ||¡}|  ||¡}|  |¡r¶|  |¡ |  ||¡}|  |¡}||krÇd}d}|sÚ|sÚd}|  ||	¡}|  |¡}|||< ||||fV  W d   ƒ n1 sïw   Y  qd S )Nc                 S   s   t |  tj¡ƒS r   )rq   rR   rQ   rr   )r%   r!   r!   r"   Ú
path_level  ó   z2HashedFilesMixin._post_process.<locals>.path_levelT)ÚkeyÚreverseÚseekr   F)ÚsortedrN   rW   Úhash_keyr[   Úhasattrr™   rL   ÚreadÚdecoder   ÚFILE_CHARSETr.   Úitemsr	   r€   ÚsubrM   Údeleter   ÚencodeÚ_save)r   r…   rŽ   r/   r•   r%   r’   Ústorager#   Úoriginal_fileÚcleaned_namer›   r[   Úhashed_file_existsr�   Úold_hashed_namerD   r:   r0   r;   r<   r   ÚexcÚcontent_fileÚ
saved_namer!   r!   r"   rˆ     sf   €




€ÿ€




Å€ûzHashedFilesMixin._post_processc                 C   s   |  dd¡S )Nú\r_   )rs   r$   r!   r!   r"   rW   H  s   zHashedFilesMixin.clean_namec                 C   s   |S r   r!   r$   r!   r!   r"   r›   K  s   zHashedFilesMixin.hash_keyc                 C   sD   t  |¡}|  |¡}|  |¡}| |¡}|d u r |  |  |¡¡}|S r   )rt   ÚnormpathrW   r›   Úgetr[   )r   r%   r/   r§   r›   Ú
cache_namer!   r!   r"   rv   N  s   



zHashedFilesMixin._stored_namec                 C   s„   |   |¡}|  |¡}| j |¡}|r|S |}t| jd ƒD ]}|   | j|d |d�¡}||kr7|| j|< |  S |}qtd|| f ƒ‚)Né   )rD   rU   z*The name '%s' could not be hashed with %r.)rW   r›   r/   r¯   r‰   rŠ   r[   rM   )r   r%   r§   r›   r°   Úintermediate_namer‘   r!   r!   r"   rk   Z  s   

ÿ
zHashedFilesMixin.stored_namer   r&   )FN)F)r'   r(   r)   r4   rŠ   r0   r   rF   r[   rj   rc   r€   r”   rˆ   rW   r›   rv   rk   r+   r!   r!   r   r"   r,   2   s     

"


60Gr,   c                       sT   e 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.jsonTc                    s    t ƒ j|i |¤Ž |  ¡ | _d S r   )r   r   Úload_manifestr/   ©r   r   r   r   r!   r"   r   w  s   zManifestFilesMixin.__init__c                 C   sV   z |   | j¡�}| ¡  ¡ W  d   ƒ W S 1 sw   Y  W d S  ty*   Y d S w r   )rN   Úmanifest_namer�   rž   rO   )r   Úmanifestr!   r!   r"   Úread_manifest{  s   
(ÿÿz ManifestFilesMixin.read_manifestc                 C   sr   |   ¡ }|d u rtƒ S z	tj|td�}W n
 tjy   Y nw | d¡}|dkr/| dtƒ ¡S td| j| jf ƒ‚)N)Úobject_pairs_hookÚversionr´   r…   z(Couldn't load manifest '%s' (version %s))	r¹   r   ÚjsonÚloadsÚJSONDecodeErrorr¯   rM   r·   Úmanifest_version)r   rD   Ústoredr»   r!   r!   r"   rµ   ‚  s   ÿ

ÿz ManifestFilesMixin.load_manifestc                 /   s.   � t ƒ | _tƒ j|i |¤ŽE d H  |  ¡  d S r   )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)r…   r»   )
r/   r¿   rL   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 r+| jr#td| ƒ‚|  	|  
|¡¡}t|ƒ}||d< d|v rC|d sC|d  d7  < t|ƒS )Nz+Missing staticfiles manifest entry for '%s'rG   rH   rI   rJ   )r   r   r#   rK   r›   r/   r¯   Úmanifest_strictrM   rW   r[   r3   r   )r   r%   rV   rW   r›   r°   r\   r!   r!   r"   rk   �  s   

zManifestFilesMixin.stored_name)r'   r(   r)   r¿   r·   rÅ   r   r¹   rµ   r”   rÁ   rk   r+   r!   r!   r   r"   r³   r  s    r³   c                   @   sB   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dS )Ú_MappingCachezG
    A small dict-like wrapper for a given cache backend instance.
    c                 C   s
   || _ d S r   )r   )r   r   r!   r!   r"   r   ³  s   
z_MappingCache.__init__c                 C   s   | j  ||¡ d S r   )r   Úset©r   r—   Úvaluer!   r!   r"   Ú__setitem__¶  ó   z_MappingCache.__setitem__c                 C   s$   | j  |¡}|d u rtd| ƒ‚|S )NzCouldn't find a file name '%s')r   r¯   ÚKeyErrorrÈ   r!   r!   r"   Ú__getitem__¹  s   z_MappingCache.__getitem__c                 C   s   | j  ¡  d S r   )r   Úclearrƒ   r!   r!   r"   rÎ   ¿  s   z_MappingCache.clearc                 C   s   | j  |¡ d S r   )r   Úset_many)r   Údatar!   r!   r"   rB   Â  r–   z_MappingCache.updateNc                 C   s"   z| | W S  t y   | Y S w r   )rÌ   )r   r—   Údefaultr!   r!   r"   r¯   Å  s
   
ÿz_MappingCache.getr   )
r'   r(   r)   r*   r   rÊ   rÍ   rÎ   rB   r¯   r!   r!   r!   r"   rÆ   ¯  s    rÆ   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚCachedFilesMixinc                    sF   t ƒ j|i |¤Ž z
ttd ƒ| _W d S  ty"   ttƒ| _Y d S w )NÚstaticfiles)r   r   rÆ   r   r/   r
   Údefault_cacher¶   r   r!   r"   r   Í  s   þzCachedFilesMixin.__init__c                 C   s    t  |  |¡ ¡ ¡ ¡ }d| S )Nzstaticfiles:%s)r?   r@   rW   r£   rC   )r   r%   r—   r!   r!   r"   r›   Õ  s   zCachedFilesMixin.hash_key)r'   r(   r)   r   r›   r+   r!   r!   r   r"   rÒ   Ì  s    rÒ   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚCachedStaticFilesStorageúh
    A static file system storage backend which also saves
    hashed copies of the files it saves.
    c                    s&   t jdtdd� tƒ j|i |¤Ž d S )NzNCachedStaticFilesStorage is deprecated in favor of ManifestStaticFilesStorage.rG   )Ú
stacklevel)ÚwarningsÚwarnr   r   r   r¶   r   r!   r"   r   ß  s
   ýz!CachedStaticFilesStorage.__init__)r'   r(   r)   r*   r   r+   r!   r!   r   r"   rÕ   Ú  s    rÕ   c                   @   s   e Zd ZdZdS )ÚManifestStaticFilesStoragerÖ   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   ÚSTATICFILES_STORAGEÚ_wrappedrƒ   r!   r!   r"   Ú_setupñ  rË   zConfiguredStorage._setupN)r'   r(   r)   rÞ   r!   r!   r!   r"   rÛ   ð  s    rÛ   )+r?   r¼   rQ   rt   r5   rØ   Úcollectionsr   Úurllib.parser   r   r   r   Údjango.confr   Ú django.contrib.staticfiles.utilsr   r	   Údjango.core.cacher
   r   rÔ   r   Údjango.core.exceptionsr   Údjango.core.files.baser   Údjango.core.files.storager   r   Údjango.utils.deprecationr   Údjango.utils.functionalr   r   r,   r³   rÆ   rÒ   rÕ   rÚ   rÛ   Ústaticfiles_storager!   r!   r!   r"   Ú<module>   s6      B=
