
    Yf7                     6   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	m
Z
mZmZ d dlmZmZmZ d dlmZmZ ddlmZ d	d
lmZ dadj3                         Zej7                  dd      j7                  dd      j7                  dd      Zd Ze G d de	             Zy)    N)parse)get_ipython)Magicsline_cell_magicmagics_classno_var_expand)argumentmagic_argumentsparse_argstring)IFramedisplay   )Profiler   )PrePostAstTransformerz
To enable asyncio mode, use <pre>%%pyinstrument --async_mode=enabled</pre><br>
Note that due to IPython limitations this will run in a separate thread!
z<pre>`z</pre>z<br>
c                      t         S )zt
    Allows the code inserted into the cell to access the pyinstrument Profiler
    instance, to start/stop it.
    )_active_profiler     X/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pyinstrument/magic/magic.py_get_active_profilerr      s
    
 r   c                        e Zd Z fdZ e        ededd       eddd	       ed
ddd	       ededd       ededd      e	e
dd                                                        Zd Z xZS )PyinstrumentMagicc                     t         |   |       t        d      | _        t        d      | _        t        | j                  | j                        | _        y )Nz[
from pyinstrument.magic.magic import _get_active_profiler; _get_active_profiler().start()
z
_get_active_profiler().stop())super__init__r   prepostr   _transformer)selfshell	__class__s     r   r   zPyinstrumentMagic.__init__#   sE    k
 ;<	1$((DIIFr   z
--intervalgMbP?zThe minimum time, in seconds, between each stack sample. See: https://pyinstrument.readthedocs.io/en/latest/reference.html#pyinstrument.Profiler.interval)typedefaulthelpz--async_modedisabledzConfigures how this Profiler tracks time in a program that uses async/await. See: https://pyinstrument.readthedocs.io/en/latest/reference.html#pyinstrument.Profiler.async_mode)r&   r'   z--heightz-hi  zOutput heightz
--timelineFzShow output timeline viewcode*z.When used as a line magic, the code to profile)r%   nargsr'   c                    t        | j                  |      }t               }|st        d      |xs |}|syt        r$t        j
                  rt        j                          | j                  |j                  v r%|j                  j                  | j                         t        |j                  |j                        a|j                  j                  | j                         |j                  dk(  r|j                  |      }n| j                  ||      }|j                  j                  | j                         |j                  dk(  rV|j                   rJt#        |j                   t              r0dt%        |j                         v rt'        t(        t*        dd       yt        j-                  |j.                  	      }t1        d
t2        j4                  j7                  |      z   d|j8                  dg      }t        j;                  |j.                  	      }	t'        |j=                         |	dd       t        j
                  rJ day)a$  
        Run a cell with the pyinstrument statistical profiler.

        Converts the line/cell's AST to something like:
            try:
                profiler.start()
                run_code
            finally:
                profiler.stop()
            profiler.output_html()
        z#couldn't get ipython shell instanceN)interval
async_moder(   zevent loop is already running)
text/plain	text/htmlT)raw)timelinezdata:text/html, z100%zstyle="resize: vertical")srcwidthheightextras)r0   r/   )r   pyinstrumentr   RuntimeErrorr   
is_runningstopr!   ast_transformersremover   r-   r.   appendrun_cellrun_cell_asyncerror_in_exec
isinstancestrr   _ASYNCIO_TEXT_WARNING_ASYNCIO_HTML_WARNINGoutput_htmlr2   r   urllibr   quoter5   output_text_repr_html_)
r"   linecellargsipr)   cell_resulthtml	as_iframeas_texts
             r   r7   zPyinstrumentMagic.pyinstrument,   s   Z t00$7]DEE|t  0 ; ;!!# 3 33&&t'8'89#T]]tW
""4#4#45??j(++d+K--b$7K
""4#4#45 OOz)));44lC/3{7P7P3QQ "7!6  ++T]]+C"V\\%7%7%==;;./	
	 #...Fi335WMSWX#....r   c                 &   t        j                         }t        j                         }	 t        j                  |j
                        j                          t        j                  |       |j                  |      }t        j                  ||      }|j                         j                  |j                  |j                         t        j                  |       S # |j                  |j                         t        j                  |       w xY w)N)target)asyncioget_event_loopnew_event_loop	threadingThreadrun_foreverstartset_event_loopr?   run_coroutine_threadsaferesultcall_soon_threadsafer:   )r"   rM   r)   old_looploopcorofutures          r   r?   z PyinstrumentMagic.run_cell_async   s     ))+%%'	-D$4$45;;=""4($$T*D55dDAF==?))%%dii0""8, %%dii0""8,s   BC 2D)N)__name__
__module____qualname__r   r
   r	   floatboolrB   r   r   r7   r?   __classcell__)r$   s   @r   r   r   !   s    G  i	  
 	 (	 =	 D    @D L-r   r   )rT   rW   urllib.parserF   astr   IPythonr   IPython.core.magicr   r   r   r   IPython.core.magic_argumentsr	   r
   r   IPython.displayr   r    r   _utilsr   r   striprD   replacerC   r   r   r   r   r   <module>rs      s         S S S S +  )  
EG 
 !!'3/77#FNNvW[\ 
 A- A- A-r   