Ë
    :vžj˜Š  ã                  ó.  — d Z ddlmZ ddlZddl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mZ ddlZddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 erddl7m8Z8  G d„ de-«      Z9 G d„ de«      Z: G d„ de1«      Z; G d„ de.«      Z< ejz                  e<«        G d „ d!e	«      Z> G d"„ d#e>«      Z? G d$„ d%e«      Z@ ejz                  e@«       y)&zKernel gateway managers.é    )ÚannotationsN)ÚEmptyÚQueue)ÚThread)Ú	monotonic)ÚTYPE_CHECKINGÚAnyÚcast)ÚAsyncKernelClient)ÚKernelClientABC)ÚKernelSpecManager)ÚKernelManagerABC)Úensure_async)Úweb)Újson_decodeÚjson_encodeÚ
url_escapeÚutf8)ÚDottedObjectNameÚInstanceÚTypeÚdefaulté   )ÚUTCÚutcnow)ÚAsyncMappingKernelManagerÚServerKernelManagerÚemit_kernel_action_event)ÚSessionManager)Úurl_path_joiné   )ÚGatewayClientÚgateway_request)ÚLoggerc                  ó²   ‡ — e Zd ZU dZi Zded<    ed«      d„ «       Z ed«      d„ «       Zˆ fd„Z	d	„ Z
d
d
dœd„Zd„ Zd„ Zdd„Zdd„Zd„ Zdd„Zˆ fd„Zˆ xZS )ÚGatewayMappingKernelManagerz[Kernel manager that supports remote kernels hosted by Jupyter Kernel or Enterprise Gateway.zdict[str, GatewayKernelManager]Ú_kernelsÚkernel_manager_classc                 ó   — y)Nz4jupyter_server.gateway.managers.GatewayKernelManager© ©Úselfs    úp/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/jupyter_server/gateway/managers.pyÚ_default_kernel_manager_classz9GatewayMappingKernelManager._default_kernel_manager_class.   s   € àEó    Úshared_contextc                 ó   — y©NFr*   r+   s    r-   Ú_default_shared_contextz3GatewayMappingKernelManager._default_shared_context2   ó   € àr/   c                óÆ   •— t        ‰| �  di |¤Ž t        t        j                  «       j
                  xs dt        j                  «       j                  xs d«      | _        y)z,Initialize a gateway mapping kernel manager.Ú Nr*   )ÚsuperÚ__init__r    r"   ÚinstanceÚurlÚkernels_endpointÚkernels_url©r,   ÚkwargsÚ	__class__s     €r-   r8   z$GatewayMappingKernelManager.__init__6   sN   ø€ ä‰ÑÑ"˜6Ò"Ü(Ü×"Ñ"Ó$×(Ñ(Ò.¨B´×0FÑ0FÓ0H×0YÑ0YÒ0_Ð]_ó
ˆÕr/   c                óX   — 	 | j                   j                  |«      S # t        $ r Y yw xY w)zCComplete override since we want to be more tolerant of missing keysN)r'   ÚpopÚKeyError)r,   Ú	kernel_ids     r-   Úremove_kernelz)GatewayMappingKernelManager.remove_kernel=   s.   € ð	Ø—=‘=×$Ñ$ YÓ/Ð/øÜò 	Ùð	ús   ‚ �	)¨)N)rC   Úpathc             ‹  óf  K  — | j                   j                  d|› d|› d�«       |€|�| j                  |«      |d<   | j                  | | j                   ¬«      } |j                  dd|i|¤Žƒ d{  –—†  |j
                  }|| j                  |<   | j                  s| j                  «        |S 7 Œ=­w)	a·  Start a kernel for a session and return its kernel_id.

        Parameters
        ----------
        kernel_id : uuid
            The uuid to associate the new kernel with. If this
            is not None, this kernel will be persistent whenever it is
            requested.
        path : API path
            The API path (unicode, '/' delimited) for the cwd.
            Will be transformed to an OS path relative to root_dir.
        z Request start kernel: kernel_id=z, path='Ú'NÚcwd)ÚparentÚlogrC   r*   )	rJ   ÚinfoÚcwd_for_pathÚkernel_manager_factoryÚstart_kernelrC   r'   Ú_initialized_cullerÚinitialize_culler)r,   rC   rE   r>   Úkms        r-   rN   z(GatewayMappingKernelManager.start_kernelD   s´   è ø€ ð 	�‰�‰Ð8¸¸À8ÈDÈ6ÐQRÐSÔTàÐ Ð!1Ø ×-Ñ-¨dÓ3ˆF�5‰Mà×(Ñ(°¸$¿(¹(Ð(ÓCˆØˆb�o‰oÑ<¨	Ð<°VÑ<×<Ð<Ø—L‘Lˆ	Ø#%ˆ�‰�iÑ à×'Ò'Ø×"Ñ"Ô$àÐð 	=ús   ‚A/B1Á1B/Á2>B1c              ƒ  ób   K  — d}| j                  t        |«      «      }|r|j                  }|S ­w)zÄReturn a dictionary of kernel information described in the
        JSON standard model.

        Parameters
        ----------
        kernel_id : uuid
            The uuid of the kernel.
        N)Ú
