
    2.a!                     p    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	      Zy)
    N)errors)get_meta_dir)METAFILE)get_current_context_name)#write_context_name_to_docker_config)Contextc                       e Zd ZdZ edd      Ze	 	 dd       Zedd       Zed        Z	ed        Z
edd	       Zed
        Zedd       Zy)
ContextAPIzfContext API.
    Contains methods for context management:
    create, list, remove, get, inspect.
    defaultswarmNc                 :   |st        j                  d      |dk(  rt        j                  d      t        j                  |      }|rt        j
                  |      d}|r|dk7  r|}t        ||      }|j                  |||||       |j                          |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_verifydef_namespace)r   MissingContextParameterContextExceptionr   load_contextContextAlreadyExistsset_endpointsave)	clsr   orchestratorhosttls_cfgdefault_namespacer   ctxendpoints	            4/usr/lib/python3/dist-packages/docker/context/api.pycreate_contextzContextAPI.create_context   s    > 00889))68 8""4(--d33LG3#HdL)dG++ 	 	- 	

    c                 f    |s
t               }|dk(  r| j                  S t        j                  |      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   s     r   get_contextzContextAPI.get_contextE   s4    , +-D9&&&##D))r!   c           
         g }t        j                  t                     D ]k  \  }}}||z   D ]]  }|t        k(  s	 t	        j
                  t        t         j                  j                  ||                  }|j                  |d          _ m | j                  g}|D ]&  }	|j                  t        j                   |	             ( |S # t        $ r*}t        j                  dj                  ||            d}~ww xY w)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   s
             r   r8   zContextAPI.contextsa   s     )+)@ 
	.%GXv"X- 	.x'.#yy gx!@A CT&\2	.
	. ''( 	8DOOG0067	8 % .$55<CC (!-. ..s   AC	C;%C66C;c                 "    | j                         S )z]Get current context.
        Returns:
            (Context): current context object.
        )r$   )r   s    r   get_current_contextzContextAPI.get_current_context|   s       r!   c                     | j                  |      }|st        j                  |      t        |      }|rt        j                  d|       y )NzFailed to set current context: )r$   r   ContextNotFoundr   r   )r   r   r   errs       r   set_current_contextzContextAPI.set_current_context   sS    ood#((..1$7))1#79 9 r!   c                    |st        j                  d      |dk(  rt        j                  d      t        j                  |      }|st        j
                  |      |t               k(  rt        d       |j                          y)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   s      r   remove_contextzContextAPI.remove_context   sw    * 00889))57 7""4(((..+--/5

r!   c                     |st        j                  d      |dk(  r| j                         S t        j                  |      }|st        j
                  |       |       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<   rA   s      r   inspect_contextzContextAPI.inspect_context   sZ    & 00889&&((""4(((..ur!   )NNNNF)N)r   )__name__
__module____qualname____doc__r   r#   classmethodr    r$   r8   r:   r>   rB   rD    r!   r   r
   r
      s     i1O=A49/ /b * *6  4 ! ! 9 9  @  r!   r
   )r)   r'   r   r   docker.context.configr   r   r   r   docker.contextr   r
   rJ   r!   r   <module>rM      s)     	  . * : E " r!   