a
    DfX!                     @   s   d dl Z d dlZd dlZ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dZdd ZdddZdd Zd ddZdg fddZed!ddZd"ddZdS )#    N)contextmanager)Profilewraps   )config)escape   )stateFc           	      C   s   ddl m} ddlm} |||d}|r2| d }n&| d }| dd  D ]}|||}qFz||}W n ty|   d}Y n0 t|dfS )	Nr   )HTMLRenderer)Sessiontimelineshow_allr	   z5<h2><b>Rendering pyinstrument session failed</b></h2> )Zpyinstrument.renderersr   Zpyinstrument.sessionr   combinerender	Exceptionr   )	sessionsr   r   r   r   rsessionsrendered r   ]/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/profile.pyrender_pyinstrument   s    

r   c                 C   s   ddl m} dd l}ddlm}m} ddlm} tj	
tj	|jdd}t|}|| }	W d    n1 st0    Y  ||d }
|dd  D ]}|
| q|	j| ||
||
dd	d
d}t|dfS )Nr   )Stats)
json_stats
table_rows)TemplateZ	templateszviz.htmlr	   )Zprofile_namer   Zcalleeszutf-8z/static/z/snakeviz/static/zbackground-color: white;)pstatsr   snakevizZsnakeviz.statsr   r   Ztornado.templater    ospathjoindirname__file__openreadaddgeneratedecodereplacer   )namer   r   r"   r   r   r    ZSNAKEVIZ_PATHfZSNAKEVIZ_TEMPLATEr!   r   r   r   r   r   render_snakeviz    s$    
*r0   Ttreec                 C   sl  ddl m} ddlm} ddlm} ddlm} ddlm	}	 ||||	d
|}
|d }t }|| |  ||j}|r|j|d ur|nd	d
}n|j|d ur|nd	d
}d|jji}|dv rt| |d< |
j|fi |}W d    n1 s0    Y  t }|dkr.|||j|| n(|dkrJ|||j| n|j|d |d | dfS )Nr   )
FileReader)FlameGraphReporter)StatsReporter)TableReporter)TreeReporter)
flamegraphstatstabler1   r   T)merge_threadsZnative_tracesr7   r9   Zmemory_recordsr7   r9   )filer   )memrayr2   Zmemray.reporters.flamegraphr3   Zmemray.reporters.statsr4   Zmemray.reporters.tabler5   Zmemray.reporters.treer6   gettempfileNamedTemporaryFilewriteflushr.   Zget_leaked_allocation_recordsZ%get_high_watermark_allocation_recordsmetadataZhas_native_tracestupleZget_memory_snapshotsZfrom_snapshotioStringIOr   seekr)   )r.   r   Zshow_memory_leaksr:   reporterr2   r3   r4   r5   r6   Zreporter_clsr   nfreaderZsnapshotkwargsZreporter_instoutr   r   r   render_memray3   sP    


$


rM   c                 K   s  ddl m}m} g }|  D ]\\}}}|s.q|dkrt||fi |\}}	|dddvrddlm}
 |
dd	d
d}|| |	||f qnt
|}|dkrt|fi |\}}	n|dkrt||\}}	|d| d|	 ddd	dd}|	||f q|s|	d|df |S )Nr   )HTMLMarkdownr=   rH   r1   r;   )TerminalZstretch_bothr   iX  )sizing_modemarginZ
min_heightpyinstrumentr"   z<iframe srcdoc="z4" width="100%" height="100%" frameBorder="0" style="z"></iframe>i   r   zNo profiling output available)ZpanerN   rO   itemsrM   r>   widgetsrP   rA   appendr   r   r0   )Z	profilersrK   rN   rO   Zprofilesr$   enginer   srcstylerP   Ztermhtmlr   r   r   get_profilesa   s8    
r[   c                    s    fddt j D S )Nc                    s>   i | ]6\\}}} r t  |rr0t |s||f|qS r   )rematch).0r.   eZpsallowdenyr   r   
<dictcomp>   s    z get_sessions.<locals>.<dictcomp>)r
   	_profilesrT   r`   r   r`   r   get_sessions   s    re   c              	      s(  ddl m}m}m}m} ddlm}m} |tt	 ddd fdd| j
