3
gD                 @   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                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__ `/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.pyr      s    zStaticFilesStorage.__init__c                s   | j stdt j|S )NzaYou're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.)r   r	   r   path)r   name)r   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")   *.css#(url\(['"]{0,1}\s*(.*?)["']{0,1}\))(@import\s*["']\s*(.*?)["'])@import url("%s")Tc                s   t  j|| i | _i | _xd| jD ]Z\}}xP|D ]H}t|ttfrL|\}}n| j}t	j
|t	j}| jj|g j||f q0W q"W d S )N)r   r   	_patternshashed_filespatterns
isinstancetuplelistdefault_templaterecompile
IGNORECASE
setdefaultappend)r   r   r   	extensionr)   patterntemplateZcompiled)r   r   r   r   7   s    

zHashedFilesMixin.__init__Nc             C   s@   |dkrdS t j }x|j D ]}|j| qW |j 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   contentr8   chunkr   r   r   	file_hashD   s    zHashedFilesMixin.file_hashc             C   s   t t|}|jj }|r,t t|jj p.|}|d k}|rz| j|sVtd|| f y| j|}W n tk
rx   |S X z| j||}W d |r|j	  X t
jj|\}}t
jj|\}	}
|rd| nd}t
jj|d|	||
f }t|}||d< d|kr|d  r|d  d7  < t|S )	Nz)The file '%s' could not be found with %r.z.%s z%s%s%s   z?#   ?)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   s2    



zHashedFilesMixin.hashed_nameFc                s   t jr| r|d }}nBt|\}}t|jjdr:|}n |f}|dk	rR||f7 }|| }t j|}	d|k}
|sv|
rtt|	}|r|d  r||d< |
r|d  r|d  d7  < t	|}	t
|	S )	z:
        Return the non-hashed URL in DEBUG mode.
        r?   /Nz?#   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urlparts)r   r   r   _urlp   s&    
zHashedFilesMixin._urlc             C   s   | j | j||S )z:
        Return the non-hashed URL in DEBUG mode.
        )r\   stored_name)r   r   rY   r   r   r   rX      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  | j  \}}tjd|r|S |jdr8|jtj r8|S t|\}}|jdrr|jtjs^t|ttjd }n.t	j
dkrnjt	j
d}tjtj||}jjt|d d}dj|jddd	 |jdd
d  }|r |d|krd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]+:rT   NT)rY   r(      z?##r`   )groupsr.   match
startswithr   r   r   AssertionErrorlenrI   sepreplace	posixpathrL   dirnamer\   _stored_namer   rJ   )	ZmatchobjmatchedrX   Zurl_pathrZ   Ztarget_nameZsource_nameZ
hashed_urlZtransformed_url)r(   r   r   r5   r   r   	converter   s$    


*z1HashedFilesMixin.url_converter.<locals>.converter)r-   )r   r   r(   r5   rl   r   )r(   r   r   r5   r   url_converter   s    -zHashedFilesMixin.url_converterc             +   s   |rdS i }fdd D }x*j  ||D ]\}}}}	|||fV  q.W  fdd|D  xPtjD ]B}
d}x2j  ||D ] \}}}}|||fV  |p|}qW |shP qhW |rddtdfV  j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   r   )pathsr   r   
<dictcomp>   s    z1HashedFilesMixin.post_process.<locals>.<dictcomp>FZAllz!Max post-process passes exceeded.)_post_processrangemax_post_process_passesRuntimeErrorr(   r:   )r   rp   dry_runoptionsr(   adjustable_pathsr   rR   	processed_isubstitutionsZsubstr   )rp   r   r   post_process   s"    zHashedFilesMixin.post_processc             c   s  dd }xt ||ddD ]}d}|| \}}|j|}	| j|}
| j|
}||krh| j||	}n|| }t|	dr|	jd | j|}d}||kr|}|	j j	d}x| j
j D ]x\}}t||frxb|D ]Z\}}| j|||}y|j||}W q tk
r, } z|d |dfV  W Y d d }~X qX qW qW |rF| j| t|j }| jrf| j|| | j||}| j|r| j| | j||}| j|}||krd}d}|s|sd}| j||	}| j|}|||< ||||fV  W d Q R X qW d S )	Nc             S   s   t | jtjS )N)re   rJ   rI   rf   )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   rp   rx   r(   r~   r   r|   Zstorager   Zoriginal_filecleaned_namer   rR   Zhashed_file_existsry   Zold_hashed_namer<   r3   r)   r4   r5   rl   exccontent_fileZ
saved_namer   r   r   rr      sZ    





(




zHashedFilesMixin._post_processc             C   s   |j ddS )N\rT   )rg   )r   r   r   r   r   rO   C  s    zHashedFilesMixin.clean_namec             C   s   |S )Nr   )r   r   r   r   r   r   F  s    zHashedFilesMixin.hash_keyc             C   sD   t j|}| j|}| j|}|j|}|d kr@| j| j|}|S )N)rh   normpathrO   r   getrR   )r   r   r(   r   r   
cache_namer   r   r   rj   I  s    



zHashedFilesMixin._stored_namec             C   s   | j |}| j|}| jj|}|r(|S |}xFt| jd D ]4}| j | j|d |d}||krl|| j|< |S |}q<W td|| f d S )Nr^   )r<   rM   z*The name '%s' could not be hashed with %r.)rO   r   r(   r   rs   rt   rR   rE   )r   r   r   r   r   Zintermediate_namer{   r   r   r   r]   U  s    


zHashedFilesMixin.stored_namer%   r&   r$   r   r#   r   )r   )N)NN)FN)F)N)F)r   r   r   r-   rt   r)   r   r   r>   rR   r\   rX   rm   r}   rr   rO   r   rj   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 )ManifestFilesMixinz1.0zstaticfiles.jsonTFc                s   t  j|| | j | _d S )N)r   r   load_manifestr(   )r   r   r   )r   r   r   r   s  s    zManifestFilesMixin.__init__c             C   s>   y$| j | j}|j j S Q R X W n tk
r8   d S X d S )N)rF   manifest_namer   r   FileNotFoundError)r   manifestr   r   r   read_manifestw  s
    z ManifestFilesMixin.read_manifestc             C   sp   | j  }|d kri S ytj|}W n tjk
r8   Y n X |jd}|dkrX|jdi S td| j| jf d S )Nversionz1.0rp   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             /   s0   i | _ t j||E d H  |jds,| j  d S )Nrv   )r(   r   r}   r   save_manifest)r   r   r   )r   r   r   r}     s    
zManifestFilesMixin.post_processc             C   sJ   | j | jd}| j| jr&| j| j tj|j }| j| jt	| d S )N)rp   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 }| j|}| jj|}|d krV| jrFtd| | j	| j
|}t|}||d< d|kr|d  r|d  d7  < t|S )Nz+Missing staticfiles manifest entry for '%s'r@   z?#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 )N)r   r   ZSTATICFILES_STORAGEZ_wrapped)r   r   r   r   _setup  s    zConfiguredStorage._setupN)r   r   r   r   r   r   r   r   r     s   r   )r7   r   rI   rh   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?