get_kernelÚstrÚkernel)r,   rC   ÚmodelrQ   s       r-   Úkernel_modelz(GatewayMappingKernelManager.kernel_model`   s0   è ø€ ð ˆØ�_‰_œS ›^Ó,ˆÙØ—I‘IˆEØˆùs   ‚-/c              ‹  óJ  K  — | j                   j                  d| j                  › �«       t        | j                  d¬«      ƒ d{  –—† }t	        |j
                  «      }i }|D ]A  }|d   }|| j                  v sŒ| j                  |   j                  |«      ƒ d{  –—†  |||<   ŒC | j                  j                  «       }g }|D ]   }||vsŒ| j                   j                  d|› d�«       	 | j                  |   j                  «       ƒ d{  –—† }|r|||<   ŒU| j                   j                  d|› d�«       | j                  j                  |d«       |j                  |«       Œ¢ t        |j                  «       «      S 7 �Œ<7 Œè7 Œx# t        j                  $ r d}Y Œ�w xY w­w)	z¨Get a list of running kernels from the Gateway server.

        We'll use this opportunity to refresh the models in each of
        the kernels we're managing.
        zRequest list kernels: ÚGET©ÚmethodNÚidzKernel zH not present in the list of kernels - possibly culled on Gateway server.z6 no longer active - probably culled on Gateway server.)rJ   Údebugr<   r#   r   Úbodyr'   Úrefresh_modelÚcopyÚwarningr   Ú	HTTPErrorrA   ÚappendÚlistÚvalues)	r,   r>   ÚresponseÚkernelsÚkernel_modelsrV   ÚkidÚour_kernelsÚ
culled_idss	            r-   Úlist_kernelsz(GatewayMappingKernelManager.list_kernelso   s•  è ø€ ð 	�‰�‰Ð/°×0@Ñ0@Ð/AÐBÔCÜ(¨×)9Ñ)9À%ÔH×HˆÜ˜hŸm™mÓ,ˆð ˆÛˆEØ˜‘+ˆCØ�d—m‘mÒ#Ø—m‘m CÑ(×6Ñ6°uÓ=×=Ð=Ø%*�˜cÒ"ð	 ð —m‘m×(Ñ(Ó*ˆØˆ
ÛˆCØ˜-Ò'à—‘× Ñ Ø˜c˜UÐ"jÐkôð!ð #'§-¡-°Ñ"4×"BÑ"BÓ"D×D�Eñ Ø).�M #Ò&à—H‘H×$Ñ$Ø! # Ð&\Ð]ôð —M‘M×%Ñ% c¨4Ô0Ø×%Ñ% cÕ*ð; ô< �M×(Ñ(Ó*Ó+Ð+ðW Iùð >øð0 EùÜ—}‘}ò !Ø ’Eð!üsg   ‚AF#ÁFÁ3F#Á:!F#ÂFÂ0F#ÃF#Ã- FÄFÄFÄA0F#ÆF#ÆFÆF ÆF#ÆF Æ F#c              ƒ  óœ   K  — | j                  |«      }t        |j                  ||¬«      «      ƒ d{  –—†  | j                  |«       y7 Œ­w)aX  Shutdown a kernel by its kernel uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to shutdown.
        now : bool
            Shutdown the kernel immediately (True) or gracefully (False)
        restart : bool
            The purpose of this shutdown is to restart the kernel (True)
        )ÚnowÚrestartN)rS   r   Úshutdown_kernelrD   )r,   rC   rn   ro   rQ   s        r-   rp   z+GatewayMappingKernelManager.shutdown_kernel£   sH   è ø€ ð �_‰_˜YÓ'ˆÜ˜2×-Ñ-°#¸wÐ-ÓGÓH×HÐHØ×Ñ˜9Õ%ð 	Iús   ‚1A³A
´Ac              ‹  ó|   K  — | j                  |«      }t         |j                  dd|i|¤Ž«      ƒ d{  –—†  y7 Œ­w)zšRestart a kernel by its kernel uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to restart.
        rn   Nr*   )rS   r   Úrestart_kernel)r,   rC   rn   r>   rQ   s        r-   rr   z*GatewayMappingKernelManager.restart_kernel³   s:   è ø€ ð �_‰_˜YÓ'ˆÜÐ,˜2×,Ñ,Ñ?°Ð?¸Ñ?Ó@×@Ò@ús   ‚2<´:µ<c              ‹  ót   K  — | j                  |«      }t        |j                  «       «      ƒ d{  –—†  y7 Œ­w)zžInterrupt a kernel by its kernel uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to interrupt.
        N)rS   r   Úinterrupt_kernel)r,   rC   r>   rQ   s       r-   rt   z,GatewayMappingKernelManager.interrupt_kernel¾   s-   è ø€ ð �_‰_˜YÓ'ˆÜ˜2×.Ñ.Ó0Ó1×1Ò1ús   ‚.8°6±8c              ƒ  óÒ   K  — t        | j                  «      }|D ]G  }| j                  |«      }t        |j	                  |¬«      «      ƒ d{  –—†  | j                  |«       ŒI y7 Œ­w)zShutdown all kernels.)rn   N)rd   r'   rS   r   rp   rD   )r,   rn   ÚkidsrC   rQ   s        r-   Úshutdown_allz(GatewayMappingKernelManager.shutdown_allÉ   sY   è ø€ ä�D—M‘MÓ"ˆÛˆIØ—‘ Ó+ˆBÜ˜r×1Ñ1°cÐ1Ó:Ó;×;Ð;Ø×Ñ˜yÕ)ñ à;ús   ‚A
A'ÁA%ÁA'c              ƒ  ór   •K  — | j                  «       ƒ d{  –—†  t        ‰| �	  «       ƒ d{  –—†  y7 Œ7 Œ­w)z@Override cull_kernels, so we can be sure their state is current.N)rl   r7   Úcull_kernels©r,   r?   s    €r-   ry   z(GatewayMappingKernelManager.cull_kernelsÑ   s3   øè ø€ à×ÑÓ!×!Ð!Ü‰gÑ"Ó$×$Ñ$ð 	"øØ$ús   ƒ7—3˜7­5®7µ7©FF©F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   Ú__annotations__r   r.   r3   r8   rD   rN   rW   rl   rp   rr   rt   rw   ry   Ú__classcell__©r?   s   @r-   r&   r&   (   s‡   ø… Ùeð 13€HÐ-Ó2áÐ#Ó$ñFó %ðFñ ÐÓñó ðô
òð /3¸ô ò8ò2,óh&ó 	Aò	2ó*÷%ð %r/   r&   c                  óX   ‡ — e Zd ZdZˆ fd„Zed„ «       Zd„ Zd
d„Zd„ Z	d„ Z
d„ Zd	„ Zˆ xZS )ÚGatewayKernelSpecManagerzA gateway kernel spec manager.c                ó‚  •— t        ‰| �  di |¤Ž t        t        j                  «       j
                  xs dt        j                  «       j                  «      }t        j                  |«      | _	        t        t        j                  «       j
                  xs dt        j                  «       j                  «      | _        y)z)Initialize a gateway kernel spec manager.r6   Nr*   )r7   r8   r    r"   r9   r:   Úkernelspecs_endpointr…   Ú_get_endpoint_for_user_filterÚbase_endpointÚkernelspecs_resource_endpointÚbase_resource_endpoint)r,   r>   r‰   r?   s      €r-   r8   z!GatewayKernelSpecManager.__init__Ú   s‘   ø€ ä‰ÑÑ"˜6Ò"Ü%Ü×"Ñ"Ó$×(Ñ(Ò.¨B´×0FÑ0FÓ0H×0]Ñ0]ó
ˆô 6×SÑSÐTaÓbˆÔÜ&3Ü×"Ñ"Ó$×(Ñ(Ò.¨BÜ×"Ñ"Ó$×BÑBó'
ˆÕ#r/   c                óV   — t         j                  j                  d«      }|r| › d|› �S | S )z#Get the endpoint for a user filter.ÚKERNEL_USERNAMEz?user=)ÚosÚenvironÚget)Údefault_endpointÚkernel_users     r-   rˆ   z6GatewayKernelSpecManager._get_endpoint_for_user_filterç   s2   € ô —j‘j—n‘nÐ%6Ó7ˆÙØ&Ð' v¨k¨]Ð;Ð;ØÐr/   c           	     óz  — | j                   si S |d   }|D ]¢  }||   d   }|D ]“  }||   }t        j                  |dd¬«      }t        |«      dkD  sŒ/t	        | j                   j
                  d|d   «      }||d   |   d   |<   ||k7  sŒg| j                  j                  d|› d|d   |   d   |   › �«       Œ• Œ¤ |S )z×Helper method that replaces any gateway base_url with the server's base_url
        This enables clients to properly route through jupyter_server to a gateway
        for kernel resources such as logo files
        ÚkernelspecsÚ	resourcesz/kernelspecs/r!   )ÚsepÚmaxsplitz'Replaced original kernel resource path z with new path )rI   rT   ÚrsplitÚlenr    Úbase_urlrJ   r]   )	r,   Úkernel_specsr”   Úkernel_namer•   Úresource_nameÚoriginal_pathÚsplit_eg_base_urlÚnew_paths	            r-   Ú"_replace_path_kernelspec_resourcesz;GatewayKernelSpecManager._replace_path_kernelspec_resourcesï   s÷   € ð
 �{Š{ØˆIØ" =Ñ1ˆÛ&ˆKØ# KÑ0°Ñ=ˆIÛ!*�Ø )¨-Ñ 8�Ü$'§J¡J¨}À/Ð\] JÓ$^Ð!ÜÐ(Ó)¨AÓ-Ü,ØŸ™×,Ñ,¨mÐ=NÈqÑ=Qó �Hð \d�L Ñ/°Ñ<¸[ÑIÈ-ÑXØ$¨Ó0ØŸ™Ÿ™ØEÀmÀ_ð U$Ø$0°Ñ$?ÀÑ$LÈ[Ñ$YÐZgÑ$hÐ#iðkõñ "+ð 'ð Ðr/   c                ó\   — |rt        | j                  t        |«      «      S | j                  S )z�Builds a url for the kernels endpoint
        Parameters
        ----------
        kernel_name : kernel name (optional)
        )r    r‰   r   )r,   rœ   s     r-   Ú_get_kernelspecs_endpoint_urlz6GatewayKernelSpecManager._get_kernelspecs_endpoint_url  s+   € ñ Ü  ×!3Ñ!3´ZÀÓ5LÓMÐMà×!Ñ!Ð!r/   c              ƒ  óR  K  — | j                  «       ƒ d{  –—† }| j                  si S | j                  j                  }|j                  d«      }||j                  k7  r3| j
                  j                  d|› d|j                  › d�«       ||_        |j                  d«      }|S 7 ŒŽ­w)z,Get all of the kernel specs for the gateway.Nr   z'Default kernel name on Gateway server (z ) differs from Notebook server (z').  Updating to Gateway server's value.r”   )Úlist_kernel_specsrI   Úkernel_managerr�   Údefault_kernel_namerJ   rK   )r,   Úfetched_kspecsrQ   Úremote_default_kernel_nameÚremote_kspecss        r-   Úget_all_specsz&GatewayKernelSpecManager.get_all_specs  s­   è ø€ à#×5Ñ5Ó7×7ˆð �{Š{ØˆIØ�[‰[×'Ñ'ˆØ%3×%7Ñ%7¸	Ó%BÐ"Ø%¨×)?Ñ)?Ò?Ø�H‰H�M‰MØ9Ð:TÐ9Uð V$Ø$&×$:Ñ$:Ð#;Ð;bðdôð &@ˆBÔ"à&×*Ñ*¨=Ó9ˆØÐð% 8ús   ‚B'–B%—BB'c              ƒ  óä   K  — | j                  «       }| j                  j                  d|› �«       t        |d¬«      ƒ d{  –—† }t	        |j
                  «      }| j                  |«      }|S 7 Œ,­w)zGet a list of kernel specs.zRequest list kernel specs at: rY   rZ   N)r£   rJ   r]   r#   r   r^   r¡   )r,   Úkernel_spec_urlrf   r›   s       r-   r¥   z*GatewayKernelSpecManager.list_kernel_specs)  sf   è ø€ à×<Ñ<Ó>ˆØ�‰�‰Ð7¸Ð7HÐIÔJÜ(¨ÀÔG×GˆÜ" 8§=¡=Ó1ˆØ×>Ñ>¸|ÓLˆØÐð Hús   ‚?A0ÁA.Á-A0c              ‹  ó�  K  — | j                  t        |«      ¬«      }| j                  j                  d|› �«       	 t	        |d¬«      ƒ d{  –—† }t        |j                  «      }|S 7 Œ# t        j                  $ rE}|j                  dk(  r0d|› dt        j                  «       j                  › �}t        |«      d‚‚ d}~ww xY w­w)	zŽGet kernel spec for kernel_name.

        Parameters
        ----------
        kernel_name : str
            The name of the kernel.
        )rœ   zRequest kernel spec at: rY   rZ   Né”  zkernelspec z! not found on Gateway server at: )r£   rT   rJ   r]   r#   r   r^   r   rb   Ústatus_coder"   r9   r:   rB   )r,   rœ   r>   r­   rf   Úkernel_specÚerrorÚmsgs           r-   Úget_kernel_specz(GatewayKernelSpecManager.get_kernel_spec2  s¿   è ø€ ð ×<Ñ<ÌÈ[ÓIYÐ<ÓZˆØ�‰�‰Ð1°/Ð1BÐCÔDð	5Ü,¨_ÀUÔK×KˆHô & h§m¡mÓ4ˆKàÐð LùÜ�}‰}ò 	Ø× Ñ  CÒ'ð $ K =Ð0QÔR_×RhÑRhÓRj×RnÑRnÐQoÐp�Ü˜s“m¨Ð-àûð	üs;   ‚:C½A+ ÁA)ÁA+ ÁCÁ)A+ Á+CÁ>A B>Â>CÃCc              ƒ  óR  K  — t        | j                  t        |«      t        |«      «      }| j                  j	                  d|› d|› �«       	 t        |d¬«      ƒ d{  –—† }|j                  }|S 7 Œ# t        j                  $ r}|j                  dk(  rd}n‚ Y d}~|S d}~ww xY w­w)zÎGet kernel spec for kernel_name.

        Parameters
        ----------
        kernel_name : str
            The name of the kernel.
        path : str
            The name of the desired resource
        zRequest kernel spec resource 'z' at: rY   rZ   Nr¯   )
r    r‹   rT   rJ   r]   r#   r^   r   rb   r°   )r,   rœ   rE   Úkernel_spec_resource_urlrf   Úkernel_spec_resourcer²   s          r-   Úget_kernel_spec_resourcez1GatewayKernelSpecManager.get_kernel_spec_resourceK  s¯   è ø€ ô $1Ø×'Ñ'¬¨[Ó)9¼3¸t»9ó$
Ð ð 	�‰�‰Ð7¸°v¸VÐD\ÐC]Ð^Ô_ð	1Ü,Ð-EÈeÔT×TˆHð $,§=¡=Ð Ø#Ð#ð UùÜ�}‰}ò 	Ø× Ñ  CÒ'Ø'+Ñ$àó %ð
 $Ð#ûð	üsB   ‚AB'ÁA3 ÁA1ÁA3 Á#B'Á1A3 Á3B$ÂBÂB'ÂB$Â$B'©N)r}   r~   r   r€   r8   Ústaticmethodrˆ   r¡   r£   r«   r¥   r´   r¸   r‚   rƒ   s   @r-   r…   r…   ×   s=   ø„ Ù(ô
ð ñ ó ð òó2	"òò,òö2$r/   r…   c                  ó(   — e Zd ZdZ ed«      Zdd„Zy)ÚGatewaySessionManagerzA gateway session manager.z;jupyter_server.gateway.managers.GatewayMappingKernelManagerc              ƒ  ór   K  — d}	 | j                   j                  |«      }|du S # t        $ r Y |du S w xY w­w)zRChecks if the kernel is still considered alive and returns true if it's not found.N)r¦   rS   Ú	Exception)r,   rC   rQ   s      r-   Úkernel_culledz#GatewaySessionManager.kernel_culledj  sQ   è ø€ à*.ˆð	ð ×$Ñ$×/Ñ/°	Ó:ˆBð �TˆzÐøô ò 	àØ�TˆzÐð	üs   ‚7†% ¡7¥	4®7³4´7N)rC   rT   ÚreturnÚbool)r}   r~   r   r€   r   r¦   r¿   r*   r/   r-   r¼   r¼   e  s   „ Ù$áÐ[Ó\€Nôr/   r¼   c                  ó  ‡ — e Zd ZU dZdZded<   dZ ed«      d„ «       Zˆ fd„Z	e
d„ «       Z ed	«      Z ed	¬
«      Zd„ Zdd„Z ed¬«      d„ «       Z ed¬«      dd„«       Z ed¬«      d„ «       Z ed¬«      d„ «       Zd„ Zdd„Zˆ xZS )ÚGatewayKernelManagerz6Manages a single kernel remotely via a Gateway Server.Nú
str | NonerC   Úcache_portsc                 ó   — yr2   r*   r+   s    r-   Ú_default_cache_portsz)GatewayKernelManager._default_cache_ports‚  r4   r/   c                ó
  •— t        ‰| �  di |¤Ž t        t        j                  «       j
                  xs dt        j                  «       j                  «      | _        |  dx| _        | _	        d| _
        t        «       | _        y)z&Initialize the gateway kernel manager.r6   NÚstartingr*   )r7   r8   r    r"   r9   r:   r;   r<   rU   rC   Úexecution_stater   Úlast_activityr=   s     €r-   r8   zGatewayKernelManager.__init__†  sm   ø€ ä‰ÑÑ"˜6Ò"Ü(Ü×"Ñ"Ó$×(Ñ(Ò.¨B´×0FÑ0FÓ0H×0YÑ0Yó
ˆÔñ 	Ø'+Ð+ˆŒ�d”nà)ˆÔÜ#›XˆÕr/   c                ó   — | j                   duS )z/Has a kernel been started that we are managing.N)rU   r+   s    r-   Ú
has_kernelzGatewayKernelManager.has_kernel’  s   € ð �{‰{ $Ð&Ð&r/   z3jupyter_server.gateway.managers.GatewayKernelClient)Úklassc                óè   — i }|j                  | j                  d¬«      «       |j                  | j                  | dœ«       | j                  |d<   |j                  |«        | j                  di |¤ŽS )z3Create a client configured to connect to our kernelT)Úsession)Úconnection_filerI   rC   r*   )ÚupdateÚget_connection_inforÑ   rC   Úclient_factory)r,   r>   Úkws      r-   ÚclientzGatewayKernelManager.clientž  sq   € àˆØ
�	‰	�$×*Ñ*°4Ð*Ó8Ô9Ø
�	‰	à#'×#7Ñ#7Øñô	
ð Ÿ.™.ˆˆ;‰ð 	�	‰	�&ÔØ"ˆt×"Ñ"Ñ( RÑ(Ð(r/   c              ƒ  óô  K  — |€{| j                   j                  d| j                  z  «       	 t        | j                  d¬«      ƒ d{  –—† }t	        |j
                  «      }| j                   j                  d|z  «       |r�t        j                  j                  |d   d	«      j                  t        ¬
«      | _        |d   | _        t!        | j"                  t$        «      r/t'        |d   «      | j"                  j(                  | j*                  <   || _        |S 7 ŒÑ# t        j                  $ rF}|j                  dk(  r+| j                   j                  d| j                  z  «       d}n‚ Y d}~�Œd}~ww xY w­w)záRefresh the kernel model.

        Parameters
        ----------
        model : dict
            The model from which to refresh the kernel.  If None, the kernel
            model is fetched from the Gateway server.
        NzRequest kernel at: %srY   rZ   r¯   zKernel not found at: %szKernel retrieved: %srË   z%Y-%m-%dT%H:%M:%S.%fZ)ÚtzinforÊ   Úconnections)rJ   r]   Ú
kernel_urlr#   r   r^   r   rb   r°   ra   ÚdatetimeÚstrptimeÚreplacer   rË   rÊ   Ú
isinstancerI   r   ÚintÚ_kernel_connectionsrC   rU   )r,   rV   rf   r²   s       r-   r_   z"GatewayKernelManager.refresh_model®  s<  è ø€ ð ˆ=Ø�H‰H�N‰NÐ2°T·_±_ÑDÔEð
3Ü!0°·±ÈÔ!O×O�ô $ H§M¡MÓ2�Ø�H‰H�N‰NÐ1°EÑ9Ô:áÜ!)×!2Ñ!2×!;Ñ!;Ø�oÑ&Ð(?ó"ç‰gœSˆgÓ!ð Ôð $)Ð):Ñ#;ˆDÔ Ü˜$Ÿ+™+Ô'@ÔAô
 CFÀeÈMÑFZÓB[�—‘×/Ñ/°·±Ñ?àˆŒØˆð3 Pùä—=‘=ò Ø×$Ñ$¨Ò+Ø—H‘H×$Ñ$Ð%>ÀÇÁÑ%PÔQØ ‘Eàõ ûðüsA   ‚+E8®D ÁDÁ	D ÁCE8ÄD ÄE5Ä/;E0Å*E8Å0E5Å5E8zKernel {kernel_id} was started.)Úsuccess_msgc              ‹  óÀ  K  — |j                  d«      }|�€8|j                  dd«      }| j                  j                  d| j                  z  «       t        j
                  j                  d«      €Qt        j                  «       j                  r3t        j                  «       j                  xs dt        j
                  d<   t        j
                  j                  «       }|j                  |j                  di «      «       |j                  «       D ��ci c]H  \  }}|j                  d	«      s/|t        j                  «       j                  j                  d
«      v r||“ŒJ }}}|j                  d«      �|j                  d«      €|d   |d<   t        ||dœ«      }t!        | j                  dddi|¬«      ƒ d{  –—† }	t#        |	j$                  «      | _        | j&                  d   | _        t+        | j                  t-        t/        | j(                  «      «      «      | _        | j                  j3                  d| j(                  › d|› �«       y|| _        t+        | j                  t-        t/        | j(                  «      «      «      | _        | j5                  «       ƒ d{  –—† | _        | j                  j3                  d| j(                  › �«       yc c}}w 7 �Œ 7 Œ;­w)a  Starts a kernel via HTTP in an asynchronous manner.

        Parameters
        ----------
        `**kwargs` : optional
             keyword arguments that are passed down to build the kernel_cmd
             and launching the kernel (e.g. Popen kwargs).
        rC   Nrœ   Úpython3zRequest new kernel at: %sr�   r6   ÚenvÚKERNEL_Ú,rH   ÚKERNEL_WORKING_DIR)Únamerä   ÚPOSTúContent-Typeúapplication/json©r[   Úheadersr^   r\   z%GatewayKernelManager started kernel: z, args: z,GatewayKernelManager using existing kernel: )r�   rJ   r]   r<   rŽ   r�   r"   r9   Ú	http_userr`   rÒ   ÚitemsÚ
startswithÚallowed_envsÚsplitr   r#   r   r^   rU   rC   r    r   rT   rÚ   rK   r_   )
r,   r>   rC   rœ   Úpayload_envsÚkÚvÚ
kernel_envÚ	json_bodyrf   s
             r-   rN   z!GatewayKernelManager.start_kernelÙ  sU  è ø€ ð —J‘J˜{Ó+ˆ	àÑØ Ÿ*™* ]°IÓ>ˆKØ�H‰H�N‰NÐ6¸×9IÑ9IÑIÔJô �z‰z�~‰~Ð/Ó0Ð8¼]×=SÑ=SÓ=U×=_Ò=_Ü0=×0FÑ0FÓ0H×0RÑ0RÒ0XÐVX”—
