î
à^Q\  ã               @   sz  d  d l  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 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 d  d l m Z d  d l m Z y d  d l Z Wn e k
 rd Z Yn Xe Gd d „  d e ƒ ƒ Z  Gd d „  d e ƒ Z! e! ƒ  Z" Gd d „  d e ƒ Z# e j$ Z% Gd d „  d e j& ƒ Z' d S)é    )Úabsolute_importÚunicode_literals)ÚOrderedDict)ÚjoinÚnormpath)Úsettings)ÚfindersÚstorage)Ústaticfiles)Úget_storage_class)Úpython_2_unicode_compatible)Ú
LazyObject)Úugettext_lazyÚ	ungettext)Úpanels)ÚThreadCollectorNc               @   sF   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 S)Ú
StaticFilezA
    Representing the different properties of a static file.
    c             C   s   | |  _  d  S)N)Úpath)Úselfr   © r   úT/var/www/dbchiro/venv/build/django-debug-toolbar/debug_toolbar/panels/staticfiles.pyÚ__init__   s    zStaticFile.__init__c             C   s   |  j  S)N)r   )r   r   r   r   Ú__str__   s    zStaticFile.__str__c             C   s   t  j |  j ƒ S)N)r   Úfindr   )r   r   r   r   Ú	real_path"   s    zStaticFile.real_pathc             C   s   t  j j |  j ƒ S)N)r	   Ústaticfiles_storageÚurlr   )r   r   r   r   r   %   s    zStaticFile.urlN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   r   c                   s%   e  Z d  Z d ‡  f d d † Z ‡  S)ÚFileCollectorNc                s6   | j  d ƒ r d  St t |  ƒ j t | ƒ | ƒ d  S)Nú/)ÚendswithÚsuperr!   Úcollectr   )r   r   Úthread)Ú	__class__r   r   r%   +   s    zFileCollector.collect)r   r   r   r%   r   r   )r'   r   r!   )   s   r!   c               @   s"   e  Z d  Z d Z d d „  Z d S)ÚDebugConfiguredStoragez¦
    A staticfiles storage class to be used for collecting which paths
    are resolved by using the {% static %} template tag (which uses the
    `url` method).
    c                s>   t  t j ƒ } G‡  f d d †  d | ƒ ‰  ˆ  t ƒ |  _ d  S)Nc                   s:   e  Z d  Z ‡ ‡  f d d †  Z ‡ ‡  f d d †  Z ‡  S)z>DebugConfiguredStorage._setup.<locals>.DebugStaticFilesStoragec                s&   t  ˆ  |  ƒ j | | Ž  | |  _ d  S)N)r$   r   Ú	collector)r   r)   ÚargsÚkwargs)ÚDebugStaticFilesStorager'   r   r   r   A   s    zGDebugConfiguredStorage._setup.<locals>.DebugStaticFilesStorage.__init__c                s&   |  j  j | ƒ t ˆ  |  ƒ j | ƒ S)N)r)   r%   r$   r   )r   r   )r,   r'   r   r   r   E   s    zBDebugConfiguredStorage._setup.<locals>.DebugStaticFilesStorage.url)r   r   r   r   r   r   )r,   )r'   r   r,   ?   s   r,   )r   r   ZSTATICFILES_STORAGEr)   Z_wrapped)r   Zconfigured_storage_clsr   )r,   r   Ú_setup;   s    
