
    Yf1                     L    d dl Z d dlmZmZ d dlmZ d dlmZmZ  G d d      Z	y)    N)
HTTPServerBaseHTTPRequestHandler)Thread)AnyOptionalc                   N    e Zd Zedededee   fd       Zedededdfd       Zy)	ScaleneJupyter
start_portend_portreturnc                 
   t        | |dz         D ]U  }	 t        j                  t        j                  t        j                        5 }|j	                  d|f       |cddd       c S  y# 1 sw Y   nxY wf# t
        $ r Y qw xY w)ab  
        Finds an available port within a given range.

        Parameters:
        - start_port (int): the starting port number to search from
        - end_port (int): the ending port number to search up to (inclusive)

        Returns:
        - int: the first available port number found in the given range, or None if no ports are available
            N)rangesocketAF_INETSOCK_STREAMbindOSError)r
   r   portss       W/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/scalene/scalene_jupyter.pyfind_available_portz"ScaleneJupyter.find_available_port   s     *hl3 	D]]6>>63E3EF  !FFB:&   	        s)   2A6A)	A6)A2	.A66	BBr   profile_fnameNc                    	
 ddl m} ddlm}  G fddt              	 G d dt
               G 	 fdd	      } |       }t        |j                  
      

j                           | |d  dd             t        
fd
      j                          dd l	}|j                  d       dd l}|j                          y )Nr   )display)IFramec                   >    e Zd ZdeddfdZdededdfdZd	 fdZy)
6ScaleneJupyter.display_profile.<locals>.RequestHandlercontentr   Nc                     | j                  d       | j                  dd       | j                          | j                  j	                  t        |d             y )N   zContent-typez	text/htmlutf8)send_responsesend_headerend_headerswfilewritebytes)selfr    s     r   _send_responsezEScaleneJupyter.display_profile.<locals>.RequestHandler._send_response'   sE    ""3'  =  "

  w!78    formatargsc                      y)z=overriding log_message to disable all messages from webserverN )r*   r-   r.   s      r   log_messagezBScaleneJupyter.display_profile.<locals>.RequestHandler.log_message-   s    r,   c                 d   | j                   dk(  r7	 t              5 }|j                         }d d d        | j                         y | j                   dk(  r#d| j                  _        | j                  d       y | j                  d       y # 1 sw Y   _xY w# t        $ r t        d       Y y w xY w)N/z&Scalene error: profile file not found.z	/shutdownT   i  )	pathopenreadr+   FileNotFoundErrorprintservershould_shutdownr$   )r*   fr    r   s      r   do_GETz=ScaleneJupyter.display_profile.<locals>.RequestHandler.do_GET1   s    99#H!-0 /A&'ffhG/++G4 YY+-26DKK/&&s+ &&s+/ / - HFGHs'   B BB BB B/.B/r   N)__name__
__module____qualname__strr+   r   r1   r=   )r   s   r   RequestHandlerr   &   s5    9c 9d 9# c d ,r,   rC   c                   "    e Zd ZdZddeddfdZy)4ScaleneJupyter.display_profile.<locals>.MyHTTPServerz)Redefine to check `should_shutdown` flag.poll_intervalr   Nc                 f    d| _         | j                   s| j                          | j                   sy y )NF)r;   handle_request)r*   rF   s     r   serve_foreverzBScaleneJupyter.display_profile.<locals>.MyHTTPServer.serve_foreverC   s,    ',$.. '')	 ..r,   )g      ?)r?   r@   rA   __doc__floatrI   r0   r,   r   MyHTTPServerrE   @   s    ;*5 *4 *r,   rL   c                       e Zd Zd fdZy)4ScaleneJupyter.display_profile.<locals>.local_serverNc                     	 df} |      | _         | j                   j                          y # t        $ r}t        d|       Y d }~y d }~ww xY w)Nr   zserver failure)httpdrI   BaseExceptionr9   )r*   server_addressberL   rC   r   s      r   
run_serverz?ScaleneJupyter.display_profile.<locals>.local_server.run_serverL   sM    &($ZN!-nn!MDJJJ,,.$ *B/s   ,0 	AA

Ar>   )r?   r@   rA   rT   )rL   rC   r   s   r   local_serverrN   K   s     r,   rU   )targetzhttp://localhost:z100%400)srcwidthheightc                  $     j                         S )N)join)server_threads   r   <lambda>z0ScaleneJupyter.display_profile.<locals>.<lambda>]   s    m002 r,      )IPython.core.displayr   IPython.displayr   r   r   r   rT   starttimesleepsysexit)r   r   r   r   rU   
the_serverrc   re   rL   rC   r]   s   ``      @@@r   display_profilezScaleneJupyter.display_profile   s    
 	1*	,3 	,4		*: 		*	 	 "^
j&;&;< 	*4&1N	
 	2399; 	

1
r,   )	r?   r@   rA   staticmethodintr   r   rB   rh   r0   r,   r   r	   r	      s[     s x}  * Fc F# F$ F Fr,   r	   )
r   http.serverr   r   	threadingr   typingr   r   r	   r0   r,   r   <module>rn      s     :   ^ ^r,   