‘
Ð,Ñ-äŸ:™:Ÿ?™?Ó,ˆLØ×Ñ §
¡
¨5°"Ó 5Ô6ð
 +×0Ñ0Ô2ôá2‘F�Q˜Ø—<‘< 	Ô*¨a´=×3IÑ3IÓ3K×3XÑ3X×3^Ñ3^Ð_bÓ3cÑ.cð �1‘Ø2ð ñ ð �z‰z˜%Ó Ð,°·±Ð@TÓ1UÐ1]Ø39¸%±=�
Ð/Ñ0ä#¨[ÀÑ$LÓMˆIä,Ø× Ñ ØØ'Ð);Ð<Øô	÷ ˆHô & h§m¡mÓ4ˆDŒKØ!Ÿ[™[¨Ñ.ˆDŒNÜ+¨D×,<Ñ,<¼jÌÈTÏ^É^ÓI\Ó>]Ó^ˆDŒOØ�H‰H�M‰MÐAÀ$Ç.Á.ÐAQÐQYÐZ`ÐYaÐbÕcà&ˆDŒNÜ+¨D×,<Ñ,<¼jÌÈTÏ^É^ÓI\Ó>]Ó^ˆDŒOØ $× 2Ñ 2Ó 4×4ˆDŒKØ�H‰H�M‰MÐHÈÏÉÐHXÐYÕZùó5ðùð 5ús3   ‚DKÄAKÅ AKÆ9KÆ:C&KÊ KÊ!9KËKz Kernel {kernel_id} was shutdown.c              ƒ  ó°  K  — | j                   rx| j                  j                  d| j                  «       	 t	        | j                  d¬«      ƒ d{  –—† }| j                  j                  d|j
                  |j                  «       yy7 Œ7# t        j                  $ r6}|j                  dk(  r| j                  j                  d«       n‚ Y d}~yd}~ww xY w­w)z5Attempts to stop the kernel process cleanly via HTTP.zRequest shutdown kernel at: %sÚDELETErZ   NzShutdown kernel response: %d %sr¯   z4Shutdown kernel response: kernel not found (ignored))
