B
    CVvg~(  ã               @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZ d dlmZ d d	lmZmZ d d
lmZ g ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZddd„Zdd„ Ze jdd�dd„ ƒZ dS )é    N)Úapps)Úsettings)Úutils)ÚError)ÚImproperlyConfigured)ÚFileSystemStorageÚStorageÚdefault_storage)Ú	safe_join)Ú
LazyObjectÚempty)Úimport_stringc               @   s*   e Zd ZdZdd„ Zd
dd„Zdd„ Zd	S )Ú
BaseFinderzN
    A base file finder to be used for custom staticfiles finder classes.
    c             K   s   t dƒ‚d S )NzUsubclasses may provide a check() method to verify the finder is configured correctly.)ÚNotImplementedError)ÚselfÚkwargs© r   úF/tmp/pip-install-o3oxmrkh/Django/django/contrib/staticfiles/finders.pyÚcheck   s    zBaseFinder.checkFc             C   s   t dƒ‚dS )zÞ
        Given a relative file path, find an absolute file path.

        If the ``all`` parameter is False (default) return only the first found
        file path; if True, return a list of all found files paths.
        z5subclasses of BaseFinder must provide a find() methodN)r   )r   ÚpathÚallr   r   r   Úfind   s    zBaseFinder.findc             C   s   t dƒ‚dS )z•
        Given an optional list of paths to ignore, return a two item iterable
        consisting of the relative path and storage instance.
        z5subclasses of BaseFinder must provide a list() methodN)r   )r   Úignore_patternsr   r   r   Úlist'   s    zBaseFinder.listN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s   
	r   c                   sF   e Zd ZdZd‡ fdd„	Zdd„ Zddd	„Zdd
d„Zdd„ Z‡  Z	S )ÚFileSystemFinderz_
    A static files finder that uses the ``STATICFILES_DIRS`` setting
    to locate files.
    Nc                s–   g | _ i | _xHtjD ]>}t|ttfƒr0|\}}nd}||f| j kr| j  ||f¡ qW x,| j D ]"\}}t|d�}||_	|| j|< q^W t
ƒ j||Ž d S )NÚ )Úlocation)Ú	locationsÚstoragesr   ÚSTATICFILES_DIRSÚ
isinstancer   ÚtupleÚappendr   ÚprefixÚsuperÚ__init__)r   Ú	app_namesÚargsr   Úrootr'   Zfilesystem_storage)Ú	__class__r   r   r)   4   s    

zFileSystemFinder.__init__c             K   s¤   g }t tjttfƒs(| tdddd�¡ xvtjD ]l}t |ttfƒrj|\}}| d¡rj| td| dd�¡ tjr0t	j
 tj¡t	j
 |¡kr0| td	d
d�¡ q0W |S )Nz4The STATICFILES_DIRS setting is not a tuple or list.z$Perhaps you forgot a trailing comma?zstaticfiles.E001)ZhintÚidú/zHThe prefix %r in the STATICFILES_DIRS setting must not end with a slash.zstaticfiles.E003)r.   zHThe STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.zstaticfiles.E002)r$   r   r#   r   r%   r&   r   ÚendswithZSTATIC_ROOTÚosr   Úabspath)r   r   Úerrorsr,   r'   r   r   r   r   F   s&    


 zFileSystemFinder.checkFc             C   sR   g }xH| j D ]>\}}|tkr&t |¡ |  |||¡}|r|s@|S | |¡ qW |S )zW
        Look for files in the extra locations as defined in STATICFILES_DIRS.
        )r!   Úsearched_locationsr&   Úfind_location)r   r   r   Úmatchesr'   r,   Úmatched_pathr   r   r   r   _   s    
zFileSystemFinder.findc             C   sN   |r0d|t jf }| |¡s dS |t|ƒd… }t||ƒ}t j |¡rJ|S dS )z‚
        Find a requested static file in a location and return the found
        absolute path (or ``None`` if no match).
        z%s%sN)r1   ÚsepÚ
startswithÚlenr
   r   Úexists)r   r,   r   r'   r   r   r   r5   n   s    

zFileSystemFinder.find_locationc             c   sB   x<| j D ]2\}}| j| }xt ||¡D ]}||fV  q(W qW dS )z2
        List all files in all locations.
        N)r!   r"   r   Ú	get_files)r   r   r'   r,   Ústorager   r   r   r   r   |   s    
zFileSystemFinder.list)N)F)N)
r   r   r   r   r)   r   r   r5   r   Ú__classcell__r   r   )r-   r   r   /   s   

r   c                   sD   e Zd ZdZeZdZd‡ fdd„	Zdd„ Zdd	d
„Z	dd„ Z
‡  ZS )ÚAppDirectoriesFinderzu
    A static files finder that looks in the directory of each app as
    specified in the source_dir attribute.
    ZstaticNc                sž   g | _ i | _t  ¡ }ˆ r2tˆ ƒ‰ ‡ fdd„|D ƒ}xX|D ]P}|  tj |j| j¡¡}tj 	|j
