HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //proc/1233/root/usr/lib/python3/dist-packages/docker/context/__pycache__/api.cpython-310.pyc
o

�2.a!�@sjddlZddlZddlmZddlmZddlmZddlmZddlmZddl	m
Z
Gdd	�d	�ZdS)
�N)�errors)�get_meta_dir)�METAFILE)�get_current_context_name)�#write_context_name_to_docker_config)�Contextc@szeZdZdZedd�Ze		ddd��Zeddd	��Zed
d��Z	edd
��Z
eddd��Zedd��Zeddd��Z
dS)�
ContextAPIzfContext API.
    Contains methods for context management:
    create, list, remove, get, inspect.
    �default�swarmNFc	Csv|st�d��|dkrt�d��t�|�}|rt�|��d}|r&|dkr&|}t||�}|j|||||d�|��|S)aRCreates a new context.
        Returns:
            (Context): a Context object.
        Raises:
            :py:class:`docker.errors.MissingContextParameter`
                If a context name is not provided.
            :py:class:`docker.errors.ContextAlreadyExists`
                If a context with the name already exists.
            :py:class:`docker.errors.ContextException`
                If name is default.

        Example:

        >>> from docker.context import ContextAPI
        >>> ctx = ContextAPI.create_context(name='test')
        >>> print(ctx.Metadata)
        {
            "Name": "test",
            "Metadata": {},
            "Endpoints": {
                "docker": {
                    "Host": "unix:///var/run/docker.sock",
                    "SkipTLSVerify": false
                }
            }
        }
        �namer	z$"default" is a reserved context name�dockerr
)�skip_tls_verify�
def_namespace)r�MissingContextParameter�ContextExceptionr�load_context�ContextAlreadyExists�set_endpoint�save)	�clsr�orchestrator�host�tls_cfg�default_namespacer
�ctx�endpoint�r�4/usr/lib/python3/dist-packages/docker/context/api.py�create_contexts(
�


�zContextAPI.create_contextcCs"|st�}|dkr|jSt�|�S)a�Retrieves a context object.
        Args:
            name (str): The name of the context

        Example:

        >>> from docker.context import ContextAPI
        >>> ctx = ContextAPI.get_context(name='test')
        >>> print(ctx.Metadata)
        {
            "Name": "test",
            "Metadata": {},
            "Endpoints": {
                "docker": {
                "Host": "unix:///var/run/docker.sock",
                "SkipTLSVerify": false
                }
            }
        }
        r	)r�DEFAULT_CONTEXTrr)rrrrr�get_contextEs

zContextAPI.get_contextc
Cs�g}t�t��D];\}}}||D]1}|tkrBzt�ttj�||���}|�	|d�Wqt
yA}z	t�d�
||���d}~wwqq|jg}|D]
}	|�	t�|	��qJ|S)z�Context list.
        Returns:
            (Context): List of context objects.
        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        �NamezFailed to load metafile {}: {}N)�os�walkrr�json�load�open�path�join�append�	Exceptionrr�formatrrr)
r�names�dirname�dirnames�fnames�filename�data�e�contextsrrrrr3as.	�������zContextAPI.contextscCs|��S)z]Get current context.
        Returns:
            (Context): current context object.
        )r )rrrr�get_current_context|szContextAPI.get_current_contextcCs8|�|�}|st�|��t|�}|rt�d|����dS)NzFailed to set current context: )r r�ContextNotFoundrr)rrr�errrrr�set_current_context�s

��zContextAPI.set_current_contextcCsV|st�d��|dkrt�d��t�|�}|st�|��|t�kr%td�|��dS)aYRemove a context. Similar to the ``docker context rm`` command.

        Args:
            name (str): The name of the context

        Raises:
            :py:class:`docker.errors.MissingContextParameter`
                If a context name is not provided.
            :py:class:`docker.errors.ContextNotFound`
                If a context with the name does not exist.
            :py:class:`docker.errors.ContextException`
                If name is default.

        Example:

        >>> from docker.context import ContextAPI
        >>> ContextAPI.remove_context(name='test')
        >>>
        rr	z#context "default" cannot be removedN)	rrrrrr5rr�remove�rrrrrr�remove_context�s
�


zContextAPI.remove_contextcCs<|st�d��|dkr|��St�|�}|st�|��|�S)aRemove a context. Similar to the ``docker context inspect`` command.

        Args:
            name (str): The name of the context

        Raises:
            :py:class:`docker.errors.MissingContextParameter`
                If a context name is not provided.
            :py:class:`docker.errors.ContextNotFound`
                If a context with the name does not exist.

        Example:

        >>> from docker.context import ContextAPI
        >>> ContextAPI.remove_context(name='test')
        >>>
        rr	)rrrrrr5r9rrr�inspect_context�s


zContextAPI.inspect_context)NNNNF)N)r	)�__name__�
__module__�__qualname__�__doc__rr�classmethodrr r3r4r7r:r;rrrrrs&
�1



 r)r$r"rr�docker.context.configrrrr�docker.contextrrrrrr�<module>s