rÍ   rJ   r]   rÚ   r#   ÚcodeÚreasonr   rb   r°   )r,   rn   ro   rf   r²   s        r-   rp   z$GatewayKernelManager.shutdown_kernel  s¤   è ø€ ð �?Š?Ø�H‰H�N‰NÐ;¸T¿_¹_ÔMðÜ!0°·±ÈÔ!R×R�Ø—‘—‘Ð@À(Ç-Á-ÐQY×Q`ÑQ`Õað	 ð Sùä—=‘=ò Ø×$Ñ$¨Ò+Ø—H‘H—N‘NÐ#YÕZàô [ûðüs@   ‚3C¶B
 ÁBÁ5B
 ÂCÂB
 Â
CÂ,CÃ	CÃCÃCz!Kernel {kernel_id} was restarted.c              ‹  óB  K  — | j                   r�| j                  €J ‚| j                  dz   }| j                  j                  d|«       t	        |dddit        i «      ¬«      ƒ d{  –—† }| j                  j                  d|j                  |j                  «       yy7 Œ7­w)	zRestarts a kernel via HTTP.Nz/restartzRequest restart kernel at: %sré   rê   rë   rì   zRestart kernel response: %d %s©rÍ   rÚ   rJ   r]   r#   r   rú   rû   )r,   rÕ   rÚ   rf   s       r-   rr   z#GatewayKernelManager.restart_kernel   sŽ   è ø€ ð
 �?Š?Ø—?‘?Ð.Ð.Ð.ØŸ™¨:Ñ5ˆJØ�H‰H�N‰NÐ:¸JÔGÜ,ØØØ'Ð);Ð<Ü  “_ô	÷ ˆHð �H‰H�N‰NÐ;¸X¿]¹]ÈHÏOÉOÕ\ð ðúó   ‚A#BÁ%BÁ&8Bz#Kernel {kernel_id} was interrupted.c              ƒ  óB  K  — | j                   r�| j                  €J ‚| j                  dz   }| j                  j                  d|«       t	        |dddit        i «      ¬«      ƒ d{  –—† }| j                  j                  d|j                  |j                  «       yy7 Œ7­w)	z*Interrupts the kernel via an HTTP request.Nz
