
    !                     p    S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
   " S S	5      rg)
    N)errors)get_meta_dir)METAFILE)get_current_context_name)#write_context_name_to_docker_config)Contextc                       \ rS rSrSr\" SS5      r\  SS j5       r\SS j5       r	\S 5       r
\S	 5       r\SS
 j5       r\S 5       r\SS j5       rSrg)
ContextAPI   zZContext API.
Contains methods for context management:
create, list, remove, get, inspect.
defaultswarmNc                 Z   U(       d  [         R                  " S5      eUS:X  a  [         R                  " S5      e[        R                  " U5      nU(       a  [         R
                  " U5      eSnU(       a  US:w  a  Un[        X5      nUR                  XUUUS9  UR                  5         U$ )a  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	            %lib/third_party/docker/context/api.pycreate_contextContextAPI.create_context   s    > 00889))68 8""4(--d33LG3#Hd)G++ 	 	- 	

    c                 t    U(       d
  [        5       nUS:X  a  U R                  $ [        R                  " U5      $ )av  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_contextContextAPI.get_contextE   s4    , +-D9&&&##D))r#   c           
         / n[         R                  " [        5       5       Hn  u  p#nXC-    H`  nU[        :X  d  M   [        R
                  " [        [         R                  R                  X%5      5      5      nUR                  US   5        Mb     Mp     U R                  /nU H(  n	UR                  [        R                   " U	5      5        M*     U$ ! [         a*  n[        R                  " SR                  XW5      5      eSnAff = f)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    r;   ContextAPI.contextsa   s     )+)@%Gv"-x'.#yy g!@A CT&\2 . *A ''(DOOG0067  % .$55<CC (-. ..s   AC
D%C??Dc                 "    U R                  5       $ )zEGet current context.
Returns:
    (Context): current context object.
)r&   )r   s    r    get_current_contextContextAPI.get_current_context|   s       r#   c                     U R                  U5      nU(       d  [        R                  " U5      e[        U5      nU(       a  [        R                  " SU 35      eg )NzFailed to set current context: )r&   r   ContextNotFoundr   r   )r   r   r   errs       r    set_current_contextContextAPI.set_current_context   sS    ood#((..1$7))1#79 9 r#   c                 .   U(       d  [         R                  " S5      eUS:X  a  [         R                  " S5      e[        R                  " U5      nU(       d  [         R
                  " U5      eU[        5       :X  a  [        S5        UR                  5         g)a  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   rA   r   r   remover   r   r   s      r    remove_contextContextAPI.remove_context   sw    * 00889))57 7""4(((..+--/5

r#   c                     U(       d  [         R                  " S5      eUS:X  a  U R                  5       $ [        R                  " U5      nU(       d  [         R
                  " U5      eU" 5       $ )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   rA   rG   s      r    inspect_contextContextAPI.inspect_context   sZ    & 00889&&((""4(((..ur#    )NNNNF)N)r   )__name__
__module____qualname____firstlineno____doc__r   r%   classmethodr!   r&   r;   r>   rC   rH   rK   __static_attributes__rM   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
   rM   r#   r    <module>rW      s)     	  . * : E " r#   