
    bi                         d dl Z d dlZd dlZd dlZd dlZ	 ddlmZ dZ
dZ G d de      ZdZddZd	 Zd
 Zd Zd Zd ZddZd Zy# e$ r  e	d      ZY ;w xY w)    N   )NpipeSocket   c                       e Zd Zy)SocketErrorN)__name__
__module____qualname__     N/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/docker/utils/socket.pyr   r      s    r   r   m   c                 L   t         j                  t         j                  t         j                  f}t	        | t
              s~t        t        d      st        j                  | gg g        nUt        j                         }|j                  | t        j                  t        j                  z         |j                          	 t        | d      r| j                  |      S t	        | t        j                        r| j                  |      S t!        j                  | j#                         |      S # t$        $ r}|j                   |vr Y d}~yd}~wt&        $ rO}t	        | t
              xr0 t)        |j*                        dkD  xr |j*                  d   t,        k(  }|rY d}~y d}~ww xY w)z+
    Reads at most n bytes from socket
    pollrecvNr    )errnoEINTREDEADLKEWOULDBLOCK
isinstancer   hasattrselectr   registerPOLLINPOLLPRIr   pysocketSocketIOreadosfilenoOSError	ExceptionlenargsNPIPE_ENDED)socketnrecoverable_errorsr   eis_pipe_endeds         r   r   r      s9   
  ++u}}e6G6GHfk*vv&MM6(B+;;=DMM&&--&.."@AIIK66";;q>!fh//0;;q>!wwv}}** 77,, - #FK8 2QVVq2k1 	  s7   ?D+ *D+ #D+ +	F#4EF#AFFF#c                     d}t        |      |k  r9t        | |t        |      z
        }|st        d      ||z  }t        |      |k  r9|S )z]
    Reads exactly n bytes from socket
    Raises SocketError if there isn't enough data
    r   zUnexpected EOF)r$   r   r   )r'   r(   data	next_datas       r   read_exactlyr/   >   sU    
 D
d)a-SY/	.//		 d)a-
 Kr   c                 t    	 t        | d      }t        j                  d|      \  }}||fS # t        $ r Y yw xY w)z
    Returns the stream and size of the next frame of data waiting to be read
    from socket, according to the protocol defined here:

    https://docs.docker.com/engine/api/v1.24/#attach-to-a-container
       )r2   z>BxxxL)r/   r   structunpack)r'   r-   streamactuals       r   next_frame_headerr7   L   sH    FA& ]]8T2NFFF	  s   + 	77c                 @    |rd t        |       D        S t        |       S )z
    Return a generator of frames read from socket. A frame is a tuple where
    the first item is the stream number and the second item is a chunk of data.

    If the tty setting is enabled, the streams are multiplexed into the stdout
    stream.
    c              3   ,   K   | ]  }t         |f  y w)N)STDOUT).0frames     r   	<genexpr>zframes_iter.<locals>.<genexpr>e   s     EEEs   )frames_iter_ttyframes_iter_no_tty)r'   ttys     r   frames_iterrA   \   s"     E_V-DEE!&))r   c              #      K   	 t        |       \  }}|dk  ry|dkD  r1t        | |      }|t        |      }|dk(  ry||z  }||f |dkD  r1Kw)zc
    Returns a generator of data read from the socket when the tty setting is
    not enabled.
    r   N)r7   r   r$   )r'   r5   r(   resultdata_lengths        r   r?   r?   j   st     
 '/q5!e&!_F~f+KaA6"" !e	 s   AAAc              #   J   K   	 t        |       }t        |      dk(  ry| w)z^
    Return a generator of data read from the socket when the tty setting is
    enabled.
    r   N)r   r$   )r'   rC   s     r   r>   r>      s-     
 fv;! s   !#c                     |du rdj                  |       S ddg}| D ]K  }|dk7  sJ |d   |d   	|d   |d<   |dxx   |d   z  cc<   .|d   	|d   |d<   <|dxx   |d   z  cc<   M t        |      S )a  
    Iterate through frames read from the socket and return the result.

    Args:

        demux (bool):
            If False, stdout and stderr are multiplexed, and the result is the
            concatenation of all the frames. If True, the streams are
            demultiplexed, and the result is a 2-tuple where each item is the
            concatenation of frames belonging to the same stream.
    Fr   N)NNr   r   )jointuple)framesdemuxoutr<   s       r   consume_socket_outputrL      s     ~ xx ,C # $$$81v~qAA%("1v~qAA%("# :r   c                 R    | t         k(  r|dfS | t        k(  rd|fS t        |  d      )z[
    Utility to demultiplex stdout and stderr when reading frames from the
    socket.
    Nz is not a valid stream)r:   STDERR
ValueError)	stream_idr-   s     r   demux_adaptorrQ      s;    
 Fd|	f	d|I;&<=>>r   )i   )F)r   r    r   r'   r   r3   	transportr   ImportErrortyper:   rN   r#   r   r&   r   r/   r7   rA   r?   r>   rL   rQ   r   r   r   <module>rU      s     	   '
 
	
	) 	 !H *#*
"J
?Q  t*Ks   A AA