o
    2.a!                     @   sj   d dl 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
 G dd	 d	ZdS )
    N)errors)get_meta_dir)METAFILE)get_current_context_name)#write_context_name_to_docker_config)Contextc                   @   sz   e Zd ZdZeddZe		dddZeddd	Zed
d Z	edd Z
edddZedd ZedddZdS )
ContextAPIzfContext API.
    Contains methods for context management:
    create, list, remove, get, inspect.
    defaultswarmNFc           	      C   sv   |st d|dkrt dt|}|rt |d}|r&|dkr&|}t||}|j|||||d |  |S )aR  Creates 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_verifyZdef_namespace)r   MissingContextParameterContextExceptionr   load_contextZContextAlreadyExistsZset_endpointZsave)	clsr   ZorchestratorZhostZtls_cfgZdefault_namespacer   ctxZendpoint r   4/usr/lib/python3/dist-packages/docker/context/api.pycreate_context   s(   



zContextAPI.create_contextc                 C   s"   |st  }|dkr| jS t|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_CONTEXTr   r   )r   r   r   r   r   get_contextE   s
   
zContextAPI.get_contextc           
      C   s   g }t t D ];\}}}|| D ]1}|tkrBzttt j||}|	|d  W q t
yA } z	td||d}~ww qq| jg}|D ]
}	|	t|	 qJ|S )zContext list.
        Returns:
            (Context): List of context objects.
        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        NamezFailed to load metafile {}: {}N)oswalkr   r   jsonloadopenpathjoinappend	Exceptionr   r   formatr   r   r   )
r   namesdirnamedirnamesfnamesfilenamedataecontextsr   r   r   r   r*   a   s.   	zContextAPI.contextsc                 C   s   |   S )z]Get current context.
        Returns:
            (Context): current context object.
        )r   )r   r   r   r   get_current_context|   s   zContextAPI.get_current_contextc                 C   s8   |  |}|st|t|}|rtd| d S )NzFailed to set current context: )r   r   ContextNotFoundr   r   )r   r   r   errr   r   r   set_current_context   s   

zContextAPI.set_current_contextc                 C   sV   |st d|dkrt dt|}|st ||t kr%td |  dS )aY  Remove 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')
        >>>
        r   r	   z#context "default" cannot be removedN)	r   r   r   r   r   r,   r   r   remover   r   r   r   r   r   remove_context   s   



zContextAPI.remove_contextc                 C   s<   |st d|dkr|  S t|}|st || S )a  Remove 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')
        >>>
        r   r	   )r   r   r   r   r   r,   r0   r   r   r   inspect_context   s   


zContextAPI.inspect_context)NNNNF)N)r	   )__name__
__module____qualname____doc__r   r   classmethodr   r   r*   r+   r.   r1   r2   r   r   r   r   r      s&    
1



 r   )r   r   r   r   Zdocker.context.configr   r   r   r   Zdocker.contextr   r   r   r   r   r   <module>   s    