3
(h~&                 @   s  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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e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)OrderedDict)apps)settings)utils)ImproperlyConfigured)FileSystemStorageStoragedefault_storage)	lru_cachesix)	safe_join)
LazyObjectempty)import_stringc               @   s"   e Zd ZdZdddZdd ZdS )	
BaseFinderzN
    A base file finder to be used for custom staticfiles finder classes.
    Fc             C   s   t ddS )a  
        Given a relative file path this ought to find an
        absolute file path.

        If the ``all`` parameter is ``False`` (default) only
        the first found file path will be returned; if set
        to ``True`` a list of all found files paths is returned.
        z5subclasses of BaseFinder must provide a find() methodN)NotImplementedError)selfpathall r   F/tmp/pip-install-q3hcpn_q/Django/django/contrib/staticfiles/finders.pyfind   s    	zBaseFinder.findc             C   s   t ddS )z
        Given an optional list of paths to ignore, this should 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      s   
r   c                   s>   e Zd ZdZd f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 | _ t | _ttjttfs&tdxptjD ]f}t|ttfrJ|\}}nd}tj	rvt
jjtj	t
jj|krvtd||f| j kr.| j j||f q.W x,| j D ]"\}}t|d}||_|| j|< qW tt| j|| d S )NzZYour STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma? zGThe STATICFILES_DIRS setting should not contain the STATIC_ROOT setting)location)	locationsr   storages
isinstancer   ZSTATICFILES_DIRSr   tupler   ZSTATIC_ROOTosr   abspathappendr   prefixsuperr   __init__)r   	app_namesargskwargsrootr(   Zfilesystem_storage)	__class__r   r   r*   2   s&    
 
zFileSystemFinder.__init__Fc             C   sR   g }xH| j D ]>\}}|tkr&tj| | j|||}|r|s@|S |j| qW |S )zd
        Looks 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   L   s    
zFileSystemFinder.findc             C   sN   |r0d|t jf }|j|s dS |t|d }t||}t jj|rJ|S dS )z
        Finds a requested static file in a location, returning the found
        absolute path (or ``None`` if no match).
        z%s%sN)r%   sep
startswithlenr   r   exists)r   r.   r   r(   r   r   r   r1   \   s    

zFileSystemFinder.find_locationc             c   sB   x<| j D ]2\}}| j| }xtj||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   j   s    
zFileSystemFinder.list)N)F)N)	r   r   r   r   r*   r   r1   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 | _ t | _t j } r4t   fdd|D }xX|D ]P}| jtjj|j| j	}tjj
|jr:|| j|j< |j| j kr:| j j|j q:W t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   r"   Zget_app_configssetstorage_classr%   r   join
source_dirisdirr    r<   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   sD   x>t j| jD ].}|jdrxtj||D ]}||fV  q*W qW dS )z5
        List all files in all app storages.
        r   N)r   
itervaluesr"   r7   r   r8   )r   r   r9   r   r   r   r   r      s    
zAppDirectoriesFinder.listFc             C   sX   g }xN| j D ]D}| j| j}|tkr.tj| | j||}|r|sF|S |j| qW |S )z9
        Looks for files in the app directories.
        )r   r"   r    r0   r'   find_in_app)r   r   r   r2   appZapp_locationmatchr   r   r   r      s    
zAppDirectoriesFinder.findc             C   s0   | j j|}|r,|j|r,|j|}|r,|S dS )zL
        Find a requested static file in an app's static locations.
        N)r"   getr7   r   )r   rG   r   r9   r3   r   r   r   rF      s    

z AppDirectoriesFinder.find_in_app)N)F)r   r   r   r   r   rA   rC   r*   r   r   rF   r:   r   r   )r/   r   r;   t   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                sV   |d k	r|| _ | j d kr&td| j t| j ttfs@| j  | _ tt| j|| d S )NzHThe staticfiles storage finder %r doesn't have a storage class assigned.)	r9   r   r/   r#   r   r   r)   rJ   r*   )r   r9   r,   r-   )r/   r   r   r*      s    


zBaseStorageFinder.__init__Fc             C   sj   y| j jd W n tk
r$   Y nBX | j jtkr@tj| j j | j j|rf| j j|}|rb|g}|S g S )zM
        Looks for files in the default file storage, if it's local.
        r   )r9   r   r   r    r0   r'   r7   )r   r   r   rH   r   r   r   r      s    zBaseStorageFinder.findc             c   s(   x"t j| j|D ]}|| jfV  qW dS )z0
        List all files of the storage.
        N)r   r8   r9   )r   r   r   r   r   r   r      s    zBaseStorageFinder.list)N)F)	r   r   r   r   r9   r*   r   r   r:   r   r   )r/   r   rJ      s
   
rJ   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                s6   t t| j|| t| jdt}|s2td| j d S )Nbase_locationzOThe storage backend of the staticfiles finder %r doesn't have a valid location.)r)   rK   r*   getattrr9   r   r   r/   )r   r,   r-   rL   )r/   r   r   r*      s
    zDefaultStorageFinder.__init__)r   r   r   r   r	   r9   r*   r:   r   r   )r/   r   rK      s   rK   Fc             C   sn   g t dd< g }xHt D ]>}|j| |d}| r8|r8|S t|ttfsL|g}|j| qW |rb|S |rjg 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   )r0   get_findersr   r#   r   r$   extend)r   r   r2   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   rN     s    rN   )maxsizec             C   s(   t | }t|ts"td|tf | S )z
    Imports 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   rR     s
    
rR   )F)#r%   collectionsr   Zdjango.appsr   Zdjango.confr   Zdjango.contrib.staticfilesr   Zdjango.core.exceptionsr   Zdjango.core.files.storager   r   r	   Zdjango.utilsr
   r   Zdjango.utils._osr   Zdjango.utils.functionalr   r   Zdjango.utils.module_loadingr   r0   objectr   r   r;   rJ   rK   r   rN   rR   r   r   r   r   <module>   s&   G@-
