o
    ^i                     @  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 )new_named_terminalr   last_activityget_terminal_modelRUNNING_TOTALinc_initialize_culler)selfr   nametermmodelr   r   c/home/ubuntu/hpml_nyu/venv/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    r5   K   s   
zTerminalManager.terminatec                   s.   t | j}|D ]}| j|ddI dH  qdS )zTerminate all terminals.Tr2   N)r.   r*   r5   )r   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   )r3   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    r3   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)	rA   debugr@   r=   r.   r*   _cull_inactive_terminal	Exception	exception)r   r   er   r   r    rD      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.Tr2   )r*   KeyErrorrA   rH   r   hasattrr   r   r=   inttotal_secondsrB   r5   )r   r   r   dt_nowdt_inactiveis_time
inactivityr   r   r    rI      s&   

z'TerminalManager._cull_inactive_terminalptywclientsr   c                 C  s   t  |_dS )zThe pre-pty read hook.N)r   r   )r   rV   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/   r0   r   r1   )r   r1   )r   r"   r   r1   )rV   r   r   r1   )__name__
__module____qualname____doc__r>   r<   r   r=   rC   r@   r!   r%   r.   r5   r:   r   r3   r   rD   rI   rW   __classcell__r   r   r7   r    r      s4    
	


	




r   )r[   
__future__r   typingtdatetimer   jupyter_server._tzr   r   jupyter_server.prometheusr   terminado.managementr   r   tornador	   tornado.ioloopr
   r   	traitletsr   traitlets.configr    TERMINAL_CURRENTLY_RUNNING_TOTALr   Dictr"   Anyr   r   r   r   r   r    <module>   s    