a
    ތxd)                     @   s   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mZ d d	lmZ d d
lmZ zd dlZW n ey   dZY n0 G dd dZG dd deZe ZG dd deZe	jZ G dd dej!Z"dS )    )OrderedDict)joinnormpath)settings)findersstorage)Warning)get_storage_class)
LazyObject)gettext_lazyngettext)panels)ThreadCollectorNc                   @   s0   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   X/var/www/html/Ranjet/env/lib/python3.9/site-packages/debug_toolbar/panels/staticfiles.py__init__   s    zStaticFile.__init__c                 C   s   | j S r   r   r   r   r   r   __str__   s    zStaticFile.__str__c                 C   s   t | jS r   )r   findr   r   r   r   r   	real_path   s    zStaticFile.real_pathc                 C   s   t j| jS r   )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  ZS )FileCollectorNc                    s$   | drd S t t|| d S )N/)endswithsupercollectr   )r   r   thread	__class__r   r   r&   '   s    
zFileCollector.collect)N)r   r   r    r&   __classcell__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                 C   s(   t tj}G dd d|}|t| _d S )Nc                       s(   e Zd Z fddZ fddZ  ZS )z>DebugConfiguredStorage._setup.<locals>.DebugStaticFilesStoragec                    s   t  j|i | || _d S r   )r%   r   	collector)r   r,   argskwargsr(   r   r   r   =   s    zGDebugConfiguredStorage._setup.<locals>.DebugStaticFilesStorage.__init__c                    s   | j | t |S r   )r,   r&   r%   r   r   r(   r   r   r   A   s    zBDebugConfiguredStorage._setup.<locals>.DebugStaticFilesStorage.url)r   r   r    r   r   r*   r   r   r(   r   DebugStaticFilesStorage<   s   r/   )r	   r   ZSTATICFILES_STORAGEr,   Z_wrapped)r   Zconfigured_storage_clsr/   r   r   r   _setup8   s    
	zDebugConfiguredStorage._setupN)r   r   r    r!   r0   r   r   r   r   r+   1   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 fddZdd Zdd Zdd Zdd Zedd Z  ZS )StaticFilesPanelz3
    A panel to display the found staticfiles.
    zStatic filesz%debug_toolbar/panels/staticfiles.htmlc                 C   s   t d| j| jd S )Nz5Static files (%(num_found)s found, %(num_used)s used))	num_foundnum_used)_r2   r3   r   r   r   r   titleS   s    zStaticFilesPanel.titlec                    s"   t  j|i | d| _i | _d S )Nr   )r%   r   r2   _paths)r   r-   r.   r(   r   r   r   Z   s    zStaticFilesPanel.__init__c                 C   s   t  t_d S r   )r+   r   r   r   r   r   r   enable_instrumentation_   s    z'StaticFilesPanel.enable_instrumentationc                 C   s
   t t_d S r   )_original_storager   r   r   r   r   r   disable_instrumentationb   s    z(StaticFilesPanel.disable_instrumentationc                 C   s   |   }|o|d S )Nr3   )Z	get_stats)r   statsr   r   r   r3   e   s    zStaticFilesPanel.num_usedc                 C   s   | j }tdd|d|i S )Nz%(num_used)s file usedz%(num_used)s files usedr3   )r3   __)r   r3   r   r   r   nav_subtitlel   s    
zStaticFilesPanel.nav_subtitlec                    s   t   t |S r   )r,   Zclear_collectionr%   process_request)r   requestr(   r   r   r=   s   s    z StaticFilesPanel.process_requestc              	   C   sD   t  }|| jt < | | jt|||  | 	 | 
 d d S )N)r2   r3   ZstaticfilesZstaticfiles_appsZstaticfiles_dirsZstaticfiles_finders)r,   Zget_collectionr6   	threadingcurrentThreadZrecord_statsr2   lenget_staticfiles_appsget_staticfiles_dirsget_staticfiles_finders)r   r>   responseZ
used_pathsr   r   r   generate_statsw   s    zStaticFilesPanel.generate_statsc           
   	   C   s   t  }t D ]}z|g D ]p\}}t|ddr@t|j|}n|}|j}d|j|j	g}|
|}||f}	||g |	 |  jd7  _qW q ty   Y q0 q|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   rG   r)   r   r   r   
setdefaultappendr2   OSError)
r   Zfinders_mappingfinderr   finder_storageZprefixed_pathZ
finder_clsZfinder_pathr   payloadr   r   r   rD      s     
z(StaticFilesPanel.get_staticfiles_findersc                 C   s8   g }t  D ]}t|t jr||j qdd |D S )zP
        Returns a list of paths to inspect for additional static files
        c                 S   s   g | ]\}}|t |fqS r   )r   ).0rG   dirr   r   r   
<listcomp>       z9StaticFilesPanel.get_staticfiles_dirs.<locals>.<listcomp>)r   rJ   
isinstanceZFileSystemFinderextend	locations)r   dirsrP   r   r   r   rC      s
    z%StaticFilesPanel.get_staticfiles_dirsc                 C   s@   g }t  D ].}t|t jr|jD ]}||vr"|| q"q|S )zJ
        Returns a list of app paths that have a static directory
        )r   rJ   rW   ZAppDirectoriesFinderappsrN   )r   r[   rP   Zappr   r   r   rB      s    
z%StaticFilesPanel.get_staticfiles_appsc                 C   s`   g }t  D ]N}z"|g D ]\}}|| qW q tyX   |tdddd Y q0 q|S )z
        Check that the integration is configured correctly for the panel.

        Specifically look for static files that haven't been collected yet.

        Return a list of :class: `django.core.checks.CheckMessage` instances.
        zAdebug_toolbar requires the STATICFILES_DIRS directories to exist.z;Running manage.py collectstatic may help uncover the issue.zdebug_toolbar.staticfiles.W001)hintid)r   rJ   rK   r   rO   rN   r   )clserrorsrP   r   rQ   r   r   r   
run_checks   s    	zStaticFilesPanel.run_checks)r   r   r    r!   nametemplatepropertyr5   r   r7   r9   r3   r4   Z	nav_titler<   r=   rF   rD   rC   rB   classmethodr`   r*   r   r   r(   r   r1   K   s(   



r1   )#collectionsr   os.pathr   r   Zdjango.confr   Zdjango.contrib.staticfilesr   r   Zdjango.core.checksr   Zdjango.core.files.storager	   Zdjango.utils.functionalr
   Zdjango.utils.translationr   r4   r   r;   Zdebug_toolbarr   Zdebug_toolbar.utilsr   r?   ImportErrorr   r"   r,   r+   r   r8   Panelr1   r   r   r   r   <module>   s&   