zDebugConfiguredStorage._setupN)r   r   r   r    r-   r   r   r   r   r(   5   s   r(   c                   sÊ   e  Z d  Z d Z d Z d Z e d d „  ƒ Z ‡  f d d †  Z d d	 „  Z	 d
 d „  Z
 e d d „  ƒ Z e d ƒ Z e d d „  ƒ Z d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z ‡  S)ÚStaticFilesPanelz3
    A panel to display the found staticfiles.
    zStatic filesz%debug_toolbar/panels/staticfiles.htmlc             C   s"   t  d ƒ i |  j d 6|  j d 6S)Nz5Static files (%(num_found)s found, %(num_used)s used)Ú	num_foundÚnum_used)Ú_r/   r0   )r   r   r   r   ÚtitleV   s    	zStaticFilesPanel.titlec                s/   t  t |  ƒ j | | Ž  d |  _ i  |  _ d  S)Nr   )r$   r.   r   r/   Ú_paths)r   r*   r+   )r'   r   r   r   [   s    	zStaticFilesPanel.__init__c             C   s   t  ƒ  t _ t _ d  S)N)r(   r	   r   r
   )r   r   r   r   Úenable_instrumentation`   s    z'StaticFilesPanel.enable_instrumentationc             C   s   t  t _ t _ d  S)N)Ú_original_storager	   r   r
   )r   r   r   r   Údisable_instrumentationc   s    z(StaticFilesPanel.disable_instrumentationc             C   s   t  |  j t j ƒ  ƒ S)N)Úlenr3   Ú	threadingÚcurrentThread)r   r   r   r   r0   f   s    zStaticFilesPanel.num_usedc             C   s$   |  j  } t d d | ƒ i | d 6S)Nz%(num_used)s file usedz%(num_used)s files usedr0   )r0   r   )r   r0   r   r   r   Únav_subtitlel   s    	zStaticFilesPanel.nav_subtitlec             C   s   t  j ƒ  d  S)N)r)   Zclear_collection)r   Úrequestr   r   r   Úprocess_requests   s    z StaticFilesPanel.process_requestc             C   sr   t  j ƒ  } | |  j t j ƒ  <|  j i |  j d 6|  j d 6| d 6|  j ƒ  d 6|  j	 ƒ  d 6|  j
 ƒ  d 6ƒ d  S)Nr/   r0   r
   Zstaticfiles_appsZstaticfiles_dirsZstaticfiles_finders)r)   Zget_collectionr3   r8   r9   Zrecord_statsr/   r0   Úget_staticfiles_appsÚget_staticfiles_dirsÚget_staticfiles_finders)r   r;   ÚresponseZ
used_pathsr   r   r   Úgenerate_statsv   s    	

zStaticFilesPanel.generate_statsc       
      C   sØ   t  ƒ  } xÈ t j ƒ  D]º } x± | j g  ƒ D]  \ } } t | d d ƒ r_ t | j | ƒ } n | } | j } d j | j | j	 g ƒ } | j
 | ƒ } | | f }	 | j | g  ƒ j |	 ƒ |  j d 7_ q, Wq W| S)z¥
        Returns a sorted mapping between the finder path and the list
        of relative and file system paths which that finder was able
        to find.
        ÚprefixNÚ.é   )r   r   Úget_findersÚlistÚgetattrr   rB   r'   r   r   r   Ú
setdefaultÚappendr/   )
r   Zfinders_mappingÚfinderr   Zfinder_storageZprefixed_pathZ
finder_clsZfinder_pathr   Úpayloadr   r   r   r?   ƒ   s    		z(StaticFilesPanel.get_staticfiles_findersc             C   sS   g  } x9 t  j ƒ  D]+ } t | t  j ƒ r | j | j ƒ q q Wd d „  | Dƒ S)zP
        Returns a list of paths to inspect for additional static files
        c             S   s(   g  |  ] \ } } | t  | ƒ f ‘ q Sr   )r   )Ú.0rB   Údirr   r   r   ú
<listcomp>¡   s   	 z9StaticFilesPanel.get_staticfiles_dirs.<locals>.<listcomp>)r   rE   Ú
isinstanceZFileSystemFinderÚextendÚ	locations)r   ÚdirsrJ   r   r   r   r>   ™   s
    z%StaticFilesPanel.get_staticfiles_dirsc             C   sf   g  } xY t  j ƒ  D]K } t | t  j ƒ r x0 | j D]" } | | k r5 | j | ƒ q5 q5 Wq q W| S)zJ
        Returns a list of app paths that have a static directory
        )r   rE   rO   ZAppDirectoriesFinderÚappsrI   )r   rS   rJ   Zappr   r   r   r=   £   s    z%StaticFilesPanel.get_staticfiles_apps)r   r   r   r    ÚnameÚtemplateÚpropertyr2   r   r4   r6   r0   r1   Z	nav_titler:   r<   rA   r?   r>   r=   r   r   )r'   r   r.   O   s   
r.   )(Ú
__future__r   r   Úcollectionsr   Úos.pathr   r   Zdjango.confr   Zdjango.contrib.staticfilesr   r	   Z'django.contrib.staticfiles.templatetagsr
   Zdjango.core.files.storager   Zdjango.utils.encodingr   Zdjango.utils.functionalr   Zdjango.utils.translationr   r1   r   Zdebug_toolbarr   Zdebug_toolbar.utilsr   r8   ÚImportErrorÚobjectr   r!   r)   r(   r   r5   ZPanelr.   r   r   r   r   Ú<module>   s,   			