o
    i                     @   s   d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZmZ dd	lmZ d
dlmZ G dd deeZdS )zgA MultiTerminalManager for use in the notebook webserver
- raises HTTPErrors
- creates REST API models
    N)	timedelta)utcnow	isoformat)NamedTermManager)web)IOLoopPeriodicCallback)Integervalidate)LoggingConfigurable   ) TERMINAL_CURRENTLY_RUNNING_TOTALc                       s   e Zd ZdZdZdZeddddZdZeedd	dZ	 fd
dZ
dd Zdd Zdd Zdd Zdd Zd$ fdd	Zdd Zdd Zdd Zdd Zd d! Zd"d# Z  ZS )%TerminalManagerz  NFr   TzTimeout (in seconds) in which a terminal has been inactive and ready to be culled.
        Values of 0 or lower disable culling.)confighelpi,  z_The interval (in seconds) on which to check for terminals exceeding the inactive timeout value.c                    s   t  j|i | d S N)super__init__)selfargskwargs	__class__ \/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/terminal/terminalmanager.pyr   )   s   zTerminalManager.__init__c                 C   s   |   \}}| ||S )Create a new terminal.)new_named_terminal_finish_creater   nametermr   r   r   create,   s   zTerminalManager.createc                 C   s4   || j v rtdd| d| |}| ||S )r   i  zA terminal with name 'z' already exists.)	terminalsr   	HTTPErrorget_terminalr   r   r   r   r   create_with_name1   s   

z TerminalManager.create_with_namec                 C   s&   t  |_| |}t  |   |S r   )r   last_activityget_terminal_modelr   inc_initialize_cullerr   r   r    modelr   r   r   r   8   s
   
zTerminalManager._finish_createc                 C   s   |  |}|S )zGet terminal 'name'.r'   )r   r   r+   r   r   r   getD   s   
zTerminalManager.getc                    s&    fdd j D }tt| |S )z$Get a list of all running terminals.c                    s   g | ]}  |qS r   r,   .0r   r   r   r   
<listcomp>K   s    z(TerminalManager.list.<locals>.<listcomp>)r"   r   setlen)r   modelsr   r0   r   listI   s
   zTerminalManager.listc                    s.   |  | t j||dI dH  t  dS )zTerminate terminal 'name'.forceN)_check_terminalr   	terminater   dec)r   r   r7   r   r   r   r9   S   s   
zTerminalManager.terminatec                    s4   dd | j D }|D ]}| j|ddI dH  qdS )zTerminate all terminals.c                 S   s   g | ]}|qS r   r   r.   r   r   r   r1   ^   s    z1TerminalManager.terminate_all.<locals>.<listcomp>Tr6   N)r"   r9   )r   termsr    r   r   r   terminate_all\   s
   zTerminalManager.terminate_allc                 C   s(   |  | | j| }|t|jd}|S )zuReturn a JSON-safe dict representing a terminal.
        For use in representing terminals in the JSON APIs.
        )r   r&   )r8   r"   r   r&   r*   r   r   r   r'   b   s   

z"TerminalManager.get_terminal_modelc                 C   s    || j vrtdd| dS )z9Check a that terminal 'name' exists and raise 404 if not.i  zTerminal not found: N)r"   r   r#   )r   r   r   r   r   r8   n   s   
zTerminalManager._check_terminalc                 C   s   | j s=| jdkr=| jdu r=t }| jdkr$| jd| j| j | j| _t	| j
d| j | _| jd| j| j | j  d| _ dS )zStart culler if 'cull_inactive_timeout' is greater than zero.
        Regardless of that value, set flag that we've been here.
        r   NzKInvalid value for 'cull_interval' detected (%s) - using default value (%s).i  zICulling terminals with inactivity > %s seconds at %s second intervals ...T)_initialized_cullercull_inactive_timeout_culler_callbackr   currentcull_intervallogwarningcull_interval_defaultr   _cull_terminalsinfostart)r   loopr   r   r   r)   s   s    



z"TerminalManager._initialize_cullerc                    sz   | j d| j| j t| jD ]*}z
| |I d H  W q ty: } z| j d| d|  W Y d }~qd }~ww d S )NzCPolling every %s seconds for terminals inactive for > %s seconds...zPThe following exception was encountered while checking the activity of terminal z: )	rB   debugrA   r>   r5   r"   _cull_inactive_terminal	Exception	exception)r   r   er   r   r   rE      s   zTerminalManager._cull_terminalsc                    s   z| j | }W n
 ty   Y d S w | jd||j t|drMt }||j }|t| jdk}|rOt	|
 }| jd|| | j|ddI d H  d S d S d S )Nzname=%s, last_activity=%sr&   )secondsz6Culling terminal '%s' due to %s seconds of inactivity.Tr6   )r"   KeyErrorrB   rI   r&   hasattrr   r   r>   inttotal_secondsrC   r9   )r   r   r    dt_nowdt_inactiveis_time
inactivityr   r   r   rJ      s"   

z'TerminalManager._cull_inactive_terminal)F)__name__
__module____qualname____doc__r?   r=   r	   r>   rD   rA   r   r!   r%   r   r-   r5   r9   r<   r'   r8   r)   rE   rJ   __classcell__r   r   r   r   r      s0    
	r   )rZ   warningsdatetimer   notebook._tzr   r   	terminador   tornador   tornado.ioloopr   r   	traitletsr	   r
   traitlets.configr   prometheus.metricsr   r   r   r   r   r   <module>   s    