U
    ™K7\Ô"  ã                   @   s,   d dl Z ddlmZmZ G dd„ deƒZdS )é    Né   )ÚauthÚutilsc                   @   s  e Zd Ze d¡e d¡dd„ ƒƒZe d¡ddd„ƒZe d¡dd	„ ƒZe d¡ddd„ƒZ	e d¡dd„ ƒZ
e d¡ddd„ƒZe d¡dd„ ƒZe d¡dd„ ƒZe d¡e d¡dd„ ƒƒZe d¡e d¡d dd„ƒƒZe d¡e d¡dd„ ƒƒZdS )!ÚPluginApiMixinz1.25Únamec                 C   sJ   |   d|¡}|}t|tƒr.dd„ t |¡D ƒ}| j||d}|  |¡ dS )aC  
            Configure a plugin.

            Args:
                name (string): The name of the plugin. The ``:latest`` tag is
                    optional, and is the default if omitted.
                options (dict): A key-value mapping of options

            Returns:
                ``True`` if successful
        z/plugins/{0}/setc                 S   s   g | ]\}}d   ||¡‘qS )z{0}={1})Úformat)Ú.0ÚkÚv© r   ú3/usr/lib/python3/dist-packages/docker/api/plugin.pyÚ
<listcomp>   s     z3PluginApiMixin.configure_plugin.<locals>.<listcomp>)ÚdataT)Ú_urlÚ
isinstanceÚdictÚsixZ	iteritemsÚ
_post_jsonÚ_raise_for_status)Úselfr   ZoptionsÚurlr   Úresr   r   r   Úconfigure_plugin   s    

zPluginApiMixin.configure_pluginFc              	   C   sV   |   d¡}tj||ttj |g ¡ƒd}| j|d|i|d}W 5 Q R X |  |¡ dS )a*  
            Create a new plugin.

            Args:
                name (string): The name of the plugin. The ``:latest`` tag is
                    optional, and is the default if omitted.
                plugin_data_dir (string): Path to the plugin data directory.
                    Plugin data directory must contain the ``config.json``
                    manifest file and the ``rootfs`` directory.
                gzip (bool): Compress the context using gzip. Default: False

            Returns:
                ``True`` if successful
        z/plugins/create)ÚrootÚgzipÚfilesr   )Úparamsr   T)r   r   Zcreate_archiveÚsetZbuildÚwalkÚ_postr   )r   r   Zplugin_data_dirr   r   Zarchvr   r   r   r   Úcreate_plugin   s    
 þ
zPluginApiMixin.create_pluginc                 C   s$   |   d|¡}|  |¡}|  |¡ dS )a  
            Disable an installed plugin.

            Args:
                name (string): The name of the plugin. The ``:latest`` tag is
                    optional, and is the default if omitted.

            Returns:
                ``True`` if successful
        z/plugins/{0}/disableT©r   r   r   )r   r   r   r   r   r   r   Údisable_plugin7   s    

zPluginApiMixin.disable_pluginr   c                 C   s0   |   d|¡}d|i}| j||d}|  |¡ dS )aV  
            Enable an installed plugin.

            Args:
                name (string): The name of the plugin. The ``:latest`` tag is
                    optional, and is the default if omitted.
                timeout (int): Operation timeout (in seconds). Default: 0

            Returns:
                ``True`` if successful
        z/plugins/{0}/enableÚtimeout©r   Tr!   )r   r   r#   r   r   r   r   r   r   Úenable_pluginH   s
    
zPluginApiMixin.enable_pluginc                 C   s   |   d|¡}|  |  |¡d¡S )a  
            Retrieve plugin metadata.

            Args:
                name (string): The name of the plugin. The ``:latest`` tag is
                    optional, and is the default if omitted.

            Returns:
                A dict containing plugin info
        z/plugins/{0}/jsonT©r   Ú_resultÚ_get)r   r   r   r   r   r   Úinspect_plugin[   s    zPluginApiMixin.inspect_pluginNc                 C   st   |   d¡}d|i}|r||d< i }t |¡\}}t | |¡}	|	rH|	|d< | j||||dd}
