a
    ø£BeX  ã                   @   sº   d Z ddlZddl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 dd	lmZmZ d
ZG dd„ de	jeƒZG dd„ deƒZG dd„ deƒZdefdefdefgZdS )z(Tornado handlers for api specifications.é    N)ÚDictÚList)Úensure_async)Úweb)Ú	isoformatÚutcfromtimestamp)Ú
authorizedé   )Ú
APIHandlerÚJupyterHandlerÚapic                   @   s6   e Zd ZdZeZdd„ Zeje	dd„ ƒƒZ
dd„ ZdS )	ÚAPISpecHandlerz A spec handler for the REST API.c                 C   s   t jj| tj t¡d� dS )z Initialize the API spec handler.)ÚpathN)r   ÚStaticFileHandlerÚ
initializeÚosr   ÚdirnameÚ__file__©Úself© r   úg/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/services/api/handlers.pyr      s    zAPISpecHandler.initializec                 C   s   | j  d¡ tj | d¡S )zGet the API spec.z+Serving api spec (experimental, incomplete)zapi.yaml)ÚlogÚwarningr   r   Úgetr   r   r   r   r      s    zAPISpecHandler.getc                 C   s   dS )zGet the content type.ztext/x-yamlr   r   r   r   r   Úget_content_type#   s    zAPISpecHandler.get_content_typeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚAUTH_RESOURCEÚauth_resourcer   r   Úauthenticatedr   r   r   r   r   r   r   r      s   r   c                   @   s*   e Zd ZdZeZdZeje	dd„ ƒƒZ
dS )ÚAPIStatusHandlerzAn API status handler.Fc                 Ã   sx   | j  dtdƒ¡}t|ƒ}t| j ¡ ƒI dH }tdd„ |D ƒƒ}t| j 	¡ ƒ}||t
|ƒ|dœ}|  tj|dd�¡ dS )	zGet the API status.Ústartedr   Nc                 s   s   | ]}|d  V  qdS )ÚconnectionsNr   )Ú.0Úkr   r   r   Ú	<genexpr>7   ó    z'APIStatusHandler.get.<locals>.<genexpr>)r$   Úlast_activityÚkernelsr%   T)Ú	sort_keys)Úsettingsr   r   r   r   Zkernel_managerZlist_kernelsÚsumZapplicationr*   ÚlenÚfinishÚjsonÚdumps)r   r$   r+   Ztotal_connectionsr*   Úmodelr   r   r   r   .   s    üzAPIStatusHandler.getN)r   r   r   r   r    r!   Z_track_activityr   r"   r   r   r   r   r   r   r#   (   s   r#   c                   @   s   e Zd ZdZejdd„ ƒZdS )ÚIdentityHandlerz%Get the current user's identity modelc              
   C   s$  |   dd¡}d|›�}|rtzt |¡}W n2 tyZ } zt d|¡|‚W Y d}~n
d}~0 0 t|tƒsxt d|¡‚ni }i }| j}| 	¡ D ]n\}}t|t
ƒr¸t|tƒr¸tdd„ |D ƒƒsÄt d|¡‚g  }	||< |D ]"}
| jj| |||
d�rÔ|	 |
¡ qÔqŠ| j |¡}||d	œ}|  t |¡¡ dS )
zGet the identity model.ÚpermissionsÚ zDpermissions should be a JSON dict of {"resource": ["action",]}, got i�  Nc                 s   s   | ]}t |tƒV  qd S )N)Ú
isinstanceÚstr)r&   Úactionr   r   r   r(   [   r)   z&IdentityHandler.get.<locals>.<genexpr>)ÚuserÚresourcer9   )Úidentityr5   )Zget_argumentr1   ÚloadsÚ
ValueErrorr   Ú	HTTPErrorr7   ÚdictZcurrent_userÚitemsr8   ÚlistÚallZ
authorizerZis_authorizedÚappendZidentity_providerZidentity_modelÚwriter2   )r   Zpermissions_jsonZbad_permissions_msgZpermissions_to_checkÚer5   r:   r;   ÚactionsÚallowedr9   r<   r3   r   r   r   r   E   s:    
$
ÿþýþzIdentityHandler.getN)r   r   r   r   r   r"   r   r   r   r   r   r4   B   s   r4   z/api/spec.yamlz/api/statusz/api/me)r   r1   r   Útypingr   r   Zjupyter_core.utilsr   Útornador   Zjupyter_server._tzr   r   Zjupyter_server.authr   Zbase.handlersr
   r   r    r   r   r#   r4   Zdefault_handlersr   r   r   r   Ú<module>   s    +ý