U
    «ÃT_:  ã                   @   s‚   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZ e  e¡ZG dd„ deƒZdS )	é    N)ÚCommand)ÚERRORÚSUCCESS)ÚConfigurationÚget_configuration_filesÚkinds)ÚPipError)Úget_progÚwrite_outputc                       sx   e Zd ZdZdZdZ‡ fdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚConfigurationCommanda9  Manage local and global configuration.

        Subcommands:

        list: List the active configuration (or from the file specified)
        edit: Edit the configuration file in an editor
        get: Get the value associated with name
        set: Set the name=value
        unset: Unset the value associated with name

        If none of --user, --global and --site are passed, a virtual
        environment configuration file is used if one is active and the file
        exists. Otherwise, all modifications happen on the to the user file by
        default.
    Tzâ
        %prog [<file-option>] list
        %prog [<file-option>] [--editor <editor-path>] edit

        %prog [<file-option>] get name
        %prog [<file-option>] set name value
        %prog [<file-option>] unset name
    c                    s„   t t| ƒj||Ž d | _| jjdddd dd� | jjdddd	d
d� | jjdddd	dd� | jjdddd	dd� | j d| j¡ d S )Nz--editorÚeditorÚstorez\Editor to use to edit the file. Uses VISUAL or EDITOR environment variables if not provided.)ÚdestÚactionÚdefaultÚhelpz--globalÚglobal_fileÚ
store_trueFz+Use the system-wide configuration file onlyz--userÚ	user_filez$Use the user configuration file onlyz--siteÚ	site_filez3Use the current environment configuration file onlyr   )Úsuperr   Ú__init__ÚconfigurationÚcmd_optsÚ
add_optionÚparserÚinsert_option_group)ÚselfÚargsÚkwargs©Ú	__class__© úH/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/commands/configuration.pyr   0   s>    úûûûzConfigurationCommand.__init__c              
   C   s  | j | j| j| j| jdœ}|r*|d |krHt d d t	|ƒ¡¡¡ t
S |d }z| j||dkd�}W n: tk
r  } zt |jd ¡ t
 W Y ¢S d }~X Y nX t|j|d�| _| j ¡  z|| ||dd … ƒ W n< tk
�r } zt |jd ¡ t
 W Y ¢S d }~X Y nX tS )	N)ÚlistÚeditÚgetÚsetÚunsetr   zNeed an action ({}) to perform.z, )r&   r'   r(   r%   )Ú
need_value)ÚisolatedÚ	load_onlyé   )Úlist_valuesÚopen_in_editorÚget_nameÚset_name_valueÚ
unset_nameÚloggerÚerrorÚformatÚjoinÚsortedr   Ú_determine_filer   r   r   Úisolated_moder   Úloadr   )r   Úoptionsr   Úhandlersr   r+   Úer"   r"   r#   ÚrunZ   s>    û	ÿ ÿ
 ÿ
zConfigurationCommand.runc                 C   s€   dd„ t j|jft j|jft j|jffD ƒ}|s`|s8d S tdd„ tƒ t j D ƒƒrXt jS t jS nt	|ƒdkrt|d S t
dƒ‚d S )Nc                 S   s   g | ]\}}|r|‘qS r"   r"   )Ú.0ÚkeyÚvaluer"   r"   r#   Ú
<listcomp>†   s    üz8ConfigurationCommand._determine_file.<locals>.<listcomp>c                 s   s   | ]}t j |¡V  qd S )N)ÚosÚpathÚexists)r>   Úsite_config_filer"   r"   r#   Ú	<genexpr>�   s   ÿz7ConfigurationCommand._determine_file.<locals>.<genexpr>r,   r   zLNeed exactly one file to operate upon (--user, --site, --global) to perform.)r   ÚUSERr   ÚGLOBALr   ÚSITEr   Úanyr   Úlenr   )r   r:   r)   Zfile_optionsr"   r"   r#   r7   …   s$    


ý
þÿz$ConfigurationCommand._determine_filec                 C   s8   | j |ddd� t| j ¡ ƒD ]\}}td||ƒ qd S )Nr$   r   ©Únz%s=%r)Ú_get_n_argsr6   r   Úitemsr
   ©r   r:   r   r?   r@   r"   r"   r#   r-   Ÿ   s    z ConfigurationCommand.list_valuesc                 C   s*   | j |ddd�}| j |¡}td|ƒ d S )Nz
get [name]r,   rL   z%s)rN   r   Ú	get_valuer
   rP   r"   r"   r#   r/   ¥   s    zConfigurationCommand.get_namec                 C   s.   | j |ddd�\}}| j ||¡ |  ¡  d S )Nzset [name] [value]é   rL   )rN   r   Ú	set_valueÚ_save_configurationrP   r"   r"   r#   r0   «   s    z#ConfigurationCommand.set_name_valuec                 C   s(   | j |ddd�}| j |¡ |  ¡  d S )Nzunset [name]r,   rL   )rN   r   Úunset_valuerT   )r   r:   r   r?   r"   r"   r#   r1   ±   s    zConfigurationCommand.unset_namec              
   C   sp   |   |¡}| j ¡ }|d kr$tdƒ‚zt ||g¡ W n4 tjk
rj } ztd |j¡ƒ‚W 5 d }~X Y nX d S )Nz%Could not determine appropriate file.z*Editor Subprocess exited with exit code {})	Ú_determine_editorr   Úget_file_to_editr   Ú
subprocessÚ
check_callÚCalledProcessErrorr4   Ú
returncode)r   r:   r   r   Úfnamer<   r"   r"   r#   r.   ·   s    

ÿÿz#ConfigurationCommand.open_in_editorc                 C   s<   t |ƒ|kr$d |tƒ |¡}t|ƒ‚|dkr4|d S |S dS )zJHelper to make sure the command got the right number of arguments
        zJGot unexpected number of arguments, expected {}. (example: "{} config {}")r,   r   N)rK   r4   r	   r   )r   r   ZexamplerM   Úmsgr"   r"   r#   rN   Æ   s    ÿ  ýz ConfigurationCommand._get_n_argsc                 C   s>   z| j  ¡  W n* tk
r8   tjddd� tdƒ‚Y nX d S )Nz:Unable to save configuration. Please report this as a bug.r,   )Úexc_infozInternal Error.)r   ÚsaveÚ	Exceptionr2   r3   r   )r   r"   r"   r#   rT   Õ   s    þz(ConfigurationCommand._save_configurationc                 C   sD   |j d k	r|j S dtjkr$tjd S dtjkr8tjd S tdƒ‚d S )NZVISUALZEDITORz"Could not determine editor to use.)r   rB   Úenvironr   )r   r:   r"   r"   r#   rV   á   s    




z&ConfigurationCommand._determine_editor)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úignore_require_venvÚusager   r=   r7   r-   r/   r0   r1   r.   rN   rT   rV   Ú__classcell__r"   r"   r    r#   r      s   	*+r   )ÚloggingrB   rX   Úpip._internal.cli.base_commandr   Úpip._internal.cli.status_codesr   r   Úpip._internal.configurationr   r   r   Úpip._internal.exceptionsr   Úpip._internal.utils.miscr	   r
   Ú	getLoggerrb   r2   r   r"   r"   r"   r#   Ú<module>   s   