/interruptzRequest interrupt kernel at: %sré   rê   rë   rì   z Interrupt kernel response: %d %srý   )r,   rÚ   rf   s      r-   rt   z%GatewayKernelManager.interrupt_kernel1  sŽ   è ø€ ð
 �?Š?Ø—?‘?Ð.Ð.Ð.ØŸ™¨<Ñ7ˆJØ�H‰H�N‰NÐ<¸jÔIÜ,ØØØ'Ð);Ð<Ü  “_ô	÷ ˆHð �H‰H�N‰NÐ=¸x¿}¹}ÈhÏoÉoÕ^ð ðúrþ   c              ƒ  ó  K  — | j                   rG| j                  «       ƒ d{  –—† | _        | j                  j	                  d| j                  › d�«       y| j                  j	                  d| j                  › d�«       y7 Œ]­w)z$Is the kernel process still running?NzThe kernel: z
 is alive.Tz no longer exists.F)rÍ   r_   rU   rJ   r]   r+   s    r-   Úis_alivezGatewayKernelManager.is_aliveB  sf   è ø€ à�?Š?à $× 2Ñ 2Ó 4×4ˆDŒKØ�H‰H�N‰N˜\¨$¯+©+¨°jÐAÔBØà�H‰H�N‰N˜\¨$¯+©+¨Ð6HÐIÔJØð 5ús   ‚ B¢B £ABc                 ó   — y)z/Clean up resources when the kernel is shut downNr*   )r,   ro   s     r-   Úcleanup_resourcesz&GatewayKernelManager.cleanup_resourcesM  ó   � r/   r¹   r{   r|   )r}   r~   r   r€   rC   r�   rU   r   rÇ   r8   ÚpropertyrÍ   r   Úclient_classr   rÔ   rÖ   r_   r   rN   rp   rr   rt   r  r  r‚   rƒ   s   @r-   rÃ   rÃ   |  sé   ø… Ù@à €IˆzÓ Ø€Fáˆ]Óñó ðô