d	 tjd
krfdd}	|dddj
|	d |dddj
|	d |dd}
nHtjdkrfdd}|dg dddj
|d }
n
|dd}
||d|
fg dddddddS )Nr   )	AccordionColumnRowTabs)CheckboxSelect)r      Zstretch_width)rR   rQ   c                     s"   t t fi |d d < d S )N)r[   re   )argsrK   )ra   rb   tabsr   r   update_profiles   s
    z'profiling_tabs.<locals>.update_profilesrd   rS   c                     s   j  j d d S )Nr   valuerm   )r   r   ro   r   r   update_pyinstrument   s    z+profiling_tabs.<locals>.update_pyinstrumentzEnable timeline)rl   r   )r.   rR   rq   zShow All)rQ   r=   c                     s    j d d S )N)rH   rp   rr   )rH   ro   r   r   update_memray   s    z%profiling_tabs.<locals>.update_memrayZReporter)r7   r9   r1   r1   )r.   optionsrq   ZConfigz#444444z#333333r   )activeZactive_header_backgroundZheader_backgroundrQ   rR   )Zlayoutrf   rg   rh   ri   rU   rj   rk   r[   re   paramZwatchr   Zprofiler)r
   ra   rb   rf   rg   rh   ri   rj   rk   rs   Zconfig_panelrt   r   )ra   rb   rH   r   rn   r   ro   r   profiling_tabs   sL    


rx   rS   c                 c   s"  | dkrNddl m} z| }|  W q tyJ   |dd}|  Y q0 n\| dkrft }|  nD| dkrddl}t  d	t	
 j }||}|  n| du rg }|V  | dkr||  nR| dkr|  || n6| dkr|ddd |t|d
  t| dS )ap  
    A context manager which profiles the body of the with statement
    with the supplied profiling engine and returns the profiling object
    in a list.

    Arguments
    ---------
    engine: str
      The profiling engine, e.g. 'pyinstrument', 'snakeviz' or 'memray'

    Returns
    -------
    sessions: list
      A list containing the profiling session.
    rS   r   )Profilerdisabled)Z
async_moder"   r=   Nz/tmprb)rS   ry   startRuntimeErrorr   enabler=   r?   
gettempdiruuiduuid4hexZTracker	__enter__rV   stopdisable__exit__r(   r)   r#   remove)rW   ry   Zprofr=   Ztmp_filetrackerr   r   r   r   profile_ctx   s8    




r   c                    s$   t tstd fdd}|S )a
  
    A decorator which may be added to any function to record profiling
    output.

    Arguments
    ---------
    name: str
      A unique name for the profiling session.
    engine: str
      The profiling engine, e.g. 'pyinstrument', 'snakeviz' or 'memray'
    zProfiler must be given a name.c                    s   t   fdd}|S )Nc                     s|   t jr t jt jv r | i |S t }| i |}W d    n1 sL0    Y  t j f  |7  < t jd |S )Nrd   )r
   ZcurdocZ
_launchingr   rd   rw   trigger)rm   rK   r   ret)rW   funcr.   r   r   wrapped   s    
,z)profile.<locals>.wrapper.<locals>.wrappedr   )r   r   rW   r.   )r   r   wrapper   s    zprofile.<locals>.wrapper)
isinstancestr
ValueError)r.   rW   r   r   r   r   profile   s    
r   )FF)TTr1   )NN)rS   )rS   )rE   r#   r\   r?   r   
contextlibr   ZcProfiler   	functoolsr   r   utilr   r
   r   r0   rM   r[   re   rx   r   r   r   r   r   r   <module>   s&   

. 
3/