o
    2.a                     @   s*   d dl mZ d dl mZ G dd dZdS )   )errors)utilsc                   @   sP   e Zd Zed			dddZdd	 Zdd
dZed		dddZdS )ExecApiMixin	containerTF Nc              
   C   s   |	durt | jdrtdt|trt |}t|	tr%t j 	|	}	|||||||||	d	}|
durEt | jdrAtd|
|d< |rL||d< nd| j
v rX| j
d |d< | d	|}| j||d
}| |dS )aC  
        Sets up an exec instance in a running container.

        Args:
            container (str): Target container where exec instance will be
                created
            cmd (str or list): Command to be executed
            stdout (bool): Attach to stdout. Default: ``True``
            stderr (bool): Attach to stderr. Default: ``True``
            stdin (bool): Attach to stdin. Default: ``False``
            tty (bool): Allocate a pseudo-TTY. Default: False
            privileged (bool): Run as privileged.
            user (str): User to execute command as. Default: root
            environment (dict or list): A dictionary or a list of strings in
                the following format ``["PASSWORD=xxx"]`` or
                ``{"PASSWORD": "xxx"}``.
            workdir (str): Path to working directory for this exec session
            detach_keys (str): Override the key sequence for detaching
                a container. Format is a single character `[a-Z]`
                or `ctrl-<value>` where `<value>` is one of:
                `a-z`, `@`, `^`, `[`, `,` or `_`.
                ~/.docker/config.json is used by default.

        Returns:
            (dict): A dictionary with an exec ``Id`` key.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        Nz1.25z;Setting environment for exec is not supported in API < 1.25)		ContainerZUserZ
PrivilegedTtyZAttachStdinZAttachStdoutZAttachStderrZCmdZEnvz1.35z/workdir is not supported for API version < 1.35Z
WorkingDirZ
detachKeysz/containers/{0}/exec)dataT)r   Z
version_ltZ_versionr   ZInvalidVersion
isinstancestrZsplit_commanddictZformat_environmentZ_general_configs_url
_post_json_result)selfr   cmdstdoutstderrstdinttyZ
privilegeduserZenvironmentZworkdirZdetach_keysr	   urlres r   5/usr/lib/python3/dist-packages/docker/api/exec_api.pyexec_create   s>   #




zExecApiMixin.exec_createc                 C   s2   t |tr
|d}| | d|}| |dS )aL  
        Return low-level information about an exec command.

        Args:
            exec_id (str): ID of the exec instance

        Returns:
            (dict): Dictionary of values returned by the endpoint.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        Idz/exec/{0}/jsonT)r
   r   getZ_getr   r   )r   exec_idr   r   r   r   exec_inspectP   s   

zExecApiMixin.exec_inspectc                 C   sF   t |tr
|d}||d}| d|}| j||d}| | dS )z
        Resize the tty session used by the specified exec command.

        Args:
            exec_id (str): ID of the exec instance
            height (int): Height of tty session
            width (int): Width of tty session
        r   )hwz/exec/{0}/resize)paramsN)r
   r   r   r   Z_postZ_raise_for_status)r   r   Zheightwidthr"   r   r   r   r   r   exec_resizec   s   



zExecApiMixin.exec_resizer   c           
      C   sd   ||d}|r	i nddd}| j | d|||dd}	|r"| |	S |r)| |	S | j|	|||dS )	a  
        Start a previously set up exec instance.

        Args:
            exec_id (str): ID of the exec instance
            detach (bool): If true, detach from the exec command.
                Default: False
            tty (bool): Allocate a pseudo-TTY. Default: False
            stream (bool): Stream response data. Default: False
            socket (bool): Return the connection socket to allow custom
                read/write operations.
            demux (bool): Return stdout and stderr separately

        Returns:

            (generator or str or tuple): If ``stream=True``, a generator
            yielding response chunks. If ``socket=True``, a socket object for
            the connection. A string containing response data otherwise. If
            ``demux=True``, a tuple with two elements of type byte: stdout and
            stderr.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        )r   ZDetachUpgradeZtcp)Z
Connectionr%   z/exec/{0}/startT)headersr	   stream)r   demux)r   r   r   Z_get_raw_response_socketZ_read_from_socket)
r   r   detachr   r'   Zsocketr(   r	   r&   r   r   r   r   
exec_startu   s$   


zExecApiMixin.exec_start)	TTFFFr   NNN)NN)FFFFF)	__name__
__module____qualname__r   Zcheck_resourcer   r   r$   r*   r   r   r   r   r      s    I
r   N)r   r   r   r   r   r   r   r   <module>   s    