o
    tf                     @  s   d Z ddlm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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ejeejf ZG dd deeZdS )zgA MultiTerminalManager for use in the notebook webserver
- raises HTTPErrors
- creates REST API models
    )annotationsN)	timedelta)	isoformatutcnow)metrics)NamedTermManagerPtyWithClients)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dZ
d-ddZd.ddZd/d0 fddZd1ddZd-ddZd2d d!Zd1d"d#Zd1d$d%Zd2d&d'Zd3d*d+Z  ZS )4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.kwargst.AnyreturnMODELc                 K  s:   | j di |\}}t |_| |}t  |   |S )zCreate a new terminal.N )Znew_named_terminalr   last_activityget_terminal_modelRUNNING_TOTALinc_initialize_culler)selfr   nametermmodelr   r   q/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyter_server_terminals/terminalmanager.pycreate1   s   
zTerminalManager.creater   strc                 C  s
   |  |S )zGet terminal 'name'.r   r   r   r   r   r   get?   s   
zTerminalManager.getlist[MODEL]c                   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>E   s    z(TerminalManager.list.<locals>.<listcomp>)	terminalsr   setlen)r   modelsr   r'   r   listC   s   zTerminalManager.listforceboolNonec                   s.   |  | t j||dI dH  t  dS )zTerminate terminal 'name'.r.   N)_check_terminalsuper	terminater   dec)r   r   r.   	__class__r   r   r4   K   s   
zTerminalManager.terminatec                   s.   t | j}|D ]}| j|ddI dH  qdS )zTerminate all terminals.Tr1   N)r-   r)   r4   )r   Ztermsr   r   r   r   terminate_allT   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   )r2   r)   r   r   )r   r   r   r   r   r   r   Z   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	   	HTTPErrorr#   r   r   r   r2   e   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   j   s(   



z"TerminalManager._initialize_cullerc                   sr   | j d| j| j t| jD ]&}z
| |I d H  W q ty6 } 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 %s: %s)	r?   debugr>   r;   r-   r)   _cull_inactive_terminal	Exception	exception)r   r   er   r   r   rB      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.Tr1   )r)   KeyErrorr?   rF   r   hasattrr   r   r;   inttotal_secondsr@   r4   )r   r   r   Zdt_nowZdt_inactiveZis_timeZ
inactivityr   r   r   rG      s&   

z'TerminalManager._cull_inactive_terminalptywclientsr   c                 C  s   t  |_dS )zThe pre-pty read hook.N)r   r   )r   rP   r   r   r   pre_pty_read_hook   s   z!TerminalManager.pre_pty_read_hook)r   r   r   r   )r   r!   r   r   )r   r%   )F)r   r!   r.   r/   r   r0   )r   r0   )r   r!   r   r0   )rP   r   r   r0   )__name__
__module____qualname____doc__r<   r:   r   r;   rA   r>   r    r$   r-   r4   r8   r   r2   r   rB   rG   rQ   __classcell__r   r   r6   r   r      s4    
	


	




r   )rU   
__future__r   typingtdatetimer   Zjupyter_server._tzr   r   Zjupyter_server.prometheusr   Zterminado.managementr   r   tornador	   Ztornado.ioloopr
   r   Z	traitletsr   Ztraitlets.configr   Z TERMINAL_CURRENTLY_RUNNING_TOTALr   Dictr!   Anyr   r   r   r   r   r   <module>   s    