&ð ñ'ó ð'ñ $Ð$YÓZ€LÙÐ UÔV€Nò)ó %ñV Ø5ôñ1[óð1[ñf Ø6ôòóðñ Ø7ôñ]óð]ñ Ø9ôñ_óð_ò	÷>r/   rÃ   c                  ó~   ‡ — e Zd ZU dZdZded<   ded<   dˆ fd„Zdd„Zdd	„Zdd
„Z	e
d„ «       Zdd„Zdd„Zdd„Zˆ xZS )ÚChannelQueuezA queue for a named channel.NrÄ   Úchannel_namerÁ   Úresponse_router_finishedc                óZ   •— t         ‰| �  «        || _        || _        || _        d| _        y)zInitialize a channel queue.FN)r7   r8   r	  Úchannel_socketrJ   r
  )r,   r	  r  rJ   r?   s       €r-   r8   zChannelQueue.__init__Z  s-   ø€ ä‰ÑÔØ(ˆÔØ,ˆÔØˆŒØ(-ˆÕ%r/   c              ƒ  ó8  K  — |€t        d«      }n|dk  rd}t        |«      ‚t        «       |z   }	 	 | j                  d¬«      S # t        $ rI | j
                  rd}t        |«      d‚t        «       |kD  r‚ t        j                  d«      ƒ d{  –—†7   Y nw xY wŒi­w)z"Asynchronously get from the queue.NÚinfr   z''timeout' must be a non-negative numberF)ÚblockzResponse router had finished)	ÚfloatÚ
ValueErrorr   r�   r   r
  ÚRuntimeErrorÚasyncioÚsleep)r,   Útimeoutr³   Úend_times       r-   Ú
_async_getzChannelQueue._async_getb  s�   è ø€ àˆ?Ü˜E“l‰GØ�qŠ[Ø;ˆCÜ˜S“/Ð!Ü“; Ñ(ˆàð'Ø—x‘x e�xÓ,Ð,øÜò 'Ø×0Ò0Ø8�CÜ& sÓ+°Ð5Ü“; Ò)ØÜ—m‘m AÓ&×&Ó&ð'úð ùs5   ‚/B²A ÁBÁA	BÂBÂBÂBÂBÂBc              �  ó  K  — |j                  dd«      }| j                  |¬«      ƒ d{  –—† }| j                  j                  d| j                  |d   |r|d   nd«       | j                  «        t        d	|«      S 7 ŒR­w)
zGet a message from the queue.r  r!   )r  Nz9Received message on channel: %s, msg_id: %s, msg_type: %sÚmsg_idÚmsg_typeÚnullúdict[str, Any])r�   r  rJ   r]   r	  Ú	task_doner
   )r,   Úargsr>   r  r³   s        r-   Úget_msgzChannelQueue.get_msgv  sv   è ø€ à—*‘*˜Y¨Ó*ˆØ—O‘O¨G�OÓ4×4ˆØ�‰�‰ØGØ×ÑØ�‰MÙ"ˆC�
ŠO¨ô		
ð 	�‰ÔÜÐ$ cÓ*Ð*ð 5ús   ‚(A?ªA=«AA?c                ó  — t        j                  |t        j                  ¬«      j	                  dd«      }| j
                  j                  d| j                  |d   |r|d   nd«       | j                  j                  |«       y)	zSend a message to the queue.)r   z</z<\/z8Sending message on channel: %s, msg_id: %s, msg_type: %sr  r  r  N)