|  |
¡ | j|
ddS )a  
            Pull and install a plugin. After the plugin is installed, it can be
            enabled using :py:meth:`~enable_plugin`.

            Args:
                remote (string): Remote reference for the plugin to install.
                    The ``:latest`` tag is optional, and is the default if
                    omitted.
                privileges (:py:class:`list`): A list of privileges the user
                    consents to grant to the plugin. Can be retrieved using
                    :py:meth:`~plugin_privileges`.
                name (string): Local name for the pulled plugin. The
                    ``:latest`` tag is optional, and is the default if omitted.

            Returns:
                An iterable object streaming the decoded API logs
        z/plugins/pullÚremoter   úX-Registry-AuthT©r   Úheadersr   Ústream©Údecode©r   r   Úresolve_repository_nameÚget_config_headerr   r   Ú_stream_helper)r   r*   Ú
privilegesr   r   r   r-   ÚregistryÚ	repo_nameÚheaderÚresponser   r   r   Úpull_pluginj   s(    
 ÿ   þ
zPluginApiMixin.pull_pluginc                 C   s   |   d¡}|  |  |¡d¡S )z
            Retrieve a list of installed plugins.

            Returns:
                A list of dicts, one per plugin
        z/pluginsTr&   )r   r   r   r   r   Úplugins   s    
zPluginApiMixin.pluginsc                 C   sT   d|i}i }t  |¡\}}t  | |¡}|r2||d< |  d¡}|  | j|||dd¡S )al  
            Retrieve list of privileges to be granted to a plugin.

            Args:
                name (string): Name of the remote plugin to examine. The
                    ``:latest`` tag is optional, and is the default if omitted.

            Returns:
                A list of dictionaries representing the plugin's
                permissions

        r*   r+   z/plugins/privileges)r   r-   T)r   r2   r3   r   r'   r(   )r   r   r   r-   r6   r7   r8   r   r   r   r   Úplugin_privileges›   s     ÿ
 ÿz PluginApiMixin.plugin_privilegesc                 C   s\   |   d|¡}i }t |¡\}}t | |¡}|r6||d< | j||d}|  |¡ | j|ddS )a  
            Push a plugin to the registry.

            Args:
                name (string): Name of the plugin to upload. The ``:latest``
                    tag is optional, and is the default if omitted.

            Returns:
                ``True`` if successful
        z/plugins/{0}/pullr+   )r-   Tr/   )r   r   r2   r3   r   r   r4   )r   r   r   r-   r6   r7   r8   r   r   r   r   Úpush_plugin¸   s    
zPluginApiMixin.push_pluginc                 C   s,   |   d|¡}| j|d|id}|  |¡ dS )aª  
            Remove an installed plugin.

            Args:
                name (string): Name of the plugin to remove. The ``:latest``
                    tag is optional, and is the default if omitted.
                force (bool): Disable the plugin before removing. This may
                    result in issues if the plugin is in use by a container.

            Returns:
                ``True`` if successful
        z/plugins/{0}Úforcer$   T)r   Z_deleter   )r   r   r>   r   r   r   r   r   Úremove_pluginÐ   s    
zPluginApiMixin.remove_pluginz1.26c                 C   sj   |   d|¡}d|i}i }t |¡\}}t | |¡}	|	r>|	|d< | j||||dd}
|  |
¡ | j|
ddS )aŽ  
            Upgrade an installed plugin.

            Args:
                name (string): Name of the plugin to upgrade. The ``:latest``
                    tag is optional and is the default if omitted.
                remote (string): Remote reference to upgrade to. The
                    ``:latest`` tag is optional and is the default if omitted.
                privileges (:py:class:`list`): A list of privileges the user
                    consents to grant to the plugin. Can be retrieved using
                    :py:meth:`~plugin_privileges`.

            Returns:
                An iterable object streaming the decoded API logs
        z/plugins/{0}/upgrader*   r+   Tr,   r/   r1   )r   r   r*   r5   r   r   r-   r6   r7   r8   r9   r   r   r   Úupgrade_pluginä   s$     ÿ   þ
zPluginApiMixin.upgrade_plugin)F)r   )N)F)Ú__name__Ú
__module__Ú__qualname__r   Zminimum_versionZcheck_resourcer   r    r"   r%   r)   r:   r;   r<   r=   r?   r@   r   r   r   r   r      s4   

%


r   )r   Ú r   r   Úobjectr   r   r   r   r   Ú<module>   s   