3
ÀÞL]Ü  ã               @   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 d dlmZ d dlmZ e jeƒZG d	d
„ d
eƒZdS )é    N)ÚCommand)ÚERRORÚSUCCESS)ÚConfigurationÚget_configuration_filesÚkinds)ÚPipError)Ú
deprecated)Úget_prog)Úrunning_under_virtualenvc                   s|   e Zd 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.
    Úconfigzâ
        %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
    z&Manage local and global configuration.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jdddd	dd� | j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 onlyz--venvÚ	venv_filezY[Deprecated] Use the current environment configuration file in a virtual environment onlyr   )Úsuperr   Ú__init__ÚconfigurationÚcmd_optsÚ
add_optionÚparserÚinsert_option_group)ÚselfÚargsÚkwargs)Ú	__class__© úC/tmp/pip-build-69lrc5qx/pip/pip/_internal/commands/configuration.pyr   /   sB    zConfigurationCommand.__init__c             C   s  | j | j| j| j| jdœ}| s,|d |krJtjdjdjt	|ƒƒƒƒ t
S |d }y| j||dkd	�}W n0 tk
r˜ } ztj|jd ƒ t
S d }~X nX t|j|d
�| _| jjƒ  y|| ||dd … ƒ W n2 tk
�r  } ztj|jd ƒ t
S d }~X 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é   )r(   r)   r*   r'   )Ú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%   Úrund   s2    

zConfigurationCommand.runc             C   s²   |j r2|j r2tƒ r*d|_tdddd� ntdƒ‚dd„ tj|jftj|j	ftj
|jffD ƒ}|s’|sjd S td	d
„ tƒ tj
 D ƒƒrŠtj
S tjS nt|ƒdkr¦|d S tdƒ‚d S )NTz&The --venv option has been deprecated.z--sitez19.3)ÚreplacementZgone_inzHLegacy --venv option requires a virtual environment. Use --site instead.c             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j|ƒV  qd S )N)ÚosÚpathÚexists)rA   Z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   r   r   r	   r   r   ÚUSERr   ÚGLOBALr   ZSITEÚanyr   Úlen)r    r<   r+   Zfile_optionsr$   r$   r%   r9   �   s2    


z$ConfigurationCommand._determine_filec             C   s>   | j |ddd� x(t| jjƒ ƒD ]\}}tjd||ƒ q W d S )Nr&   r   )Únz%s=%r)Ú_get_n_argsr8   r   Úitemsr4   Úinfo)r    r<   r!   rB   rC   r$   r$   r%   r/   ¸   s    z ConfigurationCommand.list_valuesc             C   s,   | j |ddd�}| jj|ƒ}tjd|ƒ d S )Nz
get [name]r.   )rM   z%s)rN   r   Ú	get_valuer4   rP   )r    r<   r!   rB   rC   r$   r$   r%   r1   ¾   s    zConfigurationCommand.get_namec             C   s.   | j |ddd�\}}| jj||ƒ | jƒ  d S )Nzset [name] [value]é   )rM   )rN   r   Z	set_valueÚ_save_configuration)r    r<   r!   rB   rC   r$   r$   r%   r2   Ä   s    z#ConfigurationCommand.set_name_valuec             C   s(   | j |ddd�}| jj|ƒ | jƒ  d S )Nzunset [name]r.   )rM   )rN   r   Zunset_valuerS   )r    r<   r!   rB   r$   r$   r%   r3   Ê   s    zConfigurationCommand.unset_namec             C   sp   | j |ƒ}| jjƒ }|d kr$tdƒ‚ytj||gƒ W n4 tjk
rj } ztdj|jƒƒ‚W Y d d }~X nX d S )Nz%Could not determine appropriate file.z*Editor Subprocess exited with exit code {})	Ú_determine_editorr   Zget_file_to_editr   Ú
subprocessÚ
check_callÚCalledProcessErrorr6   Ú
returncode)r    r<   r!   r   Úfnamer>   r$   r$   r%   r0   Ð   s    

z#ConfigurationCommand.open_in_editorc             C   s<   t |ƒ|kr$dj|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)rL   r6   r
   r   )r    r!   ZexamplerM   Úmsgr$   r$   r%   rN   ß   s    z ConfigurationCommand._get_n_argsc             C   s>   y| j 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Ú	Exceptionr4   r5   r   )r    r$   r$   r%   rS   î   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   rE   Úenvironr   )r    r<   r$   r$   r%   rT   ú   s    




z&ConfigurationCommand._determine_editor)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnameÚusageÚsummaryr   r?   r9   r/   r1   r2   r3   r0   rN   rS   rT   Ú__classcell__r$   r$   )r#   r%   r      s   5+)r   )ÚloggingrE   rU   Zpip._internal.cli.base_commandr   Zpip._internal.cli.status_codesr   r   Zpip._internal.configurationr   r   r   Zpip._internal.exceptionsr   Zpip._internal.utils.deprecationr	   Zpip._internal.utils.miscr
   Zpip._internal.utils.virtualenvr   Ú	getLoggerr_   r4   r   r$   r$   r$   r%   Ú<module>   s   
