a
    Ig                     @   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 d dlmZ G dd	 d	ZG d
d deZdS )    N)
hsv_to_rgb)Stats)settings)format_html)gettext_lazy)Panelc                   @   sn   e Zd ZdddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )FunctionCallr   Nr   g      ?   c                 C   sL   || _ || _|r|| _n|j| d d | _|| _|| _|p>g | _|| _d S )N   )statobjfuncstatsdepthid
parent_idshsv)selfr   r   r   r   r   r   r    r   Z/var/www/html/django/DPS/env/lib/python3.9/site-packages/debug_toolbar/panels/profiling.py__init__   s    
zFunctionCall.__init__c                 C   s   | j S N)parent_classesr   r   r   r   r      s    zFunctionCall.parent_classesc                 C   s:   t | j \}}}d|d dd|d dd|d ddS )Nzrgb(d   fz%,z%))r   r   )r   rgbr   r   r   
background    s    zFunctionCall.backgroundc                    s^   t tdrZ| j\ }}ttj}tj  tj|} |oXt	 fdddD  S dS )z
        Check if the function is from the project code.

        Project code is identified by the BASE_DIR setting
        which is used in Django projects by default.
        BASE_DIRc                 3   s    | ]}|  tjjv V  qd S r   )splitospathsep).0	directory	file_namer   r   	<genexpr>2   s   z/FunctionCall.is_project_func.<locals>.<genexpr>)zsite-packageszdist-packagesN)
hasattrr   r   strr    r"   r#   normpath
startswithany)r   _base_dirr   r'   r   is_project_func$   s    


zFunctionCall.is_project_funcc           	      C   s   | j }|d d dkrH|d }|drB|drBd|dd  S |S np| j \}}}|d}|dkrv||d	 d  }|tjd}t|dkr|tjd\}}nd
}td||||S d S )N   )~r   <>z{%s}r
   z/site-packages/   z<module>z<span class="djdt-path">{0}/</span><span class="djdt-file">{1}</span> in <span class="djdt-func">{3}</span>(<span class="djdt-lineno">{2}</span>))	r   r-   endswithfindrsplitr"   r$   lenr   )	r   	func_namenamer(   line_nummethodidxZ
split_path	file_pathr   r   r   func_std_string8   s*    
zFunctionCall.func_std_stringc           
      c   s   | j \}}}t| jj| j }t| jj| j  D ]\}\}}||d | | jd   }|d dkrjdn||d | jd   }	t	| j|| jd |t
| jd t
| | j| jg ||	dfdV  q4d S )Nr
      r   r/   )r   r   r   r   )r   r;   r   Zall_calleesr   	enumerateitemsr   r   r   r+   r   r   )
r   hsvcountir   r   Zh1s1r   r   r   subfuncsX   s    "&zFunctionCall.subfuncsc                 C   s
   | j d S )Nr
   r   r   r   r   r   rI   h   s    zFunctionCall.countc                 C   s
   | j d S )Nr2   rM   r   r   r   r   tottimek   s    zFunctionCall.tottimec                 C   s   | j \}}}}| j d S )NrC   rM   r   ccncttctr   r   r   cumtimen   s    zFunctionCall.cumtimec                 C   s"   | j \}}}}|dkrdS || S Nr   rM   rO   r   r   r   tottime_per_callr   s    zFunctionCall.tottime_per_callc                 C   s"   | j \}}}}|dkrdS || S rU   rM   rO   r   r   r   cumtime_per_callz   s    zFunctionCall.cumtime_per_callc                 C   s
   d| j  S )N   r   r   r   r   r   indent   s    zFunctionCall.indent)r   Nr   Nr	   )__name__
__module____qualname__r   r   r   r1   rB   rL   rI   rN   rT   rV   rW   rZ   r   r   r   r   r      s    
 r   c                       sL   e Zd ZdZedZdZe d Z	 fddZ
dd Z fd	d
Z  ZS )ProfilingPanelz4
    Panel that displays profiling information.
    Z	Profilingz#debug_toolbar/panels/profiling.htmlZPROFILER_CAPTURE_PROJECT_CODEc                    s   t  | _| jt j|S r   )cProfileZProfileprofilerZruncallsuperprocess_request)r   request	__class__r   r   rb      s    
zProfilingPanel.process_requestc                 C   sl   | | d|_|j|k rh| D ]D}|jd |ksP| jr"| r"|jd dkr"d|_| |||| q"d S )NFrC   r   T)appendZhas_subfuncsr   rL   r   capture_project_coder1   add_node)r   	func_listr   	max_depthZcum_timeZsubfuncr   r   r   rh      s    

zProfilingPanel.add_nodec                    s   t | dsd S | j  t| j| _| j  tt j	j
}|| jjv rt| j|dd}g }|jd t d  }| ||t d | | d|i d S )Nr`   r   rY   rC   ZPROFILER_THRESHOLD_RATIOZPROFILER_MAX_DEPTHri   )r*   r`   Zcreate_statsr   r   Zcalc_calleesr_   labelra   rb   __code__r   dt_settings
get_configrh   Zrecord_stats)r   rc   responseZ	root_funcrootri   Zcum_time_thresholdrd   r   r   generate_stats   s$    



zProfilingPanel.generate_stats)r[   r\   r]   __doc__r/   titletemplaterm   rn   rg   rb   rh   rq   __classcell__r   r   rd   r   r^      s   r^   )r_   r"   colorsysr   Zpstatsr   Zdjango.confr   Zdjango.utils.htmlr   Zdjango.utils.translationr   r/   Zdebug_toolbarrm   Zdebug_toolbar.panelsr   r   r^   r   r   r   r   <module>   s   x