¡r8|| j|j< |j| j kr8| j  |j¡ q8W tƒ j||Ž d S )Nc                s   g | ]}|j ˆ kr|‘qS r   )Úname)Ú.0Úac)r*   r   r   ú
<listcomp>–   s    z1AppDirectoriesFinder.__init__.<locals>.<listcomp>)r   r"   Zget_app_configsÚsetÚstorage_classr1   r   ÚjoinÚ
source_dirÚisdirr    r@   r&   r(   r)   )r   r*   r+   r   Zapp_configsZ
app_configZapp_storage)r-   )r*   r   r)   Ž   s    
zAppDirectoriesFinder.__init__c             c   sB   x<| j  ¡ D ].}| d¡rxt ||¡D ]}||fV  q(W qW dS )z5
        List all files in all app storages.
        r   N)r"   Úvaluesr;   r   r<   )r   r   r=   r   r   r   r   r       s    
zAppDirectoriesFinder.listFc             C   sX   g }xN| j D ]D}| j| j}|tkr.t |¡ |  ||¡}|r|sF|S | |¡ qW |S )z8
        Look for files in the app directories.
        )r   r"   r    r4   r&   Úfind_in_app)r   r   r   r6   ÚappZapp_locationÚmatchr   r   r   r   ©   s    
zAppDirectoriesFinder.findc             C   s0   | j  |¡}|r,| |¡r,| |¡}|r,|S dS )zL
        Find a requested static file in an app's static locations.
        N)r"   Úgetr;   r   )r   rK   r   r=   r7   r   r   r   rJ   ¹   s
    
z AppDirectoriesFinder.find_in_app)N)F)r   r   r   r   r   rE   rG   r)   r   r   rJ   r>   r   r   )r-   r   r?   †   s   	
r?   c                   s8   e Zd ZdZdZd
‡ fdd„	Zddd„Zdd	„ Z‡  ZS )ÚBaseStorageFinderzZ
    A base static files finder to be used to extended
    with an own storage class.
    Nc                sR   |d k	r|| _ | j d kr&td| j ƒ‚t| j ttfƒs@|   ¡ | _ tƒ j||Ž d S )NzHThe staticfiles storage finder %r doesn't have a storage class assigned.)r=   r   r-   r$   r   r   r(   r)   )r   r=   r+   r   )r-   r   r   r)   Ì   s    


zBaseStorageFinder.__init__Fc             C   sj   y| j  d¡ W n tk
r$   Y nBX | j jtkr@t | j j¡ | j  |¡rf| j  |¡}|rb|g}|S g S )zL
        Look for files in the default file storage, if it's local.
        r   )r=   r   r   r    r4   r&   r;   )r   r   r   rL   r   r   r   r   Ø   s    zBaseStorageFinder.findc             c   s(   x"t  | j|¡D ]}|| jfV  qW dS )z0
        List all files of the storage.
        N)r   r<   r=   )r   r   r   r   r   r   r   ê   s    zBaseStorageFinder.list)N)F)	r   r   r   r   r=   r)   r   r   r>   r   r   )r-   r   rN   Å   s
   
rN   c                   s$   e Zd ZdZeZ‡ fdd„Z‡  ZS )ÚDefaultStorageFinderzF
    A static files finder that uses the default storage backend.
    c                s2   t ƒ j||Ž t| jdtƒ}|s.td| j ƒ‚d S )NÚbase_locationzOThe storage backend of the staticfiles finder %r doesn't have a valid location.)r(   r)   Úgetattrr=   r   r   r-   )r   r+   r   rP   )r-   r   r   r)   ø   s
    zDefaultStorageFinder.__init__)r   r   r   r   r	   r=   r)   r>   r   r   )r-   r   rO   ò   s   rO   Fc             C   sl   g t dd…< g }xFtƒ D ]<}|j| |d�}|s6|r6|S t|ttfƒsJ|g}| |¡ qW |r`|S |rhg S dS )zÓ
    Find a static file with the given path using all enabled finders.

    If ``all`` is ``False`` (default), return the first matching
    absolute path (or ``None`` if no match). Otherwise return a list.
    N)r   )r4   Úget_findersr   r$   r   r%   Úextend)r   r   r6   ÚfinderÚresultr   r   r   r     s    r   c              c   s   xt jD ]} t| ƒV  qW d S )N)r   ZSTATICFILES_FINDERSÚ
get_finder)Zfinder_pathr   r   r   rR     s    rR   )Úmaxsizec             C   s(   t | ƒ}t|tƒs"td|tf ƒ‚|ƒ S )zƒ
    Import the staticfiles finder class described by import_path, where
    import_path is the full Python path to the class.
    z%Finder "%s" is not a subclass of "%s")r   Ú
issubclassr   r   )Zimport_pathÚFinderr   r   r   rV     s
    
rV   )F)!Ú	functoolsr1   Zdjango.appsr   Zdjango.confr   Zdjango.contrib.staticfilesr   Zdjango.core.checksr   Zdjango.core.exceptionsr   Zdjango.core.files.storager   r   r	   Zdjango.utils._osr
   Zdjango.utils.functionalr   r   Zdjango.utils.module_loadingr   r4   r   r   r?   rN   rO   r   rR   Ú	lru_cacherV   r   r   r   r   Ú<module>   s&   W?-
