o
    g                     @   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                   @   sp   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   ^/var/www/html/bloggers_ems/venv/lib/python3.10/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                 C   s8   t tdr| j\}}}ttj|v od|vod|v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_DIR/site-packages/z/dist-packages/N)hasattrr   r   strr    )r   	file_name_r   r   r   is_project_func$   s   
zFunctionCall.is_project_funcc           	      C   s   | j }|d d dkr#|d }|dr!|dr!d|dd  S |S | j \}}}|d}|dkr:||d	 d  }|tjd}t|dkrQ|tjd\}}nd
}td||||S )N   )~r   <>z{%s}r
   r!      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   
startswithendswithfindrsplitosseplenr   )	r   	func_namenamer$   line_nummethodidx
split_path	file_pathr   r   r   func_std_string4   s*   
zFunctionCall.func_std_stringc           
      c   s    d}| j \}}}t| jj| j }| jj| j  D ]G\}}|d7 }||| | jd   }|d dkr6dn
||d | jd   }	t| j|| jd |t	| j
d t	| | j| j
g ||	dfdV  qd S )Nr   r
      r%   )r   r   r   r   )r   r3   r   all_calleesr   itemsr   r   r   r#   r   r   )
r   ihsvcountr   r   h1s1r   r   r   subfuncsT   s$   &
zFunctionCall.subfuncsc                 C   
   | j d S )Nr
   r   r   r   r   r   rC   f      
zFunctionCall.countc                 C   rG   )Nr'   rH   r   r   r   r   tottimei   rI   zFunctionCall.tottimec                 C   s   | j \}}}}| j d S )Nr<   rH   r   ccncttctr   r   r   cumtimel   s   
zFunctionCall.cumtimec                 C   s"   | j \}}}}|dkrdS || S Nr   rH   rK   r   r   r   tottime_per_callp      zFunctionCall.tottime_per_callc                 C   s"   | j \}}}}|dkrdS || S rQ   rH   rK   r   r   r   cumtime_per_callx   rS   zFunctionCall.cumtime_per_callc                 C   s
   d| j  S )N   r   r   r   r   r   indent   rI   zFunctionCall.indent)r   Nr   Nr	   )__name__
__module____qualname__r   r   r   r&   r;   rF   rC   rJ   rP   rR   rT   rW   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.
    	Profilingz#debug_toolbar/panels/profiling.htmlPROFILER_CAPTURE_PROJECT_CODEc                    s   t  | _| jt j|S r   )cProfileProfileprofilerruncallsuperprocess_request)r   request	__class__r   r   rc      s   
zProfilingPanel.process_requestc                 C   sp   | | d|_|j|k r4| D ]$}|jd |ks(| jr3| r3|jd dkr3d|_| |||| qd S d S )NFr<   r   T)appendhas_subfuncsr   rF   r   capture_project_coder&   add_node)r   	func_listr   	max_depthcum_timesubfuncr   r   r   rj      s   

zProfilingPanel.add_nodec                    s   t | dsd S | j  t| j| _| j  tt j	j
}|| jjv rOt| j|dd}g }|jd t d  }| ||t d | | d|i d S d S )Nr`   r   rV   r<   PROFILER_THRESHOLD_RATIOPROFILER_MAX_DEPTHrk   )r"   r`   create_statsr   r   calc_calleesr^   labelrb   rc   __code__r   dt_settings
get_configrj   record_stats)r   rd   response	root_funcrootrk   cum_time_thresholdre   r   r   generate_stats   s&   



zProfilingPanel.generate_stats)rX   rY   rZ   __doc__r%   titletemplateru   rv   ri   rc   rj   r|   __classcell__r   r   re   r   r[      s    r[   )r^   r1   colorsysr   pstatsr   django.confr   django.utils.htmlr   django.utils.translationr   r%   debug_toolbarru   debug_toolbar.panelsr   r   r[   r   r   r   r   <module>   s    v