
    ^h                        S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
JrJrJr  SSKJr   SSKJr  SS
KJr  SSKJr  SSKJr  S rS rSS jrS r " S S\5      r\R<                  " \5        g! \ a	    SS	KJr   NLf = f)zKernel connection helpers.    N)Any)Session)WebSocketHandler)FloatInstanceUnicodedefault)LoggingConfigurable)json_default)date_default)extract_dates)_i18n   )KernelWebsocketConnectionABCc                    U R                  5       n [        U R                  S5      5      n[        R                  " U [
        S9R                  S5      nUR                  SU5        [        U5      nSUS-   -  /nUSS  H#  nUR                  US   [        U5      -   5        M%     [        R                  " S	S
US-   -  -   U/UQ76 nUR                  SU5        SR                  U5      $ )a  serialize a message as a binary blob

Header:

4 bytes: number of msg parts (nbufs) as 32b int
4 * nbufs bytes: offset for each buffer as integer as 32b int

Offsets are from the start of the buffer, including the header.

Returns
-------
The message serialized to bytes.

buffers)r	   utf8r      r   N!I    )copylistpopjsondumpsr   encodeinsertlenappendstructpackjoin)msgr   bmsgnbufsoffsetsbufoffsets_bufs          i/home/james-whalen/.local/lib/python3.13/site-packages/jupyter_server/services/kernels/connection/base.pyserialize_binary_messager,      s      ((*C3779%&G::c<077?DNN1dLEEAIGs|wr{SX-. ++cC519$55uGwGKNN1k"88Gr   c           
         [         R                  " SU SS 5      S   n[        [         R                  " SSU-  -   U SSUS-   -   5      5      nUR                  S5        / n[	        USS USS 5       H  u  pEUR                  XU 5        M     [
        R                  " US   R                  S	5      5      n[        US
   5      US
'   [        US   5      US'   USS US'   U$ )a  deserialize a message from a binary blog

Header:

4 bytes: number of msg parts (nbufs) as 32b int
4 * nbufs bytes: offset for each buffer as integer as 32b int

Offsets are from the start of the buffer, including the header.

Returns
-------
message dictionary
z!iNr   r   r   r   r   r   r   headerparent_headerr   )	r"   unpackr   r!   zipr   loadsdecoder   )r&   r'   r(   bufsstartstopr%   s          r+   deserialize_binary_messager7   5   s     MM$Ra)!,E6==sU{!2DQ%!)_4MNOGNN4D73B<5Dt$% 6
**T!W^^F+
,C!#h-0CM(_)=>C!"XC	NJr   c                    U(       a+  U" U S   5      U" U S   5      U" U S   5      U" U S   5      /nOU nUR                  S5      n/ nUR                  SS[        U5      -   S-   -  5        UR                  [        U5      US	   -   5        U H#  nUR                  [        U5      US	   -   5        M%     [        U5      R                  SS
S9nU Vs/ s H  owR                  SS
S9PM     nnSR	                  U/UQUPUQ5      nU$ s  snf )z*Serialize a message using the v1 protocol.r.   r/   metadatacontentutf-8      r   r   little)	byteorderr   )r   r!   r    to_bytesr$   )	msg_or_listchannelr#   msg_listr(   r%   offset_numberoffsetbin_msgs	            r+   serialize_msg_to_ws_v1rG   P   s   X&'_-.Z()Y'(	
 nnW%GGNN1H-123NN3w<'"+-.s3x'"+-. L))!x)@MDKLG&qH5GGLhhDDD8DEGN Ms   Dc           
      L   [         R                  U SS S5      n[        U5       Vs/ s H(  n[         R                  U SUS-   -  SUS-   -   S5      PM*     nnXS   US    R                  S5      n[        SUS-
  5       Vs/ s H  o X2   X2S-       PM     nnXE4$ s  snf s  snf )z,Deserialize a message using the v1 protocol.Nr<   r>   r   r=   r   r;   )int
from_bytesranger3   )ws_msgrD   ir(   rB   rC   s         r+   deserialize_msg_from_ws_v1rN   g   s    NN6"1:x8MMRS`MaMava1q5kAQK8(CMa   QZ'!*-44W=G=B1mVWFW=XY=XwzGEN3=XHY Zs   /BB!c                      \ rS rSrSr\" SSS\" S5      S9r\S 5       r	\S 5       r
\S	 5       r\S
 5       r\" 5       r\" S5      S 5       r\" \SS9r\" S5      S 5       r\" \5      rS rS rS\SS4S jrS\S\\   SS4S jrSrg)BaseKernelWebsocketConnectionr   zJA configurable base class for connecting Kernel WebSockets to ZMQ sockets.NTa  Preferred kernel message protocol over websocket to use (default: None). If an empty string is passed, select the legacy protocol. If None, the selected protocol will depend on what the front-end supports (usually the most recent protocol supported by the back-end and the front-end).)
allow_noneconfighelpc                     U R                   $ )zThe kernel manager.)parentselfs    r+   kernel_manager,BaseKernelWebsocketConnection.kernel_manager   s     {{r   c                 .    U R                   R                  $ )zThe multi kernel manager.)rY   rV   rW   s    r+   multi_kernel_manager2BaseKernelWebsocketConnection.multi_kernel_manager   s     "")))r   c                 .    U R                   R                  $ )zThe kernel id.)rY   	kernel_idrW   s    r+   r_   'BaseKernelWebsocketConnection.kernel_id   s     "",,,r   c                 .    U R                   R                   $ )zThe session id.)sessionrW   s    r+   
session_id(BaseKernelWebsocketConnection.session_id   s     ||###r   kernel_info_timeoutc                 .    U R                   R                  $ N)r\   re   rW   s    r+   _default_kernel_info_timeout:BaseKernelWebsocketConnection._default_kernel_info_timeout   s    ((<<<r   )klassrS   rb   c                 (    [        U R                  S9$ )N)rS   )r   rS   rW   s    r+   _default_session.BaseKernelWebsocketConnection._default_session   s    dkk**r   c                    #    [         e7f)zHandle a connect.NotImplementedErrorrW   s    r+   connect%BaseKernelWebsocketConnection.connect        !!   	c                    #    [         e7f)zHandle a disconnect.ro   rW   s    r+   
disconnect(BaseKernelWebsocketConnection.disconnect   rs   rt   incoming_msgreturnc                     [         e)zHandle an incoming message.ro   )rX   rx   s     r+   handle_incoming_message5BaseKernelWebsocketConnection.handle_incoming_message       !!r   streamoutgoing_msgc                     [         e)zHandle an outgoing message.ro   )rX   r~   r   s      r+   handle_outgoing_message5BaseKernelWebsocketConnection.handle_outgoing_message   r}   r    )__name__
__module____qualname____firstlineno____doc__r   r   kernel_ws_protocolpropertyrY   r\   r_   rc   r   re   r	   rh   r   r   rb   rl   r   websocket_handlerrq   rv   strr{   r   r   r   __static_attributes__r   r   r+   rP   rP   r   s   T 
	   * * - - $ $  '"#= $= WT2GY+ + !!12"""C "D ""c "c "t "r   rP   rg   )r   r   r"   typingr   jupyter_client.sessionr   tornado.websocketr   	traitletsr   r   r   r	   traitlets.configr
   jupyter_client.jsonutilr   ImportErrorr   r   jupyter_server.transutilsr   abcr   r,   r7   rG   rN   rP   registerr   r   r+   <module>r      s         * . 7 7 0E4 2 + -:6.@"$7 @"F  % %&C DO  EDEs   A4 4BB