o
    i                     @   s   d 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 ddlmZmZ ddlmZ dd	lmZ ejZG d
d dee	ZdS )zgA MultiTerminalManager for use in the notebook webserver
- raises HTTPErrors
- creates REST API models
    )	timedelta)	isoformatutcnow)metrics)NamedTermManager)web)IOLoopPeriodicCallback)Integer)LoggingConfigurablec                       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	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dd Z  ZS )!TerminalManagerz8A MultiTerminalManager for use in the notebook webserverNFr   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                 K   s:   | j di |\}}t |_| |}t  |   |S )zCreate a new terminal.N )new_named_terminalr   last_activityget_terminal_modelRUNNING_TOTALinc_initialize_culler)selfkwargsnametermmodelr   r   c/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server_terminals/terminalmanager.pycreate,   s   
zTerminalManager.createc                 C   s   |  |}|S )zGet terminal 'name'.r   )r   r   r   r   r   r   get:   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>A   s    z(TerminalManager.list.<locals>.<listcomp>)	terminalsr   setlen)r   modelsr   r    r   list?   s   zTerminalManager.listc                    s.   |  | t j||dI dH  t  dS )zTerminate terminal 'name'.forceN)_check_terminalsuper	terminater   dec)r   r   r(   	__class__r   r   r+   G   s   
zTerminalManager.terminatec                    s.   t | j}|D ]}| j|ddI dH  qdS )zTerminate all terminals.Tr'   N)r&   r"   r+   )r   termsr   r   r   r   terminate_allP   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   )r)   r"   r   r   )r   r   r   r   r   r   r   r   V   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: %sN)r"   r   	HTTPError)r   r   r   r   r   r)   b   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   _r   r   r   r   g   s(   



z"TerminalManager._initialize_cullerc                    sv   | j d| j| j t| jD ](}z
| |I d H  W q ty8 } z| j d	|| W Y d }~qd }~ww d S )NzCPolling every %s seconds for terminals inactive for > %s seconds...zVThe following exception was encountered while checking the activity of terminal {}: {})
r7   debugr6   r3   r&   r"   _cull_inactive_terminal	Exception	exceptionformat)r   r   er   r   r   r:      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.Tr'   )r"   KeyErrorr7   r>   r   hasattrr   r   r3   inttotal_secondsr8   r+   )r   r   r   dt_nowdt_inactiveis_time
inactivityr   r   r   r?      s&   

z'TerminalManager._cull_inactive_terminalc                 C   s   t  |_dS )zThe pre-pty read hook.N)r   r   )r   ptywclientsr   r   r   pre_pty_read_hook   s   z!TerminalManager.pre_pty_read_hook)F)__name__
__module____qualname____doc__r4   r2   r
   r3   r9   r6   r   r   r&   r+   r0   r   r)   r   r:   r?   rN   __classcell__r   r   r-   r   r      s4    		r   N)rR   datetimer   jupyter_server._tzr   r   jupyter_server.prometheusr   terminado.managementr   tornador   tornado.ioloopr   r	   	traitletsr
   traitlets.configr    TERMINAL_CURRENTLY_RUNNING_TOTALr   r   r   r   r   r   <module>   s    