ÚjsonÚdumpsr  Úserialize_datetimerÝ   rJ   r]   r	  r  Úsend)r,   r³   Úmessages      r-   r$  zChannelQueue.sendƒ  sk   € ä—*‘*˜S¬,×*IÑ*IÔJ×RÑRÐSWÐY_Ó`ˆØ�‰�‰ØFØ×ÑØ�‰MÙ"ˆC�
ŠO¨ô		
ð 	×Ñ× Ñ  Õ)r/   c                óX   — t        | t        j                  «      r| j                  «       S y)zSerialize a datetime object.N)rÞ   rÛ   Ú	timestamp)Údts    r-   r#  zChannelQueue.serialize_datetimeŽ  s#   € ô �bœ(×+Ñ+Ô,Ø—<‘<“>Ð!Ør/   c                 ó   — y)zStart the queue.Nr*   r+   s    r-   ÚstartzChannelQueue.start•  r  r/   c           	     ób  — | j                  «       sŸg }| j                  «       r=| j                  «       }|d   dk7  r|j                  |d   «       | j                  «       rŒ=| j                  dk(  rd|v ry|r9| j
                  j                  d| j                  › dt        |«      › d|› d	�«       yyy)
zStop the queue.r  ÚstatusÚiopubÚshutdown_replyNzStopping channel 'z' with z" unprocessed non-status messages: Ú.)ÚemptyÚqsizeÚ
get_nowaitrc   r	  rJ   ra   r™   )r,   Úmsgsr³   s      r-   ÚstopzChannelQueue.stop˜  s¯   € à�z‰zŒ|ð ˆDØ—*‘*”,Ø—o‘oÓ'�Ø�z‘? hÒ.Ø—K‘K  J¡Ô0ð —*‘*•,ð × Ñ  GÒ+Ð0@ÀDÑ0HØÙØ—‘× Ñ Ø(¨×):Ñ):Ð(;¸7Ä3ÀtÃ9À+ÐMoÐptÐouÐuvÐwõð ð r/   c                ó   — | j                   duS )zWhether the queue is alive.N)r  r+   s    r-   r  zChannelQueue.is_aliveª  s   € à×"Ñ"¨$Ð.Ð.r/   )r	  rT   r  zwebsocket.WebSocketrJ   r$   r¹   )r  r	   r>   r	   rÀ   r  )r³   r  rÀ   ÚNone)rÀ   r6  ©rÀ   rÁ   )r}   r~   r   r€   r	  r�   r8   r  r  r$  rº   r#  r*  r4  r  r‚   rƒ   s   @r-   r  r  T  sM   ø… Ù&à#€L�*Ó#Ø"Ó"õ.ó'ó(+ó	*ð ñó ðóó÷$/r/   r  c                  ó   — e Zd ZdZdd„Zy)ÚHBChannelQueuez"A queue for the heartbeat channel.c                ó"   — | j                  «       S )zWhether the channel is beating.)r  r+   s    r-   Ú
is_beatingzHBChannelQueue.is_beating²  s   € ð �}‰}‹Ðr/   Nr7  )r}   r~   r   r€   r;  r*   r/   r-   r9  r9  ¯  s
   „ Ù,ôr/   r9  c                  óÚ   ‡ — e Zd ZU dZdZded<   ded<   ded<   ded	<   ded
<   ded<   ded<   ˆ fd„Zdˆ fd„	Zˆ fd„Ze	d„ «       Z
e	d„ «       Ze	d„ «       Ze	d„ «       Ze	d„ «       Zd„ Zˆ xZS )ÚGatewayKernelClientaå  Communicates with a single kernel indirectly via a websocket to a gateway server.

    There are five channels associated with each kernel:

    * shell: for request/reply calls to the kernel.
    * iopub: for the kernel to publish results to frontends.
    * hb: for monitoring the kernel's heartbeat.
    * stdin: for frontends to reply to raw_input calls in the kernel.
    * control: for kernel management calls to the kernel.

    The messages that can be sent on these channels are exposed as methods of the
    client (KernelClient.execute, complete, history, etc.). These methods only
    send the message, they don't wait for a reply. To get results, use e.g.
    :meth:`get_shell_msg` to fetch messages from the shell channel.
    FrÁ   Ú_channels_stoppedzdict[str, ChannelQueue] | NoneÚ_channel_queueszChannelQueue | NoneÚ_control_channelÚ_hb_channelÚ_stdin_channelÚ_iopub_channelÚ_shell_channelc                ój   •— t        ‰| �  di |¤Ž || _        d| _        d| _        d| _        i | _        y)z#Initialize a gateway kernel client.NFr*   )r7   r8   rC   r  Úresponse_routerr>  r?  )r,   rC   r>   r?   s      €r-   r8   zGatewayKernelClient.__init__Ó  s:   ø€ ä‰ÑÑ"˜6Ò"Ø"ˆŒØ:>ˆÔØ.2ˆÔØ!&ˆÔØ!ˆÕr/   c           	   ƒ  ó¨  •K  — t        t        j                  «       j                  xs dt        j                  «       j                  t        | j                  «      d«      }t        j                  «       j                  t        j                  «       j                  t        j                  «       j                  dœ}t        j                  |t        j                  «       j                  d|¬«      | _        t        t        ‰| �A  |||||¬«      «      ƒ d{  –—†  t#        | j$                  ¬«      | _        | j&                  j)                  «        y7 Œ:­w)	zÞStarts the channels for this kernel.

        For this class, we establish a websocket connection to the destination
        and set up the channel-based queues on which applicable messages will
        be posted.
        r6   Úchannels)Úca_certsÚcertfileÚkeyfileT)r  Úenable_multithreadÚsslopt)Úshellr-  ÚstdinÚhbÚcontrolN)Útarget)r    r"   r9   Úws_urlr;   r   rC   rI  Úclient_certÚ
client_keyÚ	websocketÚcreate_connectionÚKERNEL_LAUNCH_TIMEOUTr  r   r7   Ústart_channelsr   Ú_route_responsesrF  r*  )	r,   rN  r-  rO  rP  rQ  rS  Ússl_optionsr?   s	           €r-   rY  z"GatewayKernelClient.start_channelsà  s  øè ø€ ô Ü×"Ñ"Ó$×+Ñ+Ò1¨rÜ×"Ñ"Ó$×5Ñ5Ü�t—~‘~Ó&Øó	
ˆô &×.Ñ.Ó0×9Ñ9Ü%×.Ñ.Ó0×<Ñ<Ü$×-Ñ-Ó/×:Ñ:ñ
ˆô (×9Ñ9ØÜ!×*Ñ*Ó,×BÑBØ#Øô	
ˆÔô Ü‰GÑ"¨°eÀ5ÈRÐY`Ð"Óaó
÷ 	
ð 	
ô  &¨T×-BÑ-BÔCˆÔØ×Ñ×"Ñ"Õ$ð	
ús   ƒDEÄEÄ;Ec                ób  •— t         ‰| �  «        d| _        | j                  j	                  d«       | j
                  €J ‚| j
                  j                  «        | j                  €J ‚| j                  j                  «        | j                  r"| j                  j                  «        d| _	        yy)z¡Stops all the running channels for this kernel.

        For this class, we close the websocket connection and destroy the
        channel-based queues.
        TzClosing websocket connectionN)r7   Ústop_channelsr>  rJ   r]   r  ÚcloserF  Újoinr?  Úclearrz   s    €r-   r]  z!GatewayKernelClient.stop_channels  s™   ø€ ô 	‰ÑÔØ!%ˆÔØ�‰�‰Ð5Ô6à×"Ñ"Ð.Ð.Ð.Ø×Ñ×!Ñ!Ô#Ø×#Ñ#Ð/Ð/Ð/Ø×Ñ×!Ñ!Ô#à×ÒØ× Ñ ×&Ñ&Ô(Ø#'ˆDÕ ð  r/   c                ó  — | j                   €v| j                  j                  d«       | j                  €J ‚t	        d| j                  | j                  «      | _         | j
                  €J ‚| j                   | j
                  d<   | j                   S )z-Get the shell channel object for this kernel.zcreating shell channel queuerN  )rD  rJ   r]   r  r  r?  r+   s    r-   Úshell_channelz!GatewayKernelClient.shell_channel  ó‚   € ð ×ÑÐ&Ø�H‰H�N‰NÐ9Ô:Ø×&Ñ&Ð2Ð2Ð2Ü".¨w¸×8KÑ8KÈTÏXÉXÓ"VˆDÔØ×'Ñ'Ð3Ð3Ð3Ø,0×,?Ñ,?ˆD× Ñ  Ñ)Ø×"Ñ"Ð"r/   c                ó  — | j                   €v| j                  j                  d«       | j                  €J ‚t	        d| j                  | j                  «      | _         | j
                  €J ‚| j                   | j
                  d<   | j                   S )z-Get the iopub channel object for this kernel.zcreating iopub channel queuer-  )rC  rJ   r]   r  r  r?  r+   s    r-   Úiopub_channelz!GatewayKernelClient.iopub_channel#  rc  r/   c                ó  — | j                   €v| j                  j                  d«       | j                  €J ‚t	        d| j                  | j                  «      | _         | j
                  €J ‚| j                   | j
                  d<   | j                   S )z-Get the stdin channel object for this kernel.zcreating stdin channel queuerO  )rB  rJ   r]   r  r  r?  r+   s    r-   Ústdin_channelz!GatewayKernelClient.stdin_channel.  rc  r/   c                ó  — | j                   €v| j                  j                  d«       | j                  €J ‚t	        d| j                  | j                  «      | _         | j
                  €J ‚| j                   | j
                  d<   | j                   S )z*Get the hb channel object for this kernel.zcreating hb channel queuerP  )rA  rJ   r]   r  r9  r?  r+   s    r-   Ú
