o
    Ó2.a­"  ã                   @   s"   d dl mZmZ G dd„ dƒZdS )é   )Ú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   sH   |   d|¡}|}t|tƒrdd„ | ¡ 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 )ú=© )Ú.0ÚkÚvr   r   ú3/usr/lib/python3/dist-packages/docker/api/plugin.pyÚ
<listcomp>   s    z3PluginApiMixin.configure_plugin.<locals>.<listcomp>)ÚdataT)Ú_urlÚ
isinstanceÚdictÚitemsÚ
_post_jsonÚ_raise_for_status)Úselfr   ÚoptionsÚurlr   Úresr   r   r   Úconfigure_plugin   s   

zPluginApiMixin.configure_pluginFc                 C   sj   |   d¡}tj||ttj |g ¡ƒd}| j|d|i|d}W d  ƒ n1 s)w   Y  |  |¡ 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   NT)r   r   Úcreate_archiveÚsetÚbuildÚwalkÚ_postr   )r   r   Úplugin_data_dirr   r   Ú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_plugin5   s   

zPluginApiMixin.disable_pluginé    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_pluginF   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_pluginY   s   zPluginApiMixin.inspect_pluginNc                 C   st   |   d¡}d|i}|r||d< i }t |¡\}}t | |¡}	|	r$|	|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   r2   ÚregistryÚ	repo_nameÚheaderÚresponser   r   r   Úpull_pluginh   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  | |¡}|r||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/   r0   z/plugins/privileges)r   r2   T)r   r7   r8   r   r,   r-   )r   r   r   r2   r;   r<   r=   r   r   r   r   Úplugin_privileges™   s   ÿ
ÿz PluginApiMixin.plugin_privilegesc                 C   s\   |   d|¡}i }t |¡\}}t | |¡}|r||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}/pullr0   )r2   Tr4   )r   r   r7   r8   r!   r   r9   )r   r   r   r2   r;   r<   r=   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   Ú_deleter   )r   r   rC   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/   r0   Tr1   r4   r6   )r   r   r/   r:   r   r   r2   r;   r<   r=   r>   r   r   r   Úupgrade_pluginâ   s   ÿþ
zPluginApiMixin.upgrade_plugin)F)r'   )N)Ú__name__Ú
__module__Ú__qualname__r   Úminimum_versionÚcheck_resourcer   r$   r&   r*   r.   r?   r@   rA   rB   rE   rF   r   r   r   r   r      s6    

%


r   N)Ú r   r   r   r   r   r   r   Ú<module>   s    