hb_channelzGatewayKernelClient.hb_channel9  s‚   € ð ×ÑÐ#Ø�H‰H�N‰NÐ6Ô7Ø×&Ñ&Ð2Ð2Ð2Ü-¨d°D×4GÑ4GÈÏÉÓRˆDÔØ×'Ñ'Ð3Ð3Ð3Ø)-×)9Ñ)9ˆD× Ñ  Ñ&Ø×ÑÐr/   c                ó  — | j                   €v| j                  j                  d«       | j                  €J ‚t	        d| j                  | j                  «      | _         | j
                  €J ‚| j                   | j
                  d<   | j                   S )z/Get the control channel object for this kernel.zcreating control channel queuerQ  )r@  rJ   r]   r  r  r?  r+   s    r-   Úcontrol_channelz#GatewayKernelClient.control_channelD  s…   € ð × Ñ Ð(Ø�H‰H�N‰NÐ;Ô<Ø×&Ñ&Ð2Ð2Ð2Ü$0°¸D×<OÑ<OÐQU×QYÑQYÓ$ZˆDÔ!Ø×'Ñ'Ð3Ð3Ð3Ø.2×.CÑ.CˆD× Ñ  Ñ+Ø×$Ñ$Ð$r/   c                ó`  — 	 | j                   s}| j                  €J ‚| j                  j                  «       }|snRt        t	        |«      «      }|d   }| j
                  €J ‚| j
                  |   j                  |«       | j                   sŒ}| j
                  €J ‚| j
                  j                  «       D ]	  }d|_        Œ | j                  j                  d«       y# t        j                  $ r Y Œet        $ r5}| j                   s| j                  j                  d|› d�«       Y d}~Œ�d}~ww xY w)aÎ  
        Reads responses from the websocket and routes each to the appropriate channel queue based
        on the message's channel.  It does this for the duration of the class's lifetime until the
        channels are stopped, at which time the socket is closed (unblocking the router) and
        the thread terminates.  If shutdown happens to occur while processing a response (unlikely),
        termination takes place via the loop control boolean.
        NÚchannelz"Unexpected exception encountered (Ú)Tz!Response router thread exiting...)r>  r  Úrecvr   r   r?  Ú
put_nowaitrV  Ú"WebSocketConnectionClosedExceptionÚBaseExceptionrJ   ra   re   r
  r]   )r,   Úraw_messageÚresponse_messagerm  ÚbeÚchannel_queues         r-   rZ  z$GatewayKernelClient._route_responsesO  s!  € ð	MØ×,Ò,Ø×*Ñ*Ð6Ð6Ð6Ø"×1Ñ1×6Ñ6Ó8�Ù"ØÜ#.¬t°KÓ/@Ó#AÐ Ø*¨9Ñ5�Ø×+Ñ+Ð7Ð7Ð7Ø×$Ñ$ WÑ-×8Ñ8Ð9IÔJð ×,Ó,ð$ ×#Ñ#Ð/Ð/Ð/Ø!×1Ñ1×8Ñ8Ö:ˆMØ59ˆMÕ2ð ;ð 	�‰�‰Ð:Õ;øô ×;Ñ;ò 	Ùäò 	MØ×)Ò)Ø—‘× Ñ Ð#EÀbÀTÈÐ!KÔLÿøð	Mús   ‚BC ÃD-Ã0D-Ã8+D(Ä(D-)TTTTT)r}   r~   r   r€   Úallow_stdinr�   r8   rY  r]  r  rb  re  rg  ri  rk  rZ  r‚   rƒ   s   @r-   r=  r=  ¸  s²   ø… ñð" €KØÓØ3Ó3Ø)Ó)Ø$Ó$Ø'Ó'Ø'Ó'Ø'Ó'ô"õ!%ôF(ð* ñ#ó ð#ð ñ#ó ð#ð ñ#ó ð#ð ñ ó ð ð ñ%ó ð%ö<r/   r=  )Ar€   Ú
__future__r   r  rÛ   r!  rŽ   Úqueuer   r   Ú	threadingr   Útimer   Útypingr   r	   r
   rV  Ú"jupyter_client.asynchronous.clientr   Újupyter_client.clientabcr   Újupyter_client.kernelspecr   Újupyter_client.managerabcr   Újupyter_core.utilsr   Útornador   Útornado.escaper   r   r   r   Ú	traitletsr   r   r   r   Ú_tzr   r   Úservices.kernels.kernelmanagerr   r   r   Ú services.sessions.sessionmanagerr   Úutilsr    Úgateway_clientr"   r#   Úloggingr$   r&   r…   r¼   rÃ   Úregisterr  r9  r=  r*   r/   r-   Ú<module>rŒ     sõ   ðÙ õ #ã Û Û Û 	ß Ý Ý ß +Ñ +ã Ý @Ý 4Ý 7Ý 6Ý +Ý ß EÓ Eß ?Ó ?ç ÷ñ õ
 >Ý !ß :áÝôl%Ð";ô l%ô^K$Ð0ô K$ô\˜Nô ô.R>Ð.ô R>ðj Ð × Ñ Ð.Ô /ôX/�5ô X/ôv�\ô ôv<Ð+ô v<ðr €× Ñ Ð,